yummies 7.11.0 → 7.13.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.
Files changed (159) hide show
  1. package/README.md +5 -87
  2. package/async.cjs +179 -48
  3. package/async.cjs.map +1 -1
  4. package/async.d.ts +125 -7
  5. package/async.js +180 -54
  6. package/async.js.map +1 -1
  7. package/chunk-CVq3Gv4J.cjs +50 -0
  8. package/chunk-YKewjYmz.js +37 -0
  9. package/common.cjs +48 -8
  10. package/common.cjs.map +1 -1
  11. package/common.d.ts +53 -2
  12. package/common.js +49 -11
  13. package/common.js.map +1 -1
  14. package/complex.cjs +275 -128
  15. package/complex.cjs.map +1 -1
  16. package/complex.d.ts +66 -0
  17. package/complex.js +275 -133
  18. package/complex.js.map +1 -1
  19. package/cookie.cjs +17 -7
  20. package/cookie.cjs.map +1 -1
  21. package/cookie.d.ts +26 -0
  22. package/cookie.js +18 -9
  23. package/cookie.js.map +1 -1
  24. package/css.cjs +163 -39
  25. package/css.cjs.map +1 -1
  26. package/css.d.ts +115 -6
  27. package/css.js +159 -41
  28. package/css.js.map +1 -1
  29. package/data.cjs +90 -55
  30. package/data.cjs.map +1 -1
  31. package/data.d.ts +50 -0
  32. package/data.js +91 -61
  33. package/data.js.map +1 -1
  34. package/date-time.cjs +594 -412
  35. package/date-time.cjs.map +1 -1
  36. package/date-time.d.ts +105 -0
  37. package/date-time.js +591 -421
  38. package/date-time.js.map +1 -1
  39. package/device.cjs +65 -23
  40. package/device.cjs.map +1 -1
  41. package/device.d.ts +49 -0
  42. package/device.js +66 -31
  43. package/device.js.map +1 -1
  44. package/encodings.cjs +275 -266
  45. package/encodings.cjs.map +1 -1
  46. package/encodings.d.ts +25 -0
  47. package/encodings.js +276 -268
  48. package/encodings.js.map +1 -1
  49. package/errors.cjs +36 -18
  50. package/errors.cjs.map +1 -1
  51. package/errors.d.ts +17 -0
  52. package/errors.js +35 -19
  53. package/errors.js.map +1 -1
  54. package/file.cjs +58 -24
  55. package/file.cjs.map +1 -1
  56. package/file.d.ts +32 -0
  57. package/file.js +59 -27
  58. package/file.js.map +1 -1
  59. package/format.cjs +125 -83
  60. package/format.cjs.map +1 -1
  61. package/format.d.ts +18 -0
  62. package/format.js +118 -82
  63. package/format.js.map +1 -1
  64. package/html.cjs +242 -137
  65. package/html.cjs.map +1 -1
  66. package/html.d.ts +81 -0
  67. package/html.js +239 -150
  68. package/html.js.map +1 -1
  69. package/id.cjs +90 -17
  70. package/id.cjs.map +1 -1
  71. package/id.d.ts +16 -0
  72. package/id.js +89 -24
  73. package/id.js.map +1 -1
  74. package/imports.cjs +57 -29
  75. package/imports.cjs.map +1 -1
  76. package/imports.d.ts +24 -0
  77. package/imports.js +56 -31
  78. package/imports.js.map +1 -1
  79. package/math.cjs +32 -6
  80. package/math.cjs.map +1 -1
  81. package/math.d.ts +33 -0
  82. package/math.js +33 -10
  83. package/math.js.map +1 -1
  84. package/media.cjs +291 -84
  85. package/media.cjs.map +1 -1
  86. package/media.d.ts +204 -2
  87. package/media.js +290 -93
  88. package/media.js.map +1 -1
  89. package/mobx.cjs +449 -193
  90. package/mobx.cjs.map +1 -1
  91. package/mobx.d.ts +108 -0
  92. package/mobx.js +447 -200
  93. package/mobx.js.map +1 -1
  94. package/ms.cjs +37 -10
  95. package/ms.cjs.map +1 -1
  96. package/ms.d.ts +16 -0
  97. package/ms.js +38 -13
  98. package/ms.js.map +1 -1
  99. package/number.cjs +29 -7
  100. package/number.cjs.map +1 -1
  101. package/number.d.ts +16 -0
  102. package/number.js +30 -9
  103. package/number.js.map +1 -1
  104. package/package.json +11 -3
  105. package/parser.cjs +117 -64
  106. package/parser.cjs.map +1 -1
  107. package/parser.d.ts +17 -0
  108. package/parser.js +111 -64
  109. package/parser.js.map +1 -1
  110. package/price.cjs +24 -18
  111. package/price.cjs.map +1 -1
  112. package/price.d.ts +24 -0
  113. package/price.js +25 -20
  114. package/price.js.map +1 -1
  115. package/random.cjs +95 -13
  116. package/random.cjs.map +1 -1
  117. package/random.d.ts +80 -0
  118. package/random.js +96 -22
  119. package/random.js.map +1 -1
  120. package/react.cjs +673 -214
  121. package/react.cjs.map +1 -1
  122. package/react.d.ts +21 -0
  123. package/react.js +674 -239
  124. package/react.js.map +1 -1
  125. package/sound.cjs +30 -9
  126. package/sound.cjs.map +1 -1
  127. package/sound.d.ts +16 -0
  128. package/sound.js +31 -11
  129. package/sound.js.map +1 -1
  130. package/storage.cjs +49 -50
  131. package/storage.cjs.map +1 -1
  132. package/storage.d.ts +24 -0
  133. package/storage.js +50 -53
  134. package/storage.js.map +1 -1
  135. package/text.cjs +67 -34
  136. package/text.cjs.map +1 -1
  137. package/text.d.ts +16 -0
  138. package/text.js +68 -37
  139. package/text.js.map +1 -1
  140. package/type-guard.cjs +292 -72
  141. package/type-guard.cjs.map +1 -1
  142. package/type-guard.d.ts +18 -0
  143. package/type-guard.js +288 -73
  144. package/type-guard.js.map +1 -1
  145. package/types.cjs +0 -2
  146. package/types.d.ts +41 -0
  147. package/types.global.cjs +0 -2
  148. package/types.global.d.ts +41 -0
  149. package/types.global.js +0 -2
  150. package/types.js +0 -2
  151. package/vibrate.cjs +47 -6
  152. package/vibrate.cjs.map +1 -1
  153. package/vibrate.d.ts +39 -1
  154. package/vibrate.js +48 -8
  155. package/vibrate.js.map +1 -1
  156. package/types.cjs.map +0 -1
  157. package/types.global.cjs.map +0 -1
  158. package/types.global.js.map +0 -1
  159. package/types.js.map +0 -1
