@ezez/utils 4.7.2 → 4.8.1
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/.claude/settings.local.json +7 -0
- package/CHANGELOG.md +8 -0
- package/CLAUDE.md +137 -0
- package/README.md +1 -0
- package/dist/formatHash.d.ts +3 -0
- package/dist/formatHash.d.ts.map +1 -0
- package/dist/formatHash.js +14 -0
- package/dist/formatHash.js.map +1 -0
- package/dist/getMultiple.d.ts +1 -1
- package/dist/getMultiple.d.ts.map +1 -1
- package/dist/getMultiple.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/insertSeparator.d.ts +1 -1
- package/dist/insertSeparator.d.ts.map +1 -1
- package/dist/insertSeparator.js.map +1 -1
- package/dist/mapValues.js.map +1 -1
- package/dist/sample.d.ts +1 -1
- package/dist/sample.d.ts.map +1 -1
- package/dist/seq.d.ts +1 -1
- package/dist/seq.d.ts.map +1 -1
- package/dist/seq.js.map +1 -1
- package/dist/serializeToBuffer/unserializeFromBuffer.d.ts.map +1 -1
- package/dist/serializeToBuffer/unserializeFromBuffer.js +1 -3
- package/dist/serializeToBuffer/unserializeFromBuffer.js.map +1 -1
- package/dist/setImmutable.d.ts +1 -1
- package/dist/setImmutable.d.ts.map +1 -1
- package/dist/sortByMultiple.d.ts +1 -1
- package/dist/sortByMultiple.d.ts.map +1 -1
- package/docs/assets/navigation.js +1 -1
- package/docs/assets/search.js +1 -1
- package/docs/documents/Changelog.html +72 -66
- package/docs/functions/index.assertProps.html +2 -2
- package/docs/functions/index.cap.html +2 -2
- package/docs/functions/index.capitalize.html +2 -2
- package/docs/functions/index.coalesce.html +2 -2
- package/docs/functions/index.compareArrays.html +2 -2
- package/docs/functions/index.compareProps.html +2 -2
- package/docs/functions/index.deserialize.html +2 -2
- package/docs/functions/index.ensureArray.html +2 -2
- package/docs/functions/index.ensureDate.html +2 -2
- package/docs/functions/index.ensureError.html +2 -2
- package/docs/functions/index.ensurePrefix.html +2 -2
- package/docs/functions/index.ensureSuffix.html +2 -2
- package/docs/functions/index.ensureTimestamp.html +2 -2
- package/docs/functions/index.escapeRegExp.html +2 -2
- package/docs/functions/index.formatDate.html +2 -2
- package/docs/functions/index.formatHash.html +6 -0
- package/docs/functions/index.get.html +2 -2
- package/docs/functions/index.getMultiple.html +2 -2
- package/docs/functions/index.hasProps.html +2 -2
- package/docs/functions/index.ignore.html +2 -2
- package/docs/functions/index.insertSeparator.html +2 -2
- package/docs/functions/index.isEmpty.html +2 -2
- package/docs/functions/index.isNumericString.html +2 -2
- package/docs/functions/index.isPlainObject.html +2 -2
- package/docs/functions/index.last.html +2 -2
- package/docs/functions/index.later-1.html +2 -2
- package/docs/functions/index.mapAsync.html +2 -2
- package/docs/functions/index.mapValues.html +2 -2
- package/docs/functions/index.match.html +2 -2
- package/docs/functions/index.memoize.html +2 -2
- package/docs/functions/index.merge.html +2 -2
- package/docs/functions/index.mostFrequent.html +2 -2
- package/docs/functions/index.noop.html +2 -2
- package/docs/functions/index.occurrences.html +2 -2
- package/docs/functions/index.omit.html +2 -2
- package/docs/functions/index.pick.html +2 -2
- package/docs/functions/index.pull.html +2 -2
- package/docs/functions/index.race.html +2 -2
- package/docs/functions/index.remove.html +2 -2
- package/docs/functions/index.removeCommonProperties.html +2 -2
- package/docs/functions/index.replace.html +2 -2
- package/docs/functions/index.replaceDeep.html +2 -2
- package/docs/functions/index.rethrow.html +2 -2
- package/docs/functions/index.retry.html +2 -2
- package/docs/functions/index.round.html +2 -2
- package/docs/functions/index.safe.html +2 -2
- package/docs/functions/index.sample.html +2 -2
- package/docs/functions/index.samples.html +2 -2
- package/docs/functions/index.scale.html +2 -2
- package/docs/functions/index.seq.html +2 -2
- package/docs/functions/index.seqEarlyBreak.html +2 -2
- package/docs/functions/index.serialize.html +2 -2
- package/docs/functions/index.serializeToBuffer.html +2 -2
- package/docs/functions/index.set.html +2 -2
- package/docs/functions/index.setImmutable.html +2 -2
- package/docs/functions/index.shuffle.html +2 -2
- package/docs/functions/index.sortBy.html +2 -2
- package/docs/functions/index.sortByMultiple.html +2 -2
- package/docs/functions/index.sortProps.html +2 -2
- package/docs/functions/index.stripPrefix.html +2 -2
- package/docs/functions/index.stripSuffix.html +2 -2
- package/docs/functions/index.throttle.html +2 -2
- package/docs/functions/index.toggle.html +2 -2
- package/docs/functions/index.trim.html +2 -2
- package/docs/functions/index.trimEnd.html +2 -2
- package/docs/functions/index.trimStart.html +2 -2
- package/docs/functions/index.truthy.html +2 -2
- package/docs/functions/index.unique.html +2 -2
- package/docs/functions/index.unserializeFromBuffer.html +2 -2
- package/docs/functions/index.wait.html +2 -2
- package/docs/functions/index.waitFor.html +2 -2
- package/docs/functions/index.waitSync.html +2 -2
- package/docs/index.html +3 -2
- package/docs/interfaces/index.ComparePropsOptions.html +3 -3
- package/docs/interfaces/index.GetMultipleSource.html +2 -2
- package/docs/interfaces/index.GetSource.html +2 -2
- package/docs/interfaces/index.IsNumericStringOptions.html +2 -2
- package/docs/interfaces/index.OccurencesOptions.html +2 -2
- package/docs/interfaces/index.SetImmutableSource.html +2 -2
- package/docs/interfaces/index.SetSource.html +2 -2
- package/docs/interfaces/index.ThrottleOptions.html +3 -3
- package/docs/interfaces/index.ThrottledFunctionExtras.html +3 -3
- package/docs/modules/index.html +1 -1
- package/docs/modules.html +1 -1
- package/docs/types/index.CustomDeserializers.html +1 -1
- package/docs/types/index.CustomSerializers.html +1 -1
- package/docs/types/index.Later.html +2 -2
- package/docs/types/index.MapValuesFn.html +2 -2
- package/docs/types/index.MatchCallback.html +1 -1
- package/docs/types/index.MergeTwo.html +2 -2
- package/docs/types/index.SeqEarlyBreaker.html +2 -2
- package/docs/types/index.SeqFn.html +2 -2
- package/docs/types/index.SeqFunctions.html +2 -2
- 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 +2 -2
- package/esm/formatHash.d.ts +3 -0
- package/esm/formatHash.d.ts.map +1 -0
- package/esm/formatHash.js +11 -0
- package/esm/formatHash.js.map +1 -0
- package/esm/getMultiple.d.ts +1 -1
- package/esm/getMultiple.d.ts.map +1 -1
- package/esm/getMultiple.js.map +1 -1
- package/esm/index.d.ts +1 -0
- package/esm/index.d.ts.map +1 -1
- package/esm/index.js +1 -0
- package/esm/index.js.map +1 -1
- package/esm/insertSeparator.d.ts +1 -1
- package/esm/insertSeparator.d.ts.map +1 -1
- package/esm/insertSeparator.js.map +1 -1
- package/esm/mapValues.js.map +1 -1
- package/esm/sample.d.ts +1 -1
- package/esm/sample.d.ts.map +1 -1
- package/esm/seq.d.ts +1 -1
- package/esm/seq.d.ts.map +1 -1
- package/esm/seq.js.map +1 -1
- package/esm/serializeToBuffer/unserializeFromBuffer.d.ts.map +1 -1
- package/esm/serializeToBuffer/unserializeFromBuffer.js +1 -3
- package/esm/serializeToBuffer/unserializeFromBuffer.js.map +1 -1
- package/esm/setImmutable.d.ts +1 -1
- package/esm/setImmutable.d.ts.map +1 -1
- package/esm/sortByMultiple.d.ts +1 -1
- package/esm/sortByMultiple.d.ts.map +1 -1
- package/package.json +5 -9
- package/src/formatHash.spec.ts +32 -0
- package/src/formatHash.ts +24 -0
- package/src/getMultiple.ts +1 -1
- package/src/ignore.spec.ts +1 -1
- package/src/index.ts +1 -0
- package/src/insertSeparator.ts +2 -2
- package/src/mapValues.ts +1 -1
- package/src/memoize.spec.ts +1 -1
- package/src/remove.spec.ts +1 -1
- package/src/safe.ts +1 -1
- package/src/sample.ts +2 -2
- package/src/seq.ts +3 -3
- package/src/serializeToBuffer/unserializeFromBuffer.spec.ts +10 -0
- package/src/serializeToBuffer/unserializeFromBuffer.ts +1 -4
- package/src/setImmutable.ts +1 -1
- package/src/sortByMultiple.ts +1 -1
- package/src/waitFor.spec.ts +2 -3
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const DEFAULT_SIDE_CHARS_LENGTH = 4;
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Formats given input string, taking first `sideCharsLength` and last `sideCharsLength` characters,
|
|
5
|
+
* joining them with `separator`. If there is not enough characters, returns the original string.
|
|
6
|
+
* @param hash
|
|
7
|
+
* @param sideCharsLength
|
|
8
|
+
* @param separator
|
|
9
|
+
* @example formatHash("abcdef1234567890") // "abcd…7890"
|
|
10
|
+
*/
|
|
11
|
+
const formatHash = (hash: string, sideCharsLength: number = DEFAULT_SIDE_CHARS_LENGTH, separator = "…"): string => {
|
|
12
|
+
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
|
|
13
|
+
if (hash.length <= sideCharsLength * 2) {
|
|
14
|
+
return hash;
|
|
15
|
+
}
|
|
16
|
+
const start = hash.slice(0, sideCharsLength);
|
|
17
|
+
const end = hash.slice(-sideCharsLength);
|
|
18
|
+
return `${start}${separator}${end}`;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export {
|
|
22
|
+
formatHash,
|
|
23
|
+
};
|
|
24
|
+
|
package/src/getMultiple.ts
CHANGED
|
@@ -28,7 +28,7 @@ const DEFAULT = {};
|
|
|
28
28
|
* // will return 5
|
|
29
29
|
* @returns {*} - found value or default value
|
|
30
30
|
*/
|
|
31
|
-
const getMultiple = (source: Source, defaultValue: unknown, ...paths:
|
|
31
|
+
const getMultiple = (source: Source, defaultValue: unknown, ...paths: Array<string | string[]>): unknown => {
|
|
32
32
|
const length = paths.length;
|
|
33
33
|
for (let i = 0; i < length; i++) {
|
|
34
34
|
const properties = paths[i]!;
|
package/src/ignore.spec.ts
CHANGED
package/src/index.ts
CHANGED
|
@@ -16,6 +16,7 @@ export * from "./ensureError.js";
|
|
|
16
16
|
export * from "./ensureTimestamp.js";
|
|
17
17
|
export * from "./escapeRegExp.js";
|
|
18
18
|
export * from "./formatDate.js";
|
|
19
|
+
export * from "./formatHash.js";
|
|
19
20
|
export * from "./get.js";
|
|
20
21
|
export * from "./getMultiple.js";
|
|
21
22
|
export * from "./hasProps.js";
|
package/src/insertSeparator.ts
CHANGED
|
@@ -5,14 +5,14 @@
|
|
|
5
5
|
* @param {*} separator - separator to inset
|
|
6
6
|
* @returns {Array} - new array with separator items added or same array if there isn't enough items to put separator
|
|
7
7
|
*/
|
|
8
|
-
const insertSeparator = <T, S>(source: T[], separator: S):
|
|
8
|
+
const insertSeparator = <T, S>(source: T[], separator: S): Array<T | S> => {
|
|
9
9
|
if (!Array.isArray(source)) {
|
|
10
10
|
throw new TypeError("Source must be an array");
|
|
11
11
|
}
|
|
12
12
|
if (source.length <= 1) {
|
|
13
13
|
return source;
|
|
14
14
|
}
|
|
15
|
-
const result:
|
|
15
|
+
const result: Array<T | S> = [...source];
|
|
16
16
|
for (let i = result.length - 1; i > 0; i--) {
|
|
17
17
|
result.splice(i, 0, separator);
|
|
18
18
|
}
|
package/src/mapValues.ts
CHANGED
|
@@ -35,7 +35,7 @@ type MapValuesFn<S, R> = (value: S[keyof S], key: keyof S) => R;
|
|
|
35
35
|
const mapValues = <SourceObject extends Record<string, unknown>, PossibleReturnValues>(
|
|
36
36
|
source: SourceObject, fn: MapValuesFn<SourceObject, PossibleReturnValues>,
|
|
37
37
|
): { [P in keyof SourceObject]: PossibleReturnValues } => {
|
|
38
|
-
const keys = Object.keys(source) as
|
|
38
|
+
const keys = Object.keys(source) as Array<keyof SourceObject>;
|
|
39
39
|
|
|
40
40
|
// @ts-expect-error TypeScript doesn't work well with this type with reduce
|
|
41
41
|
// @TODO check with ts 5+ or something
|
package/src/memoize.spec.ts
CHANGED
package/src/remove.spec.ts
CHANGED
package/src/safe.ts
CHANGED
|
@@ -15,7 +15,7 @@ function safe<T, Y>(fn: () => T, def?: Y): T | (Y extends undefined ? undefined
|
|
|
15
15
|
return fn();
|
|
16
16
|
}
|
|
17
17
|
catch {
|
|
18
|
-
// @ts-
|
|
18
|
+
// @ts-expect-error ts can't do conditional returns in runtime
|
|
19
19
|
return def;
|
|
20
20
|
}
|
|
21
21
|
}
|
package/src/sample.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
2
2
|
type IsTuple<T> = T extends readonly [infer A, ...(infer B)] ? true : false;
|
|
3
3
|
type MaybeTupleReturn<T> = IsTuple<T> extends true
|
|
4
|
-
? (T extends
|
|
5
|
-
: (T extends
|
|
4
|
+
? (T extends ReadonlyArray<infer U> ? U : never)
|
|
5
|
+
: (T extends Array<infer U> ? U | undefined : never);
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
8
|
* Picks a random element from an array.
|
package/src/seq.ts
CHANGED
|
@@ -15,7 +15,7 @@ type Fn<T> = () => T;
|
|
|
15
15
|
*
|
|
16
16
|
* @see {@link seq}
|
|
17
17
|
*/
|
|
18
|
-
type Functions<T> = Fn<T
|
|
18
|
+
type Functions<T> = Array<Fn<T>> | [Array<Fn<T>>];
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* A function that decides if the sequential run should be early stopped.
|
|
@@ -25,7 +25,7 @@ type Functions<T> = Fn<T>[] | [Fn<T>[]];
|
|
|
25
25
|
*/
|
|
26
26
|
type EarlyBreaker = (e: unknown) => boolean;
|
|
27
27
|
|
|
28
|
-
const run = <T>(list: Fn<T
|
|
28
|
+
const run = <T>(list: Array<Fn<T>>, earlyBreaker?: EarlyBreaker): Promise<T> => {
|
|
29
29
|
if (!list.length) {
|
|
30
30
|
return fail();
|
|
31
31
|
}
|
|
@@ -81,7 +81,7 @@ const seqEarlyBreak = <T>(earlyBreaker: EarlyBreaker | undefined, ...args: Funct
|
|
|
81
81
|
if (args.length === 1) {
|
|
82
82
|
return run(Array.isArray(args[0]) ? args[0] : [args[0]], earlyBreaker);
|
|
83
83
|
}
|
|
84
|
-
return run(args as Fn<T
|
|
84
|
+
return run(args as Array<Fn<T>>, earlyBreaker);
|
|
85
85
|
};
|
|
86
86
|
|
|
87
87
|
/**
|
|
@@ -60,4 +60,14 @@ describe("unserialize", () => {
|
|
|
60
60
|
0x35, 0x6a, 0x00, 0x22, 0x6e, 0x3a, 0x31, 0x22, 0x00,
|
|
61
61
|
]))).eql([1]);
|
|
62
62
|
});
|
|
63
|
+
|
|
64
|
+
it("unserializes multiple various arguments", async () => {
|
|
65
|
+
must(unserialize(Buffer.from([
|
|
66
|
+
0x35, 0x66, 0x00, 0x70, 0x69, 0x6e, 0x67, 0x32, 0x00,
|
|
67
|
+
0x35, 0x6a, 0x00, 0x22, 0x6e, 0x3a, 0x35, 0x22, 0x00,
|
|
68
|
+
0x35, 0x6a, 0x00, 0x22, 0x62, 0x3a, 0x31, 0x22, 0x00,
|
|
69
|
+
0x35, 0x6a, 0x00, 0x22, 0x6e, 0x3a, 0x36, 0x22, 0x00,
|
|
70
|
+
0x35, 0x6a, 0x00, 0x22, 0x6e, 0x3a, 0x39, 0x22, 0x00,
|
|
71
|
+
]))).eql(["ping2", 5, true, 6, 9]);
|
|
72
|
+
});
|
|
63
73
|
});
|
|
@@ -1,8 +1,6 @@
|
|
|
1
1
|
import { deserialize } from "../deserialize.js";
|
|
2
2
|
import { BINARY_MARK_BIN, BINARY_MARK_MAP, BINARY_MARK_STRING } from "./const.js";
|
|
3
3
|
|
|
4
|
-
const MAX_DATA_PARTS = 4;
|
|
5
|
-
|
|
6
4
|
const NOT_FOUND = -1;
|
|
7
5
|
const LAST_CHAR = -1;
|
|
8
6
|
|
|
@@ -29,8 +27,7 @@ const unserializeFromBuffer = <RT extends any[] = unknown[]>( // eslint-disable-
|
|
|
29
27
|
let startPoint = 0;
|
|
30
28
|
const result = [];
|
|
31
29
|
|
|
32
|
-
|
|
33
|
-
while (i++ < MAX_DATA_PARTS) {
|
|
30
|
+
while (startPoint < intData.length) {
|
|
34
31
|
const dataSplitPoint = intData.indexOf(0, startPoint); // find null
|
|
35
32
|
if (dataSplitPoint === NOT_FOUND) { // no null found = no data
|
|
36
33
|
break;
|
package/src/setImmutable.ts
CHANGED
|
@@ -30,7 +30,7 @@ const hasOnlyValidPathParts = (array: unknown[]) => {
|
|
|
30
30
|
* Path to store value in. Can be a dot-separated string or an array of strings.
|
|
31
31
|
* Numbers are allowed for convenience.
|
|
32
32
|
*/
|
|
33
|
-
type Path = number | string |
|
|
33
|
+
type Path = number | string | Array<number | string>;
|
|
34
34
|
|
|
35
35
|
const getPathParts = (path: Path) => {
|
|
36
36
|
if (typeof path === "number") {
|
package/src/sortByMultiple.ts
CHANGED
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
const sortByMultiple = <
|
|
31
31
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
32
32
|
T extends Record<string | number | symbol, any>,
|
|
33
|
-
K extends
|
|
33
|
+
K extends Array<keyof T>,
|
|
34
34
|
>(
|
|
35
35
|
propertyNames: K,
|
|
36
36
|
ascending: boolean | Record<K[number], boolean> = true,
|
package/src/waitFor.spec.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// eslint-disable-next-line @typescript-eslint/no-shadow
|
|
2
2
|
import must from "must";
|
|
3
3
|
|
|
4
|
-
// @ts-
|
|
4
|
+
// @ts-expect-error Complaining about root dir
|
|
5
5
|
import createSpy from "../test/createSpy";
|
|
6
6
|
import { wait } from "./wait";
|
|
7
7
|
import { waitFor } from "./waitFor";
|
|
@@ -91,8 +91,7 @@ describe("waitFor", () => {
|
|
|
91
91
|
|
|
92
92
|
it("crashes if check function crashes", async () => {
|
|
93
93
|
await waitFor(() => {
|
|
94
|
-
|
|
95
|
-
throw new Error(5);
|
|
94
|
+
throw new Error("5");
|
|
96
95
|
}, { interval: 40 }).then(() => {
|
|
97
96
|
throw new Error("Should not resolve");
|
|
98
97
|
}, (e: unknown) => {
|