@ezez/utils 4.4.0 → 4.6.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/CHANGELOG.md +21 -1
- package/dist/cap.d.ts.map +1 -1
- package/dist/cap.js.map +1 -1
- package/dist/capitalize.d.ts.map +1 -1
- package/dist/capitalize.js.map +1 -1
- package/dist/compareArrays.d.ts +4 -4
- package/dist/compareArrays.d.ts.map +1 -1
- package/dist/compareArrays.js.map +1 -1
- package/dist/deserialize.d.ts +1 -1
- package/dist/deserialize.d.ts.map +1 -1
- package/dist/deserialize.js +2 -1
- package/dist/deserialize.js.map +1 -1
- package/dist/ensureError.d.ts.map +1 -1
- package/dist/ensureError.js +3 -1
- package/dist/ensureError.js.map +1 -1
- package/dist/ensurePrefix.d.ts.map +1 -1
- package/dist/ensurePrefix.js.map +1 -1
- package/dist/ensureSuffix.d.ts.map +1 -1
- package/dist/ensureSuffix.js.map +1 -1
- package/dist/escapeRegExp.d.ts.map +1 -1
- package/dist/escapeRegExp.js.map +1 -1
- package/dist/formatDate.d.ts.map +1 -1
- package/dist/formatDate.js.map +1 -1
- package/dist/getMultiple.d.ts.map +1 -1
- package/dist/getMultiple.js.map +1 -1
- package/dist/ignore.d.ts +3 -0
- package/dist/ignore.d.ts.map +1 -0
- package/dist/ignore.js +16 -0
- package/dist/ignore.js.map +1 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -1
- package/dist/isEmpty.d.ts.map +1 -1
- package/dist/isEmpty.js +4 -4
- package/dist/isEmpty.js.map +1 -1
- package/dist/isNumericString.d.ts.map +1 -1
- package/dist/isNumericString.js.map +1 -1
- package/dist/isPlainObject.d.ts.map +1 -1
- package/dist/isPlainObject.js.map +1 -1
- package/dist/match.d.ts.map +1 -1
- package/dist/match.js.map +1 -1
- package/dist/memoize.d.ts.map +1 -1
- package/dist/memoize.js.map +1 -1
- package/dist/mostFrequent.d.ts.map +1 -1
- package/dist/mostFrequent.js.map +1 -1
- package/dist/noop.d.ts.map +1 -1
- package/dist/noop.js.map +1 -1
- package/dist/occurrences.d.ts.map +1 -1
- package/dist/occurrences.js.map +1 -1
- package/dist/removeCommonProperties.d.ts.map +1 -1
- package/dist/removeCommonProperties.js.map +1 -1
- package/dist/replace.d.ts.map +1 -1
- package/dist/replace.js.map +1 -1
- package/dist/rethrow.d.ts +1 -1
- package/dist/rethrow.d.ts.map +1 -1
- package/dist/rethrow.js.map +1 -1
- package/dist/round.d.ts.map +1 -1
- package/dist/round.js.map +1 -1
- package/dist/safe.d.ts.map +1 -1
- package/dist/safe.js.map +1 -1
- package/dist/scale.d.ts.map +1 -1
- package/dist/scale.js.map +1 -1
- package/dist/seq.d.ts.map +1 -1
- package/dist/seq.js +5 -1
- package/dist/seq.js.map +1 -1
- package/dist/serialize.d.ts.map +1 -1
- package/dist/serialize.js +2 -2
- package/dist/serialize.js.map +1 -1
- package/dist/serializeToBuffer/const.d.ts +10 -0
- package/dist/serializeToBuffer/const.d.ts.map +1 -0
- package/dist/serializeToBuffer/const.js +16 -0
- package/dist/serializeToBuffer/const.js.map +1 -0
- package/dist/serializeToBuffer/serializeToBuffer.d.ts +5 -0
- package/dist/serializeToBuffer/serializeToBuffer.d.ts.map +1 -0
- package/dist/serializeToBuffer/serializeToBuffer.js +39 -0
- package/dist/serializeToBuffer/serializeToBuffer.js.map +1 -0
- package/dist/serializeToBuffer/unserializeFromBuffer.d.ts +5 -0
- package/dist/serializeToBuffer/unserializeFromBuffer.d.ts.map +1 -0
- package/dist/serializeToBuffer/unserializeFromBuffer.js +52 -0
- package/dist/serializeToBuffer/unserializeFromBuffer.js.map +1 -0
- package/dist/setImmutable.js.map +1 -1
- package/dist/sortBy.d.ts +1 -1
- package/dist/sortBy.d.ts.map +1 -1
- package/dist/sortBy.js.map +1 -1
- package/dist/sortByMultiple.d.ts +1 -1
- package/dist/sortByMultiple.d.ts.map +1 -1
- package/dist/sortByMultiple.js.map +1 -1
- package/dist/trim.d.ts.map +1 -1
- package/dist/trim.js +1 -1
- package/dist/trim.js.map +1 -1
- package/dist/trimEnd.d.ts.map +1 -1
- package/dist/trimEnd.js.map +1 -1
- package/dist/trimStart.d.ts.map +1 -1
- package/dist/trimStart.js.map +1 -1
- package/dist/unique.d.ts +1 -1
- package/dist/unique.d.ts.map +1 -1
- package/dist/unique.js.map +1 -1
- package/dist/utils/utils.js +1 -0
- package/dist/utils/utils.js.map +1 -1
- package/dist/wait.d.ts.map +1 -1
- package/dist/wait.js.map +1 -1
- package/dist/waitFor.d.ts.map +1 -1
- package/dist/waitFor.js.map +1 -1
- package/dist/waitSync.d.ts.map +1 -1
- package/dist/waitSync.js.map +1 -1
- package/docs/assets/hierarchy.js +1 -0
- package/docs/assets/icons.js +1 -1
- package/docs/assets/icons.svg +1 -1
- package/docs/assets/main.js +5 -5
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/assets/style.css +1405 -1288
- package/docs/documents/Changelog.html +370 -0
- package/docs/functions/index.cap.html +3 -3
- package/docs/functions/index.capitalize.html +7 -7
- package/docs/functions/index.coalesce.html +8 -8
- package/docs/functions/index.compareArrays.html +5 -5
- package/docs/functions/index.compareProps.html +5 -5
- package/docs/functions/index.deserialize.html +4 -4
- package/docs/functions/index.ensureArray.html +3 -3
- package/docs/functions/index.ensureDate.html +6 -6
- package/docs/functions/index.ensureError.html +6 -6
- package/docs/functions/index.ensurePrefix.html +5 -5
- package/docs/functions/index.ensureSuffix.html +5 -5
- package/docs/functions/index.ensureTimestamp.html +6 -6
- package/docs/functions/index.escapeRegExp.html +5 -5
- package/docs/functions/index.formatDate.html +5 -2
- package/docs/functions/index.get.html +8 -8
- package/docs/functions/index.getMultiple.html +6 -6
- package/docs/functions/index.ignore.html +8 -0
- package/docs/functions/index.insertSeparator.html +5 -5
- package/docs/functions/index.isEmpty.html +12 -13
- package/docs/functions/index.isNumericString.html +3 -3
- package/docs/functions/index.isPlainObject.html +8 -8
- package/docs/functions/index.last.html +7 -7
- package/docs/functions/index.later-1.html +3 -3
- package/docs/functions/index.mapAsync.html +5 -5
- package/docs/functions/index.mapValues.html +7 -7
- package/docs/functions/index.match.html +4 -4
- package/docs/functions/index.memoize.html +4 -4
- package/docs/functions/index.merge.html +6 -6
- package/docs/functions/index.mostFrequent.html +2 -2
- package/docs/functions/index.noop.html +2 -2
- package/docs/functions/index.occurrences.html +7 -6
- package/docs/functions/index.omit.html +7 -7
- package/docs/functions/index.pick.html +7 -7
- package/docs/functions/index.pull.html +2 -2
- package/docs/functions/index.race.html +5 -5
- package/docs/functions/index.remove.html +2 -2
- package/docs/functions/index.removeCommonProperties.html +5 -5
- package/docs/functions/index.replace.html +6 -6
- package/docs/functions/index.replaceDeep.html +3 -3
- package/docs/functions/index.rethrow.html +2 -2
- package/docs/functions/index.retry.html +4 -4
- package/docs/functions/index.round.html +4 -4
- package/docs/functions/index.safe.html +5 -5
- package/docs/functions/index.sample.html +4 -4
- package/docs/functions/index.samples.html +3 -3
- package/docs/functions/index.scale.html +2 -2
- package/docs/functions/index.seq.html +7 -7
- package/docs/functions/index.seqEarlyBreak.html +6 -6
- package/docs/functions/index.serialize.html +4 -5
- package/docs/functions/index.serializeToBuffer.html +19 -0
- package/docs/functions/index.set.html +8 -8
- package/docs/functions/index.setImmutable.html +7 -7
- package/docs/functions/index.shuffle.html +3 -3
- package/docs/functions/index.sortBy.html +3 -3
- package/docs/functions/index.sortByMultiple.html +5 -5
- package/docs/functions/index.sortProps.html +4 -4
- package/docs/functions/index.stripPrefix.html +3 -3
- package/docs/functions/index.stripSuffix.html +3 -3
- package/docs/functions/index.throttle.html +4 -4
- package/docs/functions/index.toggle.html +4 -4
- package/docs/functions/index.trim.html +4 -4
- package/docs/functions/index.trimEnd.html +4 -4
- package/docs/functions/index.trimStart.html +4 -4
- package/docs/functions/index.truthy.html +4 -4
- package/docs/functions/index.unique.html +4 -4
- package/docs/functions/index.unserializeFromBuffer.html +8 -0
- package/docs/functions/index.wait.html +3 -3
- package/docs/functions/index.waitFor.html +4 -4
- package/docs/functions/index.waitSync.html +3 -3
- package/docs/index.html +16 -16
- package/docs/interfaces/index.ComparePropsOptions.html +4 -4
- package/docs/interfaces/index.GetMultipleSource.html +3 -3
- package/docs/interfaces/index.GetSource.html +3 -3
- package/docs/interfaces/index.IsNumericStringOptions.html +5 -5
- package/docs/interfaces/index.OccurencesOptions.html +2 -2
- package/docs/interfaces/index.SetImmutableSource.html +3 -3
- package/docs/interfaces/index.SetSource.html +3 -3
- package/docs/interfaces/index.ThrottleOptions.html +3 -3
- package/docs/interfaces/index.ThrottledFunctionExtras.html +4 -4
- package/docs/modules/index.html +1 -89
- package/docs/modules.html +1 -3
- package/docs/types/index.CustomDeserializers.html +1 -1
- package/docs/types/index.CustomSerializers.html +1 -1
- package/docs/types/index.Later.html +4 -4
- package/docs/types/index.MapValuesFn.html +7 -7
- package/docs/types/index.MatchCallback.html +1 -1
- package/docs/types/index.MergeTwo.html +2 -2
- package/docs/types/index.SeqEarlyBreaker.html +4 -4
- package/docs/types/index.SeqFn.html +3 -3
- package/docs/types/index.SeqFunctions.html +3 -3
- package/docs/types/index.SetImmutablePath.html +2 -2
- package/docs/types/index.ThrottledFunction.html +1 -1
- package/docs/variables/index.mapValuesUNSET.html +2 -2
- package/docs/variables/index.mergeUNSET.html +3 -3
- package/esm/cap.d.ts.map +1 -1
- package/esm/cap.js.map +1 -1
- package/esm/capitalize.d.ts.map +1 -1
- package/esm/capitalize.js.map +1 -1
- package/esm/compareArrays.d.ts +4 -4
- package/esm/compareArrays.d.ts.map +1 -1
- package/esm/compareArrays.js.map +1 -1
- package/esm/deserialize.d.ts +1 -1
- package/esm/deserialize.d.ts.map +1 -1
- package/esm/deserialize.js +1 -1
- package/esm/deserialize.js.map +1 -1
- package/esm/ensureError.d.ts.map +1 -1
- package/esm/ensureError.js +3 -1
- package/esm/ensureError.js.map +1 -1
- package/esm/ensurePrefix.d.ts.map +1 -1
- package/esm/ensurePrefix.js.map +1 -1
- package/esm/ensureSuffix.d.ts.map +1 -1
- package/esm/ensureSuffix.js.map +1 -1
- package/esm/escapeRegExp.d.ts.map +1 -1
- package/esm/escapeRegExp.js.map +1 -1
- package/esm/formatDate.d.ts.map +1 -1
- package/esm/formatDate.js.map +1 -1
- package/esm/getMultiple.d.ts.map +1 -1
- package/esm/getMultiple.js.map +1 -1
- package/esm/ignore.d.ts +3 -0
- package/esm/ignore.d.ts.map +1 -0
- package/esm/ignore.js +13 -0
- package/esm/ignore.js.map +1 -0
- package/esm/index.d.ts +3 -0
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +3 -0
- package/esm/index.js.map +1 -1
- package/esm/isEmpty.d.ts.map +1 -1
- package/esm/isEmpty.js +4 -4
- package/esm/isEmpty.js.map +1 -1
- package/esm/isNumericString.d.ts.map +1 -1
- package/esm/isNumericString.js.map +1 -1
- package/esm/isPlainObject.d.ts.map +1 -1
- package/esm/isPlainObject.js.map +1 -1
- package/esm/match.d.ts.map +1 -1
- package/esm/match.js.map +1 -1
- package/esm/memoize.d.ts.map +1 -1
- package/esm/memoize.js.map +1 -1
- package/esm/mostFrequent.d.ts.map +1 -1
- package/esm/mostFrequent.js.map +1 -1
- package/esm/noop.d.ts.map +1 -1
- package/esm/noop.js.map +1 -1
- package/esm/occurrences.d.ts.map +1 -1
- package/esm/occurrences.js.map +1 -1
- package/esm/removeCommonProperties.d.ts.map +1 -1
- package/esm/removeCommonProperties.js.map +1 -1
- package/esm/replace.d.ts.map +1 -1
- package/esm/replace.js.map +1 -1
- package/esm/rethrow.d.ts +1 -1
- package/esm/rethrow.d.ts.map +1 -1
- package/esm/rethrow.js.map +1 -1
- package/esm/round.d.ts.map +1 -1
- package/esm/round.js.map +1 -1
- package/esm/safe.d.ts.map +1 -1
- package/esm/safe.js.map +1 -1
- package/esm/scale.d.ts.map +1 -1
- package/esm/scale.js.map +1 -1
- package/esm/seq.d.ts.map +1 -1
- package/esm/seq.js +5 -1
- package/esm/seq.js.map +1 -1
- package/esm/serialize.d.ts.map +1 -1
- package/esm/serialize.js +2 -2
- package/esm/serialize.js.map +1 -1
- package/esm/serializeToBuffer/const.d.ts +10 -0
- package/esm/serializeToBuffer/const.d.ts.map +1 -0
- package/esm/serializeToBuffer/const.js +10 -0
- package/esm/serializeToBuffer/const.js.map +1 -0
- package/esm/serializeToBuffer/serializeToBuffer.d.ts +5 -0
- package/esm/serializeToBuffer/serializeToBuffer.d.ts.map +1 -0
- package/esm/serializeToBuffer/serializeToBuffer.js +36 -0
- package/esm/serializeToBuffer/serializeToBuffer.js.map +1 -0
- package/esm/serializeToBuffer/unserializeFromBuffer.d.ts +5 -0
- package/esm/serializeToBuffer/unserializeFromBuffer.d.ts.map +1 -0
- package/esm/serializeToBuffer/unserializeFromBuffer.js +48 -0
- package/esm/serializeToBuffer/unserializeFromBuffer.js.map +1 -0
- package/esm/setImmutable.js.map +1 -1
- package/esm/sortBy.d.ts +1 -1
- package/esm/sortBy.d.ts.map +1 -1
- package/esm/sortBy.js.map +1 -1
- package/esm/sortByMultiple.d.ts +1 -1
- package/esm/sortByMultiple.d.ts.map +1 -1
- package/esm/sortByMultiple.js.map +1 -1
- package/esm/trim.d.ts.map +1 -1
- package/esm/trim.js +1 -1
- package/esm/trim.js.map +1 -1
- package/esm/trimEnd.d.ts.map +1 -1
- package/esm/trimEnd.js.map +1 -1
- package/esm/trimStart.d.ts.map +1 -1
- package/esm/trimStart.js.map +1 -1
- package/esm/unique.d.ts +1 -1
- package/esm/unique.d.ts.map +1 -1
- package/esm/unique.js.map +1 -1
- package/esm/utils/utils.js +1 -0
- package/esm/utils/utils.js.map +1 -1
- package/esm/wait.d.ts.map +1 -1
- package/esm/wait.js.map +1 -1
- package/esm/waitFor.d.ts.map +1 -1
- package/esm/waitFor.js.map +1 -1
- package/esm/waitSync.d.ts.map +1 -1
- package/esm/waitSync.js.map +1 -1
- package/package.json +31 -34
- package/src/cap.ts +1 -1
- package/src/capitalize.ts +3 -3
- package/src/compareArrays.ts +2 -3
- package/src/deserialize.spec.ts +2 -1
- package/src/deserialize.ts +1 -1
- package/src/ensureError.ts +6 -2
- package/src/ensurePrefix.ts +1 -1
- package/src/ensureSuffix.ts +1 -1
- package/src/escapeRegExp.ts +1 -1
- package/src/formatDate.ts +7 -2
- package/src/get.spec.ts +0 -1
- package/src/getMultiple.ts +0 -1
- package/src/ignore.spec.ts +80 -0
- package/src/ignore.ts +38 -0
- package/src/index.ts +3 -0
- package/src/isEmpty.ts +9 -15
- package/src/isNumericString.ts +1 -1
- package/src/isPlainObject.ts +1 -1
- package/src/later.spec.ts +1 -0
- package/src/mapAsync.ts +4 -4
- package/src/match.ts +1 -1
- package/src/memoize.spec.ts +1 -1
- package/src/memoize.ts +1 -1
- package/src/mostFrequent.spec.ts +2 -0
- package/src/mostFrequent.ts +0 -1
- package/src/noop.ts +1 -1
- package/src/occurrences.ts +1 -1
- package/src/remove.spec.ts +1 -1
- package/src/removeCommonProperties.spec.ts +2 -0
- package/src/removeCommonProperties.ts +1 -1
- package/src/replace.ts +1 -1
- package/src/replaceDeep.spec.ts +2 -0
- package/src/replaceDeepByFn.spec.ts +2 -0
- package/src/rethrow.ts +1 -1
- package/src/round.ts +1 -1
- package/src/safe.ts +2 -1
- package/src/scale.ts +1 -1
- package/src/seq.ts +7 -3
- package/src/serialize.spec.ts +2 -1
- package/src/serialize.ts +4 -5
- package/src/serializeToBuffer/const.ts +16 -0
- package/src/serializeToBuffer/serialization.spec.ts +61 -0
- package/src/serializeToBuffer/serializeToBuffer.spec.ts +43 -0
- package/src/serializeToBuffer/serializeToBuffer.ts +89 -0
- package/src/serializeToBuffer/unserializeFromBuffer.spec.ts +63 -0
- package/src/serializeToBuffer/unserializeFromBuffer.ts +74 -0
- package/src/setImmutable.ts +1 -1
- package/src/sortBy.ts +3 -1
- package/src/sortByMultiple.ts +5 -3
- package/src/toggle.spec.ts +2 -0
- package/src/trim.spec.ts +2 -0
- package/src/trim.ts +3 -2
- package/src/trimEnd.spec.ts +2 -0
- package/src/trimEnd.ts +1 -1
- package/src/trimStart.spec.ts +2 -0
- package/src/trimStart.ts +1 -1
- package/src/unique.ts +2 -2
- package/src/wait.ts +1 -1
- package/src/waitFor.spec.ts +9 -8
- package/src/waitFor.ts +1 -1
- package/src/waitSync.spec.ts +2 -0
- package/src/waitSync.ts +1 -1
- package/.prettierignore +0 -3
- package/.prettierrc.json +0 -1
- package/docs/documents/CHANGELOG.html +0 -349
- package/pnpm-lock.yaml +0 -6529
- package/typedoc.cjs +0 -3
package/src/scale.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* @param {number} toMax
|
|
10
10
|
* @param {number} number
|
|
11
11
|
*/
|
|
12
|
-
const scale = (fromMin: number, fromMax: number, toMin: number, toMax: number, number: number) => {
|
|
12
|
+
const scale = (fromMin: number, fromMax: number, toMin: number, toMax: number, number: number): number => {
|
|
13
13
|
return toMin + ((number - fromMin) / (fromMax - fromMin) * (toMax - toMin));
|
|
14
14
|
};
|
|
15
15
|
|
package/src/seq.ts
CHANGED
|
@@ -48,7 +48,11 @@ const run = <T>(list: Fn<T>[], earlyBreaker?: EarlyBreaker): Promise<T> => {
|
|
|
48
48
|
}
|
|
49
49
|
const fn = promises.shift();
|
|
50
50
|
if (typeof fn !== "function") {
|
|
51
|
-
const finalError = new Error("Every function had thrown."
|
|
51
|
+
const finalError = new Error("Every function had thrown.", {
|
|
52
|
+
cause: {
|
|
53
|
+
errors,
|
|
54
|
+
},
|
|
55
|
+
});
|
|
52
56
|
// @ts-expect-error More details on error object are wanted
|
|
53
57
|
finalError.details = { errors };
|
|
54
58
|
reject(finalError);
|
|
@@ -73,7 +77,7 @@ const run = <T>(list: Fn<T>[], earlyBreaker?: EarlyBreaker): Promise<T> => {
|
|
|
73
77
|
* arguments with array
|
|
74
78
|
* @returns {*} - whatever gets returned from given functions
|
|
75
79
|
*/
|
|
76
|
-
const seqEarlyBreak = <T>(earlyBreaker: EarlyBreaker | undefined, ...args: Functions<T>) => {
|
|
80
|
+
const seqEarlyBreak = <T>(earlyBreaker: EarlyBreaker | undefined, ...args: Functions<T>): Promise<T> => {
|
|
77
81
|
if (args.length === 1) {
|
|
78
82
|
return run(Array.isArray(args[0]) ? args[0] : [args[0]], earlyBreaker);
|
|
79
83
|
}
|
|
@@ -91,7 +95,7 @@ const seqEarlyBreak = <T>(earlyBreaker: EarlyBreaker | undefined, ...args: Funct
|
|
|
91
95
|
* arguments with array
|
|
92
96
|
* @returns {*} - whatever gets returned from given functions
|
|
93
97
|
*/
|
|
94
|
-
const seq = <T>(...fns: Functions<T>) => {
|
|
98
|
+
const seq = <T>(...fns: Functions<T>): Promise<T> => {
|
|
95
99
|
return seqEarlyBreak(undefined, ...fns);
|
|
96
100
|
};
|
|
97
101
|
|
package/src/serialize.spec.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import must from "must"; // eslint-disable-line @typescript-eslint/no-shadow
|
|
2
|
+
|
|
1
3
|
import type { CustomSerializers } from "./serialize";
|
|
2
4
|
|
|
3
5
|
import { serialize } from "./serialize";
|
|
@@ -9,7 +11,6 @@ interface Test {
|
|
|
9
11
|
class Person {
|
|
10
12
|
public name: string;
|
|
11
13
|
|
|
12
|
-
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
13
14
|
public constructor(name: string) {
|
|
14
15
|
this.name = name;
|
|
15
16
|
}
|
package/src/serialize.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { sortProps } from "./sortProps.js";
|
|
2
|
-
import { replaceDeepByFn } from "./replaceDeepByFn.js";
|
|
3
1
|
import { isPlainObject } from "./isPlainObject.js";
|
|
2
|
+
import { replaceDeepByFn } from "./replaceDeepByFn.js";
|
|
3
|
+
import { sortProps } from "./sortProps.js";
|
|
4
4
|
import { DataWrapper } from "./utils/utils.js";
|
|
5
5
|
|
|
6
6
|
type CustomSerializers = {
|
|
@@ -23,8 +23,7 @@ type Options = {
|
|
|
23
23
|
*
|
|
24
24
|
* It also supports custom serializers, which can be used to serialize custom data types. Each value has a prefix which
|
|
25
25
|
* specifies the type of the value, custom serializers is a map of these prefixes to functions which can serialize the
|
|
26
|
-
* value into a string.
|
|
27
|
-
* `toJSON` it won't trigger custom serializer but will be serialized as string. `Date` class defines `toJSON` method!
|
|
26
|
+
* value into a string.
|
|
28
27
|
*
|
|
29
28
|
* The extra aim of this function is to produce the same output for "the same" data, regardless of the order of the keys
|
|
30
29
|
* (which is not guaranteed by JS spec, but in practice it is guaranteed by current implementations of all JS engines),
|
|
@@ -35,7 +34,7 @@ type Options = {
|
|
|
35
34
|
* @param customSerializers - an object with custom serializers
|
|
36
35
|
* @param options - options
|
|
37
36
|
*/
|
|
38
|
-
const serialize = (data: unknown, customSerializers?: CustomSerializers, options?: Options) => { // eslint-disable-line max-lines-per-function
|
|
37
|
+
const serialize = (data: unknown, customSerializers?: CustomSerializers, options?: Options): string => { // eslint-disable-line max-lines-per-function
|
|
39
38
|
const sourceData = Object.keys(customSerializers ?? {}).length
|
|
40
39
|
? replaceDeepByFn(
|
|
41
40
|
data,
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
const BINARY_MARK_BIN = "t";
|
|
2
|
+
const BINARY_MARK_STRING = "f";
|
|
3
|
+
const BINARY_MARK_JSON = "j";
|
|
4
|
+
|
|
5
|
+
const BINARY_MARK_MAP = {
|
|
6
|
+
[BINARY_MARK_BIN]: "binary",
|
|
7
|
+
[BINARY_MARK_STRING]: "string",
|
|
8
|
+
[BINARY_MARK_JSON]: "json",
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export {
|
|
12
|
+
BINARY_MARK_BIN,
|
|
13
|
+
BINARY_MARK_STRING,
|
|
14
|
+
BINARY_MARK_JSON,
|
|
15
|
+
BINARY_MARK_MAP,
|
|
16
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import must from "must"; // eslint-disable-line @typescript-eslint/no-shadow
|
|
2
|
+
|
|
3
|
+
import { serializeToBuffer } from "./serializeToBuffer";
|
|
4
|
+
import { unserializeFromBuffer } from "./unserializeFromBuffer";
|
|
5
|
+
|
|
6
|
+
const serialize = serializeToBuffer.bind(null, Buffer, []);
|
|
7
|
+
const unserialize = unserializeFromBuffer.bind(null, Buffer, []);
|
|
8
|
+
|
|
9
|
+
describe("serialization", () => {
|
|
10
|
+
it("serializes basic string", async () => {
|
|
11
|
+
const data = ["TEST"];
|
|
12
|
+
must(unserialize(serialize(...data))).eql(data);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("serializes json", async () => {
|
|
16
|
+
const data = [
|
|
17
|
+
{ a: 1, b: 2 },
|
|
18
|
+
];
|
|
19
|
+
must(unserialize(serialize(...data))).eql(data);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it("serializes multiple data", async () => {
|
|
23
|
+
const data = [
|
|
24
|
+
"hey",
|
|
25
|
+
3,
|
|
26
|
+
{ a: 1 },
|
|
27
|
+
Buffer.from("test"),
|
|
28
|
+
];
|
|
29
|
+
const result = unserialize(serialize(...data));
|
|
30
|
+
const buffer = result.pop() as Buffer;
|
|
31
|
+
must(result).eql(data.slice(0, 3));
|
|
32
|
+
must(buffer).be.instanceof(Buffer);
|
|
33
|
+
must(buffer.toString()).equal("test");
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("serializes undefined", async () => {
|
|
37
|
+
const data = [undefined];
|
|
38
|
+
const result = unserialize(serialize(...data));
|
|
39
|
+
must(result).have.length(1);
|
|
40
|
+
must(result[0]).be.undefined();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("serializes JSON-looking string", async () => {
|
|
44
|
+
const data = ["\"TEST\""];
|
|
45
|
+
must(unserialize(serialize(...data))).eql(data);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("serializes huge object", async () => {
|
|
49
|
+
const data = [{
|
|
50
|
+
a: 1,
|
|
51
|
+
b: false,
|
|
52
|
+
c: true,
|
|
53
|
+
d: "test",
|
|
54
|
+
e: null,
|
|
55
|
+
f: undefined,
|
|
56
|
+
g: [1, 2, 3],
|
|
57
|
+
h: { a: 1, b: [2] },
|
|
58
|
+
}];
|
|
59
|
+
must(unserialize(serialize(...data))).eql(data);
|
|
60
|
+
});
|
|
61
|
+
});
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import must from "must"; // eslint-disable-line @typescript-eslint/no-shadow
|
|
2
|
+
|
|
3
|
+
import { serializeToBuffer } from "./serializeToBuffer";
|
|
4
|
+
|
|
5
|
+
const serialize = serializeToBuffer.bind(null, Buffer, []);
|
|
6
|
+
|
|
7
|
+
describe("serialize", () => {
|
|
8
|
+
it("serializes basic string", async () => {
|
|
9
|
+
// len 4, type string, data "test", separator
|
|
10
|
+
must(serialize("test").toString("hex")).equal("3466007465737400");
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("serializes multiple strings", async () => {
|
|
14
|
+
// len 4, type string, data "test", separator
|
|
15
|
+
must(serialize("test", "test").toString("hex")).equal("34660074657374003466007465737400");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("serializes basic buffer", async () => {
|
|
19
|
+
// len 4, type bin, data "test", separator
|
|
20
|
+
must(serialize(Buffer.from("test")).toString("hex")).equal("3474007465737400");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("serializes both buffer and string", async () => {
|
|
24
|
+
// len 4, type bin, data "test", separator, len 4, bin false, data "test", separator
|
|
25
|
+
must(serialize(Buffer.from("test"), "test").toString("hex")).equal("34740074657374003466007465737400");
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it("serializes JSON-looking string", async () => {
|
|
29
|
+
// len 6, type string, data "test", separator
|
|
30
|
+
must(serialize(`"test"`).toString("hex")).equal("36660022746573742200");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
it("serializes JSON-serialized string", async () => {
|
|
34
|
+
// len 8, type string, data "test", separator
|
|
35
|
+
must(serialize(`"s:test"`).toString("hex")).equal("38660022733a746573742200");
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it("serializes number", async () => {
|
|
39
|
+
// len 5, type json, data "n:1", separator
|
|
40
|
+
must(serialize(1).toString("hex")).equal("356a00226e3a312200");
|
|
41
|
+
});
|
|
42
|
+
// TODO more tests
|
|
43
|
+
});
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { serialize } from "../serialize.js";
|
|
2
|
+
import { BINARY_MARK_BIN, BINARY_MARK_JSON, BINARY_MARK_STRING } from "./const.js";
|
|
3
|
+
|
|
4
|
+
type SerializeArgs = Parameters<typeof serialize> extends [any, ...infer Rest] ? Rest : never; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Serialize any data to a Buffer, useful when working with web sockets or other binary protocols.
|
|
8
|
+
* Make sure to understand how {@link serialize} works before using this function.
|
|
9
|
+
*
|
|
10
|
+
* To make this function work in browsers it accepts a BufferImplementation parameter.
|
|
11
|
+
* To make the serialization customizable it accepts a serializeArgs parameter.
|
|
12
|
+
* It's therefore recommended to first prepare the function with bind, like:
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* import { serializeToBuffer as serializeRaw } from "@ezez/utils";
|
|
16
|
+
* import { Buffer } from "buffer/"; // skip import in Node.js
|
|
17
|
+
*
|
|
18
|
+
* // prepare the function:
|
|
19
|
+
* const serializeToBuffer = serializeRaw.bind(null, Buffer, []); // array is customization params for serialize
|
|
20
|
+
*
|
|
21
|
+
* const serialized = serializeToBuffer("test", 1, { test: "test" });
|
|
22
|
+
* ```
|
|
23
|
+
*
|
|
24
|
+
* Otherwise pass the params each time:
|
|
25
|
+
* ```typescript
|
|
26
|
+
* import { serializeToBuffer as serializeRaw } from "@ezez/utils";
|
|
27
|
+
* const serialized = serializeToBuffer(Buffer, [], "test", 1, { test: "test" });
|
|
28
|
+
* const otherSerialized = serializeToBuffer(Buffer, [], any, other, data);
|
|
29
|
+
* ```
|
|
30
|
+
* @param BufferImplementation - Buffer implementation, in browsers use `buffer` npm package
|
|
31
|
+
* and import it from `buffer/`, in Node.js simply pass `Buffer`.
|
|
32
|
+
* @param serializeArgs - [customSerializers, options] - optional arguments if you need to serialize custom data types,
|
|
33
|
+
* see: {@link serialize}
|
|
34
|
+
* @param args - any data to serialize, can be a Buffer, string or any other data type
|
|
35
|
+
* @returns Buffer - serialized data
|
|
36
|
+
*/
|
|
37
|
+
const serializeToBuffer = (
|
|
38
|
+
BufferImplementation: typeof Buffer, serializeArgs: SerializeArgs, ...args: unknown[]
|
|
39
|
+
): Buffer => {
|
|
40
|
+
const separator = BufferImplementation.from([0x0]);
|
|
41
|
+
type DataType = {
|
|
42
|
+
mark: typeof BINARY_MARK_BIN;
|
|
43
|
+
data: Buffer;
|
|
44
|
+
} | {
|
|
45
|
+
mark: typeof BINARY_MARK_STRING;
|
|
46
|
+
data: string;
|
|
47
|
+
} | {
|
|
48
|
+
mark: typeof BINARY_MARK_JSON;
|
|
49
|
+
data: unknown;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const convertedArgs = args.map((arg) => {
|
|
53
|
+
if (arg instanceof BufferImplementation) {
|
|
54
|
+
return {
|
|
55
|
+
mark: BINARY_MARK_BIN,
|
|
56
|
+
data: arg,
|
|
57
|
+
} satisfies DataType;
|
|
58
|
+
}
|
|
59
|
+
if (typeof arg === "string") {
|
|
60
|
+
return {
|
|
61
|
+
mark: BINARY_MARK_STRING,
|
|
62
|
+
data: arg,
|
|
63
|
+
} satisfies DataType;
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
mark: BINARY_MARK_JSON,
|
|
67
|
+
data: serialize(arg, ...serializeArgs),
|
|
68
|
+
} satisfies DataType;
|
|
69
|
+
});
|
|
70
|
+
const dataToSend: Buffer[] = [];
|
|
71
|
+
let totalLength = 0;
|
|
72
|
+
convertedArgs.forEach((arg) => {
|
|
73
|
+
const len = String(arg.data.length) + arg.mark;
|
|
74
|
+
|
|
75
|
+
dataToSend.push(BufferImplementation.from(len, "utf-8"));
|
|
76
|
+
dataToSend.push(separator);
|
|
77
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
|
|
78
|
+
dataToSend.push(arg.mark === BINARY_MARK_BIN ? arg.data : BufferImplementation.from(arg.data, "utf-8"));
|
|
79
|
+
dataToSend.push(separator);
|
|
80
|
+
|
|
81
|
+
totalLength += len.length + separator.length + arg.data.length + separator.length;
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
return BufferImplementation.concat(dataToSend, totalLength);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
export {
|
|
88
|
+
serializeToBuffer,
|
|
89
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import must from "must"; // eslint-disable-line @typescript-eslint/no-shadow
|
|
2
|
+
|
|
3
|
+
import { unserializeFromBuffer } from "./unserializeFromBuffer";
|
|
4
|
+
|
|
5
|
+
const unserialize = unserializeFromBuffer.bind(null, Buffer, []);
|
|
6
|
+
|
|
7
|
+
describe("unserialize", () => {
|
|
8
|
+
it("unserializes basic string", async () => {
|
|
9
|
+
must(unserialize(Buffer.from([0x34, 0x66, 0x00, 0x74, 0x65, 0x73, 0x74, 0x00]))).eql(["test"]);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
it("unserializes multiple strings", async () => {
|
|
13
|
+
must(unserialize(Buffer.from([
|
|
14
|
+
0x34, 0x66, 0x00, 0x74, 0x65, 0x73, 0x74, 0x00,
|
|
15
|
+
0x34, 0x66, 0x00, 0x74, 0x65, 0x73, 0x74, 0x00,
|
|
16
|
+
]))).eql(["test", "test"]);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it("unserializes buffer", async () => {
|
|
20
|
+
const result = unserialize(Buffer.from([0x34, 0x74, 0x00, 0x74, 0x65, 0x73, 0x74, 0x00]));
|
|
21
|
+
must(result.length).equal(1);
|
|
22
|
+
must(result[0]).instanceof(Buffer);
|
|
23
|
+
must((result[0] as Buffer).toString()).equal("test");
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
it("unserializes both buffer and string", async () => {
|
|
27
|
+
const result = unserialize(Buffer.from([
|
|
28
|
+
0x34, 0x74, 0x00, 0x74, 0x65, 0x73, 0x74, 0x00,
|
|
29
|
+
0x34, 0x66, 0x00, 0x74, 0x65, 0x73, 0x74, 0x00,
|
|
30
|
+
]));
|
|
31
|
+
|
|
32
|
+
must(result.length).equal(2);
|
|
33
|
+
|
|
34
|
+
must(result[0]).instanceof(Buffer);
|
|
35
|
+
must((result[0] as Buffer).toString()).equal("test");
|
|
36
|
+
|
|
37
|
+
must(result[1]).equal("test");
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it("unserializes JSON-looking string as string", async () => {
|
|
41
|
+
must(unserialize(
|
|
42
|
+
Buffer.from([0x36, 0x66, 0x00, 0x22, 0x74, 0x65, 0x73, 0x74, 0x22, 0x00]),
|
|
43
|
+
)).eql([`"test"`]);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("unserializes JSON-serialized string as string", async () => {
|
|
47
|
+
must(unserialize(Buffer.from([
|
|
48
|
+
0x38, 0x66, 0x00, 0x22, 0x73, 0x3a, 0x74, 0x65, 0x73, 0x74, 0x22, 0x00,
|
|
49
|
+
]))).eql([`"s:test"`]);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("unserializes JSON-serialized json as string", async () => {
|
|
53
|
+
must(unserialize(Buffer.from([
|
|
54
|
+
0x38, 0x6a, 0x00, 0x22, 0x73, 0x3a, 0x74, 0x65, 0x73, 0x74, 0x22, 0x00,
|
|
55
|
+
]))).eql(["test"]);
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
it("unserializes number as number", async () => {
|
|
59
|
+
must(unserialize(Buffer.from([
|
|
60
|
+
0x35, 0x6a, 0x00, 0x22, 0x6e, 0x3a, 0x31, 0x22, 0x00,
|
|
61
|
+
]))).eql([1]);
|
|
62
|
+
});
|
|
63
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { deserialize } from "../deserialize.js";
|
|
2
|
+
import { BINARY_MARK_BIN, BINARY_MARK_MAP, BINARY_MARK_STRING } from "./const.js";
|
|
3
|
+
|
|
4
|
+
const MAX_DATA_PARTS = 4;
|
|
5
|
+
|
|
6
|
+
const NOT_FOUND = -1;
|
|
7
|
+
const LAST_CHAR = -1;
|
|
8
|
+
|
|
9
|
+
type DeserializeArgs = Parameters<typeof deserialize> extends [any, ...infer Rest] ? Rest : never; // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Unserialize data from a Buffer serialized with {@link serializeToBuffer}, useful when working with web sockets or
|
|
13
|
+
* other binary protocols. Make sure to understand how {@link deserialize} works before using this function.
|
|
14
|
+
* @param BufferImplementation - Buffer implementation, in browsers use `buffer` npm package
|
|
15
|
+
* and import it from `buffer/`, in Node.js simply pass `Buffer`.
|
|
16
|
+
* @param deserializeArgs - [customDeserializers] - optional arguments if you need to unserialize custom data types,
|
|
17
|
+
* @param rawData - Buffer to unserialize
|
|
18
|
+
* @returns unserialized data
|
|
19
|
+
*/
|
|
20
|
+
const unserializeFromBuffer = <RT extends any[] = unknown[]>( // eslint-disable-line @typescript-eslint/no-explicit-any,max-statements
|
|
21
|
+
BufferImplementation: typeof Buffer, deserializeArgs: DeserializeArgs, rawData: Buffer | Uint8Array,
|
|
22
|
+
): RT => {
|
|
23
|
+
const intData: Uint8Array = rawData instanceof Uint8Array ? rawData : new Uint8Array(rawData);
|
|
24
|
+
|
|
25
|
+
if (intData.length === 0) {
|
|
26
|
+
throw new Error("No data to unserialize");
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let startPoint = 0;
|
|
30
|
+
const result = [];
|
|
31
|
+
|
|
32
|
+
let i = 0;
|
|
33
|
+
while (i++ < MAX_DATA_PARTS) {
|
|
34
|
+
const dataSplitPoint = intData.indexOf(0, startPoint); // find null
|
|
35
|
+
if (dataSplitPoint === NOT_FOUND) { // no null found = no data
|
|
36
|
+
break;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
const lengthBytes = BufferImplementation.from(intData.slice(startPoint, dataSplitPoint)).toString("utf8");
|
|
40
|
+
const binaryMark = lengthBytes.slice(LAST_CHAR) as keyof typeof BINARY_MARK_MAP;
|
|
41
|
+
if (!Object.keys(BINARY_MARK_MAP).includes(binaryMark)) {
|
|
42
|
+
throw new Error(`Invalid binary mark: ${binaryMark}`);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const len = Number(lengthBytes.slice(0, lengthBytes.length - 1));
|
|
46
|
+
|
|
47
|
+
const dataStringFrom = dataSplitPoint + 1;
|
|
48
|
+
const dataStringTo = dataStringFrom + len;
|
|
49
|
+
const dataBytes = intData.slice(dataStringFrom, dataStringTo);
|
|
50
|
+
|
|
51
|
+
if (binaryMark === BINARY_MARK_BIN) {
|
|
52
|
+
result.push(dataBytes);
|
|
53
|
+
}
|
|
54
|
+
else if (binaryMark === BINARY_MARK_STRING) {
|
|
55
|
+
const stringData = BufferImplementation.from(dataBytes).toString("utf8");
|
|
56
|
+
result.push(stringData);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
const stringData = BufferImplementation.from(dataBytes).toString("utf8");
|
|
60
|
+
const jsonData = deserialize(stringData, ...deserializeArgs);
|
|
61
|
+
result.push(jsonData);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
startPoint = dataStringTo + 1; // skip separating null
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (result.length === 0) {
|
|
68
|
+
throw new Error("No data found (no split points)");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return result as RT;
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
export { unserializeFromBuffer, unserializeFromBuffer as deserializeFromBuffer };
|
package/src/setImmutable.ts
CHANGED
|
@@ -9,7 +9,7 @@ const isObject = (value: unknown) => (typeof value === "object" || typeof value
|
|
|
9
9
|
|
|
10
10
|
const clone = (value: unknown[] | Source) => {
|
|
11
11
|
if (Array.isArray(value)) {
|
|
12
|
-
return [...value
|
|
12
|
+
return [...value];
|
|
13
13
|
}
|
|
14
14
|
return { ...value };
|
|
15
15
|
};
|
package/src/sortBy.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-magic-numbers */
|
|
2
|
+
|
|
1
3
|
import { sortByMultiple } from "./sortByMultiple.js";
|
|
2
4
|
|
|
3
5
|
/**
|
|
@@ -10,7 +12,7 @@ import { sortByMultiple } from "./sortByMultiple.js";
|
|
|
10
12
|
*/
|
|
11
13
|
const sortBy = <T extends Record<string | number | symbol, any>>( // eslint-disable-line @typescript-eslint/no-explicit-any
|
|
12
14
|
propertyName: keyof T, asc = true, defaultValue: unknown = null,
|
|
13
|
-
) => (a: T, b: T) => {
|
|
15
|
+
) => (a: T, b: T): -1 | 0 | 1 => {
|
|
14
16
|
// eslint-disable-next-line @typescript-eslint/consistent-type-assertions, @typescript-eslint/no-explicit-any
|
|
15
17
|
return sortByMultiple([propertyName], asc, { [propertyName]: defaultValue } as Partial<Record<keyof T, any>>)(a, b);
|
|
16
18
|
};
|
package/src/sortByMultiple.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-magic-numbers */
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Returns a function that can be used as a callback to `.sort()` method. Returned function will sort array by given
|
|
3
5
|
* properties.
|
|
@@ -34,12 +36,12 @@ const sortByMultiple = <
|
|
|
34
36
|
ascending: boolean | Record<K[number], boolean> = true,
|
|
35
37
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
38
|
defaultValues?: Partial<Record<K[number], any>>, // Force defaultValues to use only keys from K
|
|
37
|
-
): ((a: T, b: T) =>
|
|
39
|
+
): ((a: T, b: T) => 1 | -1 | 0) => (a: T, b: T): 1 | -1 | 0 => {
|
|
38
40
|
for (const propertyName of propertyNames) {
|
|
39
41
|
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
|
|
40
42
|
const asc = typeof ascending === "boolean" ? ascending : (ascending[propertyName] ?? true);
|
|
41
|
-
const aBiggerValue = asc ? 1 : -1;
|
|
42
|
-
const bBiggerValue = asc ? -1 : 1;
|
|
43
|
+
const aBiggerValue = asc ? 1 : -1;
|
|
44
|
+
const bBiggerValue = asc ? -1 : 1;
|
|
43
45
|
|
|
44
46
|
const aValue = a[propertyName] ?? defaultValues?.[propertyName];
|
|
45
47
|
const bValue = b[propertyName] ?? defaultValues?.[propertyName];
|
package/src/toggle.spec.ts
CHANGED
package/src/trim.spec.ts
CHANGED
package/src/trim.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { trimStart } from "./trimStart.js";
|
|
2
1
|
import { trimEnd } from "./trimEnd.js";
|
|
2
|
+
import { trimStart } from "./trimStart.js";
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Removes given characters from both sides of the string.
|
|
@@ -14,10 +14,11 @@ import { trimEnd } from "./trimEnd.js";
|
|
|
14
14
|
* trim("!aaa!!!", "!"); // "aaa"
|
|
15
15
|
* trim("!aaa!!!", "!!!"); // "!aaa"
|
|
16
16
|
*/
|
|
17
|
-
const trim = (source: string, characters: string) => {
|
|
17
|
+
const trim = (source: string, characters: string): string => {
|
|
18
18
|
return trimStart(trimEnd(source, characters), characters);
|
|
19
19
|
};
|
|
20
20
|
|
|
21
21
|
export {
|
|
22
22
|
trim,
|
|
23
23
|
};
|
|
24
|
+
|
package/src/trimEnd.spec.ts
CHANGED
package/src/trimEnd.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* trimEnd("abcxzyz", "yz"); // "abcxz"
|
|
10
10
|
* trimEnd("!aaa!!", "!"); // "!aaa"
|
|
11
11
|
*/
|
|
12
|
-
const trimEnd = (source: string, characters: string) => {
|
|
12
|
+
const trimEnd = (source: string, characters: string): string => {
|
|
13
13
|
let s = source;
|
|
14
14
|
while (s.endsWith(characters)) {
|
|
15
15
|
s = s.slice(0, -characters.length);
|
package/src/trimStart.spec.ts
CHANGED
package/src/trimStart.ts
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* trimStart("abbcb", "ab"); // "bcb"
|
|
10
10
|
* trimStart("!!aaa!", "!"); // "aaa!"
|
|
11
11
|
*/
|
|
12
|
-
const trimStart = (source: string, characters: string) => {
|
|
12
|
+
const trimStart = (source: string, characters: string): string => {
|
|
13
13
|
let s = source;
|
|
14
14
|
while (s.startsWith(characters)) {
|
|
15
15
|
s = s.slice(characters.length);
|
package/src/unique.ts
CHANGED
package/src/wait.ts
CHANGED
package/src/waitFor.spec.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
2
2
|
import must from "must";
|
|
3
3
|
|
|
4
|
+
// @ts-ignore
|
|
4
5
|
import createSpy from "../test/createSpy";
|
|
5
|
-
|
|
6
|
-
import { waitFor } from "./waitFor";
|
|
7
6
|
import { wait } from "./wait";
|
|
7
|
+
import { waitFor } from "./waitFor";
|
|
8
8
|
|
|
9
9
|
describe("waitFor", () => {
|
|
10
10
|
it("calls function multiple times until it returns truthy value", async () => {
|
|
@@ -91,12 +91,13 @@ describe("waitFor", () => {
|
|
|
91
91
|
|
|
92
92
|
it("crashes if check function crashes", async () => {
|
|
93
93
|
await waitFor(() => {
|
|
94
|
+
// @ts-ignore
|
|
94
95
|
throw new Error(5);
|
|
95
|
-
}, 40).then(() => {
|
|
96
|
+
}, { interval: 40 }).then(() => {
|
|
96
97
|
throw new Error("Should not resolve");
|
|
97
|
-
}, (e) => {
|
|
98
|
+
}, (e: unknown) => {
|
|
98
99
|
must(e).instanceOf(Error);
|
|
99
|
-
must(e.message).equal("[waitFor] check function threw an error");
|
|
100
|
+
must((e as Error).message).equal("[waitFor] check function threw an error");
|
|
100
101
|
});
|
|
101
102
|
});
|
|
102
103
|
|
|
@@ -134,11 +135,11 @@ describe("waitFor", () => {
|
|
|
134
135
|
it("by crashing if check function returns rejected promise", async () => {
|
|
135
136
|
await waitFor(() => {
|
|
136
137
|
return Promise.reject(new Error("oops"));
|
|
137
|
-
}, 40).then(() => {
|
|
138
|
+
}, { interval: 40 }).then(() => {
|
|
138
139
|
throw new Error("Should not resolve");
|
|
139
|
-
}, (e) => {
|
|
140
|
+
}, (e: unknown) => {
|
|
140
141
|
must(e).instanceOf(Error);
|
|
141
|
-
must(e.message).equal("[waitFor] check function threw an error");
|
|
142
|
+
must((e as Error).message).equal("[waitFor] check function threw an error");
|
|
142
143
|
});
|
|
143
144
|
});
|
|
144
145
|
});
|
package/src/waitFor.ts
CHANGED
|
@@ -39,7 +39,7 @@ const defaultOptions: Required<Options> = {
|
|
|
39
39
|
* @param fn - callback function
|
|
40
40
|
* @param options - options object
|
|
41
41
|
*/
|
|
42
|
-
const waitFor = <T>(fn: () => MaybePromise<T>, options: Options = defaultOptions) => {
|
|
42
|
+
const waitFor = <T>(fn: () => MaybePromise<T>, options: Options = defaultOptions): Promise<T> => {
|
|
43
43
|
return new Promise<T>((resolve, reject) => {
|
|
44
44
|
let intervalTimer: TTimeout, failTimer: TTimeout;
|
|
45
45
|
|
package/src/waitSync.spec.ts
CHANGED
package/src/waitSync.ts
CHANGED
package/.prettierignore
DELETED
package/.prettierrc.json
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{}
|