From 5dc4456e13bd8fc68caf180d34cabe5db0a40019 Mon Sep 17 00:00:00 2001 From: TeamworkGuy2 Date: Sun, 20 Feb 2022 19:38:18 +0000 Subject: [PATCH] 0.22.1 - Fix an 'Array.firstProp()' bug, add 'Strings.notNullOrEmpty()', and some Arrays overloads. --- CHANGELOG.md | 15 +++++++++- package.json | 2 +- test/utils/ArraysTest.js | 16 +++++++---- test/utils/ArraysTest.ts | 18 ++++++++---- test/utils/StringsTest.js | 54 ++++++++++++++++++++++-------------- test/utils/StringsTest.ts | 58 +++++++++++++++++++++++++-------------- utils/Arrays.js | 33 +++++++--------------- utils/Arrays.ts | 18 +++++++++++- utils/Strings.js | 17 ++++++++++++ utils/Strings.ts | 19 +++++++++++++ 10 files changed, 170 insertions(+), 80 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c90c3a..f9f9c0f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,20 @@ This project does its best to adhere to [Semantic Versioning](http://semver.org/ -------- -### [0.22.0](N/A) - 2022-01-02 +### [0.22.1](N/A) - 2022-02-20 +#### Added +* `Strings.notNullOrEmpty()` and `notNullOrWhiteSpace()` + +#### Changed +* `Arrays.findMatchingProps()` overload to ensure non-null array is returned when input 'ary' is not null +* `Arrays.firstProp()` overload to ensure non-null value is returned when input 'ary' is not null and 'ensureOne' is true + +#### Fixed +* `Arrays.firstProp()` `ensureOne` parameter not working correctly, added unit test + + +-------- +### [0.22.0](https://github.com/TeamworkGuy2/ts-mortar/commit/3a9563ed7d74b79bcc4dbe277ab8d515bd0a99e6) - 2022-01-02 #### Changed * Update to TypeScript 4.4 diff --git a/package.json b/package.json index 4521075..eafe163 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ts-mortar", - "version": "0.22.0", + "version": "0.22.1", "description": "TypeScript utility similar to lodash/underscore, focused on array: add, remove, search, filter; string: is, contains, modification; and object: values, clone, hasProps, getProps, extend, map, toArray.", "author": "TeamworkGuy2", "homepage": "https://github.com/TeamworkGuy2/ts-mortar", diff --git a/test/utils/ArraysTest.js b/test/utils/ArraysTest.js index f76f760..0f812f3 100644 --- a/test/utils/ArraysTest.js +++ b/test/utils/ArraysTest.js @@ -177,8 +177,9 @@ suite("Arrays", function () { }); test("findMatchingsProp", function () { var res = Arrays.findMatchingProps([{ name: "billy", value: 5 }, { name: "sam", value: 5 }, { name: "overhill", value: 3 }], "value", 5); - var expect = [{ name: "billy", value: 5 }, { name: "sam", value: 5 }]; - asr.deepEqual(res, expect); + asr.deepEqual(res, [{ name: "billy", value: 5 }, { name: "sam", value: 5 }]); + var res2 = Arrays.findMatchingProps([], "value", 5); + asr.deepEqual(res2, []); }); test("first", function () { var ary = [{ key: 27, value: "A" }, { key: 46, value: "B" }, { key: 84, value: "C" }, { key: 84, value: "D" }]; @@ -191,11 +192,14 @@ suite("Arrays", function () { asr.throws(function () { return Arrays.first(ary, function (obj) { return obj.key === 84; }, true); }); }); test("firstProp", function () { - var res2 = Arrays.firstProp([{ name: "billy", value: 5 }, { name: "sam", value: 5 }], "value", 5); - asr.deepEqual(res2, { name: "billy", value: 5 }); + var res1 = Arrays.firstProp([{ name: "billy", value: 5 }, { name: "sam", value: 5 }], "value", 5); + asr.deepEqual(res1, { name: "billy", value: 5 }); + var res2 = Arrays.firstProp([{ name: "billy", value: 4 }, { name: "sam", value: 5 }], "value", 4, true); + asr.deepEqual(res2, { name: "billy", value: 4 }); var ary1 = [{ name: "billy", value: 4 }, { name: "sam", value: 5 }, { name: "will", value: 5 }]; - var res1 = Arrays.firstProp(ary1, "value", 5, false); - asr.deepEqual(res1, { name: "sam", value: 5 }); + var res3 = Arrays.firstProp(ary1, "value", 5, false); + asr.deepEqual(res3, { name: "sam", value: 5 }); + asr.throws(function () { return Arrays.firstProp([], "value", 5, true); }); asr.throws(function () { return Arrays.firstProp(ary1, "value", 5, true); }); }); test("getIfOneItem", function () { diff --git a/test/utils/ArraysTest.ts b/test/utils/ArraysTest.ts index 6686813..17c6d98 100644 --- a/test/utils/ArraysTest.ts +++ b/test/utils/ArraysTest.ts @@ -258,9 +258,10 @@ suite("Arrays", () => { test("findMatchingsProp", () => { var res = Arrays.findMatchingProps([{ name: "billy", value: 5 }, { name: "sam", value: 5 }, { name: "overhill", value: 3 }], "value", 5); - var expect = [{ name: "billy", value: 5 }, { name: "sam", value: 5 }]; + asr.deepEqual(res, [{ name: "billy", value: 5 }, { name: "sam", value: 5 }]); - asr.deepEqual(res, expect); + var res2 = Arrays.findMatchingProps([], "value", 5); + asr.deepEqual(res2, []); }); @@ -281,13 +282,18 @@ suite("Arrays", () => { test("firstProp", () => { - var res2 = Arrays.firstProp([{ name: "billy", value: 5 }, { name: "sam", value: 5 }], "value", 5); - asr.deepEqual(res2, { name: "billy", value: 5 }); + var res1 = Arrays.firstProp([{ name: "billy", value: 5 }, { name: "sam", value: 5 }], "value", 5); + asr.deepEqual(res1, { name: "billy", value: 5 }); + + var res2 = Arrays.firstProp([{ name: "billy", value: 4 }, { name: "sam", value: 5 }], "value", 4, true); + asr.deepEqual(res2, { name: "billy", value: 4 }); var ary1 = [{ name: "billy", value: 4 }, { name: "sam", value: 5 }, { name: "will", value: 5 }]; - var res1 = Arrays.firstProp(ary1, "value", 5, false); - asr.deepEqual(res1, { name: "sam", value: 5 }); + var res3 = Arrays.firstProp(ary1, "value", 5, false); + asr.deepEqual(res3, { name: "sam", value: 5 }); + + asr.throws(() => Arrays.firstProp([], "value", 5, true)); asr.throws(() => Arrays.firstProp(ary1, "value", 5, true)); }); diff --git a/test/utils/StringsTest.js b/test/utils/StringsTest.js index 9c5f9df..5d4f5be 100644 --- a/test/utils/StringsTest.js +++ b/test/utils/StringsTest.js @@ -4,19 +4,31 @@ var chai = require("chai"); var Strings = require("../../utils/Strings"); var asr = chai.assert; suite("Strings", function StringsTest() { - test("isNullOrEmpty", function isNullOrEmptyTest() { - asr.equal(Strings.isNullOrEmpty(""), true); - asr.equal(Strings.isNullOrEmpty(null), true); - asr.equal(Strings.isNullOrEmpty(" "), false); - asr.equal(Strings.isNullOrEmpty("abc"), false); + test("isNullOrEmpty", function () { + asr.isTrue(Strings.isNullOrEmpty("")); + asr.isTrue(Strings.isNullOrEmpty(null)); + asr.isFalse(Strings.isNullOrEmpty(" ")); + asr.isFalse(Strings.isNullOrEmpty("abc")); }); - test("isNullOrWhiteSpace", function isNullOrWhiteSpaceTest() { - asr.equal(Strings.isNullOrWhiteSpace(""), true); - asr.equal(Strings.isNullOrWhiteSpace(null), true); - asr.equal(Strings.isNullOrWhiteSpace(" "), true); - asr.equal(Strings.isNullOrWhiteSpace("abc"), false); + test("isNullOrWhiteSpace", function () { + asr.isTrue(Strings.isNullOrWhiteSpace("")); + asr.isTrue(Strings.isNullOrWhiteSpace(null)); + asr.isTrue(Strings.isNullOrWhiteSpace(" ")); + asr.isFalse(Strings.isNullOrWhiteSpace("abc")); }); - test("isCharAtDigit", function isCharAtDigitTest() { + test("notNullOrEmpty", function () { + asr.isFalse(Strings.notNullOrEmpty("")); + asr.isFalse(Strings.notNullOrEmpty(null)); + asr.isTrue(Strings.notNullOrEmpty(" ")); + asr.isTrue(Strings.notNullOrEmpty("abc")); + }); + test("notNullOrWhiteSpace", function () { + asr.isFalse(Strings.notNullOrWhiteSpace("")); + asr.isFalse(Strings.notNullOrWhiteSpace(null)); + asr.isFalse(Strings.notNullOrWhiteSpace(" ")); + asr.isTrue(Strings.notNullOrWhiteSpace("abc")); + }); + test("isCharAtDigit", function () { asr.isTrue(Strings.isCharAtDigit("1.2%", 0)); asr.isFalse(Strings.isCharAtDigit("1.2%", 1)); asr.isTrue(Strings.isCharAtDigit("1.2%", 2)); @@ -30,7 +42,7 @@ suite("Strings", function StringsTest() { asr.isFalse(Strings.isCharAtDigit(" ", null)); asr.isFalse(Strings.isCharAtDigit(null, null)); }); - test("isDigit", function isDigitTest() { + test("isDigit", function () { asr.isTrue(Strings.isDigit("5")); asr.isTrue(Strings.isDigit("123")); asr.isTrue(Strings.isDigit("0")); @@ -40,7 +52,7 @@ suite("Strings", function StringsTest() { asr.isFalse(Strings.isDigit(null)); asr.isFalse(Strings.isDigit(undefined)); }); - test("isCharAtUpperCase", function isCharAtUpperCaseTest() { + test("isCharAtUpperCase", function () { asr.isTrue(Strings.isCharAtUpperCase("AbCd", 0)); asr.isFalse(Strings.isCharAtUpperCase("AbCd", 1)); asr.isTrue(Strings.isCharAtUpperCase("AbCd", 2)); @@ -50,7 +62,7 @@ suite("Strings", function StringsTest() { asr.isFalse(Strings.isCharAtUpperCase("A", Infinity)); asr.isFalse(Strings.isCharAtUpperCase(null, null)); }); - test("isCharAtLowerCase", function isCharAtLowerCaseTest() { + test("isCharAtLowerCase", function () { asr.isTrue(Strings.isCharAtLowerCase("aBcD", 0)); asr.isFalse(Strings.isCharAtLowerCase("aBcD", 1)); asr.isTrue(Strings.isCharAtLowerCase("aBcD", 2)); @@ -60,7 +72,7 @@ suite("Strings", function StringsTest() { asr.isFalse(Strings.isCharAtLowerCase("a", Infinity)); asr.isFalse(Strings.isCharAtLowerCase(null, null)); }); - test("truncate", function truncateTest() { + test("truncate", function () { asr.equal(Strings.truncate("123", 4, "..."), "123"); asr.equal(Strings.truncate("1234", 4, "..."), "1234"); asr.equal(Strings.truncate("12345", 4, "..."), "1..."); @@ -71,13 +83,13 @@ suite("Strings", function StringsTest() { asr.equal(Strings.truncate(null, 2, "..."), ""); asr.equal(Strings.truncate(undefined, 2, "..."), ""); }); - test("looseEqual", function looseEqualTest() { + test("looseEqual", function () { asr.equal(Strings.looseEqual(" Abc", "ABC"), true); asr.equal(Strings.looseEqual(" abc", "ABC\t"), true); asr.equal(Strings.looseEqual(" \n\n", "\t"), true); asr.equal(Strings.looseEqual(" \na\n", "A\t"), true); }); - test("padStart", function padStartTest() { + test("padStart", function () { asr.equal(Strings.padStart(123, 5, '0'), "00123"); asr.equal(Strings.padStart(123, 6, '0'), "000123"); asr.equal(Strings.padStart(123, 3, '0'), "123"); @@ -87,13 +99,13 @@ suite("Strings", function StringsTest() { asr.equal(Strings.padStart(1.2, 3, "-"), "1.2"); asr.equal(Strings.padStart("A", 3, "a"), "aaA"); }); - test("padEnd", function padEndTest() { + test("padEnd", function () { asr.equal(Strings.padEnd(1.2, 5, " "), "1.2 "); asr.equal(Strings.padEnd(1.2, 6, "-"), "1.2---"); asr.equal(Strings.padEnd(1.2, 3, "-"), "1.2"); asr.equal(Strings.padEnd("A", 3, "a"), "Aaa"); }); - test("removeLeading", function removeLeadingTest() { + test("removeLeading", function () { var res1 = Strings.removeLeading("stubstubAlpha", "stub", true); asr.equal(res1, "Alpha"); var res2 = Strings.removeLeading("---", "-", false); @@ -101,7 +113,7 @@ suite("Strings", function StringsTest() { var res3 = Strings.removeLeading("AAA", "B", false); asr.equal(res3, "AAA"); }); - test("removeTrailing", function removeTrailingTest() { + test("removeTrailing", function () { var res1 = Strings.removeTrailing("alphaPiePiePie", "Pie", true); asr.equal(res1, "alpha"); var res2 = Strings.removeTrailing("---", "-", false); @@ -109,7 +121,7 @@ suite("Strings", function StringsTest() { var res3 = Strings.removeTrailing("AAA", "B", false); asr.equal(res3, "AAA"); }); - test("replaceAll", function replaceAllTest() { + test("replaceAll", function () { var res1 = Strings.replaceAll("cat in the hat", "at", "ab"); asr.equal(res1, "cab in the hab"); var res2 = Strings.replaceAll("Super", "", "-"); diff --git a/test/utils/StringsTest.ts b/test/utils/StringsTest.ts index 77144a0..3d0f9c1 100644 --- a/test/utils/StringsTest.ts +++ b/test/utils/StringsTest.ts @@ -7,23 +7,39 @@ var asr = chai.assert; suite("Strings", function StringsTest() { - test("isNullOrEmpty", function isNullOrEmptyTest() { - asr.equal(Strings.isNullOrEmpty(""), true); - asr.equal(Strings.isNullOrEmpty(null), true); - asr.equal(Strings.isNullOrEmpty(" "), false); - asr.equal(Strings.isNullOrEmpty("abc"), false); + test("isNullOrEmpty", function () { + asr.isTrue(Strings.isNullOrEmpty("")); + asr.isTrue(Strings.isNullOrEmpty(null)); + asr.isFalse(Strings.isNullOrEmpty(" ")); + asr.isFalse(Strings.isNullOrEmpty("abc")); }); - test("isNullOrWhiteSpace", function isNullOrWhiteSpaceTest() { - asr.equal(Strings.isNullOrWhiteSpace(""), true); - asr.equal(Strings.isNullOrWhiteSpace(null), true); - asr.equal(Strings.isNullOrWhiteSpace(" "), true); - asr.equal(Strings.isNullOrWhiteSpace("abc"), false); + test("isNullOrWhiteSpace", function () { + asr.isTrue(Strings.isNullOrWhiteSpace("")); + asr.isTrue(Strings.isNullOrWhiteSpace(null)); + asr.isTrue(Strings.isNullOrWhiteSpace(" ")); + asr.isFalse(Strings.isNullOrWhiteSpace("abc")); }); - test("isCharAtDigit", function isCharAtDigitTest() { + test("notNullOrEmpty", function () { + asr.isFalse(Strings.notNullOrEmpty("")); + asr.isFalse(Strings.notNullOrEmpty(null)); + asr.isTrue(Strings.notNullOrEmpty(" ")); + asr.isTrue(Strings.notNullOrEmpty("abc")); + }); + + + test("notNullOrWhiteSpace", function () { + asr.isFalse(Strings.notNullOrWhiteSpace("")); + asr.isFalse(Strings.notNullOrWhiteSpace(null)); + asr.isFalse(Strings.notNullOrWhiteSpace(" ")); + asr.isTrue(Strings.notNullOrWhiteSpace("abc")); + }); + + + test("isCharAtDigit", function () { asr.isTrue (Strings.isCharAtDigit("1.2%", 0)); asr.isFalse(Strings.isCharAtDigit("1.2%", 1)); asr.isTrue (Strings.isCharAtDigit("1.2%", 2)); @@ -40,7 +56,7 @@ suite("Strings", function StringsTest() { }); - test("isDigit", function isDigitTest() { + test("isDigit", function () { asr.isTrue(Strings.isDigit("5")); asr.isTrue(Strings.isDigit("123")); asr.isTrue(Strings.isDigit("0")); @@ -53,7 +69,7 @@ suite("Strings", function StringsTest() { }); - test("isCharAtUpperCase", function isCharAtUpperCaseTest() { + test("isCharAtUpperCase", function () { asr.isTrue (Strings.isCharAtUpperCase("AbCd", 0)); asr.isFalse(Strings.isCharAtUpperCase("AbCd", 1)); asr.isTrue (Strings.isCharAtUpperCase("AbCd", 2)); @@ -66,7 +82,7 @@ suite("Strings", function StringsTest() { }); - test("isCharAtLowerCase", function isCharAtLowerCaseTest() { + test("isCharAtLowerCase", function () { asr.isTrue (Strings.isCharAtLowerCase("aBcD", 0)); asr.isFalse(Strings.isCharAtLowerCase("aBcD", 1)); asr.isTrue (Strings.isCharAtLowerCase("aBcD", 2)); @@ -79,7 +95,7 @@ suite("Strings", function StringsTest() { }); - test("truncate", function truncateTest() { + test("truncate", function () { asr.equal(Strings.truncate("123" , 4, "..."), "123"); asr.equal(Strings.truncate("1234" , 4, "..."), "1234"); asr.equal(Strings.truncate("12345", 4, "..."), "1..."); @@ -93,7 +109,7 @@ suite("Strings", function StringsTest() { }); - test("looseEqual", function looseEqualTest() { + test("looseEqual", function () { asr.equal(Strings.looseEqual(" Abc", "ABC"), true); asr.equal(Strings.looseEqual(" abc", "ABC\t"), true); asr.equal(Strings.looseEqual(" \n\n", "\t"), true); @@ -101,7 +117,7 @@ suite("Strings", function StringsTest() { }); - test("padStart", function padStartTest() { + test("padStart", function () { asr.equal(Strings.padStart(123, 5, '0'), "00123"); asr.equal(Strings.padStart(123, 6, '0'), "000123"); asr.equal(Strings.padStart(123, 3, '0'), "123"); @@ -113,7 +129,7 @@ suite("Strings", function StringsTest() { }); - test("padEnd", function padEndTest() { + test("padEnd", function () { asr.equal(Strings.padEnd(1.2, 5, " "), "1.2 "); asr.equal(Strings.padEnd(1.2, 6, "-"), "1.2---"); asr.equal(Strings.padEnd(1.2, 3, "-"), "1.2"); @@ -121,7 +137,7 @@ suite("Strings", function StringsTest() { }); - test("removeLeading", function removeLeadingTest() { + test("removeLeading", function () { var res1 = Strings.removeLeading("stubstubAlpha", "stub", true); asr.equal(res1, "Alpha"); @@ -133,7 +149,7 @@ suite("Strings", function StringsTest() { }); - test("removeTrailing", function removeTrailingTest() { + test("removeTrailing", function () { var res1 = Strings.removeTrailing("alphaPiePiePie", "Pie", true); asr.equal(res1, "alpha"); @@ -145,7 +161,7 @@ suite("Strings", function StringsTest() { }); - test("replaceAll", function replaceAllTest() { + test("replaceAll", function () { var res1 = Strings.replaceAll("cat in the hat", "at", "ab"); asr.equal(res1, "cab in the hab"); diff --git a/utils/Arrays.js b/utils/Arrays.js index 3a0fd0b..c4f8927 100644 --- a/utils/Arrays.js +++ b/utils/Arrays.js @@ -399,17 +399,6 @@ var Arrays; notMatching: notMatching }; } - /** Search for objects in an array containing a property matching a given input property. - * For example: Arrays.findAllProp([ {name: "billy", value: 5}, {name: "sam", value: 5}, {name: "overhill", value: 3} ], "value", 5) - * returns: {name: "billy", value: 5}, {name: "sam", value: 5} - * because the matching object has a property "value" with a value of 5 - * - * @param ary the array to search - * @param propName the name of the property to search for on each object - * @param propValue the property value to compare - * @returns an array of objects containing properties named 'propName' with values equal to 'propValue', - * returns a new empty array if no matching object was found - */ function findMatchingProps(ary, propName, propValue) { if (ary == null || propName == null || propValue === undefined) { return null; @@ -489,18 +478,6 @@ var Arrays; return -1; } Arrays.lastIndex = lastIndex; - /** Search for an object in an array containing a property matching a given input property. - * Optional: throw an exception if more than one result is found. - * For example: Arrays.firstProp([ {name: "billy", value: 4}, {name: "sam", value: 5}, {name: "will", value: 5} ], "value", 5) - * returns: {name: "sam", value: 5} - * Or example: Arrays.firstProp([ {name: "billy", value: 4}, {name: "sam", value: 4}, {name: "will", value: 5} ], "value", 5, true) - * throws an error because the value appears more than once and the 'ensureOne' parameter = true - * - * @param ary the array of values to search - * @param propName the name of the property to search for on each object - * @param propValue the property value to compare - * @returns the first (lowest index) matching value from the input array, or null if a result cannot be found - */ function firstProp(ary, propName, propValue, ensureOne) { if (ensureOne === void 0) { ensureOne = false; } if (ary == null || propName == null) { @@ -519,6 +496,16 @@ var Arrays; } } resultCount++; + if (ensureOne && resultCount > 1) { + break; + } + } + } + if (ensureOne) { + if (resultCount === 0) { + throw new Error("found no results for '" + propName + "'='" + propValue + "', expected to find one"); + } + else if (resultCount > 1) { throw new Error("found multiple results for '" + propName + "'='" + propValue + "', expected to find one"); } } diff --git a/utils/Arrays.ts b/utils/Arrays.ts index c7f9b06..da30383 100644 --- a/utils/Arrays.ts +++ b/utils/Arrays.ts @@ -471,6 +471,8 @@ module Arrays { * @returns an array of objects containing properties named 'propName' with values equal to 'propValue', * returns a new empty array if no matching object was found */ + export function findMatchingProps(ary: E[] | ArrayLike, propName: K, propValue: E[K]): E[]; + export function findMatchingProps(ary: E[] | ArrayLike | null | undefined, propName: K, propValue: E[K]): E[] | null; export function findMatchingProps(ary: E[] | ArrayLike | null | undefined, propName: K, propValue: E[K]): E[] | null { if (ary == null || propName == null || propValue === undefined) { return null; } var res: E[] = []; @@ -503,7 +505,7 @@ module Arrays { */ export function firstIndex(ary: E[] | ArrayLike | null | undefined, filter: (value: E, index: number, array: E[]) => boolean, ensureOne: boolean = false): number { if (ary == null || filter == null) { return -1; } - var resultIdx: number = -1; + var resultIdx = -1; var resultCount = 0; for (var i = 0, size = ary.length; i < size; i++) { @@ -560,8 +562,11 @@ module Arrays { * @param ary the array of values to search * @param propName the name of the property to search for on each object * @param propValue the property value to compare + * @param ensureOne ensure that one result is found and returned, throw an error if zero results are found or if multiple results are found * @returns the first (lowest index) matching value from the input array, or null if a result cannot be found */ + export function firstProp(ary: E[] | ArrayLike, propName: K, propValue: E[K], ensureOne: true): E; + export function firstProp(ary: E[] | ArrayLike | null | undefined, propName: K, propValue: E[K], ensureOne?: boolean): E | null; export function firstProp(ary: E[] | ArrayLike | null | undefined, propName: K, propValue: E[K], ensureOne: boolean = false): E | null { if (ary == null || propName == null) { return null; } var result: E | null = null; @@ -578,6 +583,17 @@ module Arrays { } } resultCount++; + if (ensureOne && resultCount > 1) { + break; + } + } + } + + if (ensureOne) { + if (resultCount === 0) { + throw new Error("found no results for '" + propName + "'='" + propValue + "', expected to find one"); + } + else if (resultCount > 1) { throw new Error("found multiple results for '" + propName + "'='" + propValue + "', expected to find one"); } } diff --git a/utils/Strings.js b/utils/Strings.js index df87e4f..80ad203 100644 --- a/utils/Strings.js +++ b/utils/Strings.js @@ -21,6 +21,23 @@ var Strings; return str == null || str.trim().length === 0; } Strings.isNullOrWhiteSpace = isNullOrWhiteSpace; + /** Check that a string is NOT null nor empty + * @param str the string to check + * @returns true if the 'str' is not null and not empty, false if null or empty + */ + function notNullOrEmpty(str) { + return str != null && str.length > 0; + } + Strings.notNullOrEmpty = notNullOrEmpty; + /** Check that a string is NOT null nor empty nor contains only whitespace + * @param str the string to check + * @returns true if the 'str' is not null, not empty, nor contains only whitespace characters, + * false otherwise + */ + function notNullOrWhiteSpace(str) { + return str != null && str.trim().length > 0; + } + Strings.notNullOrWhiteSpace = notNullOrWhiteSpace; /** Check if a character at a specific index in a string is a digit * @param str the string to get the character from * @param i the index of the character diff --git a/utils/Strings.ts b/utils/Strings.ts index a90cd85..5741a36 100644 --- a/utils/Strings.ts +++ b/utils/Strings.ts @@ -23,6 +23,25 @@ module Strings { } + /** Check that a string is NOT null nor empty + * @param str the string to check + * @returns true if the 'str' is not null and not empty, false if null or empty + */ + export function notNullOrEmpty(str: string | null | undefined): str is string { + return str != null && str.length > 0; + } + + + /** Check that a string is NOT null nor empty nor contains only whitespace + * @param str the string to check + * @returns true if the 'str' is not null, not empty, nor contains only whitespace characters, + * false otherwise + */ + export function notNullOrWhiteSpace(str: string | null | undefined): str is string { + return str != null && str.trim().length > 0; + } + + /** Check if a character at a specific index in a string is a digit * @param str the string to get the character from * @param i the index of the character