package/parser.d.ts CHANGED
@@ -91,6 +91,23 @@ interface StringParserSettings<TFallback = string> {
91
91
  */
92
92
  declare const string: <TFallback = string>(input: Maybe<unknown>, settings?: Maybe<StringParserSettings<TFallback>>) => string | TFallback;
93
93
 
94
+ /**
95
+ * ---header-docs-section---
96
+ * # yummies/parser
97
+ *
98
+ * ## Description
99
+ *
100
+ * Parsers for user-entered **numbers, percents, and strings** with tolerant input and typed
101
+ * results. Use when normalizing form values, query params, or CSV-like text before validation
102
+ * schemas run, without duplicating regex and `parseFloat` edge cases in every feature module.
103
+ *
104
+ * ## Usage
105
+ *
106
+ * ```ts
107
+ * import { parser } from "yummies/parser";
108
+ * ```
109
+ */
110
+
94
111
  type _exports_NumberParserSettings<TFallback = number> = NumberParserSettings<TFallback>;
95
112
  type _exports_StringParserSettings<TFallback = string> = StringParserSettings<TFallback>;
96
113
  declare const _exports_number: typeof number;
package/parser.js CHANGED
@@ -1,69 +1,116 @@
1
- import { format } from "yummies/format";
1
+ import { n as __exportAll } from "./chunk-YKewjYmz.js";
2
2
  import { typeGuard } from "yummies/type-guard";
3
- const number = (input, userSettings) => {
4
- const settings = {
5
- ...number.defaultSettings,
6
- ...userSettings
7
- };
8
- const fallback = "fallback" in settings ? settings.fallback : 0;
9
- let result;
10
- if (typeGuard.isNumber(input)) {
11
- result = input;
12
- } else if (typeGuard.isString(input)) {
13
- const formattedInput = format.skipSpaces(input).replace(",", ".");
14
- if (formattedInput === "") {
15
- result = fallback;
16
- } else {
17
- result = Number(formattedInput);
18
- }
19
- } else {
20
- result = fallback;
21
- }
22
- if (typeGuard.isNumber(result)) {
23
- if (settings?.clamped != null) {
24
- result = Math.max(
25
- settings.clamped[0] ?? -Infinity,
26
- Math.min(result, settings.clamped[1] ?? Infinity)
27
- );
28
- }
29
- if (settings?.ceil != null) {
30
- result = Math.ceil(result);
31
- }
32
- if (settings?.floor != null) {
33
- result = Math.floor(result);
34
- }
35
- if (settings?.digits != null) {
36
- result = +result.toFixed(settings.digits);
37
- }
38
- return result;
39
- } else {
40
- return fallback;
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
- const percent = (value, maxValue, settings) => {
45
- return number(Number(value) / Number(maxValue) * 100, settings);
46
- };
47
- const string = (input, settings) => {
48
- const fallback = settings && "fallback" in settings ? settings.fallback : "";
49
- if (input == null) {
50
- return fallback;
51
- }
52
- if (typeGuard.isObject(input)) {
53
- if (settings?.prettyJson) {
54
- return JSON.stringify(input, null, 2);
55
- }
56
- return JSON.stringify(input);
57
- }
58
- return String(input);
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
- const _exports = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
61
- __proto__: null,
62
- number,
63
- percent,
64
- string
65
- }, Symbol.toStringTag, { value: "Module" }));
66
- export {
67
- _exports as parser
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
- //# sourceMappingURL=parser.js.map
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\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"],"names":[],"mappings":";;AA0CO,MAAM,SAAS,CACpB,OACA,iBACuB;AACvB,QAAM,WAAW;AAAA,IACf,GAAG,OAAO;AAAA,IACV,GAAG;AAAA,EAAA;AAGL,QAAM,WACJ,cAAc,WAAW,SAAS,WAAW;AAG/C,MAAI;AAEJ,MAAI,UAAU,SAAS,KAAK,GAAG;AAC7B,aAAS;AAAA,EACX,WAAW,UAAU,SAAS,KAAK,GAAG;AACpC,UAAM,iBAAiB,OAAO,WAAW,KAAK,EAAE,QAAQ,KAAK,GAAG;AAChE,QAAI,mBAAmB,IAAI;AACzB,eAAS;AAAA,IACX,OAAO;AACL,eAAS,OAAO,cAAc;AAAA,IAChC;AAAA,EACF,OAAO;AACL,aAAS;AAAA,EACX;AAEA,MAAI,UAAU,SAAS,MAAM,GAAG;AAC9B,QAAI,UAAU,WAAW,MAAM;AAC7B,eAAS,KAAK;AAAA,QACZ,SAAS,QAAQ,CAAC,KAAK;AAAA,QACvB,KAAK,IAAI,QAAQ,SAAS,QAAQ,CAAC,KAAK,QAAQ;AAAA,MAAA;AAAA,IAEpD;AAEA,QAAI,UAAU,QAAQ,MAAM;AAC1B,eAAS,KAAK,KAAK,MAAM;AAAA,IAC3B;AAEA,QAAI,UAAU,SAAS,MAAM;AAC3B,eAAS,KAAK,MAAM,MAAM;AAAA,IAC5B;AAEA,QAAI,UAAU,UAAU,MAAM;AAC5B,eAAS,CAAC,OAAO,QAAQ,SAAS,MAAM;AAAA,IAC1C;AAEA,WAAO;AAAA,EACT,OAAO;AACL,WAAO;AAAA,EACT;AACF;AAEA,OAAO,kBAAkB,CAAA;ACxElB,MAAM,UAAU,CACrB,OACA,UACA,aACG;AACH,SAAO,OAAmB,OAAO,KAAK,IAAI,OAAO,QAAQ,IAAK,KAAK,QAAQ;AAC7E;ACDO,MAAM,SAAS,CACpB,OACA,aACuB;AACvB,QAAM,WACJ,YAAY,cAAc,WAAY,SAAS,WAAyB;AAE1E,MAAI,SAAS,MAAM;AACjB,WAAO;AAAA,EACT;AAEA,MAAI,UAAU,SAAS,KAAK,GAAG;AAC7B,QAAI,UAAU,YAAY;AACxB,aAAO,KAAK,UAAU,OAAO,MAAM,CAAC;AAAA,IACtC;AAEA,WAAO,KAAK,UAAU,KAAK;AAAA,EAC7B;AAEA,SAAO,OAAO,KAAK;AACrB;;;;;;;"}
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","/**\n * ---header-docs-section---\n * # yummies/parser\n *\n * ## Description\n *\n * Parsers for user-entered **numbers, percents, and strings** with tolerant input and typed\n * results. Use when normalizing form values, query params, or CSV-like text before validation\n * schemas run, without duplicating regex and `parseFloat` edge cases in every feature module.\n *\n * ## Usage\n *\n * ```ts\n * import { parser } from \"yummies/parser\";\n * ```\n */\n\nexport * 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
- const formatPrice = (price, locale, currency, { withoutSymbol, customSymbol, ...options } = {}) => {
4
- const priceFormatter = new Intl.NumberFormat(locale, {
5
- style: "currency",
6
- currency,
7
- minimumFractionDigits: 0,
8
- currencyDisplay: "narrowSymbol",
9
- ...options
10
- });
11
- const zeroPrice = priceFormatter.format(0);
12
- const currencySymbol = zeroPrice.replace("0", "");
13
- const rawPrice = priceFormatter.format(price);
14
- const priceWithoutCurrency = rawPrice.replace(currencySymbol, "");
15
- if (withoutSymbol) {
16
- return priceWithoutCurrency;
17
- }
18
- return `${priceWithoutCurrency} ${customSymbol ?? (currency === "RUB" ? "р" : currencySymbol)}`.replace(/\s{2,}/, " ");
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
- //# sourceMappingURL=price.cjs.map
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"],"names":[],"mappings":";;AAMO,MAAM,cAAc,CACzB,OACA,QACA,UACA,EAAE,eAAe,cAAc,GAAG,QAAA,IAAgC,OAC/D;AACH,QAAM,iBAAiB,IAAI,KAAK,aAAa,QAAQ;AAAA,IACnD,OAAO;AAAA,IACP;AAAA,IACA,uBAAuB;AAAA,IACvB,iBAAiB;AAAA,IACjB,GAAG;AAAA,EAAA,CACJ;AAED,QAAM,YAAY,eAAe,OAAO,CAAC;AACzC,QAAM,iBAAiB,UAAU,QAAQ,KAAK,EAAE;AAChD,QAAM,WAAW,eAAe,OAAO,KAAK;AAC5C,QAAM,uBAAuB,SAAS,QAAQ,gBAAgB,EAAE;AAEhE,MAAI,eAAe;AACjB,WAAO;AAAA,EACT;AAEA,SAAO,GAAG,oBAAoB,IAC5B,iBAAiB,aAAa,QAAQ,MAAM,eAC9C,GAAG,QAAQ,UAAU,GAAG;AAC1B;;"}
1
+ {"version":3,"file":"price.cjs","names":[],"sources":["../src/price.ts"],"sourcesContent":["/**\n * ---header-docs-section---\n * # yummies/price\n *\n * ## Description\n *\n * Locale-aware **money formatting** via `Intl.NumberFormat`, with optional symbol hiding and\n * custom currency symbols for legacy UI. It wraps browser i18n APIs so storefronts and dashboards\n * share one implementation for RUB/EUR/USD-style output without pulling a heavy formatting library.\n *\n * ## Usage\n *\n * ```ts\n * import { formatPrice } from \"yummies/price\";\n * ```\n */\n\nexport 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":";;;;;;;;;;AA+BA,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
@@ -1,7 +1,31 @@
1
+ /**
2
+ * ---header-docs-section---
3
+ * # yummies/price
4
+ *
5
+ * ## Description
6
+ *
7
+ * Locale-aware **money formatting** via `Intl.NumberFormat`, with optional symbol hiding and
8
+ * custom currency symbols for legacy UI. It wraps browser i18n APIs so storefronts and dashboards
9
+ * share one implementation for RUB/EUR/USD-style output without pulling a heavy formatting library.
10
+ *
11
+ * ## Usage
12
+ *
13
+ * ```ts
14
+ * import { formatPrice } from "yummies/price";
15
+ * ```
16
+ */
1
17
  interface PriceFormatOptions extends Partial<Omit<Intl.NumberFormatOptions, 'currency'>> {
2
18
  withoutSymbol?: boolean;
3
19
  customSymbol?: string;
4
20
  }
21
+ /**
22
+ * Formats a numeric price using locale and currency options.
23
+ *
24
+ * @example
25
+ * ```ts
26
+ * formatPrice(1990, 'ru-RU', 'RUB');
27
+ * ```
28
+ */
5
29
  declare const formatPrice: (price: number, locale: string, currency?: string, { withoutSymbol, customSymbol, ...options }?: PriceFormatOptions) => string;
6
30
 
7
31
  export { formatPrice };
package/price.js CHANGED
@@ -1,21 +1,26 @@
1
- const formatPrice = (price, locale, currency, { withoutSymbol, customSymbol, ...options } = {}) => {
2
- const priceFormatter = new Intl.NumberFormat(locale, {
3
- style: "currency",
4
- currency,
5
- minimumFractionDigits: 0,
6
- currencyDisplay: "narrowSymbol",
7
- ...options
8
- });
9
- const zeroPrice = priceFormatter.format(0);
10
- const currencySymbol = zeroPrice.replace("0", "");
11
- const rawPrice = priceFormatter.format(price);
12
- const priceWithoutCurrency = rawPrice.replace(currencySymbol, "");
13
- if (withoutSymbol) {
14
- return priceWithoutCurrency;
15
- }
16
- return `${priceWithoutCurrency} ${customSymbol ?? (currency === "RUB" ? "р" : currencySymbol)}`.replace(/\s{2,}/, " ");
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
- export {
19
- formatPrice
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"],"names":[],"mappings":"AAMO,MAAM,cAAc,CACzB,OACA,QACA,UACA,EAAE,eAAe,cAAc,GAAG,QAAA,IAAgC,OAC/D;AACH,QAAM,iBAAiB,IAAI,KAAK,aAAa,QAAQ;AAAA,IACnD,OAAO;AAAA,IACP;AAAA,IACA,uBAAuB;AAAA,IACvB,iBAAiB;AAAA,IACjB,GAAG;AAAA,EAAA,CACJ;AAED,QAAM,YAAY,eAAe,OAAO,CAAC;AACzC,QAAM,iBAAiB,UAAU,QAAQ,KAAK,EAAE;AAChD,QAAM,WAAW,eAAe,OAAO,KAAK;AAC5C,QAAM,uBAAuB,SAAS,QAAQ,gBAAgB,EAAE;AAEhE,MAAI,eAAe;AACjB,WAAO;AAAA,EACT;AAEA,SAAO,GAAG,oBAAoB,IAC5B,iBAAiB,aAAa,QAAQ,MAAM,eAC9C,GAAG,QAAQ,UAAU,GAAG;AAC1B;"}
1
+ {"version":3,"file":"price.js","names":[],"sources":["../src/price.ts"],"sourcesContent":["/**\n * ---header-docs-section---\n * # yummies/price\n *\n * ## Description\n *\n * Locale-aware **money formatting** via `Intl.NumberFormat`, with optional symbol hiding and\n * custom currency symbols for legacy UI. It wraps browser i18n APIs so storefronts and dashboards\n * share one implementation for RUB/EUR/USD-style output without pulling a heavy formatting library.\n *\n * ## Usage\n *\n * ```ts\n * import { formatPrice } from \"yummies/price\";\n * ```\n */\n\nexport 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":";;;;;;;;;AA+BA,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,100 @@
1
- "use strict";
2
1
  Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const getRandomFloat = (min = 0, max = 1) => Math.random() * (max - min) + min;
4
- const getRandomInt = (min = 0, max = 1) => min === max ? min : Math.round(getRandomFloat(min, max));
5
- const getRandomChoice = (arr) => arr[getRandomInt(0, arr.length - 1)];
6
- const getRandomSizeArray = (min = 0, max = 10) => Array.from({ length: getRandomInt(min, max) }).fill(null);
7
- const getRandomBool = () => getRandomInt(0, 1) === 1;
8
- const getMajorRandomBool = () => {
9
- return getRandomInt(0, 10) <= 6;
2
+ //#region src/random.ts
3
+ /**
4
+ * ---header-docs-section---
5
+ * # yummies/random
6
+ *
7
+ * ## Description
8
+ *
9
+ * Small RNG helpers for UI demos, games, and sampling: floats, integers, and random choices from
10
+ * arrays. They wrap `Math.random` (not cryptographically secure) so prefer platform `crypto` when
11
+ * generating secrets, tokens, or lottery outcomes.
12
+ *
13
+ * ## Usage
14
+ *
15
+ * ```ts
16
+ * import { getRandomInt, getRandomChoice } from "yummies/random";
17
+ * ```
18
+ */
19
+ /**
20
+ * Returns a random floating-point number between `min` and `max`.
21
+ *
22
+ * @example
23
+ * ```ts
24
+ * const value = getRandomFloat(1, 10);
25
+ * ```
26
+ */
27
+ var getRandomFloat = (min = 0, max = 1) => Math.random() * (max - min) + min;
28
+ /**
29
+ * Returns a random integer between `min` and `max`.
30
+ *
31
+ * @example
32
+ * ```ts
33
+ * const value = getRandomInt(1, 10);
34
+ * ```
35
+ */
36
+ var getRandomInt = (min = 0, max = 1) => min === max ? min : Math.round(getRandomFloat(min, max));
37
+ /**
38
+ * Picks a random element from the provided array.
39
+ *
40
+ * @example
41
+ * ```ts
42
+ * const fruit = getRandomChoice(['apple', 'banana', 'orange']);
43
+ * ```
44
+ */
45
+ var getRandomChoice = (arr) => arr[getRandomInt(0, arr.length - 1)];
46
+ /**
47
+ * Creates an array filled with `null` values using a random length.
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * const items = getRandomSizeArray(2, 5);
52
+ * ```
53
+ */
54
+ var getRandomSizeArray = (min = 0, max = 10) => Array.from({ length: getRandomInt(min, max) }).fill(null);
55
+ /**
56
+ * Returns a uniformly random boolean.
57
+ *
58
+ * @example
59
+ * ```ts
60
+ * const value = getRandomBool();
61
+ * ```
62
+ */
63
+ var getRandomBool = () => getRandomInt(0, 1) === 1;
64
+ /**
65
+ * Returns `true` more often than `false`.
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * const value = getMajorRandomBool();
70
+ * ```
71
+ */
72
+ var getMajorRandomBool = () => {
73
+ return getRandomInt(0, 10) <= 6;
10
74
  };
11
- const getMinorRandomBool = () => {
12
- return !getMajorRandomBool();
75
+ /**
76
+ * Returns `true` less often than `false`.
77
+ *
78
+ * @example
79
+ * ```ts
80
+ * const value = getMinorRandomBool();
81
+ * ```
82
+ */
83
+ var getMinorRandomBool = () => {
84
+ return !getMajorRandomBool();
13
85
  };
14
- const getFrequencyValue = (frequency) => {
15
- return Math.random() < frequency;
86
+ /**
87
+ * Returns `true` with the provided probability from `0` to `1`.
88
+ *
89
+ * @example
90
+ * ```ts
91
+ * const shouldRun = getFrequencyValue(0.25);
92
+ * ```
93
+ */
94
+ var getFrequencyValue = (frequency) => {
95
+ return Math.random() < frequency;
16
96
  };
97
+ //#endregion
17
98
  exports.getFrequencyValue = getFrequencyValue;
18
99
  exports.getMajorRandomBool = getMajorRandomBool;
19
100
  exports.getMinorRandomBool = getMinorRandomBool;
@@ -22,4 +103,5 @@ exports.getRandomChoice = getRandomChoice;
22
103
  exports.getRandomFloat = getRandomFloat;
23
104
  exports.getRandomInt = getRandomInt;
24
105
  exports.getRandomSizeArray = getRandomSizeArray;
25
- //# sourceMappingURL=random.cjs.map
106
+
107
+ //# sourceMappingURL=random.cjs.map
package/random.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"random.cjs","sources":["../src/random.ts"],"sourcesContent":["export const getRandomFloat = <T extends number = number>(\n min = 0,\n max = 1,\n): T => (Math.random() * (max - min) + min) as T;\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\nexport const getRandomChoice = <T>(arr: T[]): T =>\n arr[getRandomInt(0, arr.length - 1)];\n\nexport const getRandomSizeArray = (min = 0, max = 10) =>\n Array.from({ length: getRandomInt(min, max) }).fill(null);\n\nexport const getRandomBool = () => getRandomInt(0, 1) === 1;\n\nexport const getMajorRandomBool = () => {\n return getRandomInt(0, 10) <= 6;\n};\n\nexport const getMinorRandomBool = () => {\n return !getMajorRandomBool();\n};\n\nexport const getFrequencyValue = (frequency: number) => {\n return Math.random() < frequency;\n};\n"],"names":[],"mappings":";;AAAO,MAAM,iBAAiB,CAC5B,MAAM,GACN,MAAM,MACC,KAAK,OAAA,KAAY,MAAM,OAAO;AAEhC,MAAM,eAAe,CAA4B,MAAM,GAAG,MAAM,MACrE,QAAQ,MAAO,MAAa,KAAK,MAAM,eAAe,KAAK,GAAG,CAAC;AAE1D,MAAM,kBAAkB,CAAI,QACjC,IAAI,aAAa,GAAG,IAAI,SAAS,CAAC,CAAC;AAE9B,MAAM,qBAAqB,CAAC,MAAM,GAAG,MAAM,OAChD,MAAM,KAAK,EAAE,QAAQ,aAAa,KAAK,GAAG,GAAG,EAAE,KAAK,IAAI;AAEnD,MAAM,gBAAgB,MAAM,aAAa,GAAG,CAAC,MAAM;AAEnD,MAAM,qBAAqB,MAAM;AACtC,SAAO,aAAa,GAAG,EAAE,KAAK;AAChC;AAEO,MAAM,qBAAqB,MAAM;AACtC,SAAO,CAAC,mBAAA;AACV;AAEO,MAAM,oBAAoB,CAAC,cAAsB;AACtD,SAAO,KAAK,WAAW;AACzB;;;;;;;;;"}
1
+ {"version":3,"file":"random.cjs","names":[],"sources":["../src/random.ts"],"sourcesContent":["/**\n * ---header-docs-section---\n * # yummies/random\n *\n * ## Description\n *\n * Small RNG helpers for UI demos, games, and sampling: floats, integers, and random choices from\n * arrays. They wrap `Math.random` (not cryptographically secure) so prefer platform `crypto` when\n * generating secrets, tokens, or lottery outcomes.\n *\n * ## Usage\n *\n * ```ts\n * import { getRandomInt, getRandomChoice } from \"yummies/random\";\n * ```\n */\n\n/**\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":";;;;;;;;;;;;;;;;;;;;;;;;;;AAyBA,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,90 @@
1
+ /**
2
+ * ---header-docs-section---
3
+ * # yummies/random
4
+ *
5
+ * ## Description
6
+ *
7
+ * Small RNG helpers for UI demos, games, and sampling: floats, integers, and random choices from
8
+ * arrays. They wrap `Math.random` (not cryptographically secure) so prefer platform `crypto` when
9
+ * generating secrets, tokens, or lottery outcomes.
10
+ *
11
+ * ## Usage
12
+ *
13
+ * ```ts
14
+ * import { getRandomInt, getRandomChoice } from "yummies/random";
15
+ * ```
16
+ */
17
+ /**
18
+ * Returns a random floating-point number between `min` and `max`.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * const value = getRandomFloat(1, 10);
23
+ * ```
24
+ */
1
25
  declare const getRandomFloat: <T extends number = number>(min?: number, max?: number) => T;
26
+ /**
27
+ * Returns a random integer between `min` and `max`.
28
+ *
29
+ * @example
30
+ * ```ts
31
+ * const value = getRandomInt(1, 10);
32
+ * ```
33
+ */
2
34
  declare const getRandomInt: <T extends number = number>(min?: number, max?: number) => T;
35
+ /**
36
+ * Picks a random element from the provided array.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * const fruit = getRandomChoice(['apple', 'banana', 'orange']);
41
+ * ```
42
+ */
3
43
  declare const getRandomChoice: <T>(arr: T[]) => T;
44
+ /**
45
+ * Creates an array filled with `null` values using a random length.
46
+ *
47
+ * @example
48
+ * ```ts
49
+ * const items = getRandomSizeArray(2, 5);
50
+ * ```
51
+ */
4
52
  declare const getRandomSizeArray: (min?: number, max?: number) => unknown[];
53
+ /**
54
+ * Returns a uniformly random boolean.
55
+ *
56
+ * @example
57
+ * ```ts
58
+ * const value = getRandomBool();
59
+ * ```
60
+ */
5
61
  declare const getRandomBool: () => boolean;
62
+ /**
63
+ * Returns `true` more often than `false`.
64
+ *
65
+ * @example
66
+ * ```ts
67
+ * const value = getMajorRandomBool();
68
+ * ```
69
+ */
6
70
  declare const getMajorRandomBool: () => boolean;
71
+ /**
72
+ * Returns `true` less often than `false`.
73
+ *
74
+ * @example
75
+ * ```ts
76
+ * const value = getMinorRandomBool();
77
+ * ```
78
+ */
7
79
  declare const getMinorRandomBool: () => boolean;
80
+ /**
81
+ * Returns `true` with the provided probability from `0` to `1`.
82
+ *
83
+ * @example
84
+ * ```ts
85
+ * const shouldRun = getFrequencyValue(0.25);
86
+ * ```
87
+ */
8
88
  declare const getFrequencyValue: (frequency: number) => boolean;
9
89
 
10
90
  export { getFrequencyValue, getMajorRandomBool, getMinorRandomBool, getRandomBool, getRandomChoice, getRandomFloat, getRandomInt, getRandomSizeArray };