@cargo-ai/utils 1.0.4
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/build/array.js +52 -0
- package/build/collection.js +155 -0
- package/build/components.js +56 -0
- package/build/cron.js +19 -0
- package/build/delay.js +52 -0
- package/build/hooks/index.js +35 -0
- package/build/hooks/useClickOutside.js +46 -0
- package/build/hooks/useCountAnimation.js +34 -0
- package/build/hooks/useDeviceType.js +54 -0
- package/build/hooks/useEventListener.js +27 -0
- package/build/hooks/useHeight.js +26 -0
- package/build/hooks/usePictureInPicture.js +126 -0
- package/build/hooks/useResizeObserver.js +35 -0
- package/build/hooks/useScrollPosition.js +22 -0
- package/build/hooks/useStepper.js +138 -0
- package/build/hooks/useStorageState.js +110 -0
- package/build/hooks/useVideoPlayer/VideoManager.js +78 -0
- package/build/hooks/useVideoPlayer/index.js +5 -0
- package/build/hooks/useVideoPlayer/useVideoPlayer.js +100 -0
- package/build/hooks/useWillUnmountEffect.js +13 -0
- package/build/index.js +28 -0
- package/build/json.js +81 -0
- package/build/random.js +7 -0
- package/build/src/array.d.ts +16 -0
- package/build/src/array.d.ts.map +1 -0
- package/build/src/array.js +81 -0
- package/build/src/base64.d.ts +3 -0
- package/build/src/base64.d.ts.map +1 -0
- package/build/src/base64.js +6 -0
- package/build/src/bytes.d.ts +7 -0
- package/build/src/bytes.d.ts.map +1 -0
- package/build/src/bytes.js +71 -0
- package/build/src/collection/deepMergeObjects.d.ts +2 -0
- package/build/src/collection/deepMergeObjects.d.ts.map +1 -0
- package/build/src/collection/deepMergeObjects.js +30 -0
- package/build/src/collection/index.d.ts +2 -0
- package/build/src/collection/index.d.ts.map +1 -0
- package/build/src/collection/index.js +1 -0
- package/build/src/collection.d.ts +22 -0
- package/build/src/collection.d.ts.map +1 -0
- package/build/src/collection.js +210 -0
- package/build/src/colors.d.ts +2 -0
- package/build/src/colors.d.ts.map +1 -0
- package/build/src/colors.js +14 -0
- package/build/src/components.d.ts +4 -0
- package/build/src/components.js +45 -0
- package/build/src/credits.d.ts +7 -0
- package/build/src/credits.d.ts.map +1 -0
- package/build/src/credits.js +88 -0
- package/build/src/cron.d.ts +3 -0
- package/build/src/cron.d.ts.map +1 -0
- package/build/src/cron.js +16 -0
- package/build/src/date/getContextTimezone.d.ts +2 -0
- package/build/src/date/getContextTimezone.d.ts.map +1 -0
- package/build/src/date/getContextTimezone.js +3 -0
- package/build/src/date/getDate.d.ts +8 -0
- package/build/src/date/getDate.d.ts.map +1 -0
- package/build/src/date/getDate.js +25 -0
- package/build/src/date/getDateString.d.ts +8 -0
- package/build/src/date/getDateString.d.ts.map +1 -0
- package/build/src/date/getDateString.js +13 -0
- package/build/src/date/getDatesDifferenceString.d.ts +2 -0
- package/build/src/date/getDatesDifferenceString.d.ts.map +1 -0
- package/build/src/date/getDatesDifferenceString.js +17 -0
- package/build/src/date/getTimeFromNowString.d.ts +2 -0
- package/build/src/date/getTimeFromNowString.d.ts.map +1 -0
- package/build/src/date/getTimeFromNowString.js +8 -0
- package/build/src/date/getTimezoneHoursOffset.d.ts +2 -0
- package/build/src/date/getTimezoneHoursOffset.d.ts.map +1 -0
- package/build/src/date/getTimezoneHoursOffset.js +7 -0
- package/build/src/date/getTimezoneOffsetString.d.ts +2 -0
- package/build/src/date/getTimezoneOffsetString.d.ts.map +1 -0
- package/build/src/date/getTimezoneOffsetString.js +8 -0
- package/build/src/date/index.d.ts +8 -0
- package/build/src/date/index.d.ts.map +1 -0
- package/build/src/date/index.js +7 -0
- package/build/src/date.d.ts +13 -0
- package/build/src/date.d.ts.map +1 -0
- package/build/src/date.js +14 -0
- package/build/src/delay.d.ts +7 -0
- package/build/src/delay.d.ts.map +1 -0
- package/build/src/delay.js +19 -0
- package/build/src/expression/getExpressionNodesReferences.d.ts +17 -0
- package/build/src/expression/getExpressionNodesReferences.d.ts.map +1 -0
- package/build/src/expression/getExpressionNodesReferences.js +71 -0
- package/build/src/expression/getObjectAccessKeysBeforeCursor.d.ts +17 -0
- package/build/src/expression/getObjectAccessKeysBeforeCursor.d.ts.map +1 -0
- package/build/src/expression/getObjectAccessKeysBeforeCursor.js +97 -0
- package/build/src/expression/index.d.ts +3 -0
- package/build/src/expression/index.d.ts.map +1 -0
- package/build/src/expression/index.js +2 -0
- package/build/src/expression/jsonSchema.d.ts +4 -0
- package/build/src/expression/jsonSchema.d.ts.map +1 -0
- package/build/src/expression/jsonSchema.js +85 -0
- package/build/src/expression/matchIsExpression.d.ts +2 -0
- package/build/src/expression/matchIsExpression.d.ts.map +1 -0
- package/build/src/expression/matchIsExpression.js +10 -0
- package/build/src/expression/matchIsExpressionObject.d.ts +2 -0
- package/build/src/expression/matchIsExpressionObject.d.ts.map +1 -0
- package/build/src/expression/matchIsExpressionObject.js +10 -0
- package/build/src/expression/matchIsExpressionString.d.ts +2 -0
- package/build/src/expression/matchIsExpressionString.d.ts.map +1 -0
- package/build/src/expression/matchIsExpressionString.js +4 -0
- package/build/src/gclid.d.ts +4 -0
- package/build/src/gclid.d.ts.map +1 -0
- package/build/src/gclid.js +30 -0
- package/build/src/hooks/index.d.ts +12 -0
- package/build/src/hooks/index.js +12 -0
- package/build/src/hooks/useClickOutside.d.ts +4 -0
- package/build/src/hooks/useClickOutside.js +42 -0
- package/build/src/hooks/useCountAnimation.d.ts +1 -0
- package/build/src/hooks/useCountAnimation.js +30 -0
- package/build/src/hooks/useDeviceType.d.ts +2 -0
- package/build/src/hooks/useDeviceType.js +50 -0
- package/build/src/hooks/useEventListener.d.ts +6 -0
- package/build/src/hooks/useEventListener.js +23 -0
- package/build/src/hooks/useHeight.d.ts +2 -0
- package/build/src/hooks/useHeight.js +20 -0
- package/build/src/hooks/usePictureInPicture.d.ts +9 -0
- package/build/src/hooks/usePictureInPicture.js +60 -0
- package/build/src/hooks/useResizeObserver.d.ts +4 -0
- package/build/src/hooks/useResizeObserver.js +31 -0
- package/build/src/hooks/useScrollPosition.d.ts +5 -0
- package/build/src/hooks/useScrollPosition.js +18 -0
- package/build/src/hooks/useStepper.d.ts +26 -0
- package/build/src/hooks/useStepper.js +136 -0
- package/build/src/hooks/useStorageState.d.ts +12 -0
- package/build/src/hooks/useStorageState.js +53 -0
- package/build/src/hooks/useVideoPlayer/VideoManager.d.ts +18 -0
- package/build/src/hooks/useVideoPlayer/VideoManager.js +72 -0
- package/build/src/hooks/useVideoPlayer/index.d.ts +1 -0
- package/build/src/hooks/useVideoPlayer/index.js +1 -0
- package/build/src/hooks/useVideoPlayer/useVideoPlayer.d.ts +15 -0
- package/build/src/hooks/useVideoPlayer/useVideoPlayer.js +96 -0
- package/build/src/hooks/useWillUnmountEffect.d.ts +1 -0
- package/build/src/hooks/useWillUnmountEffect.js +8 -0
- package/build/src/index.d.ts +27 -0
- package/build/src/index.d.ts.map +1 -0
- package/build/src/index.js +26 -0
- package/build/src/integration.d.ts +14 -0
- package/build/src/integration.d.ts.map +1 -0
- package/build/src/integration.js +72 -0
- package/build/src/json/castToJsonType.d.ts +5 -0
- package/build/src/json/castToJsonType.d.ts.map +1 -0
- package/build/src/json/castToJsonType.js +58 -0
- package/build/src/json/cleanJson.d.ts +3 -0
- package/build/src/json/cleanJson.d.ts.map +1 -0
- package/build/src/json/cleanJson.js +18 -0
- package/build/src/json/constants.d.ts +3 -0
- package/build/src/json/constants.d.ts.map +1 -0
- package/build/src/json/constants.js +9 -0
- package/build/src/json/getJsonType.d.ts +3 -0
- package/build/src/json/getJsonType.d.ts.map +1 -0
- package/build/src/json/getJsonType.js +24 -0
- package/build/src/json/index.d.ts +8 -0
- package/build/src/json/index.d.ts.map +1 -0
- package/build/src/json/index.js +7 -0
- package/build/src/json/isJson.d.ts +2 -0
- package/build/src/json/isJson.d.ts.map +1 -0
- package/build/src/json/isJson.js +9 -0
- package/build/src/json/isJsonSchemaEmpty.d.ts +3 -0
- package/build/src/json/isJsonSchemaEmpty.d.ts.map +1 -0
- package/build/src/json/isJsonSchemaEmpty.js +7 -0
- package/build/src/json/stringifyJson.d.ts +2 -0
- package/build/src/json/stringifyJson.d.ts.map +1 -0
- package/build/src/json/stringifyJson.js +6 -0
- package/build/src/json/types.d.ts +2 -0
- package/build/src/json/types.d.ts.map +1 -0
- package/build/src/json/types.js +1 -0
- package/build/src/json.d.ts +13 -0
- package/build/src/json.d.ts.map +1 -0
- package/build/src/json.js +107 -0
- package/build/src/jsonSchema/index.d.ts +10 -0
- package/build/src/jsonSchema/index.d.ts.map +1 -0
- package/build/src/jsonSchema/index.js +198 -0
- package/build/src/jsonSchema.d.ts +5 -0
- package/build/src/jsonSchema.d.ts.map +1 -0
- package/build/src/jsonSchema.js +73 -0
- package/build/src/number/formatCredits.d.ts +2 -0
- package/build/src/number/formatCredits.d.ts.map +1 -0
- package/build/src/number/formatCredits.js +7 -0
- package/build/src/number/formatPrice.d.ts +2 -0
- package/build/src/number/formatPrice.d.ts.map +1 -0
- package/build/src/number/formatPrice.js +8 -0
- package/build/src/number/getNumberWithMaxDecimals.d.ts +2 -0
- package/build/src/number/getNumberWithMaxDecimals.d.ts.map +1 -0
- package/build/src/number/getNumberWithMaxDecimals.js +3 -0
- package/build/src/number/index.d.ts +4 -0
- package/build/src/number/index.d.ts.map +1 -0
- package/build/src/number/index.js +3 -0
- package/build/src/poll.d.ts +5 -0
- package/build/src/poll.d.ts.map +1 -0
- package/build/src/poll.js +30 -0
- package/build/src/random.d.ts +2 -0
- package/build/src/random.d.ts.map +1 -0
- package/build/src/random.js +3 -0
- package/build/src/schema.d.ts +24 -0
- package/build/src/schema.d.ts.map +1 -0
- package/build/src/schema.js +315 -0
- package/build/src/searchInTree.d.ts +46 -0
- package/build/src/searchInTree.d.ts.map +1 -0
- package/build/src/searchInTree.js +67 -0
- package/build/src/storage.d.ts +13 -0
- package/build/src/storage.d.ts.map +1 -0
- package/build/src/storage.js +169 -0
- package/build/src/string/capitalizeFirstLetter.d.ts +2 -0
- package/build/src/string/capitalizeFirstLetter.d.ts.map +1 -0
- package/build/src/string/capitalizeFirstLetter.js +3 -0
- package/build/src/string/findAllIndexOf.d.ts +2 -0
- package/build/src/string/findAllIndexOf.d.ts.map +1 -0
- package/build/src/string/findAllIndexOf.js +13 -0
- package/build/src/string/getHash.d.ts +2 -0
- package/build/src/string/getHash.d.ts.map +1 -0
- package/build/src/string/getHash.js +9 -0
- package/build/src/string/index.d.ts +5 -0
- package/build/src/string/index.d.ts.map +1 -0
- package/build/src/string/index.js +4 -0
- package/build/src/string/sentenceCase.d.ts +2 -0
- package/build/src/string/sentenceCase.d.ts.map +1 -0
- package/build/src/string/sentenceCase.js +38 -0
- package/build/src/string.d.ts +2 -0
- package/build/src/string.d.ts.map +1 -0
- package/build/src/string.js +3 -0
- package/build/src/type.d.ts +10 -0
- package/build/src/type.js +1 -0
- package/build/src/types.d.ts +22 -0
- package/build/src/types.d.ts.map +1 -0
- package/build/src/types.js +1 -0
- package/build/src/user.d.ts +3 -0
- package/build/src/user.d.ts.map +1 -0
- package/build/src/user.js +7 -0
- package/build/src/validation/index.d.ts +2 -0
- package/build/src/validation/index.d.ts.map +1 -0
- package/build/src/validation/index.js +1 -0
- package/build/src/validation/matchIsObjectLike.d.ts +2 -0
- package/build/src/validation/matchIsObjectLike.d.ts.map +1 -0
- package/build/src/validation/matchIsObjectLike.js +4 -0
- package/build/src/vm.d.ts +1 -0
- package/build/src/vm.js +32 -0
- package/build/src/zod/index.d.ts +14 -0
- package/build/src/zod/index.d.ts.map +1 -0
- package/build/src/zod/index.js +210 -0
- package/build/src/zod.d.ts +8 -0
- package/build/src/zod.d.ts.map +1 -0
- package/build/src/zod.js +89 -0
- package/build/storage.js +285 -0
- package/build/string.js +17 -0
- package/build/tsconfig.tsbuildinfo +1 -0
- package/build/type.js +2 -0
- package/build/user.js +12 -0
- package/package.json +50 -0
package/build/json.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isJsonSchemaEmpty = exports.castToJsonType = exports.getJsonType = exports.isJson = exports.jsonTypes = void 0;
|
|
4
|
+
var collection_js_1 = require("./collection.js");
|
|
5
|
+
exports.jsonTypes = [
|
|
6
|
+
"string",
|
|
7
|
+
"number",
|
|
8
|
+
"object",
|
|
9
|
+
"array",
|
|
10
|
+
"date",
|
|
11
|
+
"boolean",
|
|
12
|
+
"any",
|
|
13
|
+
];
|
|
14
|
+
var isJson = function (value) {
|
|
15
|
+
try {
|
|
16
|
+
JSON.parse(value);
|
|
17
|
+
return true;
|
|
18
|
+
}
|
|
19
|
+
catch (_a) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
exports.isJson = isJson;
|
|
24
|
+
var getJsonType = function (value) {
|
|
25
|
+
if (value === null || value === undefined) {
|
|
26
|
+
return "any";
|
|
27
|
+
}
|
|
28
|
+
if (Array.isArray(value)) {
|
|
29
|
+
return "array";
|
|
30
|
+
}
|
|
31
|
+
if (value instanceof Date) {
|
|
32
|
+
return "date";
|
|
33
|
+
}
|
|
34
|
+
if (typeof value === "object") {
|
|
35
|
+
return "object";
|
|
36
|
+
}
|
|
37
|
+
if (value === true || value === false) {
|
|
38
|
+
return "boolean";
|
|
39
|
+
}
|
|
40
|
+
if (typeof value === "number") {
|
|
41
|
+
return "number";
|
|
42
|
+
}
|
|
43
|
+
if (typeof value === "string") {
|
|
44
|
+
return "string";
|
|
45
|
+
}
|
|
46
|
+
return "any";
|
|
47
|
+
};
|
|
48
|
+
exports.getJsonType = getJsonType;
|
|
49
|
+
var castToJsonType = function (value, type) {
|
|
50
|
+
try {
|
|
51
|
+
if (type === "array") {
|
|
52
|
+
return Array.isArray(value) ? value : null;
|
|
53
|
+
}
|
|
54
|
+
if (type === "string") {
|
|
55
|
+
return String(value);
|
|
56
|
+
}
|
|
57
|
+
if (type === "boolean") {
|
|
58
|
+
return Boolean(value);
|
|
59
|
+
}
|
|
60
|
+
if (type === "number") {
|
|
61
|
+
var number = Number(value);
|
|
62
|
+
if (isNaN(number)) {
|
|
63
|
+
return undefined;
|
|
64
|
+
}
|
|
65
|
+
return number;
|
|
66
|
+
}
|
|
67
|
+
if (type === "date") {
|
|
68
|
+
return new Date(value);
|
|
69
|
+
}
|
|
70
|
+
return value;
|
|
71
|
+
}
|
|
72
|
+
catch (_a) {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
exports.castToJsonType = castToJsonType;
|
|
77
|
+
var isJsonSchemaEmpty = function (jsonSchema) {
|
|
78
|
+
return jsonSchema.type === "object" &&
|
|
79
|
+
(!jsonSchema.properties || (0, collection_js_1.getKeys)(jsonSchema.properties).length === 0);
|
|
80
|
+
};
|
|
81
|
+
exports.isJsonSchemaEmpty = isJsonSchemaEmpty;
|
package/build/random.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
export declare const chunk: <T>(arr: T[], size: number) => T[][];
|
|
2
|
+
export declare const unique: <T>(arr: T[]) => T[];
|
|
3
|
+
export declare const uniqueByProperty: <T>(array: T[], getProperty: (item: T) => unknown, options?: {
|
|
4
|
+
isReverse?: boolean;
|
|
5
|
+
}) => T[];
|
|
6
|
+
export declare const uniqueByKey: <T extends Record<string, unknown>>(arr: T[], key: keyof T) => T[];
|
|
7
|
+
export declare const isUnique: <T>(arr: T[]) => boolean;
|
|
8
|
+
export declare const difference: <T>(a: T[], b: T[]) => T[];
|
|
9
|
+
export declare const shuffle: <T>(array: T[]) => T[];
|
|
10
|
+
export declare const move: <T>(array: T[], fromIndex: number, toIndex: number) => T[];
|
|
11
|
+
export declare const retrievevedMovedItem: <T>(a: T[], b: T[]) => {
|
|
12
|
+
sourceIndex: number;
|
|
13
|
+
destinationIndex: number;
|
|
14
|
+
} | undefined;
|
|
15
|
+
export declare const pickRandom: <T>(array: T[], amount?: number) => T[];
|
|
16
|
+
//# sourceMappingURL=array.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"array.d.ts","sourceRoot":"","sources":["../../src/array.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,KAAK,sBAAuB,MAAM,UAI9C,CAAC;AAEF,eAAO,MAAM,MAAM,sBAElB,CAAC;AAEF,eAAO,MAAM,gBAAgB,4CAED,OAAO,YACxB;IAAE,SAAS,CAAC,EAAE,OAAO,CAAA;CAAE,QAqBjC,CAAC;AAEF,eAAO,MAAM,WAAW,oEAKvB,CAAC;AAEF,eAAO,MAAM,QAAQ,mBAAkB,OAEtC,CAAC;AAEF,eAAO,MAAM,UAAU,4BAItB,CAAC;AAEF,eAAO,MAAM,OAAO,wBAEnB,CAAC;AAEF,eAAO,MAAM,IAAI,6BAEJ,MAAM,WACR,MAAM,QAkBhB,CAAC;AAEF,eAAO,MAAM,oBAAoB;iBAGf,MAAM;sBAAoB,MAAM;aAmBjD,CAAC;AAEF,eAAO,MAAM,UAAU,2BAA2B,MAAM,QAqBvD,CAAC"}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
export const chunk = (arr, size) => {
|
|
2
|
+
return Array.from({ length: Math.ceil(arr.length / size) }, (_, i) => arr.slice(i * size, i * size + size));
|
|
3
|
+
};
|
|
4
|
+
export const unique = (arr) => {
|
|
5
|
+
return [...new Set(arr)];
|
|
6
|
+
};
|
|
7
|
+
export const uniqueByProperty = (array, getProperty, options = {}) => {
|
|
8
|
+
const { isReverse = false } = options;
|
|
9
|
+
const arrayCopy = isReverse === true ? [...array].reverse() : array;
|
|
10
|
+
const uniqueArray = [];
|
|
11
|
+
arrayCopy.forEach((item) => {
|
|
12
|
+
const itemProperty = getProperty(item);
|
|
13
|
+
const isUnique = uniqueArray.some((otherItem) => {
|
|
14
|
+
return getProperty(otherItem) === itemProperty;
|
|
15
|
+
}) === false;
|
|
16
|
+
if (isUnique === true) {
|
|
17
|
+
uniqueArray.push(item);
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
return uniqueArray;
|
|
21
|
+
};
|
|
22
|
+
export const uniqueByKey = (arr, key) => {
|
|
23
|
+
return Array.from(new Map(arr.map((item) => [item[key], item])).values());
|
|
24
|
+
};
|
|
25
|
+
export const isUnique = (arr) => {
|
|
26
|
+
return arr.length === new Set(arr).size;
|
|
27
|
+
};
|
|
28
|
+
export const difference = (a, b) => {
|
|
29
|
+
return a.filter((element) => {
|
|
30
|
+
return !b.includes(element);
|
|
31
|
+
});
|
|
32
|
+
};
|
|
33
|
+
export const shuffle = (array) => {
|
|
34
|
+
return array.sort(() => 0.5 - Math.random());
|
|
35
|
+
};
|
|
36
|
+
export const move = (array, fromIndex, toIndex) => {
|
|
37
|
+
const newArray = [...array];
|
|
38
|
+
const startIndex = fromIndex < 0 ? newArray.length + fromIndex : fromIndex;
|
|
39
|
+
if (startIndex >= 0 && startIndex < newArray.length) {
|
|
40
|
+
const endIndex = toIndex < 0 ? newArray.length + toIndex : toIndex;
|
|
41
|
+
const [item] = newArray.splice(fromIndex, 1);
|
|
42
|
+
if (item === undefined) {
|
|
43
|
+
return newArray;
|
|
44
|
+
}
|
|
45
|
+
newArray.splice(endIndex, 0, item);
|
|
46
|
+
}
|
|
47
|
+
return newArray;
|
|
48
|
+
};
|
|
49
|
+
export const retrievevedMovedItem = (a, b) => {
|
|
50
|
+
const sourceIndex = a.findIndex((item, index) => {
|
|
51
|
+
return item !== b[index] && item !== b[index + 1];
|
|
52
|
+
});
|
|
53
|
+
if (sourceIndex === -1) {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
const sourceItem = a[sourceIndex];
|
|
57
|
+
const destinationIndex = b.findIndex((item) => {
|
|
58
|
+
return item === sourceItem;
|
|
59
|
+
});
|
|
60
|
+
if (destinationIndex === -1) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
return { sourceIndex, destinationIndex };
|
|
64
|
+
};
|
|
65
|
+
export const pickRandom = (array, amount = 1) => {
|
|
66
|
+
if (amount <= 0) {
|
|
67
|
+
return [];
|
|
68
|
+
}
|
|
69
|
+
if (amount >= array.length) {
|
|
70
|
+
return [...array];
|
|
71
|
+
}
|
|
72
|
+
const result = [];
|
|
73
|
+
const availableIndices = Array.from({ length: array.length }, (_, i) => i);
|
|
74
|
+
for (let i = 0; i < amount; i++) {
|
|
75
|
+
const randomIndex = Math.floor(Math.random() * availableIndices.length);
|
|
76
|
+
const selectedIndex = availableIndices[randomIndex];
|
|
77
|
+
result.push(array[selectedIndex]);
|
|
78
|
+
availableIndices.splice(randomIndex, 1);
|
|
79
|
+
}
|
|
80
|
+
return result;
|
|
81
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base64.d.ts","sourceRoot":"","sources":["../../src/base64.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAE5C,CAAC;AAEF,eAAO,MAAM,YAAY,UAAW,MAAM,KAAG,MAE5C,CAAC"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export declare const getByteLength: (obj: unknown) => number | undefined;
|
|
2
|
+
export declare const byteChunk: <T>(arr: T[], maxByteLength: number) => T[][];
|
|
3
|
+
export declare const byteObjectChunk: (obj: Record<string, unknown>, maxByteLength: number) => Record<string, unknown>[];
|
|
4
|
+
export declare const getBytesUnit: (bytes: number) => "Bytes" | "KB" | "MB" | "GB" | "TB";
|
|
5
|
+
export declare const getBytesValue: (bytes: number, decimals?: number) => number;
|
|
6
|
+
export declare const getBytesWithUnitString: (bytes: number, decimals?: number) => string;
|
|
7
|
+
//# sourceMappingURL=bytes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../../src/bytes.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,aAAa,QAAS,OAAO,uBAMzC,CAAC;AAEF,eAAO,MAAM,SAAS,+BAAgC,MAAM,UAmB3D,CAAC;AAEF,eAAO,MAAM,eAAe,QACrB,OAAO,MAAM,EAAE,OAAO,CAAC,iBACb,MAAM,KACpB,OAAO,MAAM,EAAE,OAAO,CAAC,EAyBzB,CAAC;AAEF,eAAO,MAAM,YAAY,UAChB,MAAM,KACZ,OAAO,GAAG,IAAI,GAAG,IAAI,GAAG,IAAI,GAAG,IAiBjC,CAAC;AAEF,eAAO,MAAM,aAAa,UAAW,MAAM,wBAAiB,MAS3D,CAAC;AAEF,eAAO,MAAM,sBAAsB,UAAW,MAAM,wBAAiB,MAEpE,CAAC"}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { fromEntries, getKeys } from "./collection.js";
|
|
2
|
+
export const getByteLength = (obj) => {
|
|
3
|
+
try {
|
|
4
|
+
return Buffer.byteLength(JSON.stringify(obj));
|
|
5
|
+
}
|
|
6
|
+
catch {
|
|
7
|
+
return undefined;
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
export const byteChunk = (arr, maxByteLength) => {
|
|
11
|
+
if (arr.length === 1) {
|
|
12
|
+
return [arr];
|
|
13
|
+
}
|
|
14
|
+
const byteLength = getByteLength(arr);
|
|
15
|
+
if (byteLength === undefined || byteLength > maxByteLength) {
|
|
16
|
+
const divider = Math.floor(arr.length / 2);
|
|
17
|
+
const arr1 = arr.slice(0, divider);
|
|
18
|
+
const arr2 = arr.slice(divider);
|
|
19
|
+
return [
|
|
20
|
+
byteChunk(arr1, maxByteLength),
|
|
21
|
+
byteChunk(arr2, maxByteLength),
|
|
22
|
+
].flat();
|
|
23
|
+
}
|
|
24
|
+
return [arr];
|
|
25
|
+
};
|
|
26
|
+
export const byteObjectChunk = (obj, maxByteLength) => {
|
|
27
|
+
const keys = getKeys(obj);
|
|
28
|
+
const byteLength = Buffer.byteLength(JSON.stringify(obj));
|
|
29
|
+
if (byteLength > maxByteLength) {
|
|
30
|
+
// if too big => we remove it
|
|
31
|
+
if (keys.length === 1) {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
const divider = Math.floor(keys.length / 2);
|
|
35
|
+
const keys1 = keys.slice(0, divider);
|
|
36
|
+
const keys2 = keys.slice(divider);
|
|
37
|
+
const obj1 = fromEntries(keys1.map((k) => [k, obj[k]]));
|
|
38
|
+
const obj2 = fromEntries(keys2.map((k) => [k, obj[k]]));
|
|
39
|
+
return [
|
|
40
|
+
byteObjectChunk(obj1, maxByteLength),
|
|
41
|
+
byteObjectChunk(obj2, maxByteLength),
|
|
42
|
+
].flat();
|
|
43
|
+
}
|
|
44
|
+
return [obj];
|
|
45
|
+
};
|
|
46
|
+
export const getBytesUnit = (bytes) => {
|
|
47
|
+
if (bytes === 0) {
|
|
48
|
+
return "Bytes";
|
|
49
|
+
}
|
|
50
|
+
const sizes = [
|
|
51
|
+
"Bytes",
|
|
52
|
+
"KB",
|
|
53
|
+
"MB",
|
|
54
|
+
"GB",
|
|
55
|
+
"TB",
|
|
56
|
+
];
|
|
57
|
+
const threshold = 1024;
|
|
58
|
+
const index = Math.floor(Math.log(bytes) / Math.log(threshold));
|
|
59
|
+
return sizes[index] || "Bytes";
|
|
60
|
+
};
|
|
61
|
+
export const getBytesValue = (bytes, decimals = 2) => {
|
|
62
|
+
if (bytes === 0) {
|
|
63
|
+
return 0;
|
|
64
|
+
}
|
|
65
|
+
const threshold = 1024;
|
|
66
|
+
const index = Math.floor(Math.log(bytes) / Math.log(threshold));
|
|
67
|
+
return parseFloat((bytes / Math.pow(threshold, index)).toFixed(decimals));
|
|
68
|
+
};
|
|
69
|
+
export const getBytesWithUnitString = (bytes, decimals = 2) => {
|
|
70
|
+
return `${getBytesValue(bytes, decimals)} ${getBytesUnit(bytes)}`;
|
|
71
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"deepMergeObjects.d.ts","sourceRoot":"","sources":["../../../src/collection/deepMergeObjects.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,gBAAgB,kCA0C5B,CAAC"}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export const deepMergeObjects = (object1, object2) => {
|
|
2
|
+
if (Array.isArray(object1) === true && Array.isArray(object2) === true) {
|
|
3
|
+
const maxLength = Math.max(object1.length, object2.length);
|
|
4
|
+
const mergedArray = [];
|
|
5
|
+
for (let index = 0; index < maxLength; index++) {
|
|
6
|
+
const object1Value = object1[index];
|
|
7
|
+
const object2Value = object2[index];
|
|
8
|
+
if (object1Value !== undefined && object2Value !== undefined) {
|
|
9
|
+
mergedArray[index] = deepMergeObjects(object1Value, object2Value);
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
mergedArray[index] =
|
|
13
|
+
object2Value !== undefined ? object2Value : object1Value;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return mergedArray;
|
|
17
|
+
}
|
|
18
|
+
const mergedObject = { ...object1 };
|
|
19
|
+
for (const key in object2) {
|
|
20
|
+
const object1Value = object1[key];
|
|
21
|
+
const object2Value = object2[key];
|
|
22
|
+
if (object1Value instanceof Object === true &&
|
|
23
|
+
object2Value instanceof Object === true) {
|
|
24
|
+
mergedObject[key] = deepMergeObjects(object1Value, object2Value);
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
mergedObject[key] = object2Value;
|
|
28
|
+
}
|
|
29
|
+
return mergedObject;
|
|
30
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/collection/index.ts"],"names":[],"mappings":"AAAA,cAAc,uBAAuB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./deepMergeObjects.js";
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { RecursiveNonNull } from "./types.js";
|
|
2
|
+
export declare const pick: <T>(obj: T, keys: (keyof T)[]) => Partial<T>;
|
|
3
|
+
export declare const getKeys: <T extends object>(object: T) => (keyof T & string)[];
|
|
4
|
+
export declare const getValues: <T extends object>(object: T) => T[keyof T][];
|
|
5
|
+
export declare const getEntries: <T extends object>(object: T) => [keyof T & string, T[keyof T & string]][];
|
|
6
|
+
export declare const fromEntries: <K extends string | symbol, V>(entries: [K, V][]) => { [key in K]: V; };
|
|
7
|
+
export declare const mapValues: <I extends object, O extends { [key in keyof I]: O[key]; }>(object: I, iteratee: <K extends keyof I & string>(value: I[K], key?: K | undefined, object?: I | undefined) => O[K]) => O;
|
|
8
|
+
export declare const groupBy: <I extends Record<string, unknown>>(array: I[], key: keyof I) => Record<string, I[]>;
|
|
9
|
+
export declare const sortBy: <I extends Record<string, unknown>>(array: I[], key: keyof I, order: "asc" | "desc") => I[];
|
|
10
|
+
export declare const areArraysEqual: <I>(array1: I[], array2: I[]) => boolean;
|
|
11
|
+
export declare const nullToUndefined: <Value>(value: Value) => RecursiveNonNull<Value>;
|
|
12
|
+
export declare const stringToDate: (value: any, options?: {
|
|
13
|
+
timezone?: string;
|
|
14
|
+
}) => any;
|
|
15
|
+
export declare const dateToString: (value: any) => any;
|
|
16
|
+
export declare const removeUndefined: (value: unknown) => unknown;
|
|
17
|
+
export declare const setFromPath: <T>(data: T, path: string[], value: unknown) => T;
|
|
18
|
+
export declare const getFromPath: (data: unknown, path: string[]) => unknown;
|
|
19
|
+
export declare const equals: (data: unknown, otherData: unknown) => boolean;
|
|
20
|
+
export declare const getPathFromJavascriptPath: (value: string) => string[];
|
|
21
|
+
export declare const merge: <T>(data: T, dataToMerge: T) => T;
|
|
22
|
+
//# sourceMappingURL=collection.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"collection.d.ts","sourceRoot":"","sources":["../../src/collection.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAEnD,eAAO,MAAM,IAAI,8CAOhB,CAAC;AAEF,eAAO,MAAM,OAAO,uDAInB,CAAC;AAEF,eAAO,MAAM,SAAS,+CAErB,CAAC;AAEF,eAAO,MAAM,UAAU,4EAMtB,CAAC;AAEF,eAAO,MAAM,WAAW,yEAQvB,CAAC;AAEF,eAAO,MAAM,SAAS,uLAgBrB,CAAC;AAEF,eAAO,MAAM,OAAO,sFAYnB,CAAC;AAEF,eAAO,MAAM,MAAM,uEAGV,KAAK,GAAG,MAAM,QAwBtB,CAAC;AAEF,eAAO,MAAM,cAAc,mCAAkC,OAM5D,CAAC;AAEF,eAAO,MAAM,eAAe,kDAmB3B,CAAC;AAEF,eAAO,MAAM,YAAY,UAEhB,GAAG,YACD;IAAE,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,KAE7B,GAiCF,CAAC;AAGF,eAAO,MAAM,YAAY,UAAW,GAAG,KAAG,GAkBzC,CAAC;AAEF,eAAO,MAAM,eAAe,UAAW,OAAO,KAAG,OAchD,CAAC;AAEF,eAAO,MAAM,WAAW,qBAAsB,MAAM,EAAE,SAAS,OAAO,MAyDrE,CAAC;AAEF,eAAO,MAAM,WAAW,SAAU,OAAO,QAAQ,MAAM,EAAE,KAAG,OAY3D,CAAC;AAEF,eAAO,MAAM,MAAM,SAAU,OAAO,aAAa,OAAO,KAAG,OAE1D,CAAC;AAEF,eAAO,MAAM,yBAAyB,UAAW,MAAM,KAAG,MAAM,EAE/D,CAAC;AAEF,eAAO,MAAM,KAAK,mCAiBjB,CAAC"}
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { isValid, parseJSON } from "date-fns";
|
|
2
|
+
import fastDeepEqual from "fast-deep-equal";
|
|
3
|
+
import { getDate } from "./date/getDate.js";
|
|
4
|
+
export const pick = (obj, keys) => {
|
|
5
|
+
const ret = {};
|
|
6
|
+
keys.forEach((key) => {
|
|
7
|
+
ret[key] = obj[key];
|
|
8
|
+
});
|
|
9
|
+
return ret;
|
|
10
|
+
};
|
|
11
|
+
export const getKeys = (object) => {
|
|
12
|
+
return Object.keys(object);
|
|
13
|
+
};
|
|
14
|
+
export const getValues = (object) => {
|
|
15
|
+
return Object.values(object);
|
|
16
|
+
};
|
|
17
|
+
export const getEntries = (object) => {
|
|
18
|
+
return Object.entries(object);
|
|
19
|
+
};
|
|
20
|
+
export const fromEntries = (entries) => {
|
|
21
|
+
const object = {};
|
|
22
|
+
for (const [key, value] of entries) {
|
|
23
|
+
object[key] = value;
|
|
24
|
+
}
|
|
25
|
+
return object;
|
|
26
|
+
};
|
|
27
|
+
export const mapValues = (object, iteratee) => {
|
|
28
|
+
const result = {};
|
|
29
|
+
for (const [key, value] of getEntries(object)) {
|
|
30
|
+
result[key] = iteratee(value, key, object);
|
|
31
|
+
}
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
export const groupBy = (array, key) => {
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
36
|
+
return array.reduce((result, currentValue) => {
|
|
37
|
+
(result[currentValue[key]] = result[currentValue[key]] || []).push(currentValue);
|
|
38
|
+
return result;
|
|
39
|
+
}, {});
|
|
40
|
+
};
|
|
41
|
+
export const sortBy = (array, key, order) => {
|
|
42
|
+
return array.sort((itemA, itemB) => {
|
|
43
|
+
const valueA = itemA[key];
|
|
44
|
+
const valueB = itemB[key];
|
|
45
|
+
if (valueA === null || valueA === undefined) {
|
|
46
|
+
return order === "asc" ? -1 : 1;
|
|
47
|
+
}
|
|
48
|
+
if (valueB === null || valueB === undefined) {
|
|
49
|
+
return order === "asc" ? 1 : -1;
|
|
50
|
+
}
|
|
51
|
+
let comparison = 0;
|
|
52
|
+
if (typeof valueA === "string" && typeof valueB === "string") {
|
|
53
|
+
comparison = valueA.localeCompare(valueB);
|
|
54
|
+
}
|
|
55
|
+
else if (typeof valueA === "number" && typeof valueB === "number") {
|
|
56
|
+
comparison = valueA - valueB;
|
|
57
|
+
}
|
|
58
|
+
return order === "asc" ? comparison : -comparison;
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
export const areArraysEqual = (array1, array2) => {
|
|
62
|
+
if (array1.length !== array2.length) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
return array1.every((value, index) => value === array2[index]);
|
|
66
|
+
};
|
|
67
|
+
export const nullToUndefined = (value) => {
|
|
68
|
+
if (value === null) {
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
if (value instanceof Array) {
|
|
72
|
+
return value.map(nullToUndefined);
|
|
73
|
+
}
|
|
74
|
+
if (value instanceof Object) {
|
|
75
|
+
return mapValues(value, (attributeValue) => {
|
|
76
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
77
|
+
return nullToUndefined(attributeValue);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
return value;
|
|
81
|
+
};
|
|
82
|
+
export const stringToDate = (
|
|
83
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
84
|
+
value, options = {}) => {
|
|
85
|
+
const { timezone = "UTC" } = options;
|
|
86
|
+
if (value === null || value === undefined) {
|
|
87
|
+
return value;
|
|
88
|
+
}
|
|
89
|
+
if (value instanceof Array) {
|
|
90
|
+
return value.map((item) => {
|
|
91
|
+
return stringToDate(item, { timezone });
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
if (value instanceof Object &&
|
|
95
|
+
Object.prototype.toString.call(value) !== "[object Date]") {
|
|
96
|
+
return mapValues(value, (item) => {
|
|
97
|
+
return stringToDate(item, { timezone });
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
if (isNaN(value) &&
|
|
101
|
+
typeof value === "string" &&
|
|
102
|
+
isValid(parseJSON(value))) {
|
|
103
|
+
const date = getDate(value, { timezone });
|
|
104
|
+
return isNaN(date.getTime()) ? value : date;
|
|
105
|
+
}
|
|
106
|
+
return value;
|
|
107
|
+
};
|
|
108
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
109
|
+
export const dateToString = (value) => {
|
|
110
|
+
if (value === null || value === undefined) {
|
|
111
|
+
return value;
|
|
112
|
+
}
|
|
113
|
+
if (value instanceof Date) {
|
|
114
|
+
return value.toISOString();
|
|
115
|
+
}
|
|
116
|
+
if (value instanceof Array) {
|
|
117
|
+
return value.map(dateToString);
|
|
118
|
+
}
|
|
119
|
+
if (value instanceof Object) {
|
|
120
|
+
return mapValues(value, dateToString);
|
|
121
|
+
}
|
|
122
|
+
return value;
|
|
123
|
+
};
|
|
124
|
+
export const removeUndefined = (value) => {
|
|
125
|
+
if (value instanceof Array) {
|
|
126
|
+
return value.filter((v) => v !== undefined).map(removeUndefined);
|
|
127
|
+
}
|
|
128
|
+
if (value instanceof Object) {
|
|
129
|
+
return fromEntries(getEntries(value)
|
|
130
|
+
.filter(([, v]) => v !== undefined)
|
|
131
|
+
.map(([k, v]) => [k, removeUndefined(v)]));
|
|
132
|
+
}
|
|
133
|
+
return value;
|
|
134
|
+
};
|
|
135
|
+
export const setFromPath = (data, path, value) => {
|
|
136
|
+
const [head, ...rest] = path;
|
|
137
|
+
if (head === undefined) {
|
|
138
|
+
return value;
|
|
139
|
+
}
|
|
140
|
+
if (data === null || data === undefined) {
|
|
141
|
+
const index = Number(head);
|
|
142
|
+
const isNumeric = isNaN(index) === false;
|
|
143
|
+
if (rest.length === 0) {
|
|
144
|
+
if (isNumeric === true) {
|
|
145
|
+
return Array.from({ length: index + 1 }, (_, i) => {
|
|
146
|
+
return i === index ? value : undefined;
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
return { [head]: value };
|
|
150
|
+
}
|
|
151
|
+
if (isNumeric === true) {
|
|
152
|
+
return Array.from({ length: index + 1 }, (_, i) => {
|
|
153
|
+
return i === index ? setFromPath(undefined, rest, value) : undefined;
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
return { [head]: setFromPath(undefined, rest, value) };
|
|
157
|
+
}
|
|
158
|
+
if (data instanceof Array) {
|
|
159
|
+
const index = Number(head);
|
|
160
|
+
const maxIndex = Math.max(data.length, index + 1);
|
|
161
|
+
if (rest.length === 0) {
|
|
162
|
+
return Array.from({ length: maxIndex }, (_, i) => {
|
|
163
|
+
return i === index ? value : data[i];
|
|
164
|
+
});
|
|
165
|
+
}
|
|
166
|
+
return Array.from({ length: maxIndex }, (_, i) => i === index ? setFromPath(data[i], rest, value) : data[i]);
|
|
167
|
+
}
|
|
168
|
+
if (typeof data === "object") {
|
|
169
|
+
if (rest.length === 0) {
|
|
170
|
+
return { ...data, [head]: value };
|
|
171
|
+
}
|
|
172
|
+
return {
|
|
173
|
+
...data,
|
|
174
|
+
[head]: setFromPath(data[head], rest, value),
|
|
175
|
+
};
|
|
176
|
+
}
|
|
177
|
+
return { [head]: setFromPath(undefined, rest, value) };
|
|
178
|
+
};
|
|
179
|
+
export const getFromPath = (data, path) => {
|
|
180
|
+
return path.reduce((acc, value) => {
|
|
181
|
+
if (acc === undefined || acc === null) {
|
|
182
|
+
return acc;
|
|
183
|
+
}
|
|
184
|
+
if (typeof acc === "object") {
|
|
185
|
+
return acc[value];
|
|
186
|
+
}
|
|
187
|
+
return undefined;
|
|
188
|
+
}, data);
|
|
189
|
+
};
|
|
190
|
+
export const equals = (data, otherData) => {
|
|
191
|
+
return fastDeepEqual(removeUndefined(data), removeUndefined(otherData));
|
|
192
|
+
};
|
|
193
|
+
export const getPathFromJavascriptPath = (value) => {
|
|
194
|
+
return value.split(/\.|\[|\]/g).filter(Boolean);
|
|
195
|
+
};
|
|
196
|
+
export const merge = (data, dataToMerge) => {
|
|
197
|
+
if (dataToMerge === undefined) {
|
|
198
|
+
return data;
|
|
199
|
+
}
|
|
200
|
+
if (data instanceof Array && dataToMerge instanceof Array) {
|
|
201
|
+
return [...data, ...dataToMerge];
|
|
202
|
+
}
|
|
203
|
+
if (data instanceof Object && dataToMerge instanceof Object) {
|
|
204
|
+
return {
|
|
205
|
+
...data,
|
|
206
|
+
...dataToMerge,
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
return dataToMerge;
|
|
210
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"colors.d.ts","sourceRoot":"","sources":["../../src/colors.ts"],"names":[],"mappings":"AAAA,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,SAAI,UAiB/C"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function hexToRgba(hex, alpha = 1) {
|
|
2
|
+
let sanitizedHex = hex.replace("#", "");
|
|
3
|
+
if (sanitizedHex.length === 3) {
|
|
4
|
+
sanitizedHex = sanitizedHex
|
|
5
|
+
.split("")
|
|
6
|
+
.map((char) => char + char)
|
|
7
|
+
.join("");
|
|
8
|
+
}
|
|
9
|
+
const bigint = parseInt(sanitizedHex, 16);
|
|
10
|
+
const r = (bigint >> 16) & 255;
|
|
11
|
+
const g = (bigint >> 8) & 255;
|
|
12
|
+
const b = bigint & 255;
|
|
13
|
+
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
|
|
14
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
export declare const isComponentElement: <Props>(component: React.FC<Props>, element: React.ReactElement) => boolean;
|
|
3
|
+
export declare const replaceComponentElements: <Props>(component: React.FC<Props>, replace: (props: Props, index: number, count: number) => React.ReactElement, children?: React.ReactNode) => React.ReactNode;
|
|
4
|
+
export declare const filterComponentElements: <Props>(component: React.FC<Props>, children?: React.ReactNode) => React.ReactElement<Props, string | React.JSXElementConstructor<any>>[];
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
export const isComponentElement = (component, element) => {
|
|
3
|
+
return component.name === element.type.name;
|
|
4
|
+
};
|
|
5
|
+
export const replaceComponentElements = (component, replace, children) => {
|
|
6
|
+
if (!children) {
|
|
7
|
+
return undefined;
|
|
8
|
+
}
|
|
9
|
+
const count = React.Children.count(children);
|
|
10
|
+
return React.Children.map(children, (child, index) => {
|
|
11
|
+
if (!React.isValidElement(child)) {
|
|
12
|
+
return child;
|
|
13
|
+
}
|
|
14
|
+
const childElement = child;
|
|
15
|
+
if (isComponentElement(component, childElement)) {
|
|
16
|
+
return replace(childElement.props, index, count);
|
|
17
|
+
}
|
|
18
|
+
if (childElement.props.children) {
|
|
19
|
+
return {
|
|
20
|
+
...childElement,
|
|
21
|
+
props: {
|
|
22
|
+
...childElement.props,
|
|
23
|
+
children: replaceComponentElements(component, replace, childElement.props.children),
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
return child;
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
export const filterComponentElements = (component, children) => {
|
|
31
|
+
const elements = [];
|
|
32
|
+
React.Children.forEach(children, (child) => {
|
|
33
|
+
if (!React.isValidElement(child)) {
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const childElement = child;
|
|
37
|
+
if (isComponentElement(component, childElement)) {
|
|
38
|
+
elements.push(childElement);
|
|
39
|
+
}
|
|
40
|
+
else if (childElement.props.children) {
|
|
41
|
+
elements.push(...filterComponentElements(component, childElement.props.children));
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
return elements;
|
|
45
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"credits.d.ts","sourceRoot":"","sources":["../../src/credits.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,WAAW,GAAG;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC;AAwFF,eAAO,MAAM,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,EAAE,CAGvD,CAAC"}
|