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/parser.js
CHANGED
|
@@ -1,69 +1,116 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { n as __exportAll } from "./chunk-YKewjYmz.js";
|
|
2
2
|
import { typeGuard } from "yummies/type-guard";
|
|
3
|
-
|
|
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
|
-
|
|
3
|
+
import { format } from "yummies/format";
|
|
4
|
+
//#region src/parser/number.ts
|
|
5
|
+
/**
|
|
6
|
+
* Parses a number from raw input and optionally clamps, rounds or limits
|
|
7
|
+
* fractional digits.
|
|
8
|
+
*
|
|
9
|
+
* Strings are normalized by removing spaces and replacing `,` with `.` before
|
|
10
|
+
* parsing. Invalid inputs return the configured fallback.
|
|
11
|
+
*
|
|
12
|
+
* @template TFallback Fallback value type returned when parsing fails.
|
|
13
|
+
* @param input Raw value to parse.
|
|
14
|
+
* @param userSettings Parser settings merged with `number.defaultSettings`.
|
|
15
|
+
* @returns Parsed number or fallback value.
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```ts
|
|
19
|
+
* number('1 234,5'); // 1234.5
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* number('bad', { fallback: 0 }); // 0
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
27
|
+
var number = (input, userSettings) => {
|
|
28
|
+
const settings = {
|
|
29
|
+
...number.defaultSettings,
|
|
30
|
+
...userSettings
|
|
31
|
+
};
|
|
32
|
+
const fallback = "fallback" in settings ? settings.fallback : 0;
|
|
33
|
+
let result;
|
|
34
|
+
if (typeGuard.isNumber(input)) result = input;
|
|
35
|
+
else if (typeGuard.isString(input)) {
|
|
36
|
+
const formattedInput = format.skipSpaces(input).replace(",", ".");
|
|
37
|
+
if (formattedInput === "") result = fallback;
|
|
38
|
+
else result = Number(formattedInput);
|
|
39
|
+
} else result = fallback;
|
|
40
|
+
if (typeGuard.isNumber(result)) {
|
|
41
|
+
if (settings?.clamped != null) result = Math.max(settings.clamped[0] ?? -Infinity, Math.min(result, settings.clamped[1] ?? Infinity));
|
|
42
|
+
if (settings?.ceil != null) result = Math.ceil(result);
|
|
43
|
+
if (settings?.floor != null) result = Math.floor(result);
|
|
44
|
+
if (settings?.digits != null) result = +result.toFixed(settings.digits);
|
|
45
|
+
return result;
|
|
46
|
+
} else return fallback;
|
|
42
47
|
};
|
|
43
48
|
number.defaultSettings = {};
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
49
|
+
//#endregion
|
|
50
|
+
//#region src/parser/percent.ts
|
|
51
|
+
/**
|
|
52
|
+
* Converts a value into a percentage of `maxValue` and parses the result with
|
|
53
|
+
* the shared numeric parser.
|
|
54
|
+
*
|
|
55
|
+
* @template TFallback Fallback value type returned when parsing fails.
|
|
56
|
+
* @param value Current value.
|
|
57
|
+
* @param maxValue Maximum value representing `100%`.
|
|
58
|
+
* @param settings Numeric parser settings for the computed percentage.
|
|
59
|
+
* @returns Parsed percentage or fallback value.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* percent(25, 200); // 12.5
|
|
64
|
+
* ```
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* percent('bad', 100, { fallback: 0 }); // 0
|
|
69
|
+
* ```
|
|
70
|
+
*/
|
|
71
|
+
var percent = (value, maxValue, settings) => {
|
|
72
|
+
return number(Number(value) / Number(maxValue) * 100, settings);
|
|
59
73
|
};
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
74
|
+
//#endregion
|
|
75
|
+
//#region src/parser/string.ts
|
|
76
|
+
/**
|
|
77
|
+
* Converts arbitrary input into a string representation.
|
|
78
|
+
*
|
|
79
|
+
* Objects are serialized with `JSON.stringify`, optionally pretty-printed, and
|
|
80
|
+
* nullish values resolve to the configured fallback.
|
|
81
|
+
*
|
|
82
|
+
* @template TFallback Fallback value type returned for nullish input.
|
|
83
|
+
* @param input Raw value to stringify.
|
|
84
|
+
* @param settings String conversion settings.
|
|
85
|
+
* @returns Stringified input or fallback value.
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* ```ts
|
|
89
|
+
* string(123); // '123'
|
|
90
|
+
* ```
|
|
91
|
+
*
|
|
92
|
+
* @example
|
|
93
|
+
* ```ts
|
|
94
|
+
* string({ id: 1 }, { prettyJson: true });
|
|
95
|
+
* ```
|
|
96
|
+
*/
|
|
97
|
+
var string = (input, settings) => {
|
|
98
|
+
const fallback = settings && "fallback" in settings ? settings.fallback : "";
|
|
99
|
+
if (input == null) return fallback;
|
|
100
|
+
if (typeGuard.isObject(input)) {
|
|
101
|
+
if (settings?.prettyJson) return JSON.stringify(input, null, 2);
|
|
102
|
+
return JSON.stringify(input);
|
|
103
|
+
}
|
|
104
|
+
return String(input);
|
|
68
105
|
};
|
|
69
|
-
//#
|
|
106
|
+
//#endregion
|
|
107
|
+
//#region src/parser/_exports.ts
|
|
108
|
+
var _exports_exports = /* @__PURE__ */ __exportAll({
|
|
109
|
+
number: () => number,
|
|
110
|
+
percent: () => percent,
|
|
111
|
+
string: () => string
|
|
112
|
+
});
|
|
113
|
+
//#endregion
|
|
114
|
+
export { _exports_exports as parser };
|
|
115
|
+
|
|
116
|
+
//# sourceMappingURL=parser.js.map
|
package/parser.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parser.js","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.js","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,UAAU,SAAS,MAAM,CAC3B,UAAS;UACA,UAAU,SAAS,MAAM,EAAE;EACpC,MAAM,iBAAiB,OAAO,WAAW,MAAM,CAAC,QAAQ,KAAK,IAAI;AACjE,MAAI,mBAAmB,GACrB,UAAS;MAET,UAAS,OAAO,eAAe;OAGjC,UAAS;AAGX,KAAI,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,UAAU,SAAS,MAAM,EAAE;AAC7B,MAAI,UAAU,WACZ,QAAO,KAAK,UAAU,OAAO,MAAM,EAAE;AAGvC,SAAO,KAAK,UAAU,MAAM;;AAG9B,QAAO,OAAO,MAAM"}
|
package/price.cjs
CHANGED
|
@@ -1,21 +1,27 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
2
|
+
//#region src/price.ts
|
|
3
|
+
/**
|
|
4
|
+
* Formats a numeric price using locale and currency options.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* formatPrice(1990, 'ru-RU', 'RUB');
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
var formatPrice = (price, locale, currency, { withoutSymbol, customSymbol, ...options } = {}) => {
|
|
12
|
+
const priceFormatter = new Intl.NumberFormat(locale, {
|
|
13
|
+
style: "currency",
|
|
14
|
+
currency,
|
|
15
|
+
minimumFractionDigits: 0,
|
|
16
|
+
currencyDisplay: "narrowSymbol",
|
|
17
|
+
...options
|
|
18
|
+
});
|
|
19
|
+
const currencySymbol = priceFormatter.format(0).replace("0", "");
|
|
20
|
+
const priceWithoutCurrency = priceFormatter.format(price).replace(currencySymbol, "");
|
|
21
|
+
if (withoutSymbol) return priceWithoutCurrency;
|
|
22
|
+
return `${priceWithoutCurrency} ${customSymbol ?? (currency === "RUB" ? "р" : currencySymbol)}`.replace(/\s{2,}/, " ");
|
|
19
23
|
};
|
|
24
|
+
//#endregion
|
|
20
25
|
exports.formatPrice = formatPrice;
|
|
21
|
-
|
|
26
|
+
|
|
27
|
+
//# sourceMappingURL=price.cjs.map
|
package/price.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"price.cjs","sources":["../src/price.ts"],"sourcesContent":["export interface PriceFormatOptions\n extends Partial<Omit<Intl.NumberFormatOptions, 'currency'>> {\n withoutSymbol?: boolean;\n customSymbol?: string;\n}\n\nexport const formatPrice = (\n price: number,\n locale: string,\n currency?: string,\n { withoutSymbol, customSymbol, ...options }: PriceFormatOptions = {},\n) => {\n const priceFormatter = new Intl.NumberFormat(locale, {\n style: 'currency',\n currency,\n minimumFractionDigits: 0,\n currencyDisplay: 'narrowSymbol',\n ...options,\n });\n\n const zeroPrice = priceFormatter.format(0);\n const currencySymbol = zeroPrice.replace('0', '');\n const rawPrice = priceFormatter.format(price);\n const priceWithoutCurrency = rawPrice.replace(currencySymbol, '');\n\n if (withoutSymbol) {\n return priceWithoutCurrency;\n }\n\n return `${priceWithoutCurrency} ${\n customSymbol ?? (currency === 'RUB' ? 'р' : currencySymbol)\n }`.replace(/\\s{2,}/, ' ');\n};\n"],"
|
|
1
|
+
{"version":3,"file":"price.cjs","names":[],"sources":["../src/price.ts"],"sourcesContent":["export interface PriceFormatOptions\n extends Partial<Omit<Intl.NumberFormatOptions, 'currency'>> {\n withoutSymbol?: boolean;\n customSymbol?: string;\n}\n\n/**\n * Formats a numeric price using locale and currency options.\n *\n * @example\n * ```ts\n * formatPrice(1990, 'ru-RU', 'RUB');\n * ```\n */\nexport const formatPrice = (\n price: number,\n locale: string,\n currency?: string,\n { withoutSymbol, customSymbol, ...options }: PriceFormatOptions = {},\n) => {\n const priceFormatter = new Intl.NumberFormat(locale, {\n style: 'currency',\n currency,\n minimumFractionDigits: 0,\n currencyDisplay: 'narrowSymbol',\n ...options,\n });\n\n const zeroPrice = priceFormatter.format(0);\n const currencySymbol = zeroPrice.replace('0', '');\n const rawPrice = priceFormatter.format(price);\n const priceWithoutCurrency = rawPrice.replace(currencySymbol, '');\n\n if (withoutSymbol) {\n return priceWithoutCurrency;\n }\n\n return `${priceWithoutCurrency} ${\n customSymbol ?? (currency === 'RUB' ? 'р' : currencySymbol)\n }`.replace(/\\s{2,}/, ' ');\n};\n"],"mappings":";;;;;;;;;;AAcA,IAAa,eACX,OACA,QACA,UACA,EAAE,eAAe,cAAc,GAAG,YAAgC,EAAE,KACjE;CACH,MAAM,iBAAiB,IAAI,KAAK,aAAa,QAAQ;EACnD,OAAO;EACP;EACA,uBAAuB;EACvB,iBAAiB;EACjB,GAAG;EACJ,CAAC;CAGF,MAAM,iBADY,eAAe,OAAO,EAAE,CACT,QAAQ,KAAK,GAAG;CAEjD,MAAM,uBADW,eAAe,OAAO,MAAM,CACP,QAAQ,gBAAgB,GAAG;AAEjE,KAAI,cACF,QAAO;AAGT,QAAO,GAAG,qBAAqB,GAC7B,iBAAiB,aAAa,QAAQ,MAAM,kBAC3C,QAAQ,UAAU,IAAI"}
|
package/price.d.ts
CHANGED
|
@@ -2,6 +2,14 @@ interface PriceFormatOptions extends Partial<Omit<Intl.NumberFormatOptions, 'cur
|
|
|
2
2
|
withoutSymbol?: boolean;
|
|
3
3
|
customSymbol?: string;
|
|
4
4
|
}
|
|
5
|
+
/**
|
|
6
|
+
* Formats a numeric price using locale and currency options.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```ts
|
|
10
|
+
* formatPrice(1990, 'ru-RU', 'RUB');
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
5
13
|
declare const formatPrice: (price: number, locale: string, currency?: string, { withoutSymbol, customSymbol, ...options }?: PriceFormatOptions) => string;
|
|
6
14
|
|
|
7
15
|
export { formatPrice };
|
package/price.js
CHANGED
|
@@ -1,21 +1,26 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
1
|
+
//#region src/price.ts
|
|
2
|
+
/**
|
|
3
|
+
* Formats a numeric price using locale and currency options.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* formatPrice(1990, 'ru-RU', 'RUB');
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
var formatPrice = (price, locale, currency, { withoutSymbol, customSymbol, ...options } = {}) => {
|
|
11
|
+
const priceFormatter = new Intl.NumberFormat(locale, {
|
|
12
|
+
style: "currency",
|
|
13
|
+
currency,
|
|
14
|
+
minimumFractionDigits: 0,
|
|
15
|
+
currencyDisplay: "narrowSymbol",
|
|
16
|
+
...options
|
|
17
|
+
});
|
|
18
|
+
const currencySymbol = priceFormatter.format(0).replace("0", "");
|
|
19
|
+
const priceWithoutCurrency = priceFormatter.format(price).replace(currencySymbol, "");
|
|
20
|
+
if (withoutSymbol) return priceWithoutCurrency;
|
|
21
|
+
return `${priceWithoutCurrency} ${customSymbol ?? (currency === "RUB" ? "р" : currencySymbol)}`.replace(/\s{2,}/, " ");
|
|
17
22
|
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
//# sourceMappingURL=price.js.map
|
|
23
|
+
//#endregion
|
|
24
|
+
export { formatPrice };
|
|
25
|
+
|
|
26
|
+
//# sourceMappingURL=price.js.map
|
package/price.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"price.js","sources":["../src/price.ts"],"sourcesContent":["export interface PriceFormatOptions\n extends Partial<Omit<Intl.NumberFormatOptions, 'currency'>> {\n withoutSymbol?: boolean;\n customSymbol?: string;\n}\n\nexport const formatPrice = (\n price: number,\n locale: string,\n currency?: string,\n { withoutSymbol, customSymbol, ...options }: PriceFormatOptions = {},\n) => {\n const priceFormatter = new Intl.NumberFormat(locale, {\n style: 'currency',\n currency,\n minimumFractionDigits: 0,\n currencyDisplay: 'narrowSymbol',\n ...options,\n });\n\n const zeroPrice = priceFormatter.format(0);\n const currencySymbol = zeroPrice.replace('0', '');\n const rawPrice = priceFormatter.format(price);\n const priceWithoutCurrency = rawPrice.replace(currencySymbol, '');\n\n if (withoutSymbol) {\n return priceWithoutCurrency;\n }\n\n return `${priceWithoutCurrency} ${\n customSymbol ?? (currency === 'RUB' ? 'р' : currencySymbol)\n }`.replace(/\\s{2,}/, ' ');\n};\n"],"
|
|
1
|
+
{"version":3,"file":"price.js","names":[],"sources":["../src/price.ts"],"sourcesContent":["export interface PriceFormatOptions\n extends Partial<Omit<Intl.NumberFormatOptions, 'currency'>> {\n withoutSymbol?: boolean;\n customSymbol?: string;\n}\n\n/**\n * Formats a numeric price using locale and currency options.\n *\n * @example\n * ```ts\n * formatPrice(1990, 'ru-RU', 'RUB');\n * ```\n */\nexport const formatPrice = (\n price: number,\n locale: string,\n currency?: string,\n { withoutSymbol, customSymbol, ...options }: PriceFormatOptions = {},\n) => {\n const priceFormatter = new Intl.NumberFormat(locale, {\n style: 'currency',\n currency,\n minimumFractionDigits: 0,\n currencyDisplay: 'narrowSymbol',\n ...options,\n });\n\n const zeroPrice = priceFormatter.format(0);\n const currencySymbol = zeroPrice.replace('0', '');\n const rawPrice = priceFormatter.format(price);\n const priceWithoutCurrency = rawPrice.replace(currencySymbol, '');\n\n if (withoutSymbol) {\n return priceWithoutCurrency;\n }\n\n return `${priceWithoutCurrency} ${\n customSymbol ?? (currency === 'RUB' ? 'р' : currencySymbol)\n }`.replace(/\\s{2,}/, ' ');\n};\n"],"mappings":";;;;;;;;;AAcA,IAAa,eACX,OACA,QACA,UACA,EAAE,eAAe,cAAc,GAAG,YAAgC,EAAE,KACjE;CACH,MAAM,iBAAiB,IAAI,KAAK,aAAa,QAAQ;EACnD,OAAO;EACP;EACA,uBAAuB;EACvB,iBAAiB;EACjB,GAAG;EACJ,CAAC;CAGF,MAAM,iBADY,eAAe,OAAO,EAAE,CACT,QAAQ,KAAK,GAAG;CAEjD,MAAM,uBADW,eAAe,OAAO,MAAM,CACP,QAAQ,gBAAgB,GAAG;AAEjE,KAAI,cACF,QAAO;AAGT,QAAO,GAAG,qBAAqB,GAC7B,iBAAiB,aAAa,QAAQ,MAAM,kBAC3C,QAAQ,UAAU,IAAI"}
|
package/random.cjs
CHANGED
|
@@ -1,19 +1,84 @@
|
|
|
1
|
-
"use strict";
|
|
2
1
|
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
//#region src/random.ts
|
|
3
|
+
/**
|
|
4
|
+
* Returns a random floating-point number between `min` and `max`.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* const value = getRandomFloat(1, 10);
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
var getRandomFloat = (min = 0, max = 1) => Math.random() * (max - min) + min;
|
|
12
|
+
/**
|
|
13
|
+
* Returns a random integer between `min` and `max`.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```ts
|
|
17
|
+
* const value = getRandomInt(1, 10);
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
var getRandomInt = (min = 0, max = 1) => min === max ? min : Math.round(getRandomFloat(min, max));
|
|
21
|
+
/**
|
|
22
|
+
* Picks a random element from the provided array.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* ```ts
|
|
26
|
+
* const fruit = getRandomChoice(['apple', 'banana', 'orange']);
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
var getRandomChoice = (arr) => arr[getRandomInt(0, arr.length - 1)];
|
|
30
|
+
/**
|
|
31
|
+
* Creates an array filled with `null` values using a random length.
|
|
32
|
+
*
|
|
33
|
+
* @example
|
|
34
|
+
* ```ts
|
|
35
|
+
* const items = getRandomSizeArray(2, 5);
|
|
36
|
+
* ```
|
|
37
|
+
*/
|
|
38
|
+
var getRandomSizeArray = (min = 0, max = 10) => Array.from({ length: getRandomInt(min, max) }).fill(null);
|
|
39
|
+
/**
|
|
40
|
+
* Returns a uniformly random boolean.
|
|
41
|
+
*
|
|
42
|
+
* @example
|
|
43
|
+
* ```ts
|
|
44
|
+
* const value = getRandomBool();
|
|
45
|
+
* ```
|
|
46
|
+
*/
|
|
47
|
+
var getRandomBool = () => getRandomInt(0, 1) === 1;
|
|
48
|
+
/**
|
|
49
|
+
* Returns `true` more often than `false`.
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* ```ts
|
|
53
|
+
* const value = getMajorRandomBool();
|
|
54
|
+
* ```
|
|
55
|
+
*/
|
|
56
|
+
var getMajorRandomBool = () => {
|
|
57
|
+
return getRandomInt(0, 10) <= 6;
|
|
10
58
|
};
|
|
11
|
-
|
|
12
|
-
|
|
59
|
+
/**
|
|
60
|
+
* Returns `true` less often than `false`.
|
|
61
|
+
*
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* const value = getMinorRandomBool();
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
var getMinorRandomBool = () => {
|
|
68
|
+
return !getMajorRandomBool();
|
|
13
69
|
};
|
|
14
|
-
|
|
15
|
-
|
|
70
|
+
/**
|
|
71
|
+
* Returns `true` with the provided probability from `0` to `1`.
|
|
72
|
+
*
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* const shouldRun = getFrequencyValue(0.25);
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
var getFrequencyValue = (frequency) => {
|
|
79
|
+
return Math.random() < frequency;
|
|
16
80
|
};
|
|
81
|
+
//#endregion
|
|
17
82
|
exports.getFrequencyValue = getFrequencyValue;
|
|
18
83
|
exports.getMajorRandomBool = getMajorRandomBool;
|
|
19
84
|
exports.getMinorRandomBool = getMinorRandomBool;
|
|
@@ -22,4 +87,5 @@ exports.getRandomChoice = getRandomChoice;
|
|
|
22
87
|
exports.getRandomFloat = getRandomFloat;
|
|
23
88
|
exports.getRandomInt = getRandomInt;
|
|
24
89
|
exports.getRandomSizeArray = getRandomSizeArray;
|
|
25
|
-
|
|
90
|
+
|
|
91
|
+
//# sourceMappingURL=random.cjs.map
|
package/random.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"random.cjs","sources":["../src/random.ts"],"sourcesContent":["
|
|
1
|
+
{"version":3,"file":"random.cjs","names":[],"sources":["../src/random.ts"],"sourcesContent":["/**\n * Returns a random floating-point number between `min` and `max`.\n *\n * @example\n * ```ts\n * const value = getRandomFloat(1, 10);\n * ```\n */\nexport const getRandomFloat = <T extends number = number>(\n min = 0,\n max = 1,\n): T => (Math.random() * (max - min) + min) as T;\n\n/**\n * Returns a random integer between `min` and `max`.\n *\n * @example\n * ```ts\n * const value = getRandomInt(1, 10);\n * ```\n */\nexport const getRandomInt = <T extends number = number>(min = 0, max = 1): T =>\n min === max ? (min as T) : (Math.round(getRandomFloat(min, max)) as T);\n\n/**\n * Picks a random element from the provided array.\n *\n * @example\n * ```ts\n * const fruit = getRandomChoice(['apple', 'banana', 'orange']);\n * ```\n */\nexport const getRandomChoice = <T>(arr: T[]): T =>\n arr[getRandomInt(0, arr.length - 1)];\n\n/**\n * Creates an array filled with `null` values using a random length.\n *\n * @example\n * ```ts\n * const items = getRandomSizeArray(2, 5);\n * ```\n */\nexport const getRandomSizeArray = (min = 0, max = 10) =>\n Array.from({ length: getRandomInt(min, max) }).fill(null);\n\n/**\n * Returns a uniformly random boolean.\n *\n * @example\n * ```ts\n * const value = getRandomBool();\n * ```\n */\nexport const getRandomBool = () => getRandomInt(0, 1) === 1;\n\n/**\n * Returns `true` more often than `false`.\n *\n * @example\n * ```ts\n * const value = getMajorRandomBool();\n * ```\n */\nexport const getMajorRandomBool = () => {\n return getRandomInt(0, 10) <= 6;\n};\n\n/**\n * Returns `true` less often than `false`.\n *\n * @example\n * ```ts\n * const value = getMinorRandomBool();\n * ```\n */\nexport const getMinorRandomBool = () => {\n return !getMajorRandomBool();\n};\n\n/**\n * Returns `true` with the provided probability from `0` to `1`.\n *\n * @example\n * ```ts\n * const shouldRun = getFrequencyValue(0.25);\n * ```\n */\nexport const getFrequencyValue = (frequency: number) => {\n return Math.random() < frequency;\n};\n"],"mappings":";;;;;;;;;;AAQA,IAAa,kBACX,MAAM,GACN,MAAM,MACC,KAAK,QAAQ,IAAI,MAAM,OAAO;;;;;;;;;AAUvC,IAAa,gBAA2C,MAAM,GAAG,MAAM,MACrE,QAAQ,MAAO,MAAa,KAAK,MAAM,eAAe,KAAK,IAAI,CAAC;;;;;;;;;AAUlE,IAAa,mBAAsB,QACjC,IAAI,aAAa,GAAG,IAAI,SAAS,EAAE;;;;;;;;;AAUrC,IAAa,sBAAsB,MAAM,GAAG,MAAM,OAChD,MAAM,KAAK,EAAE,QAAQ,aAAa,KAAK,IAAI,EAAE,CAAC,CAAC,KAAK,KAAK;;;;;;;;;AAU3D,IAAa,sBAAsB,aAAa,GAAG,EAAE,KAAK;;;;;;;;;AAU1D,IAAa,2BAA2B;AACtC,QAAO,aAAa,GAAG,GAAG,IAAI;;;;;;;;;;AAWhC,IAAa,2BAA2B;AACtC,QAAO,CAAC,oBAAoB;;;;;;;;;;AAW9B,IAAa,qBAAqB,cAAsB;AACtD,QAAO,KAAK,QAAQ,GAAG"}
|
package/random.d.ts
CHANGED
|
@@ -1,10 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Returns a random floating-point number between `min` and `max`.
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* const value = getRandomFloat(1, 10);
|
|
7
|
+
* ```
|
|
8
|
+
*/
|
|
1
9
|
declare const getRandomFloat: <T extends number = number>(min?: number, max?: number) => T;
|
|
10
|
+
/**
|
|
11
|
+
* Returns a random integer between `min` and `max`.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const value = getRandomInt(1, 10);
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
2
18
|
declare const getRandomInt: <T extends number = number>(min?: number, max?: number) => T;
|
|
19
|
+
/**
|
|
20
|
+
* Picks a random element from the provided array.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```ts
|
|
24
|
+
* const fruit = getRandomChoice(['apple', 'banana', 'orange']);
|
|
25
|
+
* ```
|
|
26
|
+
*/
|
|
3
27
|
declare const getRandomChoice: <T>(arr: T[]) => T;
|
|
28
|
+
/**
|
|
29
|
+
* Creates an array filled with `null` values using a random length.
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```ts
|
|
33
|
+
* const items = getRandomSizeArray(2, 5);
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
4
36
|
declare const getRandomSizeArray: (min?: number, max?: number) => unknown[];
|
|
37
|
+
/**
|
|
38
|
+
* Returns a uniformly random boolean.
|
|
39
|
+
*
|
|
40
|
+
* @example
|
|
41
|
+
* ```ts
|
|
42
|
+
* const value = getRandomBool();
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
5
45
|
declare const getRandomBool: () => boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Returns `true` more often than `false`.
|
|
48
|
+
*
|
|
49
|
+
* @example
|
|
50
|
+
* ```ts
|
|
51
|
+
* const value = getMajorRandomBool();
|
|
52
|
+
* ```
|
|
53
|
+
*/
|
|
6
54
|
declare const getMajorRandomBool: () => boolean;
|
|
55
|
+
/**
|
|
56
|
+
* Returns `true` less often than `false`.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* const value = getMinorRandomBool();
|
|
61
|
+
* ```
|
|
62
|
+
*/
|
|
7
63
|
declare const getMinorRandomBool: () => boolean;
|
|
64
|
+
/**
|
|
65
|
+
* Returns `true` with the provided probability from `0` to `1`.
|
|
66
|
+
*
|
|
67
|
+
* @example
|
|
68
|
+
* ```ts
|
|
69
|
+
* const shouldRun = getFrequencyValue(0.25);
|
|
70
|
+
* ```
|
|
71
|
+
*/
|
|
8
72
|
declare const getFrequencyValue: (frequency: number) => boolean;
|
|
9
73
|
|
|
10
74
|
export { getFrequencyValue, getMajorRandomBool, getMinorRandomBool, getRandomBool, getRandomChoice, getRandomFloat, getRandomInt, getRandomSizeArray };
|
package/random.js
CHANGED
|
@@ -1,25 +1,83 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
//#region src/random.ts
|
|
2
|
+
/**
|
|
3
|
+
* Returns a random floating-point number between `min` and `max`.
|
|
4
|
+
*
|
|
5
|
+
* @example
|
|
6
|
+
* ```ts
|
|
7
|
+
* const value = getRandomFloat(1, 10);
|
|
8
|
+
* ```
|
|
9
|
+
*/
|
|
10
|
+
var getRandomFloat = (min = 0, max = 1) => Math.random() * (max - min) + min;
|
|
11
|
+
/**
|
|
12
|
+
* Returns a random integer between `min` and `max`.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```ts
|
|
16
|
+
* const value = getRandomInt(1, 10);
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
var getRandomInt = (min = 0, max = 1) => min === max ? min : Math.round(getRandomFloat(min, max));
|
|
20
|
+
/**
|
|
21
|
+
* Picks a random element from the provided array.
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* const fruit = getRandomChoice(['apple', 'banana', 'orange']);
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
var getRandomChoice = (arr) => arr[getRandomInt(0, arr.length - 1)];
|
|
29
|
+
/**
|
|
30
|
+
* Creates an array filled with `null` values using a random length.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* ```ts
|
|
34
|
+
* const items = getRandomSizeArray(2, 5);
|
|
35
|
+
* ```
|
|
36
|
+
*/
|
|
37
|
+
var getRandomSizeArray = (min = 0, max = 10) => Array.from({ length: getRandomInt(min, max) }).fill(null);
|
|
38
|
+
/**
|
|
39
|
+
* Returns a uniformly random boolean.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const value = getRandomBool();
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
var getRandomBool = () => getRandomInt(0, 1) === 1;
|
|
47
|
+
/**
|
|
48
|
+
* Returns `true` more often than `false`.
|
|
49
|
+
*
|
|
50
|
+
* @example
|
|
51
|
+
* ```ts
|
|
52
|
+
* const value = getMajorRandomBool();
|
|
53
|
+
* ```
|
|
54
|
+
*/
|
|
55
|
+
var getMajorRandomBool = () => {
|
|
56
|
+
return getRandomInt(0, 10) <= 6;
|
|
8
57
|
};
|
|
9
|
-
|
|
10
|
-
|
|
58
|
+
/**
|
|
59
|
+
* Returns `true` less often than `false`.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```ts
|
|
63
|
+
* const value = getMinorRandomBool();
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
66
|
+
var getMinorRandomBool = () => {
|
|
67
|
+
return !getMajorRandomBool();
|
|
11
68
|
};
|
|
12
|
-
|
|
13
|
-
|
|
69
|
+
/**
|
|
70
|
+
* Returns `true` with the provided probability from `0` to `1`.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* ```ts
|
|
74
|
+
* const shouldRun = getFrequencyValue(0.25);
|
|
75
|
+
* ```
|
|
76
|
+
*/
|
|
77
|
+
var getFrequencyValue = (frequency) => {
|
|
78
|
+
return Math.random() < frequency;
|
|
14
79
|
};
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
getRandomBool,
|
|
20
|
-
getRandomChoice,
|
|
21
|
-
getRandomFloat,
|
|
22
|
-
getRandomInt,
|
|
23
|
-
getRandomSizeArray
|
|
24
|
-
};
|
|
25
|
-
//# sourceMappingURL=random.js.map
|
|
80
|
+
//#endregion
|
|
81
|
+
export { getFrequencyValue, getMajorRandomBool, getMinorRandomBool, getRandomBool, getRandomChoice, getRandomFloat, getRandomInt, getRandomSizeArray };
|
|
82
|
+
|
|
83
|
+
//# sourceMappingURL=random.js.map
|