@ls-stack/utils 1.0.0 → 1.1.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/dist/arrayUtils.d.ts +5 -4
- package/dist/arrayUtils.js +3 -1
- package/dist/{chunk-5SB253CL.js → chunk-W7X6NSGR.js} +13 -3
- package/dist/chunk-W7X6NSGR.js.map +1 -0
- package/dist/testUtils.js +1 -1
- package/dist/typingFnUtils.d.ts +11 -1
- package/dist/typingFnUtils.js +20 -1
- package/dist/typingFnUtils.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-5SB253CL.js.map +0 -1
package/dist/arrayUtils.d.ts
CHANGED
|
@@ -24,7 +24,7 @@ type SortOrder = 'desc' | 'asc';
|
|
|
24
24
|
*
|
|
25
25
|
* Sort by `ascending` order by default
|
|
26
26
|
*
|
|
27
|
-
* Use `Infinity` as as wildcard to
|
|
27
|
+
* Use `Infinity` as as wildcard to absolute max and min values
|
|
28
28
|
*
|
|
29
29
|
* @example
|
|
30
30
|
* const items = [1, 3, 2, 4];
|
|
@@ -37,14 +37,15 @@ type SortOrder = 'desc' | 'asc';
|
|
|
37
37
|
* // return a array to sort by multiple values
|
|
38
38
|
* const sortedItems = sortBy(items, (item) => [item.a, item.b]);
|
|
39
39
|
**/
|
|
40
|
-
declare function sortBy<T>(arr: T[], sortByValue: (item: T) => (number | string)[] | number | string,
|
|
40
|
+
declare function sortBy<T>(arr: T[], sortByValue: (item: T) => (number | string)[] | number | string, props?: {
|
|
41
41
|
order?: SortOrder | SortOrder[];
|
|
42
|
-
}): T[];
|
|
42
|
+
} | SortOrder | SortOrder[]): T[];
|
|
43
43
|
declare function arrayWithPrev<T>(array: T[]): [current: T, prev: T | null][];
|
|
44
44
|
declare function arrayWithPrevAndIndex<T>(array: T[]): {
|
|
45
45
|
item: T;
|
|
46
46
|
prev: T | null;
|
|
47
47
|
index: number;
|
|
48
48
|
}[];
|
|
49
|
+
declare function isInArray<T, const U extends T>(value: T, oneOf: readonly U[]): value is U;
|
|
49
50
|
|
|
50
|
-
export { type FilterAndMapReturn, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, sortBy };
|
|
51
|
+
export { type FilterAndMapReturn, arrayWithPrev, arrayWithPrevAndIndex, filterAndMap, isInArray, sortBy };
|
package/dist/arrayUtils.js
CHANGED
|
@@ -2,12 +2,14 @@ import {
|
|
|
2
2
|
arrayWithPrev,
|
|
3
3
|
arrayWithPrevAndIndex,
|
|
4
4
|
filterAndMap,
|
|
5
|
+
isInArray,
|
|
5
6
|
sortBy
|
|
6
|
-
} from "./chunk-
|
|
7
|
+
} from "./chunk-W7X6NSGR.js";
|
|
7
8
|
export {
|
|
8
9
|
arrayWithPrev,
|
|
9
10
|
arrayWithPrevAndIndex,
|
|
10
11
|
filterAndMap,
|
|
12
|
+
isInArray,
|
|
11
13
|
sortBy
|
|
12
14
|
};
|
|
13
15
|
//# sourceMappingURL=arrayUtils.js.map
|
|
@@ -11,7 +11,8 @@ function filterAndMap(array, mapFilter) {
|
|
|
11
11
|
}
|
|
12
12
|
return result;
|
|
13
13
|
}
|
|
14
|
-
function sortBy(arr, sortByValue,
|
|
14
|
+
function sortBy(arr, sortByValue, props = "asc") {
|
|
15
|
+
const order = Array.isArray(props) || typeof props === "string" ? props : props.order ?? "asc";
|
|
15
16
|
return [...arr].sort((a, b) => {
|
|
16
17
|
const _aPriority = sortByValue(a);
|
|
17
18
|
const _bPriority = sortByValue(b);
|
|
@@ -44,11 +45,20 @@ function arrayWithPrevAndIndex(array) {
|
|
|
44
45
|
index: i
|
|
45
46
|
}));
|
|
46
47
|
}
|
|
48
|
+
function isInArray(value, oneOf) {
|
|
49
|
+
for (let i = 0; i < oneOf.length; i++) {
|
|
50
|
+
if (oneOf[i] === value) {
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
47
56
|
|
|
48
57
|
export {
|
|
49
58
|
filterAndMap,
|
|
50
59
|
sortBy,
|
|
51
60
|
arrayWithPrev,
|
|
52
|
-
arrayWithPrevAndIndex
|
|
61
|
+
arrayWithPrevAndIndex,
|
|
62
|
+
isInArray
|
|
53
63
|
};
|
|
54
|
-
//# sourceMappingURL=chunk-
|
|
64
|
+
//# sourceMappingURL=chunk-W7X6NSGR.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/arrayUtils.ts"],"sourcesContent":["/**\n * allow to filter and map with better typing ergonomics\n *\n * In the `mapFilter` function return `false` to reject the item, or any other\n * value to map it.\n *\n * @example\n * // Filter reject and turn value into `value mapped`\n * const items = ['value', 'value', 'reject', 'reject'];\n *\n * const mappedItems = filterAndMap(items, (item) =>\n * item === 'reject'\n * ? false\n * : `${item} mapped`,\n * );\n *\n * mappedItems; // ['value mapped', 'value mapped']\n */\nexport function filterAndMap<T, R>(\n array: IterableIterator<T> | readonly T[],\n mapFilter: (item: T, index: number) => false | R,\n): R[] {\n const result: R[] = [];\n\n let i = -1;\n for (const item of array) {\n i += 1;\n const filterResult = mapFilter(item, i);\n\n if (filterResult !== false) {\n result.push(filterResult);\n }\n }\n\n return result;\n}\n\nexport type FilterAndMapReturn<T> = false | T;\n\ntype SortOrder = 'desc' | 'asc';\n\n/**\n * Sort an array based on a value\n *\n * Sort by `ascending` order by default\n *\n * Use `Infinity` as as wildcard to absolute max and min values\n *\n * @example\n * const items = [1, 3, 2, 4];\n *\n * const sortedItems = sortBy(items, (item) => item);\n * // [1, 2, 3, 4]\n *\n * const items2 = [{ a: 1, b: 2 }, { a: 2, b: 1 }, { a: 1, b: 1}]\n *\n * // return a array to sort by multiple values\n * const sortedItems = sortBy(items, (item) => [item.a, item.b]);\n **/\nexport function sortBy<T>(\n arr: T[],\n sortByValue: (item: T) => (number | string)[] | number | string,\n props: { order?: SortOrder | SortOrder[] } | SortOrder | SortOrder[] = 'asc',\n) {\n const order =\n Array.isArray(props) || typeof props === 'string' ?\n props\n : props.order ?? 'asc';\n\n return [...arr].sort((a, b) => {\n const _aPriority = sortByValue(a);\n const _bPriority = sortByValue(b);\n\n const aPriority = Array.isArray(_aPriority) ? _aPriority : [_aPriority];\n const bPriority = Array.isArray(_bPriority) ? _bPriority : [_bPriority];\n\n for (let i = 0; i < aPriority.length; i++) {\n const levelOrder: SortOrder =\n typeof order === 'string' ? order : order[i] ?? 'asc';\n\n const aP = aPriority[i] ?? 0;\n const bP = bPriority[i] ?? 0;\n\n if (aP === bP) {\n continue;\n }\n\n if (bP === Infinity || aP === -Infinity || aP < bP) {\n return levelOrder === 'asc' ? -1 : 1;\n }\n\n if (aP === Infinity || bP === -Infinity || aP > bP) {\n return levelOrder === 'asc' ? 1 : -1;\n }\n }\n\n return 0;\n });\n}\n\nexport function arrayWithPrev<T>(array: T[]): [current: T, prev: T | null][] {\n return array.map((item, i) => [item, array[i - 1] ?? null]);\n}\n\nexport function arrayWithPrevAndIndex<T>(\n array: T[],\n): { item: T; prev: T | null; index: number }[] {\n return array.map((item, i) => ({\n item,\n prev: array[i - 1] ?? null,\n index: i,\n }));\n}\n\nexport function isInArray<T, const U extends T>(\n value: T,\n oneOf: readonly U[],\n): value is U {\n for (let i = 0; i < oneOf.length; i++) {\n if (oneOf[i] === value) {\n return true;\n }\n }\n\n return false;\n}\n"],"mappings":";AAkBO,SAAS,aACd,OACA,WACK;AACL,QAAM,SAAc,CAAC;AAErB,MAAI,IAAI;AACR,aAAW,QAAQ,OAAO;AACxB,SAAK;AACL,UAAM,eAAe,UAAU,MAAM,CAAC;AAEtC,QAAI,iBAAiB,OAAO;AAC1B,aAAO,KAAK,YAAY;AAAA,IAC1B;AAAA,EACF;AAEA,SAAO;AACT;AAwBO,SAAS,OACd,KACA,aACA,QAAuE,OACvE;AACA,QAAM,QACJ,MAAM,QAAQ,KAAK,KAAK,OAAO,UAAU,WACvC,QACA,MAAM,SAAS;AAEnB,SAAO,CAAC,GAAG,GAAG,EAAE,KAAK,CAAC,GAAG,MAAM;AAC7B,UAA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artial<T extends Record<string, unknown>>(obj: T): NonPartial<T>;
|
|
4
|
+
/** a wrapper to Object.entries with a better typing inference */
|
|
5
|
+
declare function typedObjectEntries<T extends Record<string, unknown>>(obj: T): NonNullable<{
|
|
6
|
+
[K in keyof T]: [K, T[K]];
|
|
7
|
+
}[keyof T]>[];
|
|
8
|
+
/** a wrapper to Object.keys with a better typing inference */
|
|
9
|
+
declare function typedObjectKeys<T extends Record<string, unknown>>(obj: T): (keyof T)[];
|
|
10
|
+
/** a safe way to cast types, use to substitute the `as Type` */
|
|
11
|
+
declare function asType<T = unknown>(value: T): T;
|
|
12
|
+
/** narrow a string to a union of strings */
|
|
13
|
+
declare function narrowStringToUnion<const T extends string>(key: string | undefined | null, union: T[]): T | undefined;
|
|
4
14
|
|
|
5
|
-
export { asNonPartial };
|
|
15
|
+
export { asNonPartial, asType, narrowStringToUnion, typedObjectEntries, typedObjectKeys };
|
package/dist/typingFnUtils.js
CHANGED
|
@@ -2,7 +2,26 @@
|
|
|
2
2
|
function asNonPartial(obj) {
|
|
3
3
|
return obj;
|
|
4
4
|
}
|
|
5
|
+
function typedObjectEntries(obj) {
|
|
6
|
+
return Object.entries(obj);
|
|
7
|
+
}
|
|
8
|
+
function typedObjectKeys(obj) {
|
|
9
|
+
return Object.keys(obj);
|
|
10
|
+
}
|
|
11
|
+
function asType(value) {
|
|
12
|
+
return value;
|
|
13
|
+
}
|
|
14
|
+
function narrowStringToUnion(key, union) {
|
|
15
|
+
if (key && union.includes(key)) {
|
|
16
|
+
return key;
|
|
17
|
+
}
|
|
18
|
+
return void 0;
|
|
19
|
+
}
|
|
5
20
|
export {
|
|
6
|
-
asNonPartial
|
|
21
|
+
asNonPartial,
|
|
22
|
+
asType,
|
|
23
|
+
narrowStringToUnion,
|
|
24
|
+
typedObjectEntries,
|
|
25
|
+
typedObjectKeys
|
|
7
26
|
};
|
|
8
27
|
//# sourceMappingURL=typingFnUtils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/typingFnUtils.ts"],"sourcesContent":["import { NonPartial } from './typingUtils';\n\nexport function asNonPartial<T extends Record<string, unknown>>(\n obj: T,\n): NonPartial<T> {\n return obj;\n}\n"],"mappings":";AAEO,SAAS,aACd,KACe;AACf,SAAO;AACT;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/typingFnUtils.ts"],"sourcesContent":["import { NonPartial } from './typingUtils';\n\nexport function asNonPartial<T extends Record<string, unknown>>(\n obj: T,\n): NonPartial<T> {\n return obj;\n}\n\n/** a wrapper to Object.entries with a better typing inference */\nexport function typedObjectEntries<T extends Record<string, unknown>>(\n obj: T,\n): NonNullable<\n {\n [K in keyof T]: [K, T[K]];\n }[keyof T]\n>[] {\n return Object.entries(obj) as any;\n}\n\n/** a wrapper to Object.keys with a better typing inference */\nexport function typedObjectKeys<T extends Record<string, unknown>>(\n obj: T,\n): (keyof T)[] {\n return Object.keys(obj) as any;\n}\n\n/** a safe way to cast types, use to substitute the `as Type` */\nexport function asType<T = unknown>(value: T): T {\n return value;\n}\n\n/** narrow a string to a union of strings */\nexport function narrowStringToUnion<const T extends string>(\n key: string | undefined | null,\n union: T[],\n): T | undefined {\n if (key && union.includes(key as T)) {\n return key as T;\n }\n\n return undefined;\n}\n"],"mappings":";AAEO,SAAS,aACd,KACe;AACf,SAAO;AACT;AAGO,SAAS,mBACd,KAKE;AACF,SAAO,OAAO,QAAQ,GAAG;AAC3B;AAGO,SAAS,gBACd,KACa;AACb,SAAO,OAAO,KAAK,GAAG;AACxB;AAGO,SAAS,OAAoB,OAAa;AAC/C,SAAO;AACT;AAGO,SAAS,oBACd,KACA,OACe;AACf,MAAI,OAAO,MAAM,SAAS,GAAQ,GAAG;AACnC,WAAO;AAAA,EACT;AAEA,SAAO;AACT;","names":[]}
|
package/package.json
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/arrayUtils.ts"],"sourcesContent":["/**\n * allow to filter and map with better typing ergonomics\n *\n * In the `mapFilter` function return `false` to reject the item, or any other\n * value to map it.\n *\n * @example\n * // Filter reject and turn value into `value mapped`\n * const items = ['value', 'value', 'reject', 'reject'];\n *\n * const mappedItems = filterAndMap(items, (item) =>\n * item === 'reject'\n * ? false\n * : `${item} mapped`,\n * );\n *\n * mappedItems; // ['value mapped', 'value mapped']\n */\nexport function filterAndMap<T, R>(\n array: IterableIterator<T> | readonly T[],\n mapFilter: (item: T, index: number) => false | R,\n): R[] {\n const result: R[] = [];\n\n let i = -1;\n for (const item of array) {\n i += 1;\n const filterResult = mapFilter(item, i);\n\n if (filterResult !== false) {\n result.push(filterResult);\n }\n }\n\n return result;\n}\n\nexport type FilterAndMapReturn<T> = false | T;\n\ntype SortOrder = 'desc' | 'asc';\n\n/**\n * Sort an array based on a value\n *\n * Sort by `ascending` order by default\n *\n * Use `Infinity` as as wildcard to absulute max and min values\n *\n * @example\n * const items = [1, 3, 2, 4];\n *\n * const sortedItems = sortBy(items, (item) => item);\n * // [1, 2, 3, 4]\n *\n * const items2 = [{ a: 1, b: 2 }, { a: 2, b: 1 }, { a: 1, b: 1}]\n *\n * // return a array to sort by multiple values\n * const sortedItems = sortBy(items, (item) => [item.a, item.b]);\n **/\nexport function sortBy<T>(\n arr: T[],\n sortByValue: (item: T) => (number | string)[] | number | string,\n { order = 'asc' }: { order?: SortOrder | SortOrder[] } = {},\n) {\n return [...arr].sort((a, b) => {\n const _aPriority = sortByValue(a);\n const _bPriority = sortByValue(b);\n\n const aPriority = Array.isArray(_aPriority) ? _aPriority : [_aPriority];\n const bPriority = Array.isArray(_bPriority) ? _bPriority : [_bPriority];\n\n for (let i = 0; i < aPriority.length; i++) {\n const levelOrder: SortOrder =\n typeof order === 'string' ? order : order[i] ?? 'asc';\n\n const aP = aPriority[i] ?? 0;\n const bP = bPriority[i] ?? 0;\n\n if (aP === bP) {\n continue;\n }\n\n if (bP === Infinity || aP === -Infinity || aP < bP) {\n return levelOrder === 'asc' ? -1 : 1;\n }\n\n if (aP === Infinity || bP === -Infinity || aP > bP) {\n return levelOrder === 'asc' ? 1 : -1;\n }\n }\n\n return 0;\n });\n}\n\nexport function arrayWithPrev<T>(array: T[]): [current: T, prev: T | null][] {\n return array.map((item, i) => [item, array[i - 1] ?? null]);\n}\n\nexport function arrayWithPrevAndIndex<T>(\n array: T[],\n): { item: T; prev: T | null; index: number }[] {\n return array.map((item, i) => ({\n item,\n prev: array[i - 1] ?? null,\n index: i,\n }));\n}\n"],"mappings":";AAkBO,SAAS,aACd,OACA,WACK;AACL,QAAM,SAAc,CAAC;AAErB,MAAI,IAAI;AACR,aAAW,QAAQ,OAAO;AACxB,SAAK;AACL,UAAM,eAAe,UAAU,MAAM,CAAC;AAEtC,QAAI,iBAAiB,OAAO;AAC1B,aAAO,KAAK,YAAY;AAAA,IAC1B;AAAA,EACF;AAEA,SAAO;AACT;AAwBO,SAAS,OACd,KACA,aACA,EAAE,QAAQ,MAAM,IAAyC,CAAC,GAC1D;AACA,SAAO,CAAC,GAAG,GAAG,EAAE,KAAK,CAAC,GAAG,MAAM;AAC7B,UAAM,aAAa,YAAY,CAAC;AAChC,UAAM,aAAa,YAAY,CAAC;AAEhC,UAAM,YAAY,MAAM,QAAQ,UAAU,IAAI,aAAa,CAAC,UAAU;AACtE,UAAM,YAAY,MAAM,QAAQ,UAAU,IAAI,aAAa,CAAC,UAAU;AAEtE,aAAS,IAAI,GAAG,IAAI,UAAU,QAAQ,KAAK;AACzC,YAAM,aACJ,OAAO,UAAU,WAAW,QAAQ,MAAM,CAAC,KAAK;AAElD,YAAM,KAAK,UAAU,CAAC,KAAK;AAC3B,YAAM,KAAK,UAAU,CAAC,KAAK;AAE3B,UAAI,OAAO,IAAI;AACb;AAAA,MACF;AAEA,UAAI,OAAO,YAAY,OAAO,aAAa,KAAK,IAAI;AAClD,eAAO,eAAe,QAAQ,KAAK;AAAA,MACrC;AAEA,UAAI,OAAO,YAAY,OAAO,aAAa,KAAK,IAAI;AAClD,eAAO,eAAe,QAAQ,IAA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