Skip to content

Commit

Permalink
0.22.1 - Fix an 'Array.firstProp()' bug, add 'Strings.notNullOrEmpty(…
Browse files Browse the repository at this point in the history
…)', and some Arrays overloads.
  • Loading branch information
TeamworkGuy2 committed Feb 20, 2022
1 parent 3a9563e commit 5dc4456
Show file tree
Hide file tree
Showing 10 changed files with 170 additions and 80 deletions.
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
16 changes: 10 additions & 6 deletions test/utils/ArraysTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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" }];
Expand All @@ -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 () {
Expand Down
18 changes: 12 additions & 6 deletions test/utils/ArraysTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(<any[]>[], "value", 5);
asr.deepEqual(res2, []);
});


Expand All @@ -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(<typeof ary1>[], "value", 5, true));

asr.throws(() => Arrays.firstProp(ary1, "value", 5, true));
});
Expand Down
54 changes: 33 additions & 21 deletions test/utils/StringsTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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"));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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...");
Expand All @@ -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");
Expand All @@ -87,29 +99,29 @@ 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);
asr.equal(res2, "--");
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);
asr.equal(res2, "--");
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", "", "-");
Expand Down
58 changes: 37 additions & 21 deletions test/utils/StringsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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"));
Expand All @@ -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));
Expand All @@ -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));
Expand All @@ -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...");
Expand All @@ -93,15 +109,15 @@ 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);
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");
Expand All @@ -113,15 +129,15 @@ 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");
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");

Expand All @@ -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");

Expand All @@ -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");

Expand Down
Loading

0 comments on commit 5dc4456

Please sign in to comment.