@rootreeweb/js-lib 4.0.10
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/README.md +49 -0
- package/dist/cjs/browser/dom.cjs +95 -0
- package/dist/cjs/browser/dom.d.ts +44 -0
- package/dist/cjs/browser/dom.d.ts.map +1 -0
- package/dist/cjs/browser/dom.js.map +1 -0
- package/dist/cjs/browser/download.cjs +40 -0
- package/dist/cjs/browser/download.d.ts +16 -0
- package/dist/cjs/browser/download.d.ts.map +1 -0
- package/dist/cjs/browser/download.js.map +1 -0
- package/dist/cjs/browser/index.cjs +20 -0
- package/dist/cjs/browser/index.d.ts +4 -0
- package/dist/cjs/browser/index.d.ts.map +1 -0
- package/dist/cjs/browser/index.js.map +1 -0
- package/dist/cjs/browser/queryOrThrow.cjs +42 -0
- package/dist/cjs/browser/queryOrThrow.d.ts +44 -0
- package/dist/cjs/browser/queryOrThrow.d.ts.map +1 -0
- package/dist/cjs/browser/queryOrThrow.js.map +1 -0
- package/dist/cjs/general/arrays.cjs +151 -0
- package/dist/cjs/general/arrays.d.ts +70 -0
- package/dist/cjs/general/arrays.d.ts.map +1 -0
- package/dist/cjs/general/arrays.js.map +1 -0
- package/dist/cjs/general/boolean.cjs +19 -0
- package/dist/cjs/general/boolean.d.ts +10 -0
- package/dist/cjs/general/boolean.d.ts.map +1 -0
- package/dist/cjs/general/boolean.js.map +1 -0
- package/dist/cjs/general/clock.cjs +22 -0
- package/dist/cjs/general/clock.d.ts +12 -0
- package/dist/cjs/general/clock.d.ts.map +1 -0
- package/dist/cjs/general/clock.js.map +1 -0
- package/dist/cjs/general/dates/create.cjs +81 -0
- package/dist/cjs/general/dates/create.d.ts +54 -0
- package/dist/cjs/general/dates/create.d.ts.map +1 -0
- package/dist/cjs/general/dates/create.js.map +1 -0
- package/dist/cjs/general/dates/dates.cjs +88 -0
- package/dist/cjs/general/dates/dates.d.ts +56 -0
- package/dist/cjs/general/dates/dates.d.ts.map +1 -0
- package/dist/cjs/general/dates/dates.js.map +1 -0
- package/dist/cjs/general/dates/get.cjs +77 -0
- package/dist/cjs/general/dates/get.d.ts +41 -0
- package/dist/cjs/general/dates/get.d.ts.map +1 -0
- package/dist/cjs/general/dates/get.js.map +1 -0
- package/dist/cjs/general/dates/index.cjs +20 -0
- package/dist/cjs/general/dates/index.d.ts +4 -0
- package/dist/cjs/general/dates/index.d.ts.map +1 -0
- package/dist/cjs/general/dates/index.js.map +1 -0
- package/dist/cjs/general/forms.cjs +31 -0
- package/dist/cjs/general/forms.d.ts +17 -0
- package/dist/cjs/general/forms.d.ts.map +1 -0
- package/dist/cjs/general/forms.js.map +1 -0
- package/dist/cjs/general/images.cjs +23 -0
- package/dist/cjs/general/images.d.ts +17 -0
- package/dist/cjs/general/images.d.ts.map +1 -0
- package/dist/cjs/general/images.js.map +1 -0
- package/dist/cjs/general/index.cjs +31 -0
- package/dist/cjs/general/index.d.ts +15 -0
- package/dist/cjs/general/index.d.ts.map +1 -0
- package/dist/cjs/general/index.js.map +1 -0
- package/dist/cjs/general/localization.cjs +199 -0
- package/dist/cjs/general/localization.d.ts +83 -0
- package/dist/cjs/general/localization.d.ts.map +1 -0
- package/dist/cjs/general/localization.js.map +1 -0
- package/dist/cjs/general/maps.cjs +63 -0
- package/dist/cjs/general/maps.d.ts +35 -0
- package/dist/cjs/general/maps.d.ts.map +1 -0
- package/dist/cjs/general/maps.js.map +1 -0
- package/dist/cjs/general/messenger/createMessenger.cjs +68 -0
- package/dist/cjs/general/messenger/createMessenger.d.ts +10 -0
- package/dist/cjs/general/messenger/createMessenger.d.ts.map +1 -0
- package/dist/cjs/general/messenger/createMessenger.js.map +1 -0
- package/dist/cjs/general/messenger/index.cjs +19 -0
- package/dist/cjs/general/messenger/index.d.ts +3 -0
- package/dist/cjs/general/messenger/index.d.ts.map +1 -0
- package/dist/cjs/general/messenger/index.js.map +1 -0
- package/dist/cjs/general/messenger/types.cjs +3 -0
- package/dist/cjs/general/messenger/types.d.ts +26 -0
- package/dist/cjs/general/messenger/types.d.ts.map +1 -0
- package/dist/cjs/general/messenger/types.js.map +1 -0
- package/dist/cjs/general/namedEntities.cjs +260 -0
- package/dist/cjs/general/namedEntities.d.ts +256 -0
- package/dist/cjs/general/namedEntities.d.ts.map +1 -0
- package/dist/cjs/general/namedEntities.js.map +1 -0
- package/dist/cjs/general/numbers.cjs +94 -0
- package/dist/cjs/general/numbers.d.ts +43 -0
- package/dist/cjs/general/numbers.d.ts.map +1 -0
- package/dist/cjs/general/numbers.js.map +1 -0
- package/dist/cjs/general/objects.cjs +98 -0
- package/dist/cjs/general/objects.d.ts +45 -0
- package/dist/cjs/general/objects.d.ts.map +1 -0
- package/dist/cjs/general/objects.js.map +1 -0
- package/dist/cjs/general/sorting.cjs +72 -0
- package/dist/cjs/general/sorting.d.ts +32 -0
- package/dist/cjs/general/sorting.d.ts.map +1 -0
- package/dist/cjs/general/sorting.js.map +1 -0
- package/dist/cjs/general/string.cjs +73 -0
- package/dist/cjs/general/string.d.ts +31 -0
- package/dist/cjs/general/string.d.ts.map +1 -0
- package/dist/cjs/general/string.js.map +1 -0
- package/dist/cjs/general/timeout.cjs +17 -0
- package/dist/cjs/general/timeout.d.ts +8 -0
- package/dist/cjs/general/timeout.d.ts.map +1 -0
- package/dist/cjs/general/timeout.js.map +1 -0
- package/dist/cjs/index.cjs +20 -0
- package/dist/cjs/index.d.ts +4 -0
- package/dist/cjs/index.d.ts.map +1 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/node/index.cjs +30 -0
- package/dist/cjs/node/index.d.ts +25 -0
- package/dist/cjs/node/index.d.ts.map +1 -0
- package/dist/cjs/node/index.js.map +1 -0
- package/dist/esm/browser/dom.d.ts +44 -0
- package/dist/esm/browser/dom.d.ts.map +1 -0
- package/dist/esm/browser/dom.js +88 -0
- package/dist/esm/browser/dom.js.map +1 -0
- package/dist/esm/browser/download.d.ts +16 -0
- package/dist/esm/browser/download.d.ts.map +1 -0
- package/dist/esm/browser/download.js +36 -0
- package/dist/esm/browser/download.js.map +1 -0
- package/dist/esm/browser/index.d.ts +4 -0
- package/dist/esm/browser/index.d.ts.map +1 -0
- package/dist/esm/browser/index.js +4 -0
- package/dist/esm/browser/index.js.map +1 -0
- package/dist/esm/browser/queryOrThrow.d.ts +44 -0
- package/dist/esm/browser/queryOrThrow.d.ts.map +1 -0
- package/dist/esm/browser/queryOrThrow.js +39 -0
- package/dist/esm/browser/queryOrThrow.js.map +1 -0
- package/dist/esm/general/arrays.d.ts +70 -0
- package/dist/esm/general/arrays.d.ts.map +1 -0
- package/dist/esm/general/arrays.js +144 -0
- package/dist/esm/general/arrays.js.map +1 -0
- package/dist/esm/general/boolean.d.ts +10 -0
- package/dist/esm/general/boolean.d.ts.map +1 -0
- package/dist/esm/general/boolean.js +16 -0
- package/dist/esm/general/boolean.js.map +1 -0
- package/dist/esm/general/clock.d.ts +12 -0
- package/dist/esm/general/clock.d.ts.map +1 -0
- package/dist/esm/general/clock.js +19 -0
- package/dist/esm/general/clock.js.map +1 -0
- package/dist/esm/general/dates/create.d.ts +54 -0
- package/dist/esm/general/dates/create.d.ts.map +1 -0
- package/dist/esm/general/dates/create.js +73 -0
- package/dist/esm/general/dates/create.js.map +1 -0
- package/dist/esm/general/dates/dates.d.ts +56 -0
- package/dist/esm/general/dates/dates.d.ts.map +1 -0
- package/dist/esm/general/dates/dates.js +82 -0
- package/dist/esm/general/dates/dates.js.map +1 -0
- package/dist/esm/general/dates/get.d.ts +41 -0
- package/dist/esm/general/dates/get.d.ts.map +1 -0
- package/dist/esm/general/dates/get.js +70 -0
- package/dist/esm/general/dates/get.js.map +1 -0
- package/dist/esm/general/dates/index.d.ts +4 -0
- package/dist/esm/general/dates/index.d.ts.map +1 -0
- package/dist/esm/general/dates/index.js +4 -0
- package/dist/esm/general/dates/index.js.map +1 -0
- package/dist/esm/general/forms.d.ts +17 -0
- package/dist/esm/general/forms.d.ts.map +1 -0
- package/dist/esm/general/forms.js +27 -0
- package/dist/esm/general/forms.js.map +1 -0
- package/dist/esm/general/images.d.ts +17 -0
- package/dist/esm/general/images.d.ts.map +1 -0
- package/dist/esm/general/images.js +20 -0
- package/dist/esm/general/images.js.map +1 -0
- package/dist/esm/general/index.d.ts +15 -0
- package/dist/esm/general/index.d.ts.map +1 -0
- package/dist/esm/general/index.js +15 -0
- package/dist/esm/general/index.js.map +1 -0
- package/dist/esm/general/localization.d.ts +83 -0
- package/dist/esm/general/localization.d.ts.map +1 -0
- package/dist/esm/general/localization.js +188 -0
- package/dist/esm/general/localization.js.map +1 -0
- package/dist/esm/general/maps.d.ts +35 -0
- package/dist/esm/general/maps.d.ts.map +1 -0
- package/dist/esm/general/maps.js +59 -0
- package/dist/esm/general/maps.js.map +1 -0
- package/dist/esm/general/messenger/createMessenger.d.ts +10 -0
- package/dist/esm/general/messenger/createMessenger.d.ts.map +1 -0
- package/dist/esm/general/messenger/createMessenger.js +65 -0
- package/dist/esm/general/messenger/createMessenger.js.map +1 -0
- package/dist/esm/general/messenger/index.d.ts +3 -0
- package/dist/esm/general/messenger/index.d.ts.map +1 -0
- package/dist/esm/general/messenger/index.js +3 -0
- package/dist/esm/general/messenger/index.js.map +1 -0
- package/dist/esm/general/messenger/types.d.ts +26 -0
- package/dist/esm/general/messenger/types.d.ts.map +1 -0
- package/dist/esm/general/messenger/types.js +2 -0
- package/dist/esm/general/messenger/types.js.map +1 -0
- package/dist/esm/general/namedEntities.d.ts +256 -0
- package/dist/esm/general/namedEntities.d.ts.map +1 -0
- package/dist/esm/general/namedEntities.js +257 -0
- package/dist/esm/general/namedEntities.js.map +1 -0
- package/dist/esm/general/numbers.d.ts +43 -0
- package/dist/esm/general/numbers.d.ts.map +1 -0
- package/dist/esm/general/numbers.js +87 -0
- package/dist/esm/general/numbers.js.map +1 -0
- package/dist/esm/general/objects.d.ts +45 -0
- package/dist/esm/general/objects.d.ts.map +1 -0
- package/dist/esm/general/objects.js +91 -0
- package/dist/esm/general/objects.js.map +1 -0
- package/dist/esm/general/sorting.d.ts +32 -0
- package/dist/esm/general/sorting.d.ts.map +1 -0
- package/dist/esm/general/sorting.js +67 -0
- package/dist/esm/general/sorting.js.map +1 -0
- package/dist/esm/general/string.d.ts +31 -0
- package/dist/esm/general/string.d.ts.map +1 -0
- package/dist/esm/general/string.js +68 -0
- package/dist/esm/general/string.js.map +1 -0
- package/dist/esm/general/timeout.d.ts +8 -0
- package/dist/esm/general/timeout.d.ts.map +1 -0
- package/dist/esm/general/timeout.js +14 -0
- package/dist/esm/general/timeout.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +4 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/node/index.d.ts +25 -0
- package/dist/esm/node/index.d.ts.map +1 -0
- package/dist/esm/node/index.js +27 -0
- package/dist/esm/node/index.js.map +1 -0
- package/package.json +62 -0
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a decimal number to a fraction.
|
|
3
|
+
*
|
|
4
|
+
* @param decimal - The decimal number to convert.
|
|
5
|
+
* @returns An object containing the numerator and denominator of the fraction.
|
|
6
|
+
*/
|
|
7
|
+
export declare function decimalToFraction(decimal: number): {
|
|
8
|
+
denominator: number;
|
|
9
|
+
numerator: number;
|
|
10
|
+
whole: number;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* Calculate the greatest common divisor of two numbers.
|
|
14
|
+
*
|
|
15
|
+
* @param a The first number.
|
|
16
|
+
* @param b The second number.
|
|
17
|
+
* @returns The greatest common divisor.
|
|
18
|
+
*/
|
|
19
|
+
export declare function gcd(a: number, b: number): number;
|
|
20
|
+
/**
|
|
21
|
+
* Get the aspect ratio of an image.
|
|
22
|
+
*
|
|
23
|
+
* @param width The width of the image.
|
|
24
|
+
* @param height The height of the image.
|
|
25
|
+
* @returns The aspect ratio.
|
|
26
|
+
*/
|
|
27
|
+
export declare function getAspectRatio(width: number, height: number): string;
|
|
28
|
+
/**
|
|
29
|
+
* Checks if the given value is numeric.
|
|
30
|
+
*
|
|
31
|
+
* @param value The value to check.
|
|
32
|
+
* @returns A boolean value indicating whether the value is numeric.
|
|
33
|
+
*/
|
|
34
|
+
export declare function isNumeric(value: unknown): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Round a number to a specific precision.
|
|
37
|
+
*
|
|
38
|
+
* @param value The value to round.
|
|
39
|
+
* @param precision The number of decimal places to round to.
|
|
40
|
+
* @returns The rounded value.
|
|
41
|
+
*/
|
|
42
|
+
export declare function toPrecision(value: number, precision: number): number;
|
|
43
|
+
//# sourceMappingURL=numbers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"numbers.d.ts","sourceRoot":"","sources":["../../../src/general/numbers.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,OAAO,EAAE,MAAM,GAAG;IACnD,WAAW,EAAE,MAAM,CAAC;IACpB,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,CAAC;CACd,CAuCA;AAED;;;;;;GAMG;AACH,wBAAgB,GAAG,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,MAAM,CAEhD;AAED;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAGpE;AAED;;;;;GAKG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAIjD;AAED;;;;;;GAMG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAGpE"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"numbers.js","sourceRoot":"","sources":["../../../src/general/numbers.ts"],"names":[],"mappings":";;AAMA,8CA2CC;AASD,kBAEC;AASD,wCAGC;AAQD,8BAIC;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D,OAAO;QACN,WAAW,EAAE,eAAe,GAAG,OAAO;QACtC,SAAS,EAAE,aAAa,GAAG,OAAO;QAClC,KAAK;KACL,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,GAAG,CAAC,CAAS,EAAE,CAAS;IACvC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,EAAE,CAAC,GAAG,CAAC,CAAC,CAAC;AACnC,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,cAAc,CAAC,KAAa,EAAE,MAAc;IAC3D,MAAM,OAAO,GAAG,GAAG,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;IACnC,OAAO,GAAG,MAAM,CAAC,KAAK,GAAG,OAAO,CAAC,IAAI,MAAM,CAAC,MAAM,GAAG,OAAO,CAAC,EAAE,CAAC;AACjE,CAAC;AAED;;;;;GAKG;AACH,SAAgB,SAAS,CAAC,KAAc;IACvC,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,IAAI,CAAC;IAC3C,IAAI,OAAO,KAAK,KAAK,QAAQ;QAAE,OAAO,mBAAmB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;IACtE,OAAO,KAAK,CAAC;AACd,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,WAAW,CAAC,KAAa,EAAE,SAAiB;IAC3D,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,EAAE,EAAE,SAAS,CAAC,CAAC;IACvC,OAAO,IAAI,CAAC,KAAK,CAAC,KAAK,GAAG,MAAM,CAAC,GAAG,MAAM,CAAC;AAC5C,CAAC","sourcesContent":["/**\n * Converts a decimal number to a fraction.\n *\n * @param decimal - The decimal number to convert.\n * @returns An object containing the numerator and denominator of the fraction.\n */\nexport function decimalToFraction(decimal: number): {\n\tdenominator: number;\n\tnumerator: number;\n\twhole: number;\n} {\n\tconst whole = Math.floor(decimal);\n\tconst fractionalPart = decimal - whole;\n\n\tif (fractionalPart === 0) {\n\t\treturn { denominator: 1, numerator: 0, whole };\n\t}\n\n\tlet bestNumerator = 0;\n\tlet bestDenominator = 1;\n\tlet minError = Math.abs(fractionalPart - bestNumerator / bestDenominator);\n\n\tfor (let i = 1; i <= 1000; i++) {\n\t\t// Check denominators up to 1000\n\t\tfor (let j = 0; j <= i; j++) {\n\t\t\tconst currentFraction = j / i;\n\t\t\tconst error = Math.abs(fractionalPart - currentFraction);\n\n\t\t\tif (error < minError) {\n\t\t\t\tminError = error;\n\t\t\t\tbestNumerator = j;\n\t\t\t\tbestDenominator = i;\n\t\t\t\tif (error < 0.000001) {\n\t\t\t\t\t//Added a small error threshold\n\t\t\t\t\tbreak; //Exit inner loop once a reasonable value is found\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (minError < 0.000001) {\n\t\t\tbreak; //Exit outer loop\n\t\t}\n\t}\n\n\tconst divisor = gcd(bestNumerator, bestDenominator);\n\treturn {\n\t\tdenominator: bestDenominator / divisor,\n\t\tnumerator: bestNumerator / divisor,\n\t\twhole,\n\t};\n}\n\n/**\n * Calculate the greatest common divisor of two numbers.\n *\n * @param a The first number.\n * @param b The second number.\n * @returns The greatest common divisor.\n */\nexport function gcd(a: number, b: number): number {\n\treturn b == 0 ? a : gcd(b, a % b);\n}\n\n/**\n * Get the aspect ratio of an image.\n *\n * @param width The width of the image.\n * @param height The height of the image.\n * @returns The aspect ratio.\n */\nexport function getAspectRatio(width: number, height: number): string {\n\tconst divisor = gcd(width, height);\n\treturn `${String(width / divisor)}:${String(height / divisor)}`;\n}\n\n/**\n * Checks if the given value is numeric.\n *\n * @param value The value to check.\n * @returns A boolean value indicating whether the value is numeric.\n */\nexport function isNumeric(value: unknown): boolean {\n\tif (typeof value === \"number\") return true;\n\tif (typeof value === \"string\") return /^-?\\d+(?:\\.\\d+)?$/.test(value);\n\treturn false;\n}\n\n/**\n * Round a number to a specific precision.\n *\n * @param value The value to round.\n * @param precision The number of decimal places to round to.\n * @returns The rounded value.\n */\nexport function toPrecision(value: number, precision: number): number {\n\tconst factor = Math.pow(10, precision);\n\treturn Math.round(value * factor) / factor;\n}\n"]}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.deepMerge = deepMerge;
|
|
4
|
+
exports.flattenObject = flattenObject;
|
|
5
|
+
exports.isEmptyObject = isEmptyObject;
|
|
6
|
+
exports.isPlainObject = isPlainObject;
|
|
7
|
+
exports.safeishGetProperty = safeishGetProperty;
|
|
8
|
+
const arrays_js_1 = require("./arrays.cjs");
|
|
9
|
+
/**
|
|
10
|
+
* Deep merge two objects.
|
|
11
|
+
*
|
|
12
|
+
* @template T The type of the objects to merge.
|
|
13
|
+
* @template U The type of the data object.
|
|
14
|
+
* @param defaults The defaults object.
|
|
15
|
+
* @param data The data object to merge into the defaults.
|
|
16
|
+
* @returns The merged object.
|
|
17
|
+
*/
|
|
18
|
+
function deepMerge(defaults, data) {
|
|
19
|
+
const output = { ...defaults };
|
|
20
|
+
for (const key in data) {
|
|
21
|
+
if (Object.prototype.hasOwnProperty.call(data, key)) {
|
|
22
|
+
const dataValue = safeishGetProperty(data, key);
|
|
23
|
+
const defaultsValue = safeishGetProperty(defaults, key);
|
|
24
|
+
if (isObject(dataValue) && isObject(defaultsValue)) {
|
|
25
|
+
output[key] = deepMerge(defaultsValue, dataValue);
|
|
26
|
+
}
|
|
27
|
+
else if (Array.isArray(dataValue) && Array.isArray(defaultsValue)) {
|
|
28
|
+
output[key] = (0, arrays_js_1.dedupe)([...defaultsValue, ...dataValue]);
|
|
29
|
+
}
|
|
30
|
+
else {
|
|
31
|
+
output[key] = dataValue;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
return output;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Removes any unserializable objects from the object.
|
|
39
|
+
*
|
|
40
|
+
* @template T The type of the object.
|
|
41
|
+
* @param obj The object to flatten.
|
|
42
|
+
* @returns The flattened object.
|
|
43
|
+
*/
|
|
44
|
+
function flattenObject(obj) {
|
|
45
|
+
return JSON.parse(JSON.stringify(obj));
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Determines if an object is empty.
|
|
49
|
+
*
|
|
50
|
+
* @param obj The object to check.
|
|
51
|
+
* @returns True if the object is empty.
|
|
52
|
+
*/
|
|
53
|
+
function isEmptyObject(obj) {
|
|
54
|
+
return Object.keys(obj).length === 0;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Test if a value is a plain object for RSC purposes.
|
|
58
|
+
*
|
|
59
|
+
* @param value The value to check.
|
|
60
|
+
* @param parentKey The nested key being checked.
|
|
61
|
+
* @returns True if the object is deemed passable to a client component.
|
|
62
|
+
*/
|
|
63
|
+
function isPlainObject(value, parentKey) {
|
|
64
|
+
if (!isObject(value))
|
|
65
|
+
return true;
|
|
66
|
+
for (const key in value) {
|
|
67
|
+
if (!isPlainObject(value[key], key)) {
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
|
72
|
+
const ownPrototype = Object.getPrototypeOf(value);
|
|
73
|
+
const isSelfPlain = ownPrototype === Object.prototype;
|
|
74
|
+
if (!isSelfPlain) {
|
|
75
|
+
console.error(
|
|
76
|
+
// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing
|
|
77
|
+
`Non-plain object found at ${parentKey || "root"}: ${String(ownPrototype)}`);
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
return true;
|
|
81
|
+
}
|
|
82
|
+
function safeishGetProperty(object, field) {
|
|
83
|
+
if (Object.prototype.hasOwnProperty.call(object, field)) {
|
|
84
|
+
// eslint-disable-next-line security/detect-object-injection
|
|
85
|
+
return object[field];
|
|
86
|
+
}
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Check if a value is an object.
|
|
91
|
+
*
|
|
92
|
+
* @param obj The value to check.
|
|
93
|
+
* @returns True if the value is an object.
|
|
94
|
+
*/
|
|
95
|
+
function isObject(obj) {
|
|
96
|
+
return !!(obj && typeof obj === "object" && !Array.isArray(obj));
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=objects.js.map
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Deep merge two objects.
|
|
3
|
+
*
|
|
4
|
+
* @template T The type of the objects to merge.
|
|
5
|
+
* @template U The type of the data object.
|
|
6
|
+
* @param defaults The defaults object.
|
|
7
|
+
* @param data The data object to merge into the defaults.
|
|
8
|
+
* @returns The merged object.
|
|
9
|
+
*/
|
|
10
|
+
export declare function deepMerge<T extends object, U extends object>(defaults: T, data: U): T & U;
|
|
11
|
+
/**
|
|
12
|
+
* Removes any unserializable objects from the object.
|
|
13
|
+
*
|
|
14
|
+
* @template T The type of the object.
|
|
15
|
+
* @param obj The object to flatten.
|
|
16
|
+
* @returns The flattened object.
|
|
17
|
+
*/
|
|
18
|
+
export declare function flattenObject<T>(obj: T): T;
|
|
19
|
+
/**
|
|
20
|
+
* Determines if an object is empty.
|
|
21
|
+
*
|
|
22
|
+
* @param obj The object to check.
|
|
23
|
+
* @returns True if the object is empty.
|
|
24
|
+
*/
|
|
25
|
+
export declare function isEmptyObject(obj: object): boolean;
|
|
26
|
+
/**
|
|
27
|
+
* Test if a value is a plain object for RSC purposes.
|
|
28
|
+
*
|
|
29
|
+
* @param value The value to check.
|
|
30
|
+
* @param parentKey The nested key being checked.
|
|
31
|
+
* @returns True if the object is deemed passable to a client component.
|
|
32
|
+
*/
|
|
33
|
+
export declare function isPlainObject(value: unknown, parentKey?: string): boolean;
|
|
34
|
+
/**
|
|
35
|
+
* Checks that the field exists on the object itself before getting it.
|
|
36
|
+
*
|
|
37
|
+
* @template TParent The type of the object.
|
|
38
|
+
* @template TField The key of the of the object.
|
|
39
|
+
* @param object The object to get the property from.
|
|
40
|
+
* @param field The field to get.
|
|
41
|
+
* @returns The value of the field, or undefined if it does not exist.
|
|
42
|
+
*/
|
|
43
|
+
export declare function safeishGetProperty<TParent extends object, TField extends keyof TParent>(object: TParent, field: TField): TParent[TField];
|
|
44
|
+
export declare function safeishGetProperty<TParent extends object>(object: TParent, field: PropertyKey): unknown;
|
|
45
|
+
//# sourceMappingURL=objects.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"objects.d.ts","sourceRoot":"","sources":["../../../src/general/objects.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AACH,wBAAgB,SAAS,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,EAC3D,QAAQ,EAAE,CAAC,EACX,IAAI,EAAE,CAAC,GACL,CAAC,GAAG,CAAC,CAmBP;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,GAAG,CAAC,CAE1C;AAED;;;;;GAKG;AACH,wBAAgB,aAAa,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAElD;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,KAAK,EAAE,OAAO,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,OAAO,CAkBzE;AAED;;;;;;;;GAQG;AACH,wBAAgB,kBAAkB,CACjC,OAAO,SAAS,MAAM,EACtB,MAAM,SAAS,MAAM,OAAO,EAC3B,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;AACnD,wBAAgB,kBAAkB,CAAC,OAAO,SAAS,MAAM,EACxD,MAAM,EAAE,OAAO,EACf,KAAK,EAAE,WAAW,GAChB,OAAO,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"objects.js","sourceRoot":"","sources":["../../../src/general/objects.ts"],"names":[],"mappings":";;AAWA,8BAsBC;A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,CAAC,MAAM,KAAK,CAAC,CAAC;AACtC,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,aAAa,CAAC,KAAc,EAAE,SAAkB;IAC/D,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC;QAAE,OAAO,IAAI,CAAC;IAClC,KAAK,MAAM,GAAG,IAAI,KAAK,EAAE,CAAC;QACzB,IAAI,CAAC,aAAa,CAAC,KAAK,CAAC,GAAyB,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC;YAC3D,OAAO,KAAK,CAAC;QACd,CAAC;IACF,CAAC;IACD,mEAAmE;IACnE,MAAM,YAAY,GAAG,MAAM,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;IAClD,MAAM,WAAW,GAAG,YAAY,KAAK,MAAM,CAAC,SAAS,CAAC;IACtD,IAAI,CAAC,WAAW,EAAE,CAAC;QAClB,OAAO,CAAC,KAAK;QACZ,wEAAwE;QACxE,6BAA6B,SAAS,IAAI,MAAM,KAAK,MAAM,CAAC,YAAY,CAAC,EAAE,CAC3E,CAAC;QACF,OAAO,KAAK,CAAC;IACd,CAAC;IACD,OAAO,IAAI,CAAC;AACb,CAAC;AAmBD,SAAgB,kBAAkB,CACjC,MAAe,EACf,KAAkB;IAElB,IAAI,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,MAAM,EAAE,KAAK,CAAC,EAAE,CAAC;QACzD,4DAA4D;QAC5D,OAAQ,MAAuC,CAAC,KAAK,CAAC,CAAC;IACxD,CAAC;IACD,OAAO,SAAS,CAAC;AAClB,CAAC;AACD;;;;;GAKG;AACH,SAAS,QAAQ,CAAC,GAAY;IAC7B,OAAO,CAAC,CAAC,CAAC,GAAG,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC;AAClE,CAAC","sourcesContent":["import { dedupe } from \"./arrays.js\";\n\n/**\n * Deep merge two objects.\n *\n * @template T The type of the objects to merge.\n * @template U The type of the data object.\n * @param defaults The defaults object.\n * @param data The data object to merge into the defaults.\n * @returns The merged object.\n */\nexport function deepMerge<T extends object, U extends object>(\n\tdefaults: T,\n\tdata: U,\n): T & U {\n\tconst output = { ...defaults } as T & U;\n\n\tfor (const key in data) {\n\t\tif (Object.prototype.hasOwnProperty.call(data, key)) {\n\t\t\tconst dataValue = safeishGetProperty(data, key as keyof U);\n\t\t\tconst defaultsValue = safeishGetProperty(defaults as T & U, key);\n\n\t\t\tif (isObject(dataValue) && isObject(defaultsValue)) {\n\t\t\t\toutput[key] = deepMerge(defaultsValue, dataValue);\n\t\t\t} else if (Array.isArray(dataValue) && Array.isArray(defaultsValue)) {\n\t\t\t\t(output as any)[key] = dedupe([...defaultsValue, ...dataValue]);\n\t\t\t} else {\n\t\t\t\t(output as any)[key] = dataValue;\n\t\t\t}\n\t\t}\n\t}\n\n\treturn output;\n}\n\n/**\n * Removes any unserializable objects from the object.\n *\n * @template T The type of the object.\n * @param obj The object to flatten.\n * @returns The flattened object.\n */\nexport function flattenObject<T>(obj: T): T {\n\treturn JSON.parse(JSON.stringify(obj)) as T;\n}\n\n/**\n * Determines if an object is empty.\n *\n * @param obj The object to check.\n * @returns True if the object is empty.\n */\nexport function isEmptyObject(obj: object): boolean {\n\treturn Object.keys(obj).length === 0;\n}\n\n/**\n * Test if a value is a plain object for RSC purposes.\n *\n * @param value The value to check.\n * @param parentKey The nested key being checked.\n * @returns True if the object is deemed passable to a client component.\n */\nexport function isPlainObject(value: unknown, parentKey?: string): boolean {\n\tif (!isObject(value)) return true;\n\tfor (const key in value) {\n\t\tif (!isPlainObject(value[key as keyof typeof value], key)) {\n\t\t\treturn false;\n\t\t}\n\t}\n\t// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment\n\tconst ownPrototype = Object.getPrototypeOf(value);\n\tconst isSelfPlain = ownPrototype === Object.prototype;\n\tif (!isSelfPlain) {\n\t\tconsole.error(\n\t\t\t// eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing\n\t\t\t`Non-plain object found at ${parentKey || \"root\"}: ${String(ownPrototype)}`,\n\t\t);\n\t\treturn false;\n\t}\n\treturn true;\n}\n\n/**\n * Checks that the field exists on the object itself before getting it.\n *\n * @template TParent The type of the object.\n * @template TField The key of the of the object.\n * @param object The object to get the property from.\n * @param field The field to get.\n * @returns The value of the field, or undefined if it does not exist.\n */\nexport function safeishGetProperty<\n\tTParent extends object,\n\tTField extends keyof TParent,\n>(object: TParent, field: TField): TParent[TField];\nexport function safeishGetProperty<TParent extends object>(\n\tobject: TParent,\n\tfield: PropertyKey,\n): unknown;\nexport function safeishGetProperty<TParent extends object>(\n\tobject: TParent,\n\tfield: PropertyKey,\n): unknown {\n\tif (Object.prototype.hasOwnProperty.call(object, field)) {\n\t\t// eslint-disable-next-line security/detect-object-injection\n\t\treturn (object as Record<PropertyKey, unknown>)[field];\n\t}\n\treturn undefined;\n}\n/**\n * Check if a value is an object.\n *\n * @param obj The value to check.\n * @returns True if the value is an object.\n */\nfunction isObject(obj: unknown): obj is Record<PropertyKey, unknown> {\n\treturn !!(obj && typeof obj === \"object\" && !Array.isArray(obj));\n}\n"]}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.compareForSort = compareForSort;
|
|
4
|
+
exports.compareForSortByField = compareForSortByField;
|
|
5
|
+
exports.sortOnField = sortOnField;
|
|
6
|
+
const objects_js_1 = require("./objects.cjs");
|
|
7
|
+
/**
|
|
8
|
+
* Compare two values for sorting
|
|
9
|
+
*
|
|
10
|
+
* @param aValue The first value to compare
|
|
11
|
+
* @param bValue The second value to compare
|
|
12
|
+
* @param ascending Whether to sort ascending
|
|
13
|
+
* @returns The sort order
|
|
14
|
+
* @throws {TypeError} An error if the types are not supported
|
|
15
|
+
*/
|
|
16
|
+
function compareForSort(aValue, bValue, ascending = true) {
|
|
17
|
+
let diff = 0;
|
|
18
|
+
if (aValue && !bValue) {
|
|
19
|
+
diff = 1;
|
|
20
|
+
}
|
|
21
|
+
else if (!aValue && bValue) {
|
|
22
|
+
diff = -1;
|
|
23
|
+
}
|
|
24
|
+
else if (aValue || bValue) {
|
|
25
|
+
if (typeof aValue === "string" && typeof bValue === "string") {
|
|
26
|
+
diff = aValue.localeCompare(bValue);
|
|
27
|
+
}
|
|
28
|
+
else if (typeof aValue === "number" && typeof bValue === "number") {
|
|
29
|
+
diff = aValue - bValue;
|
|
30
|
+
}
|
|
31
|
+
else if (aValue instanceof Date && bValue instanceof Date) {
|
|
32
|
+
diff = aValue.getTime() - bValue.getTime();
|
|
33
|
+
}
|
|
34
|
+
else if (typeof aValue !== "boolean" || typeof bValue !== "boolean") {
|
|
35
|
+
throw new TypeError(`Unsupported types for sorting: A type: "${typeof aValue}" A Value: "${String(aValue)}" B Type: "${typeof bValue}" && B Value: "${String(bValue)}"`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (ascending) {
|
|
39
|
+
return diff;
|
|
40
|
+
}
|
|
41
|
+
return Object.is(-diff, -0) ? 0 : -diff;
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Compare two objects for sorting by a field
|
|
45
|
+
*
|
|
46
|
+
* @template T The type of the objects
|
|
47
|
+
* @param a The first object to compare
|
|
48
|
+
* @param b The second object to compare
|
|
49
|
+
* @param field The field to compare by
|
|
50
|
+
* @param ascending Whether to sort ascending
|
|
51
|
+
* @returns The sort order
|
|
52
|
+
*/
|
|
53
|
+
function compareForSortByField(a, b, field, ascending = true) {
|
|
54
|
+
const valueA = (0, objects_js_1.safeishGetProperty)(a, field);
|
|
55
|
+
const valueB = (0, objects_js_1.safeishGetProperty)(b, field);
|
|
56
|
+
return compareForSort(valueA, valueB, ascending);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Sort an array of objects by a field
|
|
60
|
+
*
|
|
61
|
+
* @template T The type of the objects
|
|
62
|
+
* @param rows The rows to sort
|
|
63
|
+
* @param field The field to sort by
|
|
64
|
+
* @param ascending Whether to sort ascending
|
|
65
|
+
* @returns The sorted rows
|
|
66
|
+
*/
|
|
67
|
+
function sortOnField(rows, field, ascending = true) {
|
|
68
|
+
return rows
|
|
69
|
+
.slice()
|
|
70
|
+
.sort((a, b) => compareForSortByField(a, b, field, ascending));
|
|
71
|
+
}
|
|
72
|
+
//# sourceMappingURL=sorting.js.map
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Compare two values for sorting
|
|
3
|
+
*
|
|
4
|
+
* @param aValue The first value to compare
|
|
5
|
+
* @param bValue The second value to compare
|
|
6
|
+
* @param ascending Whether to sort ascending
|
|
7
|
+
* @returns The sort order
|
|
8
|
+
* @throws {TypeError} An error if the types are not supported
|
|
9
|
+
*/
|
|
10
|
+
export declare function compareForSort(aValue: unknown, bValue: unknown, ascending?: boolean): number;
|
|
11
|
+
/**
|
|
12
|
+
* Compare two objects for sorting by a field
|
|
13
|
+
*
|
|
14
|
+
* @template T The type of the objects
|
|
15
|
+
* @param a The first object to compare
|
|
16
|
+
* @param b The second object to compare
|
|
17
|
+
* @param field The field to compare by
|
|
18
|
+
* @param ascending Whether to sort ascending
|
|
19
|
+
* @returns The sort order
|
|
20
|
+
*/
|
|
21
|
+
export declare function compareForSortByField<T extends object>(a: T, b: T, field: keyof T & string, ascending?: boolean): number;
|
|
22
|
+
/**
|
|
23
|
+
* Sort an array of objects by a field
|
|
24
|
+
*
|
|
25
|
+
* @template T The type of the objects
|
|
26
|
+
* @param rows The rows to sort
|
|
27
|
+
* @param field The field to sort by
|
|
28
|
+
* @param ascending Whether to sort ascending
|
|
29
|
+
* @returns The sorted rows
|
|
30
|
+
*/
|
|
31
|
+
export declare function sortOnField<T extends object>(rows: T[], field: keyof T & string, ascending?: boolean): T[];
|
|
32
|
+
//# sourceMappingURL=sorting.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sorting.d.ts","sourceRoot":"","sources":["../../../src/general/sorting.ts"],"names":[],"mappings":"AAEA;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAC7B,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,OAAO,EACf,SAAS,UAAO,GACd,MAAM,CAwBR;AAED;;;;;;;;;GASG;AACH,wBAAgB,qBAAqB,CAAC,CAAC,SAAS,MAAM,EACrD,CAAC,EAAE,CAAC,EACJ,CAAC,EAAE,CAAC,EACJ,KAAK,EAAE,MAAM,CAAC,GAAG,MAAM,EACvB,SAAS,UAAO,GACd,MAAM,CAIR;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,CAAC,SAAS,MAAM,EAC3C,IAAI,EAAE,CAAC,EAAE,EACT,KAAK,EAAE,MAAM,CAAC,GAAG,MAAM,EACvB,SAAS,UAAO,GACd,CAAC,EAAE,CAIL"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sorting.js","sourceRoot":"","sources":["../../../src/general/sorting.ts"],"names":[],"mappings":";;AAWA,wCA4BC;AAYD,sDASC;AAWD,kCAQC;AA/ED,6CAAkD;AAElD;;;;;;;;GAQG;AACH,SAAgB,cAAc,CAC7B,MAAe,EACf,MAAe,EACf,SAAS,GAAG,IAAI;IAEhB,IAAI,IAAI,GAAG,CAAC,CAAC;IACb,IAAI,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC;QACvB,IAAI,GAAG,CAAC,CAAC;IACV,CAAC;SAAM,IAAI,CAAC,MAAM,IAAI,MAAM,EAAE,CAAC;QAC9B,IAAI,GAAG,CAAC,CAAC,CAAC;IACX,CAAC;SAAM,IAAI,MAAM,IAAI,MAAM,EAAE,CAAC;QAC7B,IAAI,OAAO,MAAM,KAAK,QAAQ,IAAI,OAAO,MAAM,KAAK,QAAQ,EAAE,CAAC;YAC9D,IAAI,GAAG,MAAM,CAAC,aAAa,CAAC,MAAM,CAAC,CAAC;QACrC,CAAC;aAAM,IAAI,OAAO,MAAM,KAAK,QAAQ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qB,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,SAAS,CAAC,CAAC,CAAC;AACjE,CAAC","sourcesContent":["import { safeishGetProperty } from \"./objects.js\";\n\n/**\n * Compare two values for sorting\n *\n * @param aValue The first value to compare\n * @param bValue The second value to compare\n * @param ascending Whether to sort ascending\n * @returns The sort order\n * @throws {TypeError} An error if the types are not supported\n */\nexport function compareForSort(\n\taValue: unknown,\n\tbValue: unknown,\n\tascending = true,\n): number {\n\tlet diff = 0;\n\tif (aValue && !bValue) {\n\t\tdiff = 1;\n\t} else if (!aValue && bValue) {\n\t\tdiff = -1;\n\t} else if (aValue || bValue) {\n\t\tif (typeof aValue === \"string\" && typeof bValue === \"string\") {\n\t\t\tdiff = aValue.localeCompare(bValue);\n\t\t} else if (typeof aValue === \"number\" && typeof bValue === \"number\") {\n\t\t\tdiff = aValue - bValue;\n\t\t} else if (aValue instanceof Date && bValue instanceof Date) {\n\t\t\tdiff = aValue.getTime() - bValue.getTime();\n\t\t} else if (typeof aValue !== \"boolean\" || typeof bValue !== \"boolean\") {\n\t\t\tthrow new TypeError(\n\t\t\t\t`Unsupported types for sorting: A type: \"${typeof aValue}\" A Value: \"${String(aValue)}\" B Type: \"${typeof bValue}\" && B Value: \"${String(bValue)}\"`,\n\t\t\t);\n\t\t}\n\t}\n\n\tif (ascending) {\n\t\treturn diff;\n\t}\n\treturn Object.is(-diff, -0) ? 0 : -diff;\n}\n\n/**\n * Compare two objects for sorting by a field\n *\n * @template T The type of the objects\n * @param a The first object to compare\n * @param b The second object to compare\n * @param field The field to compare by\n * @param ascending Whether to sort ascending\n * @returns The sort order\n */\nexport function compareForSortByField<T extends object>(\n\ta: T,\n\tb: T,\n\tfield: keyof T & string,\n\tascending = true,\n): number {\n\tconst valueA = safeishGetProperty(a, field);\n\tconst valueB = safeishGetProperty(b, field);\n\treturn compareForSort(valueA, valueB, ascending);\n}\n\n/**\n * Sort an array of objects by a field\n *\n * @template T The type of the objects\n * @param rows The rows to sort\n * @param field The field to sort by\n * @param ascending Whether to sort ascending\n * @returns The sorted rows\n */\nexport function sortOnField<T extends object>(\n\trows: T[],\n\tfield: keyof T & string,\n\tascending = true,\n): T[] {\n\treturn rows\n\t\t.slice()\n\t\t.sort((a, b) => compareForSortByField(a, b, field, ascending));\n}\n"]}
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.capitalizeString = capitalizeString;
|
|
4
|
+
exports.decodeHTMLEntities = decodeHTMLEntities;
|
|
5
|
+
exports.toTitleCase = toTitleCase;
|
|
6
|
+
const namedEntities_js_1 = require("./namedEntities.cjs");
|
|
7
|
+
/**
|
|
8
|
+
* Capitalizes a string.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* const str = capitalizeString("hello world!");
|
|
12
|
+
* console.log(str);
|
|
13
|
+
* // Hello world!
|
|
14
|
+
*
|
|
15
|
+
* @param str The string to capitalize.
|
|
16
|
+
* @returns The capitalized string.
|
|
17
|
+
*/
|
|
18
|
+
function capitalizeString(str) {
|
|
19
|
+
return (str[0] ?? "").toUpperCase() + str.slice(1);
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Decode HTML entities in a string.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* decodeHTMLEntities("That's cool!"); // => "That's cool!"
|
|
26
|
+
* decodeHTMLEntities("<div>"); // => "<div>" Note that this should be used with caution, as it can be a security risk.
|
|
27
|
+
*
|
|
28
|
+
* @param string The string to decode
|
|
29
|
+
* @returns The decoded string
|
|
30
|
+
*/
|
|
31
|
+
function decodeHTMLEntities(string) {
|
|
32
|
+
let str = string.replace(/&#(\d+);/g, (_, dec) => {
|
|
33
|
+
return String.fromCharCode(dec);
|
|
34
|
+
});
|
|
35
|
+
for (const [key, value] of Object.entries(namedEntities_js_1.namedEntities)) {
|
|
36
|
+
// From an internal source.
|
|
37
|
+
// eslint-disable-next-line security/detect-non-literal-regexp
|
|
38
|
+
str = str.replace(new RegExp(`&${key};`, "g"), value);
|
|
39
|
+
}
|
|
40
|
+
return str;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Converts a string to title-case.
|
|
44
|
+
*
|
|
45
|
+
* @param string The text to convert to title-case
|
|
46
|
+
* @returns The text in title-case
|
|
47
|
+
*/
|
|
48
|
+
function toTitleCase(string) {
|
|
49
|
+
if (string.length === 0) {
|
|
50
|
+
return string;
|
|
51
|
+
}
|
|
52
|
+
return handleSplitter(handleSplitter(string.toLowerCase(), " "), "\n");
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Converts a string to sentence-case, for a given splitter.
|
|
56
|
+
*
|
|
57
|
+
* @param string The text to convert to sentence-case
|
|
58
|
+
* @param splitter The character to split the string by
|
|
59
|
+
* @returns The text in sentence-case
|
|
60
|
+
*/
|
|
61
|
+
function handleSplitter(string, splitter) {
|
|
62
|
+
const sentence = string.split(splitter);
|
|
63
|
+
for (let i = 0; i < sentence.length; i++) {
|
|
64
|
+
// eslint-disable-next-line security/detect-object-injection
|
|
65
|
+
if (!sentence[i]?.length) {
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
// eslint-disable-next-line security/detect-object-injection, @typescript-eslint/no-non-null-assertion
|
|
69
|
+
sentence[i] = capitalizeString(sentence[i]);
|
|
70
|
+
}
|
|
71
|
+
return sentence.join(splitter);
|
|
72
|
+
}
|
|
73
|
+
//# sourceMappingURL=string.js.map
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Capitalizes a string.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* const str = capitalizeString("hello world!");
|
|
6
|
+
* console.log(str);
|
|
7
|
+
* // Hello world!
|
|
8
|
+
*
|
|
9
|
+
* @param str The string to capitalize.
|
|
10
|
+
* @returns The capitalized string.
|
|
11
|
+
*/
|
|
12
|
+
export declare function capitalizeString(str: string): string;
|
|
13
|
+
/**
|
|
14
|
+
* Decode HTML entities in a string.
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* decodeHTMLEntities("That's cool!"); // => "That's cool!"
|
|
18
|
+
* decodeHTMLEntities("<div>"); // => "<div>" Note that this should be used with caution, as it can be a security risk.
|
|
19
|
+
*
|
|
20
|
+
* @param string The string to decode
|
|
21
|
+
* @returns The decoded string
|
|
22
|
+
*/
|
|
23
|
+
export declare function decodeHTMLEntities(string: string): string;
|
|
24
|
+
/**
|
|
25
|
+
* Converts a string to title-case.
|
|
26
|
+
*
|
|
27
|
+
* @param string The text to convert to title-case
|
|
28
|
+
* @returns The text in title-case
|
|
29
|
+
*/
|
|
30
|
+
export declare function toTitleCase(string: string): string;
|
|
31
|
+
//# sourceMappingURL=string.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"string.d.ts","sourceRoot":"","sources":["../../../src/general/string.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;GAUG;AACH,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAEpD;AAED;;;;;;;;;GASG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAUzD;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,GAAG,MAAM,CAKlD"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"string.js","sourceRoot":"","sources":["../../../src/general/string.ts"],"names":[],"mappings":";;AAaA,4CAEC;AAYD,gDAUC;AAQD,kCAKC;AAlDD,yDAAmD;AAEnD;;;;;;;;;;GAUG;AACH,SAAgB,gBAAgB,CAAC,GAAW;IAC3C,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,IAAI,EAAE,CAAC,CAAC,WAAW,EAAE,GAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;AACpD,CAAC;AAED;;;;;;;;;GASG;AACH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IAAI,CAAC,QAAQ,CAAC,CAAC;AAChC,CAAC","sourcesContent":["import { namedEntities } from \"./namedEntities.js\";\n\n/**\n * Capitalizes a string.\n *\n * @example\n * \tconst str = capitalizeString(\"hello world!\");\n * \tconsole.log(str);\n * \t// Hello world!\n *\n * @param str The string to capitalize.\n * @returns The capitalized string.\n */\nexport function capitalizeString(str: string): string {\n\treturn (str[0] ?? \"\").toUpperCase() + str.slice(1);\n}\n\n/**\n * Decode HTML entities in a string.\n *\n * @example\n * \tdecodeHTMLEntities(\"That's cool!\"); // => \"That's cool!\"\n * \tdecodeHTMLEntities(\"<div>\"); // => \"<div>\" Note that this should be used with caution, as it can be a security risk.\n *\n * @param string The string to decode\n * @returns The decoded string\n */\nexport function decodeHTMLEntities(string: string): string {\n\tlet str = string.replace(/&#(\\d+);/g, (_, dec: number) => {\n\t\treturn String.fromCharCode(dec);\n\t});\n\tfor (const [key, value] of Object.entries(namedEntities)) {\n\t\t// From an internal source.\n\t\t// eslint-disable-next-line security/detect-non-literal-regexp\n\t\tstr = str.replace(new RegExp(`&${key};`, \"g\"), value);\n\t}\n\treturn str;\n}\n\n/**\n * Converts a string to title-case.\n *\n * @param string The text to convert to title-case\n * @returns The text in title-case\n */\nexport function toTitleCase(string: string): string {\n\tif (string.length === 0) {\n\t\treturn string;\n\t}\n\treturn handleSplitter(handleSplitter(string.toLowerCase(), \" \"), \"\\n\");\n}\n\n/**\n * Converts a string to sentence-case, for a given splitter.\n *\n * @param string The text to convert to sentence-case\n * @param splitter The character to split the string by\n * @returns The text in sentence-case\n */\nfunction handleSplitter(string: string, splitter: string) {\n\tconst sentence = string.split(splitter);\n\tfor (let i = 0; i < sentence.length; i++) {\n\t\t// eslint-disable-next-line security/detect-object-injection\n\t\tif (!sentence[i]?.length) {\n\t\t\tcontinue;\n\t\t}\n\t\t// eslint-disable-next-line security/detect-object-injection, @typescript-eslint/no-non-null-assertion\n\t\tsentence[i] = capitalizeString(sentence[i]!);\n\t}\n\treturn sentence.join(splitter);\n}\n"]}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.waitForTimeout = waitForTimeout;
|
|
4
|
+
/**
|
|
5
|
+
* Wait for a set amount of time.
|
|
6
|
+
*
|
|
7
|
+
* @param timeout The amount of time to wait.
|
|
8
|
+
* @returns A promise that resolves after the timeout.
|
|
9
|
+
*/
|
|
10
|
+
function waitForTimeout(timeout) {
|
|
11
|
+
return new Promise((resolve) => {
|
|
12
|
+
setTimeout(() => {
|
|
13
|
+
resolve(true);
|
|
14
|
+
}, timeout);
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
//# sourceMappingURL=timeout.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeout.d.ts","sourceRoot":"","sources":["../../../src/general/timeout.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAM7D"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"timeout.js","sourceRoot":"","sources":["../../../src/general/timeout.ts"],"names":[],"mappings":";;AAMA,wCAMC;AAZD;;;;;GAKG;AACH,SAAgB,cAAc,CAAC,OAAe;IAC7C,OAAO,IAAI,OAAO,CAAO,CAAC,OAAO,EAAE,EAAE;QACpC,UAAU,CAAC,GAAG,EAAE;YACf,OAAO,CAAC,IAAI,CAAC,CAAC;QACf,CAAC,EAAE,OAAO,CAAC,CAAC;IACb,CAAC,CAAC,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Wait for a set amount of time.\n *\n * @param timeout The amount of time to wait.\n * @returns A promise that resolves after the timeout.\n */\nexport function waitForTimeout(timeout: number): Promise<true> {\n\treturn new Promise<true>((resolve) => {\n\t\tsetTimeout(() => {\n\t\t\tresolve(true);\n\t\t}, timeout);\n\t});\n}\n"]}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./browser/index.cjs"), exports);
|
|
18
|
+
__exportStar(require("./general/index.cjs"), exports);
|
|
19
|
+
__exportStar(require("./node/index.cjs"), exports);
|
|
20
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC;AACnC,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,qDAAmC;AACnC,qDAAmC;AACnC,kDAAgC","sourcesContent":["export * from \"./browser/index.js\";\nexport * from \"./general/index.js\";\nexport * from \"./node/index.js\";\n"]}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.checkEnvironmentVariables = checkEnvironmentVariables;
|
|
4
|
+
const objects_js_1 = require("../general/objects.cjs");
|
|
5
|
+
/**
|
|
6
|
+
* Checks if the expected environment variables are set.
|
|
7
|
+
*
|
|
8
|
+
* @template TKey An expected environment key.
|
|
9
|
+
* @param expected The expected environment variables.
|
|
10
|
+
* @returns A key, value set of the environment variables requested for
|
|
11
|
+
* validation.
|
|
12
|
+
* @throws {TypeError} If an expected environment variable hasn't been set.
|
|
13
|
+
*/
|
|
14
|
+
function checkEnvironmentVariables(expected) {
|
|
15
|
+
const env = process.env.NODE_ENV;
|
|
16
|
+
const result = {};
|
|
17
|
+
for (const variable of expected) {
|
|
18
|
+
const key = (typeof variable === "string" ? variable : variable.name);
|
|
19
|
+
const value = (0, objects_js_1.safeishGetProperty)(process.env, key);
|
|
20
|
+
if (!value) {
|
|
21
|
+
const message = typeof variable === "string"
|
|
22
|
+
? `Environment variable ${variable} is not set in ${String(env)} environment`
|
|
23
|
+
: variable.message;
|
|
24
|
+
throw new TypeError(message);
|
|
25
|
+
}
|
|
26
|
+
result[key] = value;
|
|
27
|
+
}
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type ExpectedEnvironmentSuccess<TKey extends string> = Record<ExtractVariableNames<ExpectedEnvironmentVariables<TKey>>, string>;
|
|
2
|
+
/**
|
|
3
|
+
* The expected environment variables.
|
|
4
|
+
*
|
|
5
|
+
* @template TKey An expected environment key.
|
|
6
|
+
*/
|
|
7
|
+
export type ExpectedEnvironmentVariables<TKey extends string> = TKey[] | {
|
|
8
|
+
message: string;
|
|
9
|
+
name: TKey;
|
|
10
|
+
}[];
|
|
11
|
+
type ExtractVariableNames<T extends ExpectedEnvironmentVariables<string>> = T extends string[] ? T[number] : T extends {
|
|
12
|
+
name: infer N;
|
|
13
|
+
}[] ? N extends string ? N : never : never;
|
|
14
|
+
/**
|
|
15
|
+
* Checks if the expected environment variables are set.
|
|
16
|
+
*
|
|
17
|
+
* @template TKey An expected environment key.
|
|
18
|
+
* @param expected The expected environment variables.
|
|
19
|
+
* @returns A key, value set of the environment variables requested for
|
|
20
|
+
* validation.
|
|
21
|
+
* @throws {TypeError} If an expected environment variable hasn't been set.
|
|
22
|
+
*/
|
|
23
|
+
export declare function checkEnvironmentVariables<TKey extends string>(expected: ExpectedEnvironmentVariables<TKey>): ExpectedEnvironmentSuccess<TKey>;
|
|
24
|
+
export {};
|
|
25
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/node/index.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,0BAA0B,CAAC,IAAI,SAAS,MAAM,IAAI,MAAM,CACnE,oBAAoB,CAAC,4BAA4B,CAAC,IAAI,CAAC,CAAC,EACxD,MAAM,CACN,CAAC;AAEF;;;;GAIG;AACH,MAAM,MAAM,4BAA4B,CAAC,IAAI,SAAS,MAAM,IACzD,IAAI,EAAE,GACN;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,IAAI,CAAA;CAAE,EAAE,CAAC;AAErC,KAAK,oBAAoB,CAAC,CAAC,SAAS,4BAA4B,CAAC,MAAM,CAAC,IACvE,CAAC,SAAS,MAAM,EAAE,GACf,CAAC,CAAC,MAAM,CAAC,GACT,CAAC,SAAS;IAAE,IAAI,EAAE,MAAM,CAAC,CAAA;CAAE,EAAE,GAC5B,CAAC,SAAS,MAAM,GACf,CAAC,GACD,KAAK,GACN,KAAK,CAAC;AACX;;;;;;;;GAQG;AACH,wBAAgB,yBAAyB,CAAC,IAAI,SAAS,MAAM,EAC5D,QAAQ,EAAE,4BAA4B,CAAC,IAAI,CAAC,GAC1C,0BAA0B,CAAC,IAAI,CAAC,CAkBlC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/node/index.ts"],"names":[],"mappings":";;AAiCA,8DAoBC;AArDD,sDAA2D;AAwB3D;;;;;;;;GAQG;AACH,SAAgB,yBAAyB,CACxC,QAA4C;IAE5C,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,CAAC;IACjC,MAAM,MAAM,GAA8C,EAAE,CAAC;IAC7D,KAAK,MAAM,QAAQ,IAAI,QAAQ,EAAE,CAAC;QACjC,MAAM,GAAG,GAAG,CACX,OAAO,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,CAAC,CAAC,QAAQ,CAAC,IAAI,CACd,CAAC;QAC3C,MAAM,KAAK,GAAG,IAAA,+BAAkB,EAAC,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QACnD,IAAI,CAAC,KAAK,EAAE,CAAC;YACZ,MAAM,OAAO,GACZ,OAAO,QAAQ,KAAK,QAAQ;gBAC3B,CAAC,CAAC,wBAAwB,QAAQ,kBAAkB,MAAM,CAAC,GAAG,CAAC,cAAc;gBAC7E,CAAC,CAAC,QAAQ,CAAC,OAAO,CAAC;YACrB,MAAM,IAAI,SAAS,CAAC,OAAO,CAAC,CAAC;QAC9B,CAAC;QACD,MAAM,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IACrB,CAAC;IACD,OAAO,MAA0C,CAAC;AACnD,CAAC","sourcesContent":["import { safeishGetProperty } from \"../general/objects.js\";\n\nexport type ExpectedEnvironmentSuccess<TKey extends string> = Record<\n\tExtractVariableNames<ExpectedEnvironmentVariables<TKey>>,\n\tstring\n>;\n\n/**\n * The expected environment variables.\n *\n * @template TKey An expected environment key.\n */\nexport type ExpectedEnvironmentVariables<TKey extends string> =\n\t| TKey[]\n\t| { message: string; name: TKey }[];\n\ntype ExtractVariableNames<T extends ExpectedEnvironmentVariables<string>> =\n\tT extends string[]\n\t\t? T[number]\n\t\t: T extends { name: infer N }[]\n\t\t\t? N extends string\n\t\t\t\t? N\n\t\t\t\t: never\n\t\t\t: never;\n/**\n * Checks if the expected environment variables are set.\n *\n * @template TKey An expected environment key.\n * @param expected The expected environment variables.\n * @returns A key, value set of the environment variables requested for\n * validation.\n * @throws {TypeError} If an expected environment variable hasn't been set.\n */\nexport function checkEnvironmentVariables<TKey extends string>(\n\texpected: ExpectedEnvironmentVariables<TKey>,\n): ExpectedEnvironmentSuccess<TKey> {\n\tconst env = process.env.NODE_ENV;\n\tconst result: Partial<ExpectedEnvironmentSuccess<TKey>> = {};\n\tfor (const variable of expected) {\n\t\tconst key = (\n\t\t\ttypeof variable === \"string\" ? variable : variable.name\n\t\t) as ExtractVariableNames<typeof expected>;\n\t\tconst value = safeishGetProperty(process.env, key);\n\t\tif (!value) {\n\t\t\tconst message =\n\t\t\t\ttypeof variable === \"string\"\n\t\t\t\t\t? `Environment variable ${variable} is not set in ${String(env)} environment`\n\t\t\t\t\t: variable.message;\n\t\t\tthrow new TypeError(message);\n\t\t}\n\t\tresult[key] = value;\n\t}\n\treturn result as ExpectedEnvironmentSuccess<TKey>;\n}\n"]}
|