yummies 7.19.4 → 7.20.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/assert.cjs +1 -1
- package/assert.cjs.map +1 -1
- package/assert.js +1 -1
- package/assert.js.map +1 -1
- package/async.cjs +147 -137
- package/async.cjs.map +1 -1
- package/async.d.ts +98 -131
- package/async.js +147 -137
- package/async.js.map +1 -1
- package/common.cjs +3 -1
- package/common.cjs.map +1 -1
- package/common.d.ts +18 -24
- package/common.js +3 -1
- package/common.js.map +1 -1
- package/complex.cjs +1 -0
- package/complex.cjs.map +1 -1
- package/complex.js +1 -0
- package/complex.js.map +1 -1
- package/cookie.cjs +1 -1
- package/cookie.cjs.map +1 -1
- package/cookie.d.ts +0 -18
- package/cookie.js +1 -1
- package/cookie.js.map +1 -1
- package/css-cx.cjs +78 -0
- package/css-cx.cjs.map +1 -0
- package/css-cx.d.ts +69 -0
- package/css-cx.js +74 -0
- package/css-cx.js.map +1 -0
- package/css.cjs +32 -111
- package/css.cjs.map +1 -1
- package/css.d.ts +32 -42
- package/css.js +30 -107
- package/css.js.map +1 -1
- package/data.cjs +60 -50
- package/data.cjs.map +1 -1
- package/data.d.ts +21 -34
- package/data.js +60 -50
- package/data.js.map +1 -1
- package/date-time.cjs +104 -98
- package/date-time.cjs.map +1 -1
- package/date-time.d.ts +51 -57
- package/date-time.js +104 -97
- package/date-time.js.map +1 -1
- package/device.cjs +14 -19
- package/device.cjs.map +1 -1
- package/device.d.ts +15 -26
- package/device.js +14 -19
- package/device.js.map +1 -1
- package/encodings.cjs +1 -1
- package/encodings.cjs.map +1 -1
- package/encodings.d.ts +0 -17
- package/encodings.js +1 -1
- package/encodings.js.map +1 -1
- package/errors.cjs +1 -18
- package/errors.cjs.map +1 -1
- package/errors.d.ts +0 -17
- package/errors.js +1 -17
- package/errors.js.map +1 -1
- package/file.cjs +3 -17
- package/file.cjs.map +1 -1
- package/file.d.ts +1 -16
- package/file.js +3 -17
- package/file.js.map +1 -1
- package/format.cjs +1 -1
- package/format.cjs.map +1 -1
- package/format.js +1 -1
- package/format.js.map +1 -1
- package/html.cjs +112 -175
- package/html.cjs.map +1 -1
- package/html.d.ts +58 -57
- package/html.js +111 -172
- package/html.js.map +1 -1
- package/id-nanoid.cjs +36 -0
- package/id-nanoid.cjs.map +1 -0
- package/id-nanoid.d.ts +26 -0
- package/id-nanoid.js +32 -0
- package/id-nanoid.js.map +1 -0
- package/id.cjs +10 -45
- package/id.cjs.map +1 -1
- package/id.d.ts +27 -36
- package/id.js +6 -40
- package/id.js.map +1 -1
- package/imports.cjs +3 -18
- package/imports.cjs.map +1 -1
- package/imports.d.ts +1 -16
- package/imports.js +3 -17
- package/imports.js.map +1 -1
- package/math.cjs +16 -12
- package/math.cjs.map +1 -1
- package/math.d.ts +9 -24
- package/math.js +16 -12
- package/math.js.map +1 -1
- package/media.cjs +88 -89
- package/media.cjs.map +1 -1
- package/media.d.ts +72 -80
- package/media.js +88 -88
- package/media.js.map +1 -1
- package/mobx.cjs +0 -1
- package/mobx.cjs.map +1 -1
- package/mobx.js.map +1 -1
- package/ms.cjs +3 -17
- package/ms.cjs.map +1 -1
- package/ms.d.ts +1 -16
- package/ms.js +3 -17
- package/ms.js.map +1 -1
- package/number.cjs +1 -17
- package/number.cjs.map +1 -1
- package/number.d.ts +0 -16
- package/number.js +1 -17
- package/number.js.map +1 -1
- package/package.json +6 -6
- package/parser.cjs +1 -1
- package/parser.cjs.map +1 -1
- package/parser.js +1 -1
- package/parser.js.map +1 -1
- package/price.cjs +1 -1
- package/price.cjs.map +1 -1
- package/price.d.ts +0 -16
- package/price.js +1 -1
- package/price.js.map +1 -1
- package/random.cjs +37 -39
- package/random.cjs.map +1 -1
- package/random.d.ts +31 -40
- package/random.js +37 -39
- package/random.js.map +1 -1
- package/react.cjs +0 -1
- package/react.cjs.map +1 -1
- package/react.js.map +1 -1
- package/sanitize-html.cjs +82 -0
- package/sanitize-html.cjs.map +1 -0
- package/sanitize-html.d.ts +28 -0
- package/sanitize-html.js +79 -0
- package/sanitize-html.js.map +1 -0
- package/sound.cjs +1 -17
- package/sound.cjs.map +1 -1
- package/sound.d.ts +0 -16
- package/sound.js +1 -17
- package/sound.js.map +1 -1
- package/storage.cjs +7 -5
- package/storage.cjs.map +1 -1
- package/storage.d.ts +3 -17
- package/storage.js +7 -5
- package/storage.js.map +1 -1
- package/text.cjs +3 -17
- package/text.cjs.map +1 -1
- package/text.d.ts +1 -16
- package/text.js +3 -17
- package/text.js.map +1 -1
- package/type-guard.cjs +38 -1
- package/type-guard.cjs.map +1 -1
- package/type-guard.d.ts +38 -0
- package/type-guard.js +38 -1
- package/type-guard.js.map +1 -1
- package/types.d.ts +10 -1
- package/types.global.d.ts +9 -0
- package/vibrate.cjs +1 -17
- package/vibrate.cjs.map +1 -1
- package/vibrate.d.ts +0 -16
- package/vibrate.js +1 -17
- package/vibrate.js.map +1 -1
- /package/{chunk-pbuEa-1d.js → chunk-D7D4PA-g.js} +0 -0
- /package/{chunk-CZWwpsFl.cjs → chunk-DakpK96I.cjs} +0 -0
package/data.cjs
CHANGED
|
@@ -1,5 +1,42 @@
|
|
|
1
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
-
//#region src/data.ts
|
|
2
|
+
//#region src/data/flat-map-deep.ts
|
|
3
|
+
/**
|
|
4
|
+
* Recursively flattens a nested array and maps the collected values.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* flatMapDeep([1, [2, [3]]], (value) => value * 2); // [2, 4, 6]
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
var flatMapDeep = (arr, fn) => {
|
|
12
|
+
const source = [];
|
|
13
|
+
const collect = (value) => {
|
|
14
|
+
if (!Array.isArray(value)) {
|
|
15
|
+
source.push(value);
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
for (const item of value) collect(item);
|
|
19
|
+
};
|
|
20
|
+
collect(arr);
|
|
21
|
+
return source.map((value, i) => fn(value, i, source));
|
|
22
|
+
};
|
|
23
|
+
//#endregion
|
|
24
|
+
//#region src/data/has-enumerable-keys.ts
|
|
25
|
+
/**
|
|
26
|
+
* Checks whether an object has at least one enumerable key.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```ts
|
|
30
|
+
* hasEnumerableKeys({ id: 1 }); // true
|
|
31
|
+
* hasEnumerableKeys({}); // false
|
|
32
|
+
* ```
|
|
33
|
+
*/
|
|
34
|
+
var hasEnumerableKeys = (input) => {
|
|
35
|
+
for (const _key in input) return true;
|
|
36
|
+
return false;
|
|
37
|
+
};
|
|
38
|
+
//#endregion
|
|
39
|
+
//#region src/data/is-shallow-equal.ts
|
|
3
40
|
/**
|
|
4
41
|
* Performs a shallow comparison for arrays, plain objects, dates and regular expressions.
|
|
5
42
|
*
|
|
@@ -30,50 +67,23 @@ var isShallowEqual = (a, b) => {
|
|
|
30
67
|
for (const key of aKeys) if (!Object.hasOwn(bObj, key) || a[key] !== bObj[key]) return false;
|
|
31
68
|
return true;
|
|
32
69
|
};
|
|
70
|
+
//#endregion
|
|
71
|
+
//#region src/data/is-unsafe-property.ts
|
|
72
|
+
var UNSAFE_PROPERTY_KEYS = new Set([
|
|
73
|
+
"__proto__",
|
|
74
|
+
"prototype",
|
|
75
|
+
"constructor"
|
|
76
|
+
]);
|
|
33
77
|
/**
|
|
34
|
-
* Checks whether
|
|
35
|
-
*
|
|
36
|
-
* @example
|
|
37
|
-
* ```ts
|
|
38
|
-
* hasEnumerableKeys({ id: 1 }); // true
|
|
39
|
-
* hasEnumerableKeys({}); // false
|
|
40
|
-
* ```
|
|
41
|
-
*/
|
|
42
|
-
var hasEnumerableKeys = (input) => {
|
|
43
|
-
for (const _key in input) return true;
|
|
44
|
-
return false;
|
|
45
|
-
};
|
|
46
|
-
/**
|
|
47
|
-
* Wraps a value in an array when it is not already an array.
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* ```ts
|
|
51
|
-
* toArray('item'); // ['item']
|
|
52
|
-
* ```
|
|
53
|
-
*/
|
|
54
|
-
var toArray = (value) => {
|
|
55
|
-
return Array.isArray(value) ? value : [value];
|
|
56
|
-
};
|
|
57
|
-
/**
|
|
58
|
-
* Recursively flattens a nested array and maps the collected values.
|
|
78
|
+
* Checks whether a property key is unsafe and can lead to prototype pollution.
|
|
59
79
|
*
|
|
60
80
|
* @example
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* ```
|
|
81
|
+
* isUnsafeProperty('__proto__'); // true
|
|
82
|
+
* isUnsafeProperty('name'); // false
|
|
64
83
|
*/
|
|
65
|
-
var
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (!Array.isArray(value)) {
|
|
69
|
-
source.push(value);
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
for (const item of value) collect(item);
|
|
73
|
-
};
|
|
74
|
-
collect(arr);
|
|
75
|
-
return source.map((value, i) => fn(value, i, source));
|
|
76
|
-
};
|
|
84
|
+
var isUnsafeProperty = (key) => UNSAFE_PROPERTY_KEYS.has(key);
|
|
85
|
+
//#endregion
|
|
86
|
+
//#region src/data/safe-json-parse.ts
|
|
77
87
|
/**
|
|
78
88
|
* Parses JSON safely and returns a fallback value when parsing fails.
|
|
79
89
|
*
|
|
@@ -90,19 +100,19 @@ var safeJsonParse = (json, fallback = null) => {
|
|
|
90
100
|
return fallback;
|
|
91
101
|
}
|
|
92
102
|
};
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
"prototype",
|
|
96
|
-
"constructor"
|
|
97
|
-
]);
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region src/data/to-array.ts
|
|
98
105
|
/**
|
|
99
|
-
*
|
|
106
|
+
* Wraps a value in an array when it is not already an array.
|
|
100
107
|
*
|
|
101
108
|
* @example
|
|
102
|
-
*
|
|
103
|
-
*
|
|
109
|
+
* ```ts
|
|
110
|
+
* toArray('item'); // ['item']
|
|
111
|
+
* ```
|
|
104
112
|
*/
|
|
105
|
-
var
|
|
113
|
+
var toArray = (value) => {
|
|
114
|
+
return Array.isArray(value) ? value : [value];
|
|
115
|
+
};
|
|
106
116
|
//#endregion
|
|
107
117
|
exports.flatMapDeep = flatMapDeep;
|
|
108
118
|
exports.hasEnumerableKeys = hasEnumerableKeys;
|
package/data.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data.cjs","names":[],"sources":["../src/data.ts"],"sourcesContent":["/**\n *
|
|
1
|
+
{"version":3,"file":"data.cjs","names":[],"sources":["../src/data/flat-map-deep.ts","../src/data/has-enumerable-keys.ts","../src/data/is-shallow-equal.ts","../src/data/is-unsafe-property.ts","../src/data/safe-json-parse.ts","../src/data/to-array.ts"],"sourcesContent":["type DeepArray<TValue> = TValue | Array<DeepArray<TValue>>;\n\n/**\n * Recursively flattens a nested array and maps the collected values.\n *\n * @example\n * ```ts\n * flatMapDeep([1, [2, [3]]], (value) => value * 2); // [2, 4, 6]\n * ```\n */\nexport const flatMapDeep = <TSource, TNewValue>(\n arr: DeepArray<TSource>,\n fn: (value: TSource, i: number, arr: TSource[]) => TNewValue,\n): TNewValue[] => {\n const source: TSource[] = [];\n\n const collect = (value: DeepArray<TSource>): void => {\n if (!Array.isArray(value)) {\n source.push(value);\n return;\n }\n\n for (const item of value) {\n collect(item);\n }\n };\n\n collect(arr);\n\n return source.map((value, i) => fn(value, i, source));\n};\n","import type { AnyObject } from 'yummies/types';\n\n/**\n * Checks whether an object has at least one enumerable key.\n *\n * @example\n * ```ts\n * hasEnumerableKeys({ id: 1 }); // true\n * hasEnumerableKeys({}); // false\n * ```\n */\nexport const hasEnumerableKeys = (input: AnyObject): boolean => {\n for (const _key in input) {\n return true;\n }\n return false;\n};\n","import type { AnyObject } from 'yummies/types';\n\n/**\n * Performs a shallow comparison for arrays, plain objects, dates and regular expressions.\n *\n * @example\n * ```ts\n * isShallowEqual({ id: 1 }, { id: 1 }); // true\n * ```\n */\nexport const isShallowEqual = (a: unknown, b: unknown): boolean => {\n if (a === b) return true;\n\n if (\n typeof a !== 'object' ||\n typeof b !== 'object' ||\n a === null ||\n b === null\n ) {\n return false;\n }\n\n if (a.constructor !== b.constructor) return false;\n\n const isArrayA = Array.isArray(a);\n\n if (isArrayA !== Array.isArray(b)) return false;\n\n if (isArrayA) {\n const arrA = a as unknown[];\n const arrB = b as unknown[];\n if (arrA.length !== arrB.length) return false;\n\n for (const [i, element] of arrA.entries()) {\n if (element !== arrB[i]) return false;\n }\n return true;\n }\n\n if (a instanceof Date) return a.getTime() === (b as Date).getTime();\n\n if (a instanceof RegExp) return a.toString() === (b as RegExp).toString();\n\n const aKeys = Object.keys(a);\n const bKeys = Object.keys(b);\n\n if (aKeys.length !== bKeys.length) return false;\n\n const bObj = b as AnyObject;\n for (const key of aKeys) {\n if (!Object.hasOwn(bObj, key) || (a as AnyObject)[key] !== bObj[key]) {\n return false;\n }\n }\n\n return true;\n};\n","const UNSAFE_PROPERTY_KEYS = new Set(['__proto__', 'prototype', 'constructor']);\n\n/**\n * Checks whether a property key is unsafe and can lead to prototype pollution.\n *\n * @example\n * isUnsafeProperty('__proto__'); // true\n * isUnsafeProperty('name'); // false\n */\nexport const isUnsafeProperty = (key: any) => UNSAFE_PROPERTY_KEYS.has(key);\n","import type { Maybe } from 'yummies/types';\n\n/**\n * Parses JSON safely and returns a fallback value when parsing fails.\n *\n * @example\n * ```ts\n * safeJsonParse('{\"enabled\":true}', {}); // { enabled: true }\n * ```\n */\nexport const safeJsonParse = <TValue = any, TFallback = null>(\n json: Maybe<string>,\n fallback: TFallback = null as TFallback,\n): TValue | TFallback => {\n if (json == null) return fallback;\n\n try {\n return JSON.parse(json);\n } catch {\n return fallback;\n }\n};\n","/**\n * Wraps a value in an array when it is not already an array.\n *\n * @example\n * ```ts\n * toArray('item'); // ['item']\n * ```\n */\nexport const toArray = <TValue>(value: TValue | TValue[]): TValue[] => {\n return Array.isArray(value) ? value : [value];\n};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AAM,EAAgB,SAAS,KAAK,MAC9D,OAAO;CAIX,OAAO;AACT;;;ACxDA,IAAM,uBAAuB,IAAI,IAAI;CAAC;CAAa;CAAa;AAAa,CAAC;;;;;;;;AAS9E,IAAa,oBAAoB,QAAa,qBAAqB,IAAI,GAAG;;;;;;;;;;;ACC1E,IAAa,iBACX,MACA,WAAsB,SACC;CACvB,IAAI,QAAQ,MAAM,OAAO;CAEzB,IAAI;EACF,OAAO,KAAK,MAAM,IAAI;CACxB,QAAQ;EACN,OAAO;CACT;AACF;;;;;;;;;;;ACbA,IAAa,WAAmB,UAAuC;CACrE,OAAO,MAAM,QAAQ,KAAK,IAAI,QAAQ,CAAC,KAAK;AAC9C"}
|
package/data.d.ts
CHANGED
|
@@ -1,32 +1,16 @@
|
|
|
1
1
|
import { AnyObject, Maybe } from 'yummies/types';
|
|
2
2
|
|
|
3
|
+
type DeepArray<TValue> = TValue | Array<DeepArray<TValue>>;
|
|
3
4
|
/**
|
|
4
|
-
*
|
|
5
|
-
* # yummies/data
|
|
6
|
-
*
|
|
7
|
-
* ## Description
|
|
8
|
-
*
|
|
9
|
-
* General-purpose data helpers: shallow equality, normalizing values to arrays, and recursive
|
|
10
|
-
* `flatMap`-style transforms. They complement `Array`/`Object` builtins when you need stable
|
|
11
|
-
* comparisons for memoization or small structural utilities without pulling a large lodash-style
|
|
12
|
-
* dependency into the bundle.
|
|
13
|
-
*
|
|
14
|
-
* ## Usage
|
|
15
|
-
*
|
|
16
|
-
* ```ts
|
|
17
|
-
* import { isShallowEqual, toArray } from "yummies/data";
|
|
18
|
-
* ```
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
|
-
/**
|
|
22
|
-
* Performs a shallow comparison for arrays, plain objects, dates and regular expressions.
|
|
5
|
+
* Recursively flattens a nested array and maps the collected values.
|
|
23
6
|
*
|
|
24
7
|
* @example
|
|
25
8
|
* ```ts
|
|
26
|
-
*
|
|
9
|
+
* flatMapDeep([1, [2, [3]]], (value) => value * 2); // [2, 4, 6]
|
|
27
10
|
* ```
|
|
28
11
|
*/
|
|
29
|
-
declare const
|
|
12
|
+
declare const flatMapDeep: <TSource, TNewValue>(arr: DeepArray<TSource>, fn: (value: TSource, i: number, arr: TSource[]) => TNewValue) => TNewValue[];
|
|
13
|
+
|
|
30
14
|
/**
|
|
31
15
|
* Checks whether an object has at least one enumerable key.
|
|
32
16
|
*
|
|
@@ -37,25 +21,26 @@ declare const isShallowEqual: (a: unknown, b: unknown) => boolean;
|
|
|
37
21
|
* ```
|
|
38
22
|
*/
|
|
39
23
|
declare const hasEnumerableKeys: (input: AnyObject) => boolean;
|
|
24
|
+
|
|
40
25
|
/**
|
|
41
|
-
*
|
|
26
|
+
* Performs a shallow comparison for arrays, plain objects, dates and regular expressions.
|
|
42
27
|
*
|
|
43
28
|
* @example
|
|
44
29
|
* ```ts
|
|
45
|
-
*
|
|
30
|
+
* isShallowEqual({ id: 1 }, { id: 1 }); // true
|
|
46
31
|
* ```
|
|
47
32
|
*/
|
|
48
|
-
declare const
|
|
49
|
-
|
|
33
|
+
declare const isShallowEqual: (a: unknown, b: unknown) => boolean;
|
|
34
|
+
|
|
50
35
|
/**
|
|
51
|
-
*
|
|
36
|
+
* Checks whether a property key is unsafe and can lead to prototype pollution.
|
|
52
37
|
*
|
|
53
38
|
* @example
|
|
54
|
-
*
|
|
55
|
-
*
|
|
56
|
-
* ```
|
|
39
|
+
* isUnsafeProperty('__proto__'); // true
|
|
40
|
+
* isUnsafeProperty('name'); // false
|
|
57
41
|
*/
|
|
58
|
-
declare const
|
|
42
|
+
declare const isUnsafeProperty: (key: any) => boolean;
|
|
43
|
+
|
|
59
44
|
/**
|
|
60
45
|
* Parses JSON safely and returns a fallback value when parsing fails.
|
|
61
46
|
*
|
|
@@ -65,13 +50,15 @@ declare const flatMapDeep: <TSource, TNewValue>(arr: DeepArray<TSource>, fn: (va
|
|
|
65
50
|
* ```
|
|
66
51
|
*/
|
|
67
52
|
declare const safeJsonParse: <TValue = any, TFallback = null>(json: Maybe<string>, fallback?: TFallback) => TValue | TFallback;
|
|
53
|
+
|
|
68
54
|
/**
|
|
69
|
-
*
|
|
55
|
+
* Wraps a value in an array when it is not already an array.
|
|
70
56
|
*
|
|
71
57
|
* @example
|
|
72
|
-
*
|
|
73
|
-
*
|
|
58
|
+
* ```ts
|
|
59
|
+
* toArray('item'); // ['item']
|
|
60
|
+
* ```
|
|
74
61
|
*/
|
|
75
|
-
declare const
|
|
62
|
+
declare const toArray: <TValue>(value: TValue | TValue[]) => TValue[];
|
|
76
63
|
|
|
77
64
|
export { flatMapDeep, hasEnumerableKeys, isShallowEqual, isUnsafeProperty, safeJsonParse, toArray };
|
package/data.js
CHANGED
|
@@ -1,4 +1,41 @@
|
|
|
1
|
-
//#region src/data.ts
|
|
1
|
+
//#region src/data/flat-map-deep.ts
|
|
2
|
+
/**
|
|
3
|
+
* Recursively flattens a nested array and maps the collected values.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* flatMapDeep([1, [2, [3]]], (value) => value * 2); // [2, 4, 6]
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
var flatMapDeep = (arr, fn) => {
|
|
11
|
+
const source = [];
|
|
12
|
+
const collect = (value) => {
|
|
13
|
+
if (!Array.isArray(value)) {
|
|
14
|
+
source.push(value);
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
for (const item of value) collect(item);
|
|
18
|
+
};
|
|
19
|
+
collect(arr);
|
|
20
|
+
return source.map((value, i) => fn(value, i, source));
|
|
21
|
+
};
|
|
22
|
+
//#endregion
|
|
23
|
+
//#region src/data/has-enumerable-keys.ts
|
|
24
|
+
/**
|
|
25
|
+
* Checks whether an object has at least one enumerable key.
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```ts
|
|
29
|
+
* hasEnumerableKeys({ id: 1 }); // true
|
|
30
|
+
* hasEnumerableKeys({}); // false
|
|
31
|
+
* ```
|
|
32
|
+
*/
|
|
33
|
+
var hasEnumerableKeys = (input) => {
|
|
34
|
+
for (const _key in input) return true;
|
|
35
|
+
return false;
|
|
36
|
+
};
|
|
37
|
+
//#endregion
|
|
38
|
+
//#region src/data/is-shallow-equal.ts
|
|
2
39
|
/**
|
|
3
40
|
* Performs a shallow comparison for arrays, plain objects, dates and regular expressions.
|
|
4
41
|
*
|
|
@@ -29,50 +66,23 @@ var isShallowEqual = (a, b) => {
|
|
|
29
66
|
for (const key of aKeys) if (!Object.hasOwn(bObj, key) || a[key] !== bObj[key]) return false;
|
|
30
67
|
return true;
|
|
31
68
|
};
|
|
69
|
+
//#endregion
|
|
70
|
+
//#region src/data/is-unsafe-property.ts
|
|
71
|
+
var UNSAFE_PROPERTY_KEYS = new Set([
|
|
72
|
+
"__proto__",
|
|
73
|
+
"prototype",
|
|
74
|
+
"constructor"
|
|
75
|
+
]);
|
|
32
76
|
/**
|
|
33
|
-
* Checks whether
|
|
34
|
-
*
|
|
35
|
-
* @example
|
|
36
|
-
* ```ts
|
|
37
|
-
* hasEnumerableKeys({ id: 1 }); // true
|
|
38
|
-
* hasEnumerableKeys({}); // false
|
|
39
|
-
* ```
|
|
40
|
-
*/
|
|
41
|
-
var hasEnumerableKeys = (input) => {
|
|
42
|
-
for (const _key in input) return true;
|
|
43
|
-
return false;
|
|
44
|
-
};
|
|
45
|
-
/**
|
|
46
|
-
* Wraps a value in an array when it is not already an array.
|
|
47
|
-
*
|
|
48
|
-
* @example
|
|
49
|
-
* ```ts
|
|
50
|
-
* toArray('item'); // ['item']
|
|
51
|
-
* ```
|
|
52
|
-
*/
|
|
53
|
-
var toArray = (value) => {
|
|
54
|
-
return Array.isArray(value) ? value : [value];
|
|
55
|
-
};
|
|
56
|
-
/**
|
|
57
|
-
* Recursively flattens a nested array and maps the collected values.
|
|
77
|
+
* Checks whether a property key is unsafe and can lead to prototype pollution.
|
|
58
78
|
*
|
|
59
79
|
* @example
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* ```
|
|
80
|
+
* isUnsafeProperty('__proto__'); // true
|
|
81
|
+
* isUnsafeProperty('name'); // false
|
|
63
82
|
*/
|
|
64
|
-
var
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (!Array.isArray(value)) {
|
|
68
|
-
source.push(value);
|
|
69
|
-
return;
|
|
70
|
-
}
|
|
71
|
-
for (const item of value) collect(item);
|
|
72
|
-
};
|
|
73
|
-
collect(arr);
|
|
74
|
-
return source.map((value, i) => fn(value, i, source));
|
|
75
|
-
};
|
|
83
|
+
var isUnsafeProperty = (key) => UNSAFE_PROPERTY_KEYS.has(key);
|
|
84
|
+
//#endregion
|
|
85
|
+
//#region src/data/safe-json-parse.ts
|
|
76
86
|
/**
|
|
77
87
|
* Parses JSON safely and returns a fallback value when parsing fails.
|
|
78
88
|
*
|
|
@@ -89,19 +99,19 @@ var safeJsonParse = (json, fallback = null) => {
|
|
|
89
99
|
return fallback;
|
|
90
100
|
}
|
|
91
101
|
};
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
"prototype",
|
|
95
|
-
"constructor"
|
|
96
|
-
]);
|
|
102
|
+
//#endregion
|
|
103
|
+
//#region src/data/to-array.ts
|
|
97
104
|
/**
|
|
98
|
-
*
|
|
105
|
+
* Wraps a value in an array when it is not already an array.
|
|
99
106
|
*
|
|
100
107
|
* @example
|
|
101
|
-
*
|
|
102
|
-
*
|
|
108
|
+
* ```ts
|
|
109
|
+
* toArray('item'); // ['item']
|
|
110
|
+
* ```
|
|
103
111
|
*/
|
|
104
|
-
var
|
|
112
|
+
var toArray = (value) => {
|
|
113
|
+
return Array.isArray(value) ? value : [value];
|
|
114
|
+
};
|
|
105
115
|
//#endregion
|
|
106
116
|
export { flatMapDeep, hasEnumerableKeys, isShallowEqual, isUnsafeProperty, safeJsonParse, toArray };
|
|
107
117
|
|
package/data.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"data.js","names":[],"sources":["../src/data.ts"],"sourcesContent":["/**\n *
|
|
1
|
+
{"version":3,"file":"data.js","names":[],"sources":["../src/data/flat-map-deep.ts","../src/data/has-enumerable-keys.ts","../src/data/is-shallow-equal.ts","../src/data/is-unsafe-property.ts","../src/data/safe-json-parse.ts","../src/data/to-array.ts"],"sourcesContent":["type DeepArray<TValue> = TValue | Array<DeepArray<TValue>>;\n\n/**\n * Recursively flattens a nested array and maps the collected values.\n *\n * @example\n * ```ts\n * flatMapDeep([1, [2, [3]]], (value) => value * 2); // [2, 4, 6]\n * ```\n */\nexport const flatMapDeep = <TSource, TNewValue>(\n arr: DeepArray<TSource>,\n fn: (value: TSource, i: number, arr: TSource[]) => TNewValue,\n): TNewValue[] => {\n const source: TSource[] = [];\n\n const collect = (value: DeepArray<TSource>): void => {\n if (!Array.isArray(value)) {\n source.push(value);\n return;\n }\n\n for (const item of value) {\n collect(item);\n }\n };\n\n collect(arr);\n\n return source.map((value, i) => fn(value, i, source));\n};\n","import type { AnyObject } from 'yummies/types';\n\n/**\n * Checks whether an object has at least one enumerable key.\n *\n * @example\n * ```ts\n * hasEnumerableKeys({ id: 1 }); // true\n * hasEnumerableKeys({}); // false\n * ```\n */\nexport const hasEnumerableKeys = (input: AnyObject): boolean => {\n for (const _key in input) {\n return true;\n }\n return false;\n};\n","import type { AnyObject } from 'yummies/types';\n\n/**\n * Performs a shallow comparison for arrays, plain objects, dates and regular expressions.\n *\n * @example\n * ```ts\n * isShallowEqual({ id: 1 }, { id: 1 }); // true\n * ```\n */\nexport const isShallowEqual = (a: unknown, b: unknown): boolean => {\n if (a === b) return true;\n\n if (\n typeof a !== 'object' ||\n typeof b !== 'object' ||\n a === null ||\n b === null\n ) {\n return false;\n }\n\n if (a.constructor !== b.constructor) return false;\n\n const isArrayA = Array.isArray(a);\n\n if (isArrayA !== Array.isArray(b)) return false;\n\n if (isArrayA) {\n const arrA = a as unknown[];\n const arrB = b as unknown[];\n if (arrA.length !== arrB.length) return false;\n\n for (const [i, element] of arrA.entries()) {\n if (element !== arrB[i]) return false;\n }\n return true;\n }\n\n if (a instanceof Date) return a.getTime() === (b as Date).getTime();\n\n if (a instanceof RegExp) return a.toString() === (b as RegExp).toString();\n\n const aKeys = Object.keys(a);\n const bKeys = Object.keys(b);\n\n if (aKeys.length !== bKeys.length) return false;\n\n const bObj = b as AnyObject;\n for (const key of aKeys) {\n if (!Object.hasOwn(bObj, key) || (a as AnyObject)[key] !== bObj[key]) {\n return false;\n }\n }\n\n return true;\n};\n","const UNSAFE_PROPERTY_KEYS = new Set(['__proto__', 'prototype', 'constructor']);\n\n/**\n * Checks whether a property key is unsafe and can lead to prototype pollution.\n *\n * @example\n * isUnsafeProperty('__proto__'); // true\n * isUnsafeProperty('name'); // false\n */\nexport const isUnsafeProperty = (key: any) => UNSAFE_PROPERTY_KEYS.has(key);\n","import type { Maybe } from 'yummies/types';\n\n/**\n * Parses JSON safely and returns a fallback value when parsing fails.\n *\n * @example\n * ```ts\n * safeJsonParse('{\"enabled\":true}', {}); // { enabled: true }\n * ```\n */\nexport const safeJsonParse = <TValue = any, TFallback = null>(\n json: Maybe<string>,\n fallback: TFallback = null as TFallback,\n): TValue | TFallback => {\n if (json == null) return fallback;\n\n try {\n return JSON.parse(json);\n } catch {\n return fallback;\n }\n};\n","/**\n * Wraps a value in an array when it is not already an array.\n *\n * @example\n * ```ts\n * toArray('item'); // ['item']\n * ```\n */\nexport const toArray = <TValue>(value: TValue | TValue[]): TValue[] => {\n return Array.isArray(value) ? value : [value];\n};\n"],"mappings":";;;;;;;;;AAUA,IAAa,eACX,KACA,OACgB;CAChB,MAAM,SAAoB,CAAC;CAE3B,MAAM,WAAW,UAAoC;EACnD,IAAI,CAAC,MAAM,QAAQ,KAAK,GAAG;GACzB,OAAO,KAAK,KAAK;GACjB;EACF;EAEA,KAAK,MAAM,QAAQ,OACjB,QAAQ,IAAI;CAEhB;CAEA,QAAQ,GAAG;CAEX,OAAO,OAAO,KAAK,OAAO,MAAM,GAAG,OAAO,GAAG,MAAM,CAAC;AAC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