@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
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
const isObject = (value) => (typeof value === "object" || typeof value === "function") && value !== null;
|
|
2
|
+
const clone = (value) => {
|
|
3
|
+
if (Array.isArray(value)) {
|
|
4
|
+
return [...value];
|
|
5
|
+
}
|
|
6
|
+
return { ...value };
|
|
7
|
+
};
|
|
8
|
+
const hasOnlyValidPathParts = (array) => {
|
|
9
|
+
if (!array.length) {
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
return array.every(item => {
|
|
13
|
+
const type = typeof item;
|
|
14
|
+
return (type === "string" && item !== "") || type === "number";
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
const getPathParts = (path) => {
|
|
18
|
+
if (typeof path === "number") {
|
|
19
|
+
return [String(path)];
|
|
20
|
+
}
|
|
21
|
+
if (typeof path === "string") {
|
|
22
|
+
return path.split(".");
|
|
23
|
+
}
|
|
24
|
+
if (Array.isArray(path)) {
|
|
25
|
+
return path;
|
|
26
|
+
}
|
|
27
|
+
throw new TypeError("Path must be a string, a number or an array of strings and numbers");
|
|
28
|
+
};
|
|
29
|
+
const setImmutable = (source, path, value) => {
|
|
30
|
+
const pathParts = getPathParts(path);
|
|
31
|
+
const isValidPath = hasOnlyValidPathParts(pathParts);
|
|
32
|
+
if (!isValidPath) {
|
|
33
|
+
throw new TypeError("Path must not be empty or contain empty parts");
|
|
34
|
+
}
|
|
35
|
+
const len = pathParts.length;
|
|
36
|
+
const result = isObject(source) ? clone(source) : {};
|
|
37
|
+
let current = result;
|
|
38
|
+
for (let i = 0; i < len; i++) {
|
|
39
|
+
const isLast = i === len - 1;
|
|
40
|
+
const key = pathParts[i];
|
|
41
|
+
if (isLast) {
|
|
42
|
+
current[key] = value;
|
|
43
|
+
return result;
|
|
44
|
+
}
|
|
45
|
+
if (!isObject(current[key])) {
|
|
46
|
+
current[key] = {};
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
current[key] = clone(current[key]);
|
|
50
|
+
}
|
|
51
|
+
current = current[key];
|
|
52
|
+
}
|
|
53
|
+
return result;
|
|
54
|
+
};
|
|
55
|
+
export { setImmutable };
|
|
56
|
+
//# sourceMappingURL=setImmutable.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"setImmutable.js","sourceRoot":"","sources":["../src/setImmutable.ts"],"names":[],"mappings":"AAOA,MAAM,QAAQ,GAAG,CAAC,KAAc,EAAE,EAAE,CAAC,CAAC,OAAO,KAAK,KAAK,QAAQ,IAAI,OAAO,KAAK,KAAK,UAAU,CAAC,IAAI,KAAK,KAAK,IAAI,CAAC;AAElH,MAAM,KAAK,GAAG,CAAC,KAAyB,EAAE,EAAE;IACxC,IAAI,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE;QACtB,OAAO,CAAC,GAAG,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CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC;IAErD,IAAI,OAAO,GAAqB,MAAM,CAAC;IACvC,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,EAAE,CAAC,EAAE,EAAE;QAC1B,MAAM,MAAM,GAAG,CAAC,KAAK,GAAG,GAAG,CAAC,CAAC;QAC7B,MAAM,GAAG,GAAG,SAAS,CAAC,CAAC,CAAC,CAAC;QACzB,IAAI,MAAM,EAAE;YACP,OAAkB,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;YACjC,OAAO,MAAM,CAAC;SACjB;QACD,IAAI,CAAC,QAAQ,CAAE,OAAkB,CAAC,GAAG,CAAC,CAAC,EAAE;YACpC,OAAkB,CAAC,GAAG,CAAC,GAAG,EAAE,CAAC;SACjC;aACI;YACA,OAAkB,CAAC,GAAG,CAAC,GAAG,KAAK,CAAE,OAAkB,CAAC,GAAG,CAAuB,CAAC,CAAC;SACpF;QACD,OAAO,GAAI,OAAkB,CAAC,GAAG,CAAC,CAAC;KACtC;IACD,OAAO,MAAM,CAAC;AAClB,CAAC,CAAC;AAEF,OAAO,EAAE,YAAY,EAAE,CAAC"}
|
package/esm/sortBy.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sortBy.d.ts","sourceRoot":"","sources":["../src/sortBy.ts"],"names":[],"mappings":"AAOA,QAAA,MAAM,MAAM,2DAAwD,OAAO,+BAY1E,CAAC;AAEF,OAAO,EACH,MAAM,GACT,CAAC"}
|
package/esm/sortBy.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
const sortBy = (propertyName, asc = true, defaultValue = null) => (a, b) => {
|
|
2
|
+
var _a, _b;
|
|
3
|
+
if (a[propertyName] === b[propertyName]) {
|
|
4
|
+
return 0;
|
|
5
|
+
}
|
|
6
|
+
if (((_a = a[propertyName]) !== null && _a !== void 0 ? _a : defaultValue) > ((_b = b[propertyName]) !== null && _b !== void 0 ? _b : defaultValue)) {
|
|
7
|
+
return asc ? 1 : -1;
|
|
8
|
+
}
|
|
9
|
+
return asc ? -1 : 1;
|
|
10
|
+
};
|
|
11
|
+
export { sortBy, };
|
|
12
|
+
//# sourceMappingURL=sortBy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sortBy.js","sourceRoot":"","sources":["../src/sortBy.ts"],"names":[],"mappings":"AAOA,MAAM,MAAM,GAAG,CAAI,YAAqB,EAAE,GAAG,GAAG,IAAI,EAAE,eAAwB,IAAI,EAAE,EAAE,CAAC,CAAC,CAAI,EAAE,CAAI,EAAE,EAAE;;IAClG,IAAI,CAAC,CAAC,YAAY,CAAC,KAAK,CAAC,CAAC,YAAY,CAAC,EAAE;QACrC,OAAO,CAAC,CAAC;KACZ;IAGD,IAAI,CAAC,MAAA,CAAC,CAAC,YAAY,CAAC,mCAAI,YAAY,CAAC,GAAG,CAAC,MAAA,CAAC,CAAC,YAAY,CAAC,mCAAI,YAAY,CAAC,EAAE;QAEvE,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;KACvB;IAED,OAAO,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AACxB,CAAC,CAAC;AAEF,OAAO,EACH,MAAM,GACT,CAAC"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
interface Opts {
|
|
2
|
+
leading?: boolean;
|
|
3
|
+
trailing?: boolean;
|
|
4
|
+
}
|
|
5
|
+
interface Extras {
|
|
6
|
+
cancel: () => void;
|
|
7
|
+
flush: () => void;
|
|
8
|
+
}
|
|
9
|
+
type CanReturnUndefined<F extends (...args: any[]) => any> = (...args: Parameters<F>) => ReturnType<F> | undefined;
|
|
10
|
+
declare const throttle: <RT, F extends (...args: any[]) => RT>(fn: F, time?: number | [number, ...number[]], options?: Opts) => CanReturnUndefined<F> & Extras;
|
|
11
|
+
export { throttle, };
|
|
12
|
+
export type { Extras as ThrottledFunctionExtras, Opts as ThrottleOptions, CanReturnUndefined as ThrottledFunction, };
|
|
13
|
+
//# sourceMappingURL=throttle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttle.d.ts","sourceRoot":"","sources":["../src/throttle.ts"],"names":[],"mappings":"AAAA,UAAU,IAAI;IAIV,OAAO,CAAC,EAAE,OAAO,CAAC;IAIlB,QAAQ,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,UAAU,MAAM;IAIZ,MAAM,EAAE,MAAM,IAAI,CAAC;IAInB,KAAK,EAAE,MAAM,IAAI,CAAC;CACrB;AAQD,KAAK,kBAAkB,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,GAAG,IAAI,CAAC,GAAG,IAAI,EAAE,UAAU,CAAC,CAAC,CAAC,KAAK,UAAU,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC;AAoBnH,QAAA,MAAM,QAAQ,2BAA4B,GAAG,EAAE,uBAC9B,MAAM,GAAG,CAAC,MAAM,EAAE,GAAG,MAAM,EAAE,CAAC,YAAgB,IAAI,mCA6FlE,CAAC;AAEF,OAAO,EACH,QAAQ,GACX,CAAC;AAEF,YAAY,EACR,MAAM,IAAI,uBAAuB,EACjC,IAAI,IAAI,eAAe,EACvB,kBAAkB,IAAI,iBAAiB,GAC1C,CAAC"}
|
package/esm/throttle.js
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
const defaultOptions = {
|
|
2
|
+
leading: true,
|
|
3
|
+
trailing: true,
|
|
4
|
+
};
|
|
5
|
+
const throttle = (fn, time = 0, options) => {
|
|
6
|
+
var _a, _b;
|
|
7
|
+
const opts = {
|
|
8
|
+
leading: (_a = options === null || options === void 0 ? void 0 : options.leading) !== null && _a !== void 0 ? _a : defaultOptions.leading,
|
|
9
|
+
trailing: (_b = options === null || options === void 0 ? void 0 : options.trailing) !== null && _b !== void 0 ? _b : defaultOptions.trailing,
|
|
10
|
+
};
|
|
11
|
+
if (!opts.trailing && !opts.leading) {
|
|
12
|
+
throw new TypeError("Throttle with trailing & leading options `false` won't do anything.");
|
|
13
|
+
}
|
|
14
|
+
if (typeof time !== "number" && !time.length) {
|
|
15
|
+
throw new TypeError("`time` must be an array with at least one number.");
|
|
16
|
+
}
|
|
17
|
+
const finalTime = typeof time === "number" ? time : time[time.length - 1];
|
|
18
|
+
let lastRun = 0, timeoutId = null, lastResult, lastArgs, lastTime = typeof time === "number" ? time : time[0], leadingCalled = false;
|
|
19
|
+
const times = typeof time === "number" ? [time] : [...time];
|
|
20
|
+
const throttledFn = ((...args) => {
|
|
21
|
+
var _a, _b, _c;
|
|
22
|
+
lastArgs = args;
|
|
23
|
+
if (timeoutId !== null) {
|
|
24
|
+
return lastResult;
|
|
25
|
+
}
|
|
26
|
+
if (!opts.trailing) {
|
|
27
|
+
if (Date.now() - lastRun < lastTime) {
|
|
28
|
+
return lastResult;
|
|
29
|
+
}
|
|
30
|
+
lastTime = (_a = times.shift()) !== null && _a !== void 0 ? _a : finalTime;
|
|
31
|
+
lastRun = Date.now();
|
|
32
|
+
lastResult = fn(...args);
|
|
33
|
+
return lastResult;
|
|
34
|
+
}
|
|
35
|
+
const diffLastRun = Date.now() - lastRun;
|
|
36
|
+
if (opts.leading && (!leadingCalled || typeof time === "number") && diffLastRun >= ((_b = times[0]) !== null && _b !== void 0 ? _b : finalTime)) {
|
|
37
|
+
lastRun = Date.now();
|
|
38
|
+
lastResult = fn(...args);
|
|
39
|
+
leadingCalled = true;
|
|
40
|
+
return lastResult;
|
|
41
|
+
}
|
|
42
|
+
if (lastRun || !opts.leading) {
|
|
43
|
+
lastTime = (_c = times.shift()) !== null && _c !== void 0 ? _c : finalTime;
|
|
44
|
+
}
|
|
45
|
+
timeoutId = setTimeout(() => {
|
|
46
|
+
timeoutId = null;
|
|
47
|
+
lastRun = Date.now();
|
|
48
|
+
lastResult = fn(...args);
|
|
49
|
+
}, lastRun ? (lastTime - diffLastRun + 1) : lastTime);
|
|
50
|
+
return lastResult;
|
|
51
|
+
});
|
|
52
|
+
throttledFn.cancel = () => {
|
|
53
|
+
timeoutId !== null && clearTimeout(timeoutId);
|
|
54
|
+
timeoutId = null;
|
|
55
|
+
lastRun = 0;
|
|
56
|
+
lastResult = undefined;
|
|
57
|
+
times.length = 0;
|
|
58
|
+
if (typeof time === "number") {
|
|
59
|
+
times.push(time);
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
times.push(...time);
|
|
63
|
+
}
|
|
64
|
+
lastTime = typeof time === "number" ? time : time[0];
|
|
65
|
+
};
|
|
66
|
+
throttledFn.flush = () => {
|
|
67
|
+
if (timeoutId !== null) {
|
|
68
|
+
lastRun = Date.now();
|
|
69
|
+
lastResult = fn(...lastArgs);
|
|
70
|
+
clearTimeout(timeoutId);
|
|
71
|
+
timeoutId = null;
|
|
72
|
+
return throttledFn(...lastArgs);
|
|
73
|
+
}
|
|
74
|
+
return lastResult;
|
|
75
|
+
};
|
|
76
|
+
return throttledFn;
|
|
77
|
+
};
|
|
78
|
+
export { throttle, };
|
|
79
|
+
//# sourceMappingURL=throttle.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"throttle.js","sourceRoot":"","sources":["../src/throttle.ts"],"names":[],"mappings":"AAsBA,MAAM,cAAc,GAAmB;IACnC,OAAO,EAAE,IAAI;IACb,QAAQ,EAAE,IAAI;CACjB,CAAC;AAuBF,MAAM,QAAQ,GAAG,CACb,EAAK,EAAE,OAAuC,CAAC,EAAE,OAAc,EACjC,EAAE;;IAChC,MAAM,IAAI,GAAmB;QACzB,OAAO,EAAE,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,OAAO,mCAAI,cAAc,CAAC,OAAO;QACnD,QAAQ,EAAE,MAAA,OAAO,aAAP,OAAO,uBAAP,OAAO,CAAE,QAAQ,mCAAI,cAAc,CAAC,QAAQ;KACz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,IAAI,CAAC,CAAC;SACpB;aACI;YACD,KAAK,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC,CAAC;SACvB;QACD,QAAQ,GAAG,OAAO,IAAI,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACzD,CAAC,CAAC;IACF,WAAW,CAAC,KAAK,GAAG,GAAG,EAAE;QACrB,IAAI,SAAS,KAAK,IAAI,EAAE;YACpB,OAAO,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;YACrB,UAAU,GAAG,EAAE,CAAC,GAAG,QAAQ,CAAC,CAAC;YAC7B,YAAY,CAAC,SAAS,CAAC,CAAC;YACxB,SAAS,GAAG,IAAI,CAAC;YACjB,OAAO,WAAW,CAAC,GAAG,QAAQ,CAAC,CAAC;SACnC;QACD,OAAO,UAAU,CAAC;IACtB,CAAC,CAAC;IAEF,OAAO,WAAW,CAAC;AACvB,CAAC,CAAC;AAEF,OAAO,EACH,QAAQ,GACX,CAAC"}
|
package/esm/truthy.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"truthy.d.ts","sourceRoot":"","sources":["../src/truthy.ts"],"names":[],"mappings":"AAMA,QAAA,MAAM,MAAM,6DAEX,CAAC;AAEF,OAAO,EACH,MAAM,GACT,CAAC"}
|
package/esm/truthy.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"truthy.js","sourceRoot":"","sources":["../src/truthy.ts"],"names":[],"mappings":"AAMA,MAAM,MAAM,GAAG,CAAI,GAA0C,EAAY,EAAE;IACvE,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC;AACxB,CAAC,CAAC;AAEF,OAAO,EACH,MAAM,GACT,CAAC"}
|
package/esm/wait.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wait.d.ts","sourceRoot":"","sources":["../src/wait.ts"],"names":[],"mappings":"AAKA,QAAA,MAAM,IAAI,oCAIT,CAAC;AAEF,OAAO,EACH,IAAI,GACP,CAAC"}
|
package/esm/wait.js
ADDED
package/esm/wait.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"wait.js","sourceRoot":"","sources":["../src/wait.ts"],"names":[],"mappings":"AAKA,MAAM,IAAI,GAAG,CAAC,MAAM,GAAG,CAAC,EAAE,EAAE;IACxB,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;QACjC,UAAU,CAAC,GAAG,EAAE,GAAG,OAAO,EAAE,CAAC,CAAC,CAAC,EAAE,MAAM,CAAC,CAAC;IAC7C,CAAC,CAAC,CAAC;AACP,CAAC,CAAC;AAEF,OAAO,EACH,IAAI,GACP,CAAC"}
|
package/esm/waitFor.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"waitFor.d.ts","sourceRoot":"","sources":["../src/waitFor.ts"],"names":[],"mappings":"AAcA,QAAA,MAAM,OAAO,qEA8BZ,CAAC;AAEF,OAAO,EAAE,OAAO,EAAE,CAAC"}
|
package/esm/waitFor.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const DEFAULT_INTERVAL = 50;
|
|
2
|
+
const waitFor = (fn, interval = DEFAULT_INTERVAL, timeout = Infinity) => {
|
|
3
|
+
return new Promise((resolve, reject) => {
|
|
4
|
+
let intervalTimer, failTimer;
|
|
5
|
+
if (isFinite(timeout)) {
|
|
6
|
+
failTimer = setTimeout(() => {
|
|
7
|
+
reject(new Error("[waitFor] Timeout"));
|
|
8
|
+
clearInterval(intervalTimer);
|
|
9
|
+
}, timeout);
|
|
10
|
+
}
|
|
11
|
+
intervalTimer = setInterval(() => {
|
|
12
|
+
try {
|
|
13
|
+
const result = fn();
|
|
14
|
+
if (result) {
|
|
15
|
+
clearTimeout(failTimer);
|
|
16
|
+
clearInterval(intervalTimer);
|
|
17
|
+
resolve(result);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
clearTimeout(failTimer);
|
|
22
|
+
clearInterval(intervalTimer);
|
|
23
|
+
const e = new Error("[waitFor] check function threw an error");
|
|
24
|
+
e.details = { error };
|
|
25
|
+
reject(e);
|
|
26
|
+
}
|
|
27
|
+
}, interval);
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
export { waitFor };
|
|
31
|
+
//# sourceMappingURL=waitFor.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"waitFor.js","sourceRoot":"","sources":["../src/waitFor.ts"],"names":[],"mappings":"AAAA,MAAM,gBAAgB,GAAG,EAAE,CAAC;AAc5B,MAAM,OAAO,GAAG,CAAI,EAAW,EAAE,QAAQ,GAAG,gBAAgB,EAAE,OAAO,GAAG,QAAQ,EAAE,EAAE;IAChF,OAAO,IAAI,OAAO,CAAI,CAAC,OAAO,EAAE,MAAM,EAAE,EAAE;QACtC,IAAI,aAAwB,EAAE,SAAmB,CAAC;QAElD,IAAI,QAAQ,CAAC,OAAO,CAAC,EAAE;YACnB,SAAS,GAAG,UAAU,CAAC,GAAG,EAAE;gBACxB,MAAM,CAAC,IAAI,KAAK,CAAC,mBAAmB,CAAC,CAAC,CAAC;gBACvC,aAAa,CAAC,aAAa,CAAC,CAAC;YACjC,CAAC,EAAE,OAAO,CAAC,CAAC;SACf;QAED,aAAa,GAAG,WAAW,CAAC,GAAG,EAAE;YAC7B,IAAI;gBACA,MAAM,MAAM,GAAG,EAAE,EAAE,CAAC;gBACpB,IAAI,MAAM,EAAE;oBACR,YAAY,CAAC,SAAS,CAAC,CAAC;oBACxB,aAAa,CAAC,aAAa,CAAC,CAAC;oBAC7B,OAAO,CAAC,MAAM,CAAC,CAAC;iBACnB;aACJ;YACD,OAAO,KAAc,EAAE;gBACnB,YAAY,CAAC,SAAS,CAAC,CAAC;gBACxB,aAAa,CAAC,aAAa,CAAC,CAAC;gBAE7B,MAAM,CAAC,GAAkC,IAAI,KAAK,CAAC,yCAAyC,CAAC,CAAC;gBAC9F,CAAC,CAAC,OAAO,GAAG,EAAE,KAAK,EAAE,CAAC;gBACtB,MAAM,CAAC,CAAC,CAAC,CAAC;aACb;QACL,CAAC,EAAE,QAAQ,CAAC,CAAC;IACjB,CAAC,CAAC,CAAC;AACP,CAAC,CAAC;AAEF,OAAO,EAAE,OAAO,EAAE,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"waitSync.d.ts","sourceRoot":"","sources":["../src/waitSync.ts"],"names":[],"mappings":"AAKA,QAAA,MAAM,QAAQ,2BAIb,CAAC;AAEF,OAAO,EACH,QAAQ,GACX,CAAC"}
|
package/esm/waitSync.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"waitSync.js","sourceRoot":"","sources":["../src/waitSync.ts"],"names":[],"mappings":"AAKA,MAAM,QAAQ,GAAG,CAAC,MAAM,GAAG,CAAC,EAAE,EAAE;IAC5B,MAAM,CAAC,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;IAErB,OAAO,IAAI,CAAC,GAAG,EAAE,GAAG,CAAC,GAAG,MAAM,EAAE,GAAE;AACtC,CAAC,CAAC;AAEF,OAAO,EACH,QAAQ,GACX,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@ezez/utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"repository": "https://github.com/dzek69/bottom-line.git",
|
|
5
|
+
"author": "Jacek Nowacki @dzek69 <git-public@dzek.eu>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"test": "NODE_ENV=test jest",
|
|
9
|
+
"test:lodashImports": "node test/lodash-tree.cjs",
|
|
10
|
+
"docs": "typedoc src/index.ts --skipErrorChecking --out docs --includeVersion --pluginPages ./pagesconfig.json",
|
|
11
|
+
"compile": "yarn compile:esm && yarn compile:cjs",
|
|
12
|
+
"compile:esm": "rm -rf esm && tsc --project tsconfig.esm.json && node ./build-scripts/compile.esm.after.mjs",
|
|
13
|
+
"compile:cjs": "rm -rf dist && tsc --project tsconfig.cjs.json && node ./build-scripts/compile.cjs.after.mjs",
|
|
14
|
+
"typecheck": "tsc --noEmit",
|
|
15
|
+
"lint": "eslint src --ext .ts,.tsx,.js,.jsx,.mjs",
|
|
16
|
+
"lint:fix": "yarn lint --fix",
|
|
17
|
+
"prepack": "yarn compile",
|
|
18
|
+
"prepublishOnly": "yarn audit && yarn lint && yarn test && yarn docs",
|
|
19
|
+
"start:dev": "nodemon",
|
|
20
|
+
"start:dev:compatibility": "TS_NODE_FILES=true yarn start:dev",
|
|
21
|
+
"prepare": "husky install",
|
|
22
|
+
"updates": "npx --yes npm-check-updates --dep prod",
|
|
23
|
+
"updates:dev": "npx --yes npm-check-updates --dep dev",
|
|
24
|
+
"updates:all": "npx --yes npm-check-updates"
|
|
25
|
+
},
|
|
26
|
+
"exports": {
|
|
27
|
+
".": {
|
|
28
|
+
"require": "./dist/index.js",
|
|
29
|
+
"types": "./esm/index.d.ts",
|
|
30
|
+
"default": "./esm/index.js"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"main": "./dist/index.js",
|
|
34
|
+
"types": "./esm/index.d.ts",
|
|
35
|
+
"module": "./esm/index.js",
|
|
36
|
+
"type": "module",
|
|
37
|
+
"devDependencies": {
|
|
38
|
+
"@babel/core": "^7.19.3",
|
|
39
|
+
"@babel/preset-env": "^7.19.3",
|
|
40
|
+
"@babel/preset-typescript": "^7.18.6",
|
|
41
|
+
"@dzek69/eslint-config-base": "^2.3.0",
|
|
42
|
+
"@dzek69/eslint-config-typescript": "^1.0.2",
|
|
43
|
+
"@types/jest": "^29.0.3",
|
|
44
|
+
"@types/lodash": "^4.14.168",
|
|
45
|
+
"@typescript-eslint/eslint-plugin": "^5.38.1",
|
|
46
|
+
"@typescript-eslint/parser": "^5.38.1",
|
|
47
|
+
"babel-plugin-module-extension": "^0.1.3",
|
|
48
|
+
"eslint": "^8.24.0",
|
|
49
|
+
"fs-extra": "^10.1.0",
|
|
50
|
+
"husky": "^8.0.1",
|
|
51
|
+
"jest": "^29.1.1",
|
|
52
|
+
"lodash": "^4.17.21",
|
|
53
|
+
"must": "^0.13.4",
|
|
54
|
+
"nodemon": "^2.0.20",
|
|
55
|
+
"ts-node": "^10.9.1",
|
|
56
|
+
"typedoc": "^0.23.15",
|
|
57
|
+
"typescript": "^4.9.5",
|
|
58
|
+
"eslint-plugin-import": "^2.26.0",
|
|
59
|
+
"@dzek69/eslint-config-import": "^1.0.0",
|
|
60
|
+
"@dzek69/eslint-config-import-typescript": "^1.0.0",
|
|
61
|
+
"@knodes/typedoc-plugin-pages": "^0.23.1",
|
|
62
|
+
"prettier": "^2.8.3"
|
|
63
|
+
},
|
|
64
|
+
"husky": {
|
|
65
|
+
"hooks": {
|
|
66
|
+
"pre-push": "yarn prepublishOnly && yarn compile"
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
"libraryTemplate": {
|
|
70
|
+
"version": "3.8.0",
|
|
71
|
+
"language": "typescript",
|
|
72
|
+
"fixDefaultForCommonJS": false,
|
|
73
|
+
"jsx": false
|
|
74
|
+
}
|
|
75
|
+
}
|
package/src/cap.spec.ts
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { cap } from "./cap.js";
|
|
2
|
+
|
|
3
|
+
describe("cap", () => {
|
|
4
|
+
it("caps too big values", () => {
|
|
5
|
+
cap(200, 100).must.equal(100);
|
|
6
|
+
cap(101, 100).must.equal(100);
|
|
7
|
+
cap(100.0001, 100).must.equal(100);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
it("leaves not big values", () => {
|
|
11
|
+
cap(100, 100).must.equal(100);
|
|
12
|
+
cap(99.999, 100).must.equal(99.999);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("caps too small values", () => {
|
|
16
|
+
cap(-100, 100, 5).must.equal(5);
|
|
17
|
+
cap(4, 100, 5).must.equal(5);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("leaves not too small values", () => {
|
|
21
|
+
cap(6, 100, 5).must.equal(6);
|
|
22
|
+
cap(5, 100, 5).must.equal(5);
|
|
23
|
+
cap(23, 100, 5).must.equal(23);
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("throws when max expected value is lower than min expected value", () => {
|
|
27
|
+
(() => cap(6.4, 5, 7)).must.throw();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
it("allows the same min and max value", function() {
|
|
31
|
+
(() => cap(5, 5, 5)).must.not.throw();
|
|
32
|
+
cap(5, 5, 5).must.equal(5);
|
|
33
|
+
cap(4, 5, 5).must.equal(5);
|
|
34
|
+
cap(6, 5, 5).must.equal(5);
|
|
35
|
+
});
|
|
36
|
+
});
|
package/src/cap.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a number capped between an upper and lower limit.
|
|
3
|
+
* @param {number} value - value to cap
|
|
4
|
+
* @param {number} upperLimit - upper limit, inclusive
|
|
5
|
+
* @param {number} [lowerLimit] - lower limit, inclusive
|
|
6
|
+
*/
|
|
7
|
+
const cap = (value: number, upperLimit: number, lowerLimit?: number) => {
|
|
8
|
+
if (lowerLimit == null) {
|
|
9
|
+
return Math.min(value, upperLimit);
|
|
10
|
+
}
|
|
11
|
+
if (upperLimit < lowerLimit) {
|
|
12
|
+
throw new TypeError("upperLimit must be greater than lowerLimit");
|
|
13
|
+
}
|
|
14
|
+
return Math.min(upperLimit, Math.max(lowerLimit, value));
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export {
|
|
18
|
+
cap,
|
|
19
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { capitalize } from "./capitalize.js";
|
|
2
|
+
|
|
3
|
+
describe("capitalize", () => {
|
|
4
|
+
it("capitalizes first letter", () => {
|
|
5
|
+
const string = "abcd";
|
|
6
|
+
capitalize(string).must.equal("Abcd");
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it("doesn't touch other characters by default", function() {
|
|
10
|
+
const string = "aBcD";
|
|
11
|
+
capitalize(string).must.equal("ABcD");
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
it("converts other to small caps if reqested", function() {
|
|
15
|
+
const string = "aBcD";
|
|
16
|
+
capitalize(string, true).must.equal("Abcd");
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capitalize the first letter of a string.
|
|
3
|
+
* @param {string} text - source string
|
|
4
|
+
* @param {boolean} [restLowercase=false] - should the rest of the string be forced to lowercase?
|
|
5
|
+
* @example capitalize("hello") // "Hello"
|
|
6
|
+
* @example capitalize("hello my Friend", true) // "Hello my friend"
|
|
7
|
+
* @example capitalize("hello my Friend", false) // "Hello my Friend"
|
|
8
|
+
*/
|
|
9
|
+
const capitalize = (text: string, restLowercase = false) => {
|
|
10
|
+
const rest = restLowercase ? text.slice(1).toLowerCase() : text.slice(1);
|
|
11
|
+
return text.charAt(0).toUpperCase() + rest;
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export {
|
|
15
|
+
capitalize,
|
|
16
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { coalesce } from "./coalesce.js";
|
|
2
|
+
|
|
3
|
+
describe("coalesce", () => {
|
|
4
|
+
it("works with basic values", () => {
|
|
5
|
+
coalesce(undefined, null, 69).must.equal(69);
|
|
6
|
+
coalesce(null, null, undefined, 69, null).must.equal(69);
|
|
7
|
+
});
|
|
8
|
+
|
|
9
|
+
it("returns falsy values", () => {
|
|
10
|
+
coalesce(undefined, null, false, 5).must.equal(false);
|
|
11
|
+
coalesce(undefined, null, 0, 5).must.equal(0);
|
|
12
|
+
coalesce(undefined, null, "", 5).must.equal("");
|
|
13
|
+
coalesce(undefined, null, NaN, 5).must.be.nan();
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
it("returns null if no non-nil value is found", () => {
|
|
17
|
+
(coalesce(null, undefined) === null).must.be.true();
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("returns null if no arguments are given", () => {
|
|
21
|
+
(coalesce() === null).must.be.true();
|
|
22
|
+
});
|
|
23
|
+
});
|
package/src/coalesce.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns first non-nil (not undefined, not null) value from given arguments.
|
|
3
|
+
*
|
|
4
|
+
* @param {...*} args - values
|
|
5
|
+
* @example coalesce(null, undefined, void 0, 5); // returns 5
|
|
6
|
+
* @example coalesce(4, null, 6, undefined); // returns 4
|
|
7
|
+
* @example coalesce(undefined); // returns null
|
|
8
|
+
* @example coalesce(); // returns null
|
|
9
|
+
* @returns {*|null} first non-nil value or null
|
|
10
|
+
*/
|
|
11
|
+
const coalesce = <T>(...args: T[]): NonNullable<T> | null => {
|
|
12
|
+
for (let i = 0; i < args.length; i++) {
|
|
13
|
+
const item = args[i];
|
|
14
|
+
if (item != null) {
|
|
15
|
+
return item;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
return null;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export { coalesce };
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { ensureArray } from "./ensureArray.js";
|
|
2
|
+
|
|
3
|
+
describe("ensureArray", () => {
|
|
4
|
+
it("must return given value untouched if the value is an array", () => {
|
|
5
|
+
singleValue: {
|
|
6
|
+
const x = [5];
|
|
7
|
+
const result = ensureArray(x);
|
|
8
|
+
result.must.be.an.array();
|
|
9
|
+
result.must.have.length(1);
|
|
10
|
+
result.must.equal(x);
|
|
11
|
+
result.must.eql([5]);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
noValue: {
|
|
15
|
+
const x = [];
|
|
16
|
+
const result = ensureArray(x);
|
|
17
|
+
result.must.be.an.array();
|
|
18
|
+
result.must.have.length(0);
|
|
19
|
+
result.must.equal(x);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
manyValues: {
|
|
23
|
+
const x = [1, "5"];
|
|
24
|
+
const result = ensureArray(x);
|
|
25
|
+
result.must.be.an.array();
|
|
26
|
+
result.must.have.length(2);
|
|
27
|
+
result.must.equal(x);
|
|
28
|
+
result.must.eql([1, "5"]);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
arrayOfArrays: {
|
|
32
|
+
const x = [[1], [2]];
|
|
33
|
+
const result = ensureArray(x);
|
|
34
|
+
result.must.be.an.array();
|
|
35
|
+
result.must.have.length(2);
|
|
36
|
+
result.must.equal(x);
|
|
37
|
+
result.must.eql([[1], [2]]);
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("must arrarize object", () => {
|
|
42
|
+
const y = { title: "dzek" };
|
|
43
|
+
const result = ensureArray(y);
|
|
44
|
+
result.must.be.an.array();
|
|
45
|
+
result.must.have.length(1);
|
|
46
|
+
result[0].must.equal(y);
|
|
47
|
+
result.must.eql([{ title: "dzek" }]);
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it("must arrarize function", () => {
|
|
51
|
+
const y = () => 5;
|
|
52
|
+
|
|
53
|
+
const result = ensureArray(y);
|
|
54
|
+
result.must.be.an.array();
|
|
55
|
+
result.must.have.length(1);
|
|
56
|
+
result[0].must.equal(y);
|
|
57
|
+
result.must.eql([y]);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("must arrarize null", () => {
|
|
61
|
+
const result = ensureArray(null);
|
|
62
|
+
result.must.be.an.array();
|
|
63
|
+
result.must.have.length(1);
|
|
64
|
+
result.must.eql([null]);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("must arrarize string", () => {
|
|
68
|
+
const result = ensureArray("hey joe");
|
|
69
|
+
result.must.be.an.array();
|
|
70
|
+
result.must.have.length(1);
|
|
71
|
+
result.must.eql(["hey joe"]);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it("must arrarize number", () => {
|
|
75
|
+
const result = ensureArray(123);
|
|
76
|
+
result.must.be.an.array();
|
|
77
|
+
result.must.have.length(1);
|
|
78
|
+
result.must.eql([123]);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("must arrarize NaN", () => {
|
|
82
|
+
const result = ensureArray(NaN);
|
|
83
|
+
result.must.be.an.array();
|
|
84
|
+
result.must.have.length(1);
|
|
85
|
+
result.must.eql([NaN]);
|
|
86
|
+
});
|
|
87
|
+
});
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wraps value in an array until the value is already an array
|
|
3
|
+
* @param {*} value - value to be wrapped
|
|
4
|
+
* @returns {Array}
|
|
5
|
+
*/
|
|
6
|
+
const ensureArray = <T>(value: T | T[]): T[] => {
|
|
7
|
+
if (Array.isArray(value)) {
|
|
8
|
+
return value;
|
|
9
|
+
}
|
|
10
|
+
return [value];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export { ensureArray };
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { ensureError } from "./ensureError.js";
|
|
2
|
+
|
|
3
|
+
describe("ensureError", () => {
|
|
4
|
+
it("returns unmodified error if error given", () => {
|
|
5
|
+
const e = new Error("Test");
|
|
6
|
+
|
|
7
|
+
const result = ensureError(e);
|
|
8
|
+
(e === result).must.be.true();
|
|
9
|
+
result.message.must.equal("Test");
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("returns error on anything else than error", () => {
|
|
13
|
+
const eNull = ensureError(null);
|
|
14
|
+
eNull.must.be.instanceof(Error);
|
|
15
|
+
eNull.message.must.equal("Expected error instance, got something else: null");
|
|
16
|
+
|
|
17
|
+
const eUndefined = ensureError(undefined);
|
|
18
|
+
eUndefined.must.be.instanceof(Error);
|
|
19
|
+
eUndefined.message.must.equal("Expected error instance, got something else: undefined");
|
|
20
|
+
|
|
21
|
+
const eNumber = ensureError(6);
|
|
22
|
+
eNumber.must.be.instanceof(Error);
|
|
23
|
+
eNumber.message.must.equal("Expected error instance, got something else: 6");
|
|
24
|
+
|
|
25
|
+
const eFunction = ensureError(() => {});
|
|
26
|
+
eFunction.must.be.instanceof(Error);
|
|
27
|
+
eFunction.message.must.equal("Expected error instance, got something else: () => {}");
|
|
28
|
+
});
|
|
29
|
+
});
|