yummies 7.10.0 → 7.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/async.cjs +162 -48
- package/async.cjs.map +1 -1
- package/async.d.ts +112 -13
- package/async.js +163 -54
- package/async.js.map +1 -1
- package/chunk-CVq3Gv4J.cjs +50 -0
- package/chunk-YKewjYmz.js +37 -0
- package/common.cjs +48 -8
- package/common.cjs.map +1 -1
- package/common.d.ts +35 -2
- package/common.js +49 -11
- package/common.js.map +1 -1
- package/complex.cjs +275 -104
- package/complex.cjs.map +1 -1
- package/complex.d.ts +133 -13
- package/complex.js +275 -109
- package/complex.js.map +1 -1
- package/cookie.cjs +17 -7
- package/cookie.cjs.map +1 -1
- package/cookie.d.ts +8 -0
- package/cookie.js +18 -9
- package/cookie.js.map +1 -1
- package/css.cjs +147 -39
- package/css.cjs.map +1 -1
- package/css.d.ts +98 -6
- package/css.js +143 -41
- package/css.js.map +1 -1
- package/data.cjs +90 -55
- package/data.cjs.map +1 -1
- package/data.d.ts +32 -0
- package/data.js +91 -61
- package/data.js.map +1 -1
- package/date-time.cjs +578 -412
- package/date-time.cjs.map +1 -1
- package/date-time.d.ts +88 -0
- package/date-time.js +575 -421
- package/date-time.js.map +1 -1
- package/device.cjs +48 -23
- package/device.cjs.map +1 -1
- package/device.d.ts +32 -0
- package/device.js +49 -31
- package/device.js.map +1 -1
- package/encodings.cjs +275 -266
- package/encodings.cjs.map +1 -1
- package/encodings.d.ts +8 -0
- package/encodings.js +276 -268
- package/encodings.js.map +1 -1
- package/errors.cjs +20 -18
- package/errors.cjs.map +1 -1
- package/errors.js +19 -19
- package/errors.js.map +1 -1
- package/file.cjs +42 -24
- package/file.cjs.map +1 -1
- package/file.d.ts +16 -0
- package/file.js +43 -27
- package/file.js.map +1 -1
- package/format.cjs +125 -83
- package/format.cjs.map +1 -1
- package/format.d.ts +89 -4
- package/format.js +118 -82
- package/format.js.map +1 -1
- package/html.cjs +226 -137
- package/html.cjs.map +1 -1
- package/html.d.ts +67 -3
- package/html.js +223 -150
- package/html.js.map +1 -1
- package/id.cjs +74 -17
- package/id.cjs.map +1 -1
- package/id.d.ts +10 -10
- package/id.js +73 -24
- package/id.js.map +1 -1
- package/imports.cjs +41 -29
- package/imports.cjs.map +1 -1
- package/imports.d.ts +13 -4
- package/imports.js +40 -31
- package/imports.js.map +1 -1
- package/math.cjs +32 -6
- package/math.cjs.map +1 -1
- package/math.d.ts +17 -1
- package/math.js +33 -10
- package/math.js.map +1 -1
- package/media.cjs +275 -84
- package/media.cjs.map +1 -1
- package/media.d.ts +188 -2
- package/media.js +274 -93
- package/media.js.map +1 -1
- package/mobx.cjs +353 -193
- package/mobx.cjs.map +1 -1
- package/mobx.d.ts +172 -6
- package/mobx.js +351 -200
- package/mobx.js.map +1 -1
- package/ms.cjs +21 -10
- package/ms.cjs.map +1 -1
- package/ms.d.ts +1 -1
- package/ms.js +22 -13
- package/ms.js.map +1 -1
- package/number.cjs +13 -7
- package/number.cjs.map +1 -1
- package/number.js +14 -9
- package/number.js.map +1 -1
- package/package.json +13 -5
- package/parser.cjs +117 -64
- package/parser.cjs.map +1 -1
- package/parser.d.ts +63 -0
- package/parser.js +111 -64
- package/parser.js.map +1 -1
- package/price.cjs +24 -18
- package/price.cjs.map +1 -1
- package/price.d.ts +8 -0
- package/price.js +25 -20
- package/price.js.map +1 -1
- package/random.cjs +79 -13
- package/random.cjs.map +1 -1
- package/random.d.ts +64 -0
- package/random.js +80 -22
- package/random.js.map +1 -1
- package/react.cjs +673 -214
- package/react.cjs.map +1 -1
- package/react.d.ts +428 -9
- package/react.js +674 -239
- package/react.js.map +1 -1
- package/sound.cjs +14 -9
- package/sound.cjs.map +1 -1
- package/sound.d.ts +1 -1
- package/sound.js +15 -11
- package/sound.js.map +1 -1
- package/storage.cjs +49 -50
- package/storage.cjs.map +1 -1
- package/storage.d.ts +15 -6
- package/storage.js +50 -53
- package/storage.js.map +1 -1
- package/text.cjs +51 -34
- package/text.cjs.map +1 -1
- package/text.d.ts +5 -5
- package/text.js +52 -37
- package/text.js.map +1 -1
- package/type-guard.cjs +292 -72
- package/type-guard.cjs.map +1 -1
- package/type-guard.d.ts +199 -42
- package/type-guard.js +288 -73
- package/type-guard.js.map +1 -1
- package/types.cjs +0 -2
- package/types.global.cjs +0 -2
- package/types.global.js +0 -2
- package/types.js +0 -2
- package/vibrate.cjs +31 -6
- package/vibrate.cjs.map +1 -1
- package/vibrate.d.ts +23 -1
- package/vibrate.js +32 -8
- package/vibrate.js.map +1 -1
- package/types.cjs.map +0 -1
- package/types.global.cjs.map +0 -1
- package/types.global.js.map +0 -1
- package/types.js.map +0 -1
package/ms.cjs
CHANGED
|
@@ -1,14 +1,25 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
//#region src/ms.ts
|
|
3
|
+
var unitsToMs = {
|
|
4
|
+
ms: 1,
|
|
5
|
+
sec: 1e3,
|
|
6
|
+
min: 1e3 * 60,
|
|
7
|
+
hour: 1e3 * 60 * 60,
|
|
8
|
+
day: 1e3 * 60 * 60 * 24,
|
|
9
|
+
week: 1e3 * 60 * 60 * 24 * 7
|
|
10
10
|
};
|
|
11
|
-
|
|
11
|
+
/**
|
|
12
|
+
* Converts a value in the specified unit to milliseconds.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* ms(1, 'min') // 60_000
|
|
17
|
+
* ms(30, 'sec') // 30_000
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
var ms = (value, unit = "ms") => value * unitsToMs[unit];
|
|
21
|
+
//#endregion
|
|
12
22
|
exports.ms = ms;
|
|
13
23
|
exports.unitsToMs = unitsToMs;
|
|
14
|
-
|
|
24
|
+
|
|
25
|
+
//# sourceMappingURL=ms.cjs.map
|
package/ms.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ms.cjs","sources":["../src/ms.ts"],"sourcesContent":["export const unitsToMs = {\n ms: 1,\n sec: 1000,\n min: 1000 * 60,\n hour: 1000 * 60 * 60,\n day: 1000 * 60 * 60 * 24,\n week: 1000 * 60 * 60 * 24 * 7,\n} as const;\n\n/**\n *
|
|
1
|
+
{"version":3,"file":"ms.cjs","names":[],"sources":["../src/ms.ts"],"sourcesContent":["export const unitsToMs = {\n ms: 1,\n sec: 1000,\n min: 1000 * 60,\n hour: 1000 * 60 * 60,\n day: 1000 * 60 * 60 * 24,\n week: 1000 * 60 * 60 * 24 * 7,\n} as const;\n\n/**\n * Converts a value in the specified unit to milliseconds.\n *\n * @example\n * ```ts\n * ms(1, 'min') // 60_000\n * ms(30, 'sec') // 30_000\n * ```\n */\nexport const ms = (value: number, unit: keyof typeof unitsToMs = 'ms') =>\n value * unitsToMs[unit];\n"],"mappings":";;AAAA,IAAa,YAAY;CACvB,IAAI;CACJ,KAAK;CACL,KAAK,MAAO;CACZ,MAAM,MAAO,KAAK;CAClB,KAAK,MAAO,KAAK,KAAK;CACtB,MAAM,MAAO,KAAK,KAAK,KAAK;CAC7B;;;;;;;;;;AAWD,IAAa,MAAM,OAAe,OAA+B,SAC/D,QAAQ,UAAU"}
|
package/ms.d.ts
CHANGED
package/ms.js
CHANGED
|
@@ -1,14 +1,23 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
//#region src/ms.ts
|
|
2
|
+
var unitsToMs = {
|
|
3
|
+
ms: 1,
|
|
4
|
+
sec: 1e3,
|
|
5
|
+
min: 1e3 * 60,
|
|
6
|
+
hour: 1e3 * 60 * 60,
|
|
7
|
+
day: 1e3 * 60 * 60 * 24,
|
|
8
|
+
week: 1e3 * 60 * 60 * 24 * 7
|
|
8
9
|
};
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
/**
|
|
11
|
+
* Converts a value in the specified unit to milliseconds.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* ms(1, 'min') // 60_000
|
|
16
|
+
* ms(30, 'sec') // 30_000
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
var ms = (value, unit = "ms") => value * unitsToMs[unit];
|
|
20
|
+
//#endregion
|
|
21
|
+
export { ms, unitsToMs };
|
|
22
|
+
|
|
23
|
+
//# sourceMappingURL=ms.js.map
|
package/ms.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ms.js","sources":["../src/ms.ts"],"sourcesContent":["export const unitsToMs = {\n ms: 1,\n sec: 1000,\n min: 1000 * 60,\n hour: 1000 * 60 * 60,\n day: 1000 * 60 * 60 * 24,\n week: 1000 * 60 * 60 * 24 * 7,\n} as const;\n\n/**\n *
|
|
1
|
+
{"version":3,"file":"ms.js","names":[],"sources":["../src/ms.ts"],"sourcesContent":["export const unitsToMs = {\n ms: 1,\n sec: 1000,\n min: 1000 * 60,\n hour: 1000 * 60 * 60,\n day: 1000 * 60 * 60 * 24,\n week: 1000 * 60 * 60 * 24 * 7,\n} as const;\n\n/**\n * Converts a value in the specified unit to milliseconds.\n *\n * @example\n * ```ts\n * ms(1, 'min') // 60_000\n * ms(30, 'sec') // 30_000\n * ```\n */\nexport const ms = (value: number, unit: keyof typeof unitsToMs = 'ms') =>\n value * unitsToMs[unit];\n"],"mappings":";AAAA,IAAa,YAAY;CACvB,IAAI;CACJ,KAAK;CACL,KAAK,MAAO;CACZ,MAAM,MAAO,KAAK;CAClB,KAAK,MAAO,KAAK,KAAK;CACtB,MAAM,MAAO,KAAK,KAAK,KAAK;CAC7B;;;;;;;;;;AAWD,IAAa,MAAM,OAAe,OAA+B,SAC/D,QAAQ,UAAU"}
|
package/number.cjs
CHANGED
|
@@ -1,11 +1,17 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/number.ts
|
|
3
|
+
/**
|
|
4
|
+
* Works like `parseFloat(number.toFixed(4))` but performance better
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* round(191.212999999999999999999999, 4) // 191.213
|
|
8
|
+
*/
|
|
3
9
|
function round(value, decimalPlaces = 0) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const factor = 10 ** decimalPlaces;
|
|
8
|
-
return Math.round(value * factor) / factor;
|
|
10
|
+
if (!decimalPlaces) return Math.round(value);
|
|
11
|
+
const factor = 10 ** decimalPlaces;
|
|
12
|
+
return Math.round(value * factor) / factor;
|
|
9
13
|
}
|
|
14
|
+
//#endregion
|
|
10
15
|
exports.round = round;
|
|
11
|
-
|
|
16
|
+
|
|
17
|
+
//# sourceMappingURL=number.cjs.map
|
package/number.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"number.cjs","sources":["../src/number.ts"],"sourcesContent":["/**\n * Works like `parseFloat(number.toFixed(4))` but performance better\n *\n * @example\n * round(191.212999999999999999999999, 4) // 191.213\n */\nexport function round(value: number, decimalPlaces: number = 0): number {\n if (!decimalPlaces) {\n return Math.round(value);\n }\n\n const factor = 10 ** decimalPlaces;\n return Math.round(value * factor) / factor;\n}\n"],"
|
|
1
|
+
{"version":3,"file":"number.cjs","names":[],"sources":["../src/number.ts"],"sourcesContent":["/**\n * Works like `parseFloat(number.toFixed(4))` but performance better\n *\n * @example\n * round(191.212999999999999999999999, 4) // 191.213\n */\nexport function round(value: number, decimalPlaces: number = 0): number {\n if (!decimalPlaces) {\n return Math.round(value);\n }\n\n const factor = 10 ** decimalPlaces;\n return Math.round(value * factor) / factor;\n}\n"],"mappings":";;;;;;;;AAMA,SAAgB,MAAM,OAAe,gBAAwB,GAAW;AACtE,KAAI,CAAC,cACH,QAAO,KAAK,MAAM,MAAM;CAG1B,MAAM,SAAS,MAAM;AACrB,QAAO,KAAK,MAAM,QAAQ,OAAO,GAAG"}
|
package/number.js
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
|
+
//#region src/number.ts
|
|
2
|
+
/**
|
|
3
|
+
* Works like `parseFloat(number.toFixed(4))` but performance better
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* round(191.212999999999999999999999, 4) // 191.213
|
|
7
|
+
*/
|
|
1
8
|
function round(value, decimalPlaces = 0) {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const factor = 10 ** decimalPlaces;
|
|
6
|
-
return Math.round(value * factor) / factor;
|
|
9
|
+
if (!decimalPlaces) return Math.round(value);
|
|
10
|
+
const factor = 10 ** decimalPlaces;
|
|
11
|
+
return Math.round(value * factor) / factor;
|
|
7
12
|
}
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
//# sourceMappingURL=number.js.map
|
|
13
|
+
//#endregion
|
|
14
|
+
export { round };
|
|
15
|
+
|
|
16
|
+
//# sourceMappingURL=number.js.map
|
package/number.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"number.js","sources":["../src/number.ts"],"sourcesContent":["/**\n * Works like `parseFloat(number.toFixed(4))` but performance better\n *\n * @example\n * round(191.212999999999999999999999, 4) // 191.213\n */\nexport function round(value: number, decimalPlaces: number = 0): number {\n if (!decimalPlaces) {\n return Math.round(value);\n }\n\n const factor = 10 ** decimalPlaces;\n return Math.round(value * factor) / factor;\n}\n"],"
|
|
1
|
+
{"version":3,"file":"number.js","names":[],"sources":["../src/number.ts"],"sourcesContent":["/**\n * Works like `parseFloat(number.toFixed(4))` but performance better\n *\n * @example\n * round(191.212999999999999999999999, 4) // 191.213\n */\nexport function round(value: number, decimalPlaces: number = 0): number {\n if (!decimalPlaces) {\n return Math.round(value);\n }\n\n const factor = 10 ** decimalPlaces;\n return Math.round(value * factor) / factor;\n}\n"],"mappings":";;;;;;;AAMA,SAAgB,MAAM,OAAe,gBAAwB,GAAW;AACtE,KAAI,CAAC,cACH,QAAO,KAAK,MAAM,MAAM;CAG1B,MAAM,SAAS,MAAM;AACrB,QAAO,KAAK,MAAM,QAAQ,OAAO,GAAG"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "yummies",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.12.0",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"javascript",
|
|
6
6
|
"typescript",
|
|
@@ -33,10 +33,10 @@
|
|
|
33
33
|
"dependencies": {
|
|
34
34
|
"class-variance-authority": "^0.7.1",
|
|
35
35
|
"clsx": "^2.1.1",
|
|
36
|
-
"dayjs": "^1.11.
|
|
37
|
-
"dompurify": "^3.
|
|
38
|
-
"nanoid": "^5.1.
|
|
39
|
-
"tailwind-merge": "^3.
|
|
36
|
+
"dayjs": "^1.11.20",
|
|
37
|
+
"dompurify": "^3.3.3",
|
|
38
|
+
"nanoid": "^5.1.7",
|
|
39
|
+
"tailwind-merge": "^3.5.0"
|
|
40
40
|
},
|
|
41
41
|
"type": "module",
|
|
42
42
|
"exports": {
|
|
@@ -46,6 +46,14 @@
|
|
|
46
46
|
"require": "./async.cjs",
|
|
47
47
|
"default": "./async.js"
|
|
48
48
|
},
|
|
49
|
+
"./chunk-CVq3Gv4J": {
|
|
50
|
+
"require": "./chunk-CVq3Gv4J.cjs",
|
|
51
|
+
"default": "./chunk-CVq3Gv4J.cjs"
|
|
52
|
+
},
|
|
53
|
+
"./chunk-YKewjYmz": {
|
|
54
|
+
"import": "./chunk-YKewjYmz.js",
|
|
55
|
+
"default": "./chunk-YKewjYmz.js"
|
|
56
|
+
},
|
|
49
57
|
"./common": {
|
|
50
58
|
"types": "./common.d.ts",
|
|
51
59
|
"import": "./common.js",
|
package/parser.cjs
CHANGED
|
@@ -1,69 +1,122 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
const
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
2
|
+
const require_chunk = require("./chunk-CVq3Gv4J.cjs");
|
|
3
|
+
let yummies_type_guard = require("yummies/type-guard");
|
|
4
|
+
let yummies_format = require("yummies/format");
|
|
5
|
+
//#region src/parser/number.ts
|
|
6
|
+
/**
|
|
7
|
+
* Parses a number from raw input and optionally clamps, rounds or limits
|
|
8
|
+
* fractional digits.
|
|
9
|
+
*
|
|
10
|
+
* Strings are normalized by removing spaces and replacing `,` with `.` before
|
|
11
|
+
* parsing. Invalid inputs return the configured fallback.
|
|
12
|
+
*
|
|
13
|
+
* @template TFallback Fallback value type returned when parsing fails.
|
|
14
|
+
* @param input Raw value to parse.
|
|
15
|
+
* @param userSettings Parser settings merged with `number.defaultSettings`.
|
|
16
|
+
* @returns Parsed number or fallback value.
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```ts
|
|
20
|
+
* number('1 234,5'); // 1234.5
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* number('bad', { fallback: 0 }); // 0
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
var number = (input, userSettings) => {
|
|
29
|
+
const settings = {
|
|
30
|
+
...number.defaultSettings,
|
|
31
|
+
...userSettings
|
|
32
|
+
};
|
|
33
|
+
const fallback = "fallback" in settings ? settings.fallback : 0;
|
|
34
|
+
let result;
|
|
35
|
+
if (yummies_type_guard.typeGuard.isNumber(input)) result = input;
|
|
36
|
+
else if (yummies_type_guard.typeGuard.isString(input)) {
|
|
37
|
+
const formattedInput = yummies_format.format.skipSpaces(input).replace(",", ".");
|
|
38
|
+
if (formattedInput === "") result = fallback;
|
|
39
|
+
else result = Number(formattedInput);
|
|
40
|
+
} else result = fallback;
|
|
41
|
+
if (yummies_type_guard.typeGuard.isNumber(result)) {
|
|
42
|
+
if (settings?.clamped != null) result = Math.max(settings.clamped[0] ?? -Infinity, Math.min(result, settings.clamped[1] ?? Infinity));
|
|
43
|
+
if (settings?.ceil != null) result = Math.ceil(result);
|
|
44
|
+
if (settings?.floor != null) result = Math.floor(result);
|
|
45
|
+
if (settings?.digits != null) result = +result.toFixed(settings.digits);
|
|
46
|
+
return result;
|
|
47
|
+
} else return fallback;
|
|
44
48
|
};
|
|
45
49
|
number.defaultSettings = {};
|
|
46
|
-
|
|
47
|
-
|
|
50
|
+
//#endregion
|
|
51
|
+
//#region src/parser/percent.ts
|
|
52
|
+
/**
|
|
53
|
+
* Converts a value into a percentage of `maxValue` and parses the result with
|
|
54
|
+
* the shared numeric parser.
|
|
55
|
+
*
|
|
56
|
+
* @template TFallback Fallback value type returned when parsing fails.
|
|
57
|
+
* @param value Current value.
|
|
58
|
+
* @param maxValue Maximum value representing `100%`.
|
|
59
|
+
* @param settings Numeric parser settings for the computed percentage.
|
|
60
|
+
* @returns Parsed percentage or fallback value.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* percent(25, 200); // 12.5
|
|
65
|
+
* ```
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* percent('bad', 100, { fallback: 0 }); // 0
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
72
|
+
var percent = (value, maxValue, settings) => {
|
|
73
|
+
return number(Number(value) / Number(maxValue) * 100, settings);
|
|
48
74
|
};
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
75
|
+
//#endregion
|
|
76
|
+
//#region src/parser/string.ts
|
|
77
|
+
/**
|
|
78
|
+
* Converts arbitrary input into a string representation.
|
|
79
|
+
*
|
|
80
|
+
* Objects are serialized with `JSON.stringify`, optionally pretty-printed, and
|
|
81
|
+
* nullish values resolve to the configured fallback.
|
|
82
|
+
*
|
|
83
|
+
* @template TFallback Fallback value type returned for nullish input.
|
|
84
|
+
* @param input Raw value to stringify.
|
|
85
|
+
* @param settings String conversion settings.
|
|
86
|
+
* @returns Stringified input or fallback value.
|
|
87
|
+
*
|
|
88
|
+
* @example
|
|
89
|
+
* ```ts
|
|
90
|
+
* string(123); // '123'
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* @example
|
|
94
|
+
* ```ts
|
|
95
|
+
* string({ id: 1 }, { prettyJson: true });
|
|
96
|
+
* ```
|
|
97
|
+
*/
|
|
98
|
+
var string = (input, settings) => {
|
|
99
|
+
const fallback = settings && "fallback" in settings ? settings.fallback : "";
|
|
100
|
+
if (input == null) return fallback;
|
|
101
|
+
if (yummies_type_guard.typeGuard.isObject(input)) {
|
|
102
|
+
if (settings?.prettyJson) return JSON.stringify(input, null, 2);
|
|
103
|
+
return JSON.stringify(input);
|
|
104
|
+
}
|
|
105
|
+
return String(input);
|
|
61
106
|
};
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
//#
|
|
107
|
+
//#endregion
|
|
108
|
+
//#region src/parser/_exports.ts
|
|
109
|
+
var _exports_exports = /* @__PURE__ */ require_chunk.__exportAll({
|
|
110
|
+
number: () => number,
|
|
111
|
+
percent: () => percent,
|
|
112
|
+
string: () => string
|
|
113
|
+
});
|
|
114
|
+
//#endregion
|
|
115
|
+
Object.defineProperty(exports, "parser", {
|
|
116
|
+
enumerable: true,
|
|
117
|
+
get: function() {
|
|
118
|
+
return _exports_exports;
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
//# sourceMappingURL=parser.cjs.map
|
package/parser.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parser.cjs","sources":["../src/parser/number.ts","../src/parser/percent.ts","../src/parser/string.ts"],"sourcesContent":["import { format } from 'yummies/format';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { Maybe } from 'yummies/types';\n\nexport interface NumberParserSettings<TFallback = number> {\n digits?: number;\n fallback?: TFallback;\n /**\n * Round to upper boundary\n * 5.1 -> 6\n */\n ceil?: boolean;\n /**\n * Round to bottom boundary\n * 5.9 -> 5\n */\n floor?: boolean;\n clamped?: [min?: Maybe<number>, max?: Maybe<number>];\n}\n\nexport const number = <TFallback = number>(\n input: Maybe<unknown>,\n userSettings?: Maybe<NumberParserSettings<TFallback>>,\n): number | TFallback => {\n const settings = {\n ...number.defaultSettings,\n ...userSettings,\n };\n\n const fallback = (\n 'fallback' in settings ? settings.fallback : 0\n ) as TFallback;\n\n let result: number;\n\n if (typeGuard.isNumber(input)) {\n result = input;\n } else if (typeGuard.isString(input)) {\n const formattedInput = format.skipSpaces(input).replace(',', '.');\n if (formattedInput === '') {\n result = fallback as any;\n } else {\n result = Number(formattedInput);\n }\n } else {\n result = fallback as any;\n }\n\n if (typeGuard.isNumber(result)) {\n if (settings?.clamped != null) {\n result = Math.max(\n settings.clamped[0] ?? -Infinity,\n Math.min(result, settings.clamped[1] ?? Infinity),\n );\n }\n\n if (settings?.ceil != null) {\n result = Math.ceil(result);\n }\n\n if (settings?.floor != null) {\n result = Math.floor(result);\n }\n\n if (settings?.digits != null) {\n result = +result.toFixed(settings.digits);\n }\n\n return result;\n } else {\n return fallback;\n }\n};\n\nnumber.defaultSettings = {} as NumberParserSettings;\n","import type { Maybe } from 'yummies/types';\n\nimport { type NumberParserSettings, number } from './number.js';\n\nexport const percent = <TFallback = number>(\n value: Maybe<string | number>,\n maxValue?: Maybe<string | number>,\n settings?: Maybe<NumberParserSettings<TFallback>>,\n) => {\n return number<TFallback>((Number(value) / Number(maxValue)) * 100, settings);\n};\n","import { typeGuard } from 'yummies/type-guard';\nimport type { Maybe } from 'yummies/types';\n\nexport interface StringParserSettings<TFallback = string> {\n fallback?: TFallback;\n prettyJson?: boolean;\n}\n\nexport const string = <TFallback = string>(\n input: Maybe<unknown>,\n settings?: Maybe<StringParserSettings<TFallback>>,\n): string | TFallback => {\n const fallback =\n settings && 'fallback' in settings ? (settings.fallback as TFallback) : '';\n\n if (input == null) {\n return fallback;\n }\n\n if (typeGuard.isObject(input)) {\n if (settings?.prettyJson) {\n return JSON.stringify(input, null, 2);\n }\n\n return JSON.stringify(input);\n }\n\n return String(input);\n};\n"
|
|
1
|
+
{"version":3,"file":"parser.cjs","names":[],"sources":["../src/parser/number.ts","../src/parser/percent.ts","../src/parser/string.ts","../src/parser/_exports.ts"],"sourcesContent":["import { format } from 'yummies/format';\nimport { typeGuard } from 'yummies/type-guard';\nimport type { Maybe } from 'yummies/types';\n\nexport interface NumberParserSettings<TFallback = number> {\n digits?: number;\n fallback?: TFallback;\n /**\n * Round to upper boundary\n * 5.1 -> 6\n */\n ceil?: boolean;\n /**\n * Round to bottom boundary\n * 5.9 -> 5\n */\n floor?: boolean;\n clamped?: [min?: Maybe<number>, max?: Maybe<number>];\n}\n\n/**\n * Parses a number from raw input and optionally clamps, rounds or limits\n * fractional digits.\n *\n * Strings are normalized by removing spaces and replacing `,` with `.` before\n * parsing. Invalid inputs return the configured fallback.\n *\n * @template TFallback Fallback value type returned when parsing fails.\n * @param input Raw value to parse.\n * @param userSettings Parser settings merged with `number.defaultSettings`.\n * @returns Parsed number or fallback value.\n *\n * @example\n * ```ts\n * number('1 234,5'); // 1234.5\n * ```\n *\n * @example\n * ```ts\n * number('bad', { fallback: 0 }); // 0\n * ```\n */\nexport const number = <TFallback = number>(\n input: Maybe<unknown>,\n userSettings?: Maybe<NumberParserSettings<TFallback>>,\n): number | TFallback => {\n const settings = {\n ...number.defaultSettings,\n ...userSettings,\n };\n\n const fallback = (\n 'fallback' in settings ? settings.fallback : 0\n ) as TFallback;\n\n let result: number;\n\n if (typeGuard.isNumber(input)) {\n result = input;\n } else if (typeGuard.isString(input)) {\n const formattedInput = format.skipSpaces(input).replace(',', '.');\n if (formattedInput === '') {\n result = fallback as any;\n } else {\n result = Number(formattedInput);\n }\n } else {\n result = fallback as any;\n }\n\n if (typeGuard.isNumber(result)) {\n if (settings?.clamped != null) {\n result = Math.max(\n settings.clamped[0] ?? -Infinity,\n Math.min(result, settings.clamped[1] ?? Infinity),\n );\n }\n\n if (settings?.ceil != null) {\n result = Math.ceil(result);\n }\n\n if (settings?.floor != null) {\n result = Math.floor(result);\n }\n\n if (settings?.digits != null) {\n result = +result.toFixed(settings.digits);\n }\n\n return result;\n } else {\n return fallback;\n }\n};\n\nnumber.defaultSettings = {} as NumberParserSettings;\n","import type { Maybe } from 'yummies/types';\n\nimport { type NumberParserSettings, number } from './number.js';\n\n/**\n * Converts a value into a percentage of `maxValue` and parses the result with\n * the shared numeric parser.\n *\n * @template TFallback Fallback value type returned when parsing fails.\n * @param value Current value.\n * @param maxValue Maximum value representing `100%`.\n * @param settings Numeric parser settings for the computed percentage.\n * @returns Parsed percentage or fallback value.\n *\n * @example\n * ```ts\n * percent(25, 200); // 12.5\n * ```\n *\n * @example\n * ```ts\n * percent('bad', 100, { fallback: 0 }); // 0\n * ```\n */\nexport const percent = <TFallback = number>(\n value: Maybe<string | number>,\n maxValue?: Maybe<string | number>,\n settings?: Maybe<NumberParserSettings<TFallback>>,\n) => {\n return number<TFallback>((Number(value) / Number(maxValue)) * 100, settings);\n};\n","import { typeGuard } from 'yummies/type-guard';\nimport type { Maybe } from 'yummies/types';\n\nexport interface StringParserSettings<TFallback = string> {\n fallback?: TFallback;\n prettyJson?: boolean;\n}\n\n/**\n * Converts arbitrary input into a string representation.\n *\n * Objects are serialized with `JSON.stringify`, optionally pretty-printed, and\n * nullish values resolve to the configured fallback.\n *\n * @template TFallback Fallback value type returned for nullish input.\n * @param input Raw value to stringify.\n * @param settings String conversion settings.\n * @returns Stringified input or fallback value.\n *\n * @example\n * ```ts\n * string(123); // '123'\n * ```\n *\n * @example\n * ```ts\n * string({ id: 1 }, { prettyJson: true });\n * ```\n */\nexport const string = <TFallback = string>(\n input: Maybe<unknown>,\n settings?: Maybe<StringParserSettings<TFallback>>,\n): string | TFallback => {\n const fallback =\n settings && 'fallback' in settings ? (settings.fallback as TFallback) : '';\n\n if (input == null) {\n return fallback;\n }\n\n if (typeGuard.isObject(input)) {\n if (settings?.prettyJson) {\n return JSON.stringify(input, null, 2);\n }\n\n return JSON.stringify(input);\n }\n\n return String(input);\n};\n","export * from './number.js';\nexport * from './percent.js';\nexport * from './string.js';\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AA0CA,IAAa,UACX,OACA,iBACuB;CACvB,MAAM,WAAW;EACf,GAAG,OAAO;EACV,GAAG;EACJ;CAED,MAAM,WACJ,cAAc,WAAW,SAAS,WAAW;CAG/C,IAAI;AAEJ,KAAI,mBAAA,UAAU,SAAS,MAAM,CAC3B,UAAS;UACA,mBAAA,UAAU,SAAS,MAAM,EAAE;EACpC,MAAM,iBAAiB,eAAA,OAAO,WAAW,MAAM,CAAC,QAAQ,KAAK,IAAI;AACjE,MAAI,mBAAmB,GACrB,UAAS;MAET,UAAS,OAAO,eAAe;OAGjC,UAAS;AAGX,KAAI,mBAAA,UAAU,SAAS,OAAO,EAAE;AAC9B,MAAI,UAAU,WAAW,KACvB,UAAS,KAAK,IACZ,SAAS,QAAQ,MAAM,WACvB,KAAK,IAAI,QAAQ,SAAS,QAAQ,MAAM,SAAS,CAClD;AAGH,MAAI,UAAU,QAAQ,KACpB,UAAS,KAAK,KAAK,OAAO;AAG5B,MAAI,UAAU,SAAS,KACrB,UAAS,KAAK,MAAM,OAAO;AAG7B,MAAI,UAAU,UAAU,KACtB,UAAS,CAAC,OAAO,QAAQ,SAAS,OAAO;AAG3C,SAAO;OAEP,QAAO;;AAIX,OAAO,kBAAkB,EAAE;;;;;;;;;;;;;;;;;;;;;;;ACxE3B,IAAa,WACX,OACA,UACA,aACG;AACH,QAAO,OAAmB,OAAO,MAAM,GAAG,OAAO,SAAS,GAAI,KAAK,SAAS;;;;;;;;;;;;;;;;;;;;;;;;;ACA9E,IAAa,UACX,OACA,aACuB;CACvB,MAAM,WACJ,YAAY,cAAc,WAAY,SAAS,WAAyB;AAE1E,KAAI,SAAS,KACX,QAAO;AAGT,KAAI,mBAAA,UAAU,SAAS,MAAM,EAAE;AAC7B,MAAI,UAAU,WACZ,QAAO,KAAK,UAAU,OAAO,MAAM,EAAE;AAGvC,SAAO,KAAK,UAAU,MAAM;;AAG9B,QAAO,OAAO,MAAM"}
|
package/parser.d.ts
CHANGED
|
@@ -15,17 +15,80 @@ interface NumberParserSettings<TFallback = number> {
|
|
|
15
15
|
floor?: boolean;
|
|
16
16
|
clamped?: [min?: Maybe<number>, max?: Maybe<number>];
|
|
17
17
|
}
|
|
18
|
+
/**
|
|
19
|
+
* Parses a number from raw input and optionally clamps, rounds or limits
|
|
20
|
+
* fractional digits.
|
|
21
|
+
*
|
|
22
|
+
* Strings are normalized by removing spaces and replacing `,` with `.` before
|
|
23
|
+
* parsing. Invalid inputs return the configured fallback.
|
|
24
|
+
*
|
|
25
|
+
* @template TFallback Fallback value type returned when parsing fails.
|
|
26
|
+
* @param input Raw value to parse.
|
|
27
|
+
* @param userSettings Parser settings merged with `number.defaultSettings`.
|
|
28
|
+
* @returns Parsed number or fallback value.
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* number('1 234,5'); // 1234.5
|
|
33
|
+
* ```
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* number('bad', { fallback: 0 }); // 0
|
|
38
|
+
* ```
|
|
39
|
+
*/
|
|
18
40
|
declare const number: {
|
|
19
41
|
<TFallback = number>(input: Maybe<unknown>, userSettings?: Maybe<NumberParserSettings<TFallback>>): number | TFallback;
|
|
20
42
|
defaultSettings: NumberParserSettings<number>;
|
|
21
43
|
};
|
|
22
44
|
|
|
45
|
+
/**
|
|
46
|
+
* Converts a value into a percentage of `maxValue` and parses the result with
|
|
47
|
+
* the shared numeric parser.
|
|
48
|
+
*
|
|
49
|
+
* @template TFallback Fallback value type returned when parsing fails.
|
|
50
|
+
* @param value Current value.
|
|
51
|
+
* @param maxValue Maximum value representing `100%`.
|
|
52
|
+
* @param settings Numeric parser settings for the computed percentage.
|
|
53
|
+
* @returns Parsed percentage or fallback value.
|
|
54
|
+
*
|
|
55
|
+
* @example
|
|
56
|
+
* ```ts
|
|
57
|
+
* percent(25, 200); // 12.5
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* @example
|
|
61
|
+
* ```ts
|
|
62
|
+
* percent('bad', 100, { fallback: 0 }); // 0
|
|
63
|
+
* ```
|
|
64
|
+
*/
|
|
23
65
|
declare const percent: <TFallback = number>(value: Maybe<string | number>, maxValue?: Maybe<string | number>, settings?: Maybe<NumberParserSettings<TFallback>>) => number | TFallback;
|
|
24
66
|
|
|
25
67
|
interface StringParserSettings<TFallback = string> {
|
|
26
68
|
fallback?: TFallback;
|
|
27
69
|
prettyJson?: boolean;
|
|
28
70
|
}
|
|
71
|
+
/**
|
|
72
|
+
* Converts arbitrary input into a string representation.
|
|
73
|
+
*
|
|
74
|
+
* Objects are serialized with `JSON.stringify`, optionally pretty-printed, and
|
|
75
|
+
* nullish values resolve to the configured fallback.
|
|
76
|
+
*
|
|
77
|
+
* @template TFallback Fallback value type returned for nullish input.
|
|
78
|
+
* @param input Raw value to stringify.
|
|
79
|
+
* @param settings String conversion settings.
|
|
80
|
+
* @returns Stringified input or fallback value.
|
|
81
|
+
*
|
|
82
|
+
* @example
|
|
83
|
+
* ```ts
|
|
84
|
+
* string(123); // '123'
|
|
85
|
+
* ```
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* string({ id: 1 }, { prettyJson: true });
|
|
90
|
+
* ```
|
|
91
|
+
*/
|
|
29
92
|
declare const string: <TFallback = string>(input: Maybe<unknown>, settings?: Maybe<StringParserSettings<TFallback>>) => string | TFallback;
|
|
30
93
|
|
|
31
94
|
type _exports_NumberParserSettings<TFallback = number> = NumberParserSettings<TFallback>;
|