@ezez/utils 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.prettierignore +3 -0
- package/.prettierrc.json +1 -0
- package/CHANGELOG.md +244 -0
- package/LICENSE +21 -0
- package/README.md +79 -0
- package/babel.config.cjs +6 -0
- package/dist/cap.d.ts +3 -0
- package/dist/cap.d.ts.map +1 -0
- package/dist/cap.js +14 -0
- package/dist/cap.js.map +1 -0
- package/dist/capitalize.d.ts +3 -0
- package/dist/capitalize.d.ts.map +1 -0
- package/dist/capitalize.js +9 -0
- package/dist/capitalize.js.map +1 -0
- package/dist/coalesce.d.ts +3 -0
- package/dist/coalesce.d.ts.map +1 -0
- package/dist/coalesce.js +14 -0
- package/dist/coalesce.js.map +1 -0
- package/dist/ensureArray.d.ts +3 -0
- package/dist/ensureArray.d.ts.map +1 -0
- package/dist/ensureArray.js +11 -0
- package/dist/ensureArray.js.map +1 -0
- package/dist/ensureError.d.ts +3 -0
- package/dist/ensureError.d.ts.map +1 -0
- package/dist/ensureError.js +11 -0
- package/dist/ensureError.js.map +1 -0
- package/dist/get.d.ts +7 -0
- package/dist/get.d.ts.map +1 -0
- package/dist/get.js +19 -0
- package/dist/get.js.map +1 -0
- package/dist/getMultiple.d.ts +7 -0
- package/dist/getMultiple.d.ts.map +1 -0
- package/dist/getMultiple.js +18 -0
- package/dist/getMultiple.js.map +1 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +49 -0
- package/dist/index.js.map +1 -0
- package/dist/insertSeparator.d.ts +3 -0
- package/dist/insertSeparator.d.ts.map +1 -0
- package/dist/insertSeparator.js +18 -0
- package/dist/insertSeparator.js.map +1 -0
- package/dist/isEmpty.d.ts +3 -0
- package/dist/isEmpty.d.ts.map +1 -0
- package/dist/isEmpty.js +26 -0
- package/dist/isEmpty.js.map +1 -0
- package/dist/isPlainObject.d.ts +3 -0
- package/dist/isPlainObject.d.ts.map +1 -0
- package/dist/isPlainObject.js +10 -0
- package/dist/isPlainObject.js.map +1 -0
- package/dist/last.d.ts +3 -0
- package/dist/last.d.ts.map +1 -0
- package/dist/last.js +6 -0
- package/dist/last.js.map +1 -0
- package/dist/mapAsync.d.ts +3 -0
- package/dist/mapAsync.d.ts.map +1 -0
- package/dist/mapAsync.js +22 -0
- package/dist/mapAsync.js.map +1 -0
- package/dist/mapValues.d.ts +6 -0
- package/dist/mapValues.d.ts.map +1 -0
- package/dist/mapValues.js +18 -0
- package/dist/mapValues.js.map +1 -0
- package/dist/match.d.ts +8 -0
- package/dist/match.d.ts.map +1 -0
- package/dist/match.js +20 -0
- package/dist/match.js.map +1 -0
- package/dist/merge.d.ts +15 -0
- package/dist/merge.d.ts.map +1 -0
- package/dist/merge.js +28 -0
- package/dist/merge.js.map +1 -0
- package/dist/mostFrequent.d.ts +3 -0
- package/dist/mostFrequent.d.ts.map +1 -0
- package/dist/mostFrequent.js +21 -0
- package/dist/mostFrequent.js.map +1 -0
- package/dist/noop.d.ts +3 -0
- package/dist/noop.d.ts.map +1 -0
- package/dist/noop.js +6 -0
- package/dist/noop.js.map +1 -0
- package/dist/omit.d.ts +5 -0
- package/dist/omit.d.ts.map +1 -0
- package/dist/omit.js +21 -0
- package/dist/omit.js.map +1 -0
- package/dist/package.json +1 -0
- package/dist/pick.d.ts +5 -0
- package/dist/pick.d.ts.map +1 -0
- package/dist/pick.js +20 -0
- package/dist/pick.js.map +1 -0
- package/dist/pull.d.ts +3 -0
- package/dist/pull.d.ts.map +1 -0
- package/dist/pull.js +14 -0
- package/dist/pull.js.map +1 -0
- package/dist/remove.d.ts +3 -0
- package/dist/remove.d.ts.map +1 -0
- package/dist/remove.js +18 -0
- package/dist/remove.js.map +1 -0
- package/dist/rethrow.d.ts +3 -0
- package/dist/rethrow.d.ts.map +1 -0
- package/dist/rethrow.js +8 -0
- package/dist/rethrow.js.map +1 -0
- package/dist/scale.d.ts +3 -0
- package/dist/scale.d.ts.map +1 -0
- package/dist/scale.js +8 -0
- package/dist/scale.js.map +1 -0
- package/dist/seq.d.ts +8 -0
- package/dist/seq.d.ts.map +1 -0
- package/dist/seq.js +45 -0
- package/dist/seq.js.map +1 -0
- package/dist/set.d.ts +7 -0
- package/dist/set.d.ts.map +1 -0
- package/dist/set.js +25 -0
- package/dist/set.js.map +1 -0
- package/dist/setImmutable.d.ts +8 -0
- package/dist/setImmutable.d.ts.map +1 -0
- package/dist/setImmutable.js +59 -0
- package/dist/setImmutable.js.map +1 -0
- package/dist/sortBy.d.ts +3 -0
- package/dist/sortBy.d.ts.map +1 -0
- package/dist/sortBy.js +15 -0
- package/dist/sortBy.js.map +1 -0
- package/dist/throttle.d.ts +13 -0
- package/dist/throttle.d.ts.map +1 -0
- package/dist/throttle.js +82 -0
- package/dist/throttle.js.map +1 -0
- package/dist/truthy.d.ts +3 -0
- package/dist/truthy.d.ts.map +1 -0
- package/dist/truthy.js +8 -0
- package/dist/truthy.js.map +1 -0
- package/dist/wait.d.ts +3 -0
- package/dist/wait.d.ts.map +1 -0
- package/dist/wait.js +10 -0
- package/dist/wait.js.map +1 -0
- package/dist/waitFor.d.ts +3 -0
- package/dist/waitFor.d.ts.map +1 -0
- package/dist/waitFor.js +34 -0
- package/dist/waitFor.js.map +1 -0
- package/dist/waitSync.d.ts +3 -0
- package/dist/waitSync.d.ts.map +1 -0
- package/dist/waitSync.js +9 -0
- package/dist/waitSync.js.map +1 -0
- package/docs/.nojekyll +1 -0
- package/docs/assets/highlight.css +78 -0
- package/docs/assets/main.js +58 -0
- package/docs/assets/pages.css +14 -0
- package/docs/assets/search.js +1 -0
- package/docs/assets/style.css +1280 -0
- package/docs/functions/cap.html +116 -0
- package/docs/functions/capitalize.html +121 -0
- package/docs/functions/coalesce.html +127 -0
- package/docs/functions/ensureArray.html +114 -0
- package/docs/functions/ensureError.html +110 -0
- package/docs/functions/get.html +138 -0
- package/docs/functions/getMultiple.html +129 -0
- package/docs/functions/insertSeparator.html +123 -0
- package/docs/functions/isEmpty.html +139 -0
- package/docs/functions/isPlainObject.html +108 -0
- package/docs/functions/last.html +126 -0
- package/docs/functions/mapAsync.html +144 -0
- package/docs/functions/mapValues.html +130 -0
- package/docs/functions/match.html +122 -0
- package/docs/functions/merge.html +464 -0
- package/docs/functions/mostFrequent.html +111 -0
- package/docs/functions/noop.html +101 -0
- package/docs/functions/omit.html +129 -0
- package/docs/functions/pick.html +129 -0
- package/docs/functions/pull.html +113 -0
- package/docs/functions/remove.html +129 -0
- package/docs/functions/rethrow.html +106 -0
- package/docs/functions/scale.html +117 -0
- package/docs/functions/seq.html +128 -0
- package/docs/functions/seqEarlyBreak.html +126 -0
- package/docs/functions/set.html +138 -0
- package/docs/functions/setImmutable.html +137 -0
- package/docs/functions/sortBy.html +135 -0
- package/docs/functions/throttle.html +131 -0
- package/docs/functions/truthy.html +118 -0
- package/docs/functions/wait.html +109 -0
- package/docs/functions/waitFor.html +130 -0
- package/docs/functions/waitSync.html +109 -0
- package/docs/index.html +182 -0
- package/docs/interfaces/GetMultipleSource.html +106 -0
- package/docs/interfaces/GetSource.html +106 -0
- package/docs/interfaces/SetImmutableSource.html +106 -0
- package/docs/interfaces/SetSource.html +106 -0
- package/docs/interfaces/ThrottleOptions.html +80 -0
- package/docs/interfaces/ThrottledFunctionExtras.html +96 -0
- package/docs/modules.html +152 -0
- package/docs/pages/Introduction.html +94 -0
- package/docs/types/MapValuesFn.html +134 -0
- package/docs/types/MatchCallback.html +113 -0
- package/docs/types/SeqEarlyBreaker.html +114 -0
- package/docs/types/SeqFn.html +112 -0
- package/docs/types/SeqFunctions.html +105 -0
- package/docs/types/SetImmutablePath.html +99 -0
- package/docs/types/ThrottledFunction.html +113 -0
- package/docs/variables/mapValuesUNSET.html +101 -0
- package/docs/variables/mergeUNSET.html +103 -0
- package/esm/cap.d.ts +3 -0
- package/esm/cap.d.ts.map +1 -0
- package/esm/cap.js +11 -0
- package/esm/cap.js.map +1 -0
- package/esm/capitalize.d.ts +3 -0
- package/esm/capitalize.d.ts.map +1 -0
- package/esm/capitalize.js +6 -0
- package/esm/capitalize.js.map +1 -0
- package/esm/coalesce.d.ts +3 -0
- package/esm/coalesce.d.ts.map +1 -0
- package/esm/coalesce.js +11 -0
- package/esm/coalesce.js.map +1 -0
- package/esm/ensureArray.d.ts +3 -0
- package/esm/ensureArray.d.ts.map +1 -0
- package/esm/ensureArray.js +8 -0
- package/esm/ensureArray.js.map +1 -0
- package/esm/ensureError.d.ts +3 -0
- package/esm/ensureError.d.ts.map +1 -0
- package/esm/ensureError.js +8 -0
- package/esm/ensureError.js.map +1 -0
- package/esm/get.d.ts +7 -0
- package/esm/get.d.ts.map +1 -0
- package/esm/get.js +16 -0
- package/esm/get.js.map +1 -0
- package/esm/getMultiple.d.ts +7 -0
- package/esm/getMultiple.d.ts.map +1 -0
- package/esm/getMultiple.js +15 -0
- package/esm/getMultiple.js.map +1 -0
- package/esm/index.d.ts +33 -0
- package/esm/index.d.ts.map +1 -0
- package/esm/index.js +33 -0
- package/esm/index.js.map +1 -0
- package/esm/insertSeparator.d.ts +3 -0
- package/esm/insertSeparator.d.ts.map +1 -0
- package/esm/insertSeparator.js +15 -0
- package/esm/insertSeparator.js.map +1 -0
- package/esm/isEmpty.d.ts +3 -0
- package/esm/isEmpty.d.ts.map +1 -0
- package/esm/isEmpty.js +23 -0
- package/esm/isEmpty.js.map +1 -0
- package/esm/isPlainObject.d.ts +3 -0
- package/esm/isPlainObject.d.ts.map +1 -0
- package/esm/isPlainObject.js +7 -0
- package/esm/isPlainObject.js.map +1 -0
- package/esm/last.d.ts +3 -0
- package/esm/last.d.ts.map +1 -0
- package/esm/last.js +3 -0
- package/esm/last.js.map +1 -0
- package/esm/mapAsync.d.ts +3 -0
- package/esm/mapAsync.d.ts.map +1 -0
- package/esm/mapAsync.js +19 -0
- package/esm/mapAsync.js.map +1 -0
- package/esm/mapValues.d.ts +6 -0
- package/esm/mapValues.d.ts.map +1 -0
- package/esm/mapValues.js +14 -0
- package/esm/mapValues.js.map +1 -0
- package/esm/match.d.ts +8 -0
- package/esm/match.d.ts.map +1 -0
- package/esm/match.js +17 -0
- package/esm/match.js.map +1 -0
- package/esm/merge.d.ts +15 -0
- package/esm/merge.d.ts.map +1 -0
- package/esm/merge.js +24 -0
- package/esm/merge.js.map +1 -0
- package/esm/mostFrequent.d.ts +3 -0
- package/esm/mostFrequent.d.ts.map +1 -0
- package/esm/mostFrequent.js +18 -0
- package/esm/mostFrequent.js.map +1 -0
- package/esm/noop.d.ts +3 -0
- package/esm/noop.d.ts.map +1 -0
- package/esm/noop.js +3 -0
- package/esm/noop.js.map +1 -0
- package/esm/omit.d.ts +5 -0
- package/esm/omit.d.ts.map +1 -0
- package/esm/omit.js +20 -0
- package/esm/omit.js.map +1 -0
- package/esm/pick.d.ts +5 -0
- package/esm/pick.d.ts.map +1 -0
- package/esm/pick.js +17 -0
- package/esm/pick.js.map +1 -0
- package/esm/pull.d.ts +3 -0
- package/esm/pull.d.ts.map +1 -0
- package/esm/pull.js +11 -0
- package/esm/pull.js.map +1 -0
- package/esm/remove.d.ts +3 -0
- package/esm/remove.d.ts.map +1 -0
- package/esm/remove.js +15 -0
- package/esm/remove.js.map +1 -0
- package/esm/rethrow.d.ts +3 -0
- package/esm/rethrow.d.ts.map +1 -0
- package/esm/rethrow.js +5 -0
- package/esm/rethrow.js.map +1 -0
- package/esm/scale.d.ts +3 -0
- package/esm/scale.d.ts.map +1 -0
- package/esm/scale.js +5 -0
- package/esm/scale.js.map +1 -0
- package/esm/seq.d.ts +8 -0
- package/esm/seq.d.ts.map +1 -0
- package/esm/seq.js +41 -0
- package/esm/seq.js.map +1 -0
- package/esm/set.d.ts +7 -0
- package/esm/set.d.ts.map +1 -0
- package/esm/set.js +22 -0
- package/esm/set.js.map +1 -0
- package/esm/setImmutable.d.ts +8 -0
- package/esm/setImmutable.d.ts.map +1 -0
- package/esm/setImmutable.js +56 -0
- package/esm/setImmutable.js.map +1 -0
- package/esm/sortBy.d.ts +3 -0
- package/esm/sortBy.d.ts.map +1 -0
- package/esm/sortBy.js +12 -0
- package/esm/sortBy.js.map +1 -0
- package/esm/throttle.d.ts +13 -0
- package/esm/throttle.d.ts.map +1 -0
- package/esm/throttle.js +79 -0
- package/esm/throttle.js.map +1 -0
- package/esm/truthy.d.ts +3 -0
- package/esm/truthy.d.ts.map +1 -0
- package/esm/truthy.js +5 -0
- package/esm/truthy.js.map +1 -0
- package/esm/wait.d.ts +3 -0
- package/esm/wait.d.ts.map +1 -0
- package/esm/wait.js +7 -0
- package/esm/wait.js.map +1 -0
- package/esm/waitFor.d.ts +3 -0
- package/esm/waitFor.d.ts.map +1 -0
- package/esm/waitFor.js +31 -0
- package/esm/waitFor.js.map +1 -0
- package/esm/waitSync.d.ts +3 -0
- package/esm/waitSync.d.ts.map +1 -0
- package/esm/waitSync.js +6 -0
- package/esm/waitSync.js.map +1 -0
- package/package.json +75 -0
- package/src/cap.spec.ts +36 -0
- package/src/cap.ts +19 -0
- package/src/capitalize.spec.ts +18 -0
- package/src/capitalize.ts +16 -0
- package/src/coalesce.spec.ts +23 -0
- package/src/coalesce.ts +21 -0
- package/src/ensureArray.spec.ts +87 -0
- package/src/ensureArray.ts +13 -0
- package/src/ensureError.spec.ts +29 -0
- package/src/ensureError.ts +15 -0
- package/src/get.spec.ts +183 -0
- package/src/get.ts +53 -0
- package/src/getMultiple.spec.ts +25 -0
- package/src/getMultiple.ts +47 -0
- package/src/index.ts +33 -0
- package/src/insertSeparator.spec.ts +29 -0
- package/src/insertSeparator.ts +22 -0
- package/src/isEmpty.spec.ts +130 -0
- package/src/isEmpty.ts +50 -0
- package/src/isPlainObject.spec.ts +42 -0
- package/src/isPlainObject.ts +18 -0
- package/src/last.spec.ts +88 -0
- package/src/last.ts +12 -0
- package/src/mapAsync.spec.ts +39 -0
- package/src/mapAsync.ts +41 -0
- package/src/mapValues.spec.ts +178 -0
- package/src/mapValues.ts +57 -0
- package/src/match.spec.ts +11 -0
- package/src/match.ts +30 -0
- package/src/merge.spec.ts +69 -0
- package/src/merge.ts +58 -0
- package/src/mostFrequent.spec.ts +35 -0
- package/src/mostFrequent.ts +27 -0
- package/src/noop.ts +8 -0
- package/src/omit.spec.ts +181 -0
- package/src/omit.ts +43 -0
- package/src/pick.spec.ts +168 -0
- package/src/pick.ts +39 -0
- package/src/pull.spec.ts +54 -0
- package/src/pull.ts +18 -0
- package/src/remove.spec.ts +63 -0
- package/src/remove.ts +26 -0
- package/src/rethrow.ts +12 -0
- package/src/scale.ts +18 -0
- package/src/seq.spec.ts +157 -0
- package/src/seq.ts +104 -0
- package/src/set.spec.ts +348 -0
- package/src/set.ts +59 -0
- package/src/setImmutable.spec.ts +241 -0
- package/src/setImmutable.ts +104 -0
- package/src/sortBy.spec.ts +56 -0
- package/src/sortBy.ts +24 -0
- package/src/throttle.spec.ts +136 -0
- package/src/throttle.ts +153 -0
- package/src/truthy.spec.ts +21 -0
- package/src/truthy.ts +13 -0
- package/src/wait.spec.ts +21 -0
- package/src/wait.ts +14 -0
- package/src/waitFor.ts +47 -0
- package/src/waitSync.spec.ts +21 -0
- package/src/waitSync.ts +14 -0
- package/tutorials/Introduction.md +1 -0
package/src/omit.spec.ts
ADDED
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { omit as _omit } from "lodash";
|
|
2
|
+
|
|
3
|
+
import { omit } from "./omit.js";
|
|
4
|
+
|
|
5
|
+
describe("omit", () => {
|
|
6
|
+
it("returns empty object when source is not given, behaves like lodash", () => {
|
|
7
|
+
caseundefined: {
|
|
8
|
+
const value = omit(undefined, ["name", "count"]);
|
|
9
|
+
value.must.eql({});
|
|
10
|
+
|
|
11
|
+
const _value = _omit(undefined, ["name", "count"]);
|
|
12
|
+
_value.must.eql({});
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
casenull: {
|
|
16
|
+
const value = omit(null, ["name", "count"]);
|
|
17
|
+
value.must.eql({});
|
|
18
|
+
|
|
19
|
+
const _value = _omit(null, ["name", "count"]);
|
|
20
|
+
_value.must.eql({});
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it("returns cloned object when properties are not given, behaves like lodash", () => {
|
|
25
|
+
const source = {
|
|
26
|
+
title: "Master od disaster",
|
|
27
|
+
count: 1,
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
caseundefined: {
|
|
31
|
+
const value = omit(source);
|
|
32
|
+
value.must.eql({
|
|
33
|
+
title: "Master od disaster",
|
|
34
|
+
count: 1,
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const _value = _omit(source);
|
|
38
|
+
_value.must.eql({
|
|
39
|
+
title: "Master od disaster",
|
|
40
|
+
count: 1,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
caseempty: {
|
|
45
|
+
const value = omit(source, []);
|
|
46
|
+
value.must.eql({
|
|
47
|
+
title: "Master od disaster",
|
|
48
|
+
count: 1,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const _value = _omit(source, []);
|
|
52
|
+
_value.must.eql({
|
|
53
|
+
title: "Master od disaster",
|
|
54
|
+
count: 1,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it("returns empty object when source is number, behaves like lodash", () => {
|
|
60
|
+
const value = omit(2, ["title"]);
|
|
61
|
+
value.must.eql({});
|
|
62
|
+
|
|
63
|
+
const _value = _omit(2, ["title"]);
|
|
64
|
+
_value.must.eql({});
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("returns empty object when source is string, behaves NOT like lodash", () => {
|
|
68
|
+
const value = omit("two", ["title"]);
|
|
69
|
+
value.must.eql({});
|
|
70
|
+
|
|
71
|
+
const _value = _omit("two", ["title"]);
|
|
72
|
+
_value.must.eql({ 0: "t", 1: "w", 2: "o" });
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("returns new object when source is function, behaves like lodash", () => {
|
|
76
|
+
const source = () => {};
|
|
77
|
+
source.title = "ABCD";
|
|
78
|
+
source.weight = "XXX";
|
|
79
|
+
|
|
80
|
+
const value = omit(source, ["title"]);
|
|
81
|
+
value.must.eql({
|
|
82
|
+
weight: "XXX",
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const _value = _omit(source, ["title"]);
|
|
86
|
+
_value.must.eql({
|
|
87
|
+
weight: "XXX",
|
|
88
|
+
});
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("returns new object without given properties, behaves like lodash", () => {
|
|
92
|
+
const source = {
|
|
93
|
+
a: 5,
|
|
94
|
+
b: "string",
|
|
95
|
+
c: undefined,
|
|
96
|
+
d: 0,
|
|
97
|
+
e: null,
|
|
98
|
+
f: false,
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const value = omit(source, ["a", "d", "g"]);
|
|
102
|
+
value.must.eql({
|
|
103
|
+
b: "string",
|
|
104
|
+
c: undefined,
|
|
105
|
+
e: null,
|
|
106
|
+
f: false,
|
|
107
|
+
});
|
|
108
|
+
value.must.not.equal(source);
|
|
109
|
+
|
|
110
|
+
const _value = _omit(source, ["a", "d", "g"]);
|
|
111
|
+
_value.must.eql({
|
|
112
|
+
b: "string",
|
|
113
|
+
c: undefined,
|
|
114
|
+
e: null,
|
|
115
|
+
f: false,
|
|
116
|
+
});
|
|
117
|
+
_value.must.not.equal(source);
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it("returns inherited properties (as own), behaves like lodash", () => {
|
|
121
|
+
const Test = function() {};
|
|
122
|
+
Test.prototype.name = "xxx";
|
|
123
|
+
|
|
124
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
125
|
+
const testInstance = new Test();
|
|
126
|
+
testInstance.length = 100;
|
|
127
|
+
|
|
128
|
+
const value = omit(testInstance, ["title"]);
|
|
129
|
+
value.must.eql({
|
|
130
|
+
name: "xxx",
|
|
131
|
+
length: 100,
|
|
132
|
+
});
|
|
133
|
+
value.hasOwnProperty("name").must.be.true();
|
|
134
|
+
value.hasOwnProperty("length").must.be.true();
|
|
135
|
+
|
|
136
|
+
const _value = _omit(testInstance, ["title"]);
|
|
137
|
+
_value.must.eql({
|
|
138
|
+
name: "xxx",
|
|
139
|
+
length: 100,
|
|
140
|
+
});
|
|
141
|
+
_value.hasOwnProperty("name").must.be.true();
|
|
142
|
+
_value.hasOwnProperty("length").must.be.true();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it("skips non-enumerable properties, behaves like lodash", () => {
|
|
146
|
+
const source = {
|
|
147
|
+
aaa: 123,
|
|
148
|
+
title: 666,
|
|
149
|
+
};
|
|
150
|
+
Object.defineProperty(source, "name", {
|
|
151
|
+
enumerable: false,
|
|
152
|
+
value: "test",
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
const value = omit(source, ["title"]);
|
|
156
|
+
value.must.eql({
|
|
157
|
+
aaa: 123,
|
|
158
|
+
});
|
|
159
|
+
("name" in value).must.be.false();
|
|
160
|
+
|
|
161
|
+
const _value = _omit(source, ["title"]);
|
|
162
|
+
_value.must.eql({
|
|
163
|
+
aaa: 123,
|
|
164
|
+
});
|
|
165
|
+
("name" in _value).must.be.false();
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("returns object when array is given as source, behaves like lodash", () => {
|
|
169
|
+
const source = ["a", "b", "c"];
|
|
170
|
+
|
|
171
|
+
const value = omit(source, [0, "2", 15, "xxx"]);
|
|
172
|
+
value.must.eql({
|
|
173
|
+
1: "b",
|
|
174
|
+
});
|
|
175
|
+
|
|
176
|
+
const _value = _omit(source, [0, "2", 15, "xxx"]);
|
|
177
|
+
_value.must.eql({
|
|
178
|
+
1: "b",
|
|
179
|
+
});
|
|
180
|
+
});
|
|
181
|
+
});
|
package/src/omit.ts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
// TODO verify & maybe fix typings when object is an Array
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns new object with copied all properties without these specified.
|
|
5
|
+
*
|
|
6
|
+
* @param {Object} object - source object
|
|
7
|
+
* @param {Array.<string>} props - properties to skip
|
|
8
|
+
* @example
|
|
9
|
+
* omit({ name: "Jack", age: 69 }, ["age", "title"]);
|
|
10
|
+
* // { name: "Jack" }
|
|
11
|
+
* @example
|
|
12
|
+
* omit(["hello", "world"], [0]);
|
|
13
|
+
* // { 1: "world" }
|
|
14
|
+
* @returns {Object} - new object without given properties
|
|
15
|
+
*/
|
|
16
|
+
const omit = <T extends object, K extends keyof T>(
|
|
17
|
+
object: T | null, props: K[],
|
|
18
|
+
): T extends null ? { [key: string]: never } : Omit<T, K> => {
|
|
19
|
+
if (!object || (typeof object !== "object" && typeof object !== "function")) {
|
|
20
|
+
// @ts-expect-error TS can't handle implementation of dynamic return types yet
|
|
21
|
+
return {};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (!Array.isArray(props) || !props.length) {
|
|
25
|
+
// @ts-expect-error TS can't handle implementation of dynamic return types yet
|
|
26
|
+
return {
|
|
27
|
+
...object,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const useProps = props.map(String);
|
|
32
|
+
|
|
33
|
+
const result = {};
|
|
34
|
+
for (const key in object) {
|
|
35
|
+
if (!useProps.includes(key)) {
|
|
36
|
+
(result as T)[key] = object[key];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
// @ts-expect-error TS can't handle implementation of dynamic return types yet
|
|
40
|
+
return result;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export { omit };
|
package/src/pick.spec.ts
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
import { pick as _pick } from "lodash";
|
|
2
|
+
|
|
3
|
+
import { pick } from "./pick.js";
|
|
4
|
+
|
|
5
|
+
describe("pick", () => {
|
|
6
|
+
it("returns empty object when source is not given, behaves like lodash", () => {
|
|
7
|
+
caseundefined: {
|
|
8
|
+
const value = pick(undefined, ["name", "count"]);
|
|
9
|
+
value.must.eql({});
|
|
10
|
+
|
|
11
|
+
// eslint-disable-next-line @typescript-eslint/no-confusing-void-expression
|
|
12
|
+
const _value = _pick(undefined, ["name", "count"]);
|
|
13
|
+
_value.must.eql({});
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
casenull: {
|
|
17
|
+
const value = pick(null, ["name", "count"]);
|
|
18
|
+
value.must.eql({});
|
|
19
|
+
|
|
20
|
+
const _value = _pick(null, ["name", "count"]);
|
|
21
|
+
_value.must.eql({});
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("returns empty object when properties to copy are not given, behaves like lodash", () => {
|
|
26
|
+
const source = {
|
|
27
|
+
title: "Master od disaster",
|
|
28
|
+
count: 1,
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
caseundefined: {
|
|
32
|
+
const value = pick(source);
|
|
33
|
+
value.must.eql({});
|
|
34
|
+
|
|
35
|
+
const _value = _pick(source);
|
|
36
|
+
_value.must.eql({});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
caseempty: {
|
|
40
|
+
const value = pick(source, []);
|
|
41
|
+
value.must.eql({});
|
|
42
|
+
|
|
43
|
+
const _value = _pick(source, []);
|
|
44
|
+
_value.must.eql({});
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("returns empty object when source is primitive, behaves like lodash", () => {
|
|
49
|
+
number: {
|
|
50
|
+
const value = pick(2, ["title"]);
|
|
51
|
+
value.must.eql({});
|
|
52
|
+
|
|
53
|
+
const _value = _pick(2, ["title"]);
|
|
54
|
+
_value.must.eql({});
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
string: {
|
|
58
|
+
const value = pick("two", ["title"]);
|
|
59
|
+
value.must.eql({});
|
|
60
|
+
|
|
61
|
+
const _value = _pick("two", ["title"]);
|
|
62
|
+
_value.must.eql({});
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("returns new object with expected properties, ignores not defined properties, behaves like lodash", () => {
|
|
67
|
+
const source = {
|
|
68
|
+
a: 5,
|
|
69
|
+
b: "string",
|
|
70
|
+
c: undefined,
|
|
71
|
+
d: 0,
|
|
72
|
+
e: null,
|
|
73
|
+
f: false,
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
const value = pick(source, ["a", "b", "c", "d", "e", "f", "g"]);
|
|
77
|
+
value.must.eql({
|
|
78
|
+
a: 5,
|
|
79
|
+
b: "string",
|
|
80
|
+
c: undefined,
|
|
81
|
+
d: 0,
|
|
82
|
+
e: null,
|
|
83
|
+
f: false,
|
|
84
|
+
});
|
|
85
|
+
value.must.not.equal(source);
|
|
86
|
+
|
|
87
|
+
const _value = _pick(source, ["a", "b", "c", "d", "e", "f", "g"]);
|
|
88
|
+
_value.must.eql({
|
|
89
|
+
a: 5,
|
|
90
|
+
b: "string",
|
|
91
|
+
c: undefined,
|
|
92
|
+
d: 0,
|
|
93
|
+
e: null,
|
|
94
|
+
f: false,
|
|
95
|
+
});
|
|
96
|
+
_value.must.not.equal(source);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("returns new object when source is function, behaves like lodash", () => {
|
|
100
|
+
const source = () => {};
|
|
101
|
+
source.title = "ABCD";
|
|
102
|
+
|
|
103
|
+
const value = pick(source, ["title"]);
|
|
104
|
+
value.must.eql({
|
|
105
|
+
title: "ABCD",
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const _value = _pick(source, ["title"]);
|
|
109
|
+
_value.must.eql({
|
|
110
|
+
title: "ABCD",
|
|
111
|
+
});
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("returns inherited properties, behaves like lodash", () => {
|
|
115
|
+
const Test = function() {};
|
|
116
|
+
Test.prototype.count = 1;
|
|
117
|
+
|
|
118
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
119
|
+
const testInstance = new Test();
|
|
120
|
+
testInstance.length = 2;
|
|
121
|
+
|
|
122
|
+
const value = pick(testInstance, ["count", "length"]);
|
|
123
|
+
value.must.eql({
|
|
124
|
+
count: 1,
|
|
125
|
+
length: 2,
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
const _value = _pick(testInstance, ["count", "length"]);
|
|
129
|
+
_value.must.eql({
|
|
130
|
+
count: 1,
|
|
131
|
+
length: 2,
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("returns non-enumerable properties, behaves like lodash", () => {
|
|
136
|
+
const source = {};
|
|
137
|
+
Object.defineProperty(source, "count", {
|
|
138
|
+
enumerable: false,
|
|
139
|
+
value: 23,
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
const value = pick(source, ["count"]);
|
|
143
|
+
value.must.eql({
|
|
144
|
+
count: 23,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const _value = _pick(source, ["count"]);
|
|
148
|
+
_value.must.eql({
|
|
149
|
+
count: 23,
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("returns object when array is given as source, behaves like lodash", () => {
|
|
154
|
+
const source = ["a", "b", "c"];
|
|
155
|
+
|
|
156
|
+
const value = pick(source, [0, "2", 15, "xxx"]);
|
|
157
|
+
value.must.eql({
|
|
158
|
+
0: "a",
|
|
159
|
+
2: "c",
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
const _value = _pick(source, [0, "2", 15, "xxx"]);
|
|
163
|
+
_value.must.eql({
|
|
164
|
+
0: "a",
|
|
165
|
+
2: "c",
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
});
|
package/src/pick.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
// TODO verify & maybe fix typings when object is an Array
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Returns new object with copied given properties from source object.
|
|
5
|
+
*
|
|
6
|
+
* @param {Object} object - source object
|
|
7
|
+
* @param {Array.<string>} props - properties to copy
|
|
8
|
+
* @example
|
|
9
|
+
* pick({ name: "Jack", age: 69 }, ["age", "title"]);
|
|
10
|
+
* // { age: 69 }
|
|
11
|
+
* @example
|
|
12
|
+
* pick(["hello", "world"], [0]);
|
|
13
|
+
* // { 0: hello }
|
|
14
|
+
* @returns {Object} - new object with given properties
|
|
15
|
+
*/
|
|
16
|
+
const pick = <T extends object, K extends keyof T>(
|
|
17
|
+
object: T | null, props: K[],
|
|
18
|
+
): T extends null ? { [ key: string]: never } : Pick<T, K> => {
|
|
19
|
+
if (
|
|
20
|
+
!object
|
|
21
|
+
|| (typeof object !== "object" && typeof object !== "function")
|
|
22
|
+
|| !Array.isArray(props)
|
|
23
|
+
|| !props.length
|
|
24
|
+
) {
|
|
25
|
+
// @ts-expect-error TS can't handle implementation of dynamic return types yet
|
|
26
|
+
return {};
|
|
27
|
+
}
|
|
28
|
+
const result = {};
|
|
29
|
+
props.forEach(property => {
|
|
30
|
+
if (property in object) {
|
|
31
|
+
(result as T)[property] = object[property];
|
|
32
|
+
}
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
// @ts-expect-error TS can't handle implementation of dynamic return types yet
|
|
36
|
+
return result;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
export { pick };
|
package/src/pull.spec.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { pull } from "./pull.js";
|
|
2
|
+
|
|
3
|
+
describe("pull", function() {
|
|
4
|
+
it("should remove all given values", function() {
|
|
5
|
+
const source = [1, 2, 1, 2, 1, 2, 1, 1, 2, 2, 1, 1, 2, 1, 2, 2];
|
|
6
|
+
const result = pull(source, 1);
|
|
7
|
+
(source === result).must.be.true();
|
|
8
|
+
|
|
9
|
+
result.includes(1).must.be.false();
|
|
10
|
+
result.must.eql([2, 2, 2, 2, 2, 2, 2, 2]);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("should be able to clear the array", function() {
|
|
14
|
+
const source = [1, 2, 1, 2, 1, 2, 1, 1, 2, 2, 1, 1, 2, 1, 2, 2];
|
|
15
|
+
const result = pull(source, 1, 2);
|
|
16
|
+
(source === result).must.be.true();
|
|
17
|
+
result.must.have.length(0);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should remove NaNs", function() {
|
|
21
|
+
const source = [1, NaN, 3];
|
|
22
|
+
pull(source, NaN);
|
|
23
|
+
source.must.eql([1, 3]);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("should remove nothing", function() {
|
|
27
|
+
const source = [1, NaN, 3, undefined];
|
|
28
|
+
pull(source);
|
|
29
|
+
source.must.eql([1, NaN, 3, undefined]);
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("should remove null or undefined or referenced objects", function() {
|
|
33
|
+
const obj = {};
|
|
34
|
+
const source = [1, undefined, 2, null, 3, obj, 4, {}];
|
|
35
|
+
pull(source, obj, null, undefined);
|
|
36
|
+
source.must.eql([1, 2, 3, 4, {}]);
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should work with sparse arrays, removing holes when requested to remove undefined", function() {
|
|
40
|
+
// eslint-disable-next-line no-sparse-arrays
|
|
41
|
+
const source = [1, , 3, , 5];
|
|
42
|
+
pull(source, undefined);
|
|
43
|
+
source.must.eql([1, 3, 5]);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("should work with sparse arrays, keeping holes when requested", function() {
|
|
47
|
+
// eslint-disable-next-line no-sparse-arrays
|
|
48
|
+
const source = [1, , 3, , 5];
|
|
49
|
+
pull(source, null);
|
|
50
|
+
// eslint-disable-next-line no-sparse-arrays
|
|
51
|
+
source.must.eql([1, ,3, , 5]);
|
|
52
|
+
("1" in source).must.be.false();
|
|
53
|
+
});
|
|
54
|
+
});
|
package/src/pull.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modifies the array, removing all occurrences of specified values
|
|
3
|
+
* @param {Array} array
|
|
4
|
+
* @param {...*}valuesToPull
|
|
5
|
+
*/
|
|
6
|
+
const pull = <T>(array: T[], ...valuesToPull: T[]): T[] => {
|
|
7
|
+
const len = array.length;
|
|
8
|
+
for (let i = len - 1; i >= 0; i--) {
|
|
9
|
+
if (valuesToPull.includes(array[i])) {
|
|
10
|
+
array.splice(i, 1);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return array;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export {
|
|
17
|
+
pull,
|
|
18
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import { remove as _remove } from "lodash";
|
|
2
|
+
|
|
3
|
+
import createSpy from "../test/createSpy.js";
|
|
4
|
+
|
|
5
|
+
import { remove } from "./remove.js";
|
|
6
|
+
|
|
7
|
+
describe("remove", () => {
|
|
8
|
+
it("removes values", () => {
|
|
9
|
+
const fn = x => x % 2 === 0;
|
|
10
|
+
|
|
11
|
+
const src = [1, 2, 3, 4, 5, 6, 7];
|
|
12
|
+
const _src = [...src];
|
|
13
|
+
|
|
14
|
+
remove(src, fn);
|
|
15
|
+
src.must.eql([1, 3, 5, 7]);
|
|
16
|
+
|
|
17
|
+
_remove(_src, fn);
|
|
18
|
+
_src.must.eql([1, 3, 5, 7]);
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
it("returns removed values", () => {
|
|
22
|
+
const fn = x => x % 2 === 0;
|
|
23
|
+
|
|
24
|
+
const src = [1, 2, 3, 4, 5, 6, 7];
|
|
25
|
+
const _src = [...src];
|
|
26
|
+
|
|
27
|
+
const removed = remove(src, fn);
|
|
28
|
+
removed.must.eql([2, 4, 6]);
|
|
29
|
+
|
|
30
|
+
const _removed = _remove(_src, fn);
|
|
31
|
+
_removed.must.eql([2, 4, 6]);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("calls test function with correct arguments", () => {
|
|
35
|
+
const fn = x => x % 2 === 0;
|
|
36
|
+
const spy = createSpy(fn);
|
|
37
|
+
const _spy = createSpy(fn);
|
|
38
|
+
|
|
39
|
+
const src = [1, 2, 3, 4, 5, 6, 7];
|
|
40
|
+
const _src = [...src];
|
|
41
|
+
const srcCopy = [...src];
|
|
42
|
+
|
|
43
|
+
remove(src, spy);
|
|
44
|
+
spy.__spy.calls.must.have.length(7);
|
|
45
|
+
spy.__spy.calls[0].must.eql([1, 0, srcCopy]);
|
|
46
|
+
spy.__spy.calls[1].must.eql([2, 1, srcCopy]);
|
|
47
|
+
spy.__spy.calls[2].must.eql([3, 2, srcCopy]);
|
|
48
|
+
spy.__spy.calls[3].must.eql([4, 3, srcCopy]);
|
|
49
|
+
spy.__spy.calls[4].must.eql([5, 4, srcCopy]);
|
|
50
|
+
spy.__spy.calls[5].must.eql([6, 5, srcCopy]);
|
|
51
|
+
spy.__spy.calls[6].must.eql([7, 6, srcCopy]);
|
|
52
|
+
|
|
53
|
+
remove(_src, _spy);
|
|
54
|
+
_spy.__spy.calls.must.have.length(7);
|
|
55
|
+
_spy.__spy.calls[0].must.eql([1, 0, srcCopy]);
|
|
56
|
+
_spy.__spy.calls[1].must.eql([2, 1, srcCopy]);
|
|
57
|
+
_spy.__spy.calls[2].must.eql([3, 2, srcCopy]);
|
|
58
|
+
_spy.__spy.calls[3].must.eql([4, 3, srcCopy]);
|
|
59
|
+
_spy.__spy.calls[4].must.eql([5, 4, srcCopy]);
|
|
60
|
+
_spy.__spy.calls[5].must.eql([6, 5, srcCopy]);
|
|
61
|
+
_spy.__spy.calls[6].must.eql([7, 6, srcCopy]);
|
|
62
|
+
});
|
|
63
|
+
});
|
package/src/remove.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Removes values from array using function as predicate. Returns removed values.
|
|
3
|
+
*
|
|
4
|
+
* @param {Array} array
|
|
5
|
+
* @param {function} fn
|
|
6
|
+
* @returns {Array}
|
|
7
|
+
*/
|
|
8
|
+
const remove = <T>(array: T[], fn: (elem: T, num: number, list: T[]) => boolean): T[] => {
|
|
9
|
+
const len = array.length;
|
|
10
|
+
|
|
11
|
+
const idsToRemove = []; // not removing instantly to feed `fn` untouched array for each iteration, lodash does that
|
|
12
|
+
const removedValues = [];
|
|
13
|
+
|
|
14
|
+
for (let counter = 0; counter < len; counter++) {
|
|
15
|
+
if (fn(array[counter], counter, array)) {
|
|
16
|
+
idsToRemove.push(counter - idsToRemove.length);
|
|
17
|
+
removedValues.push(array[counter]);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
idsToRemove.forEach(id => array.splice(id, 1));
|
|
22
|
+
|
|
23
|
+
return removedValues;
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
export { remove };
|
package/src/rethrow.ts
ADDED
package/src/scale.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Changes the scale of given value, i.e.: replaces values from 2.9 to 3.3 into values from 0 to 100. Useful for i.e.:
|
|
3
|
+
* displaying measured battery voltage as a percent value.
|
|
4
|
+
* Pro-tip: use `Function.prototype.bind` to create yourself a function with bound 4 first arguments to re-use without
|
|
5
|
+
* repeating the from-to scale.
|
|
6
|
+
* @param {number} fromMin
|
|
7
|
+
* @param {number} fromMax
|
|
8
|
+
* @param {number} toMin
|
|
9
|
+
* @param {number} toMax
|
|
10
|
+
* @param {number} number
|
|
11
|
+
*/
|
|
12
|
+
const scale = (fromMin: number, fromMax: number, toMin: number, toMax: number, number: number) => {
|
|
13
|
+
return toMin + ((number - fromMin) / (fromMax - fromMin) * (toMax - toMin));
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export {
|
|
17
|
+
scale,
|
|
18
|
+
};
|