@kikiutils/shared 15.0.2 → 15.1.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 (56) hide show
  1. package/README.md +137 -67
  2. package/dist/buffer.js.map +1 -1
  3. package/dist/classes/path.d.ts +8 -0
  4. package/dist/classes/path.d.ts.map +1 -1
  5. package/dist/classes/path.js +5 -0
  6. package/dist/classes/path.js.map +1 -1
  7. package/dist/classes/precision-number.d.ts +215 -11
  8. package/dist/classes/precision-number.d.ts.map +1 -1
  9. package/dist/classes/precision-number.js +268 -25
  10. package/dist/classes/precision-number.js.map +1 -1
  11. package/dist/classes/ssh-client.js.map +1 -1
  12. package/dist/clipboard.js.map +1 -1
  13. package/dist/consola.js.map +1 -1
  14. package/dist/crypto-hash.d.ts +7 -7
  15. package/dist/crypto-hash.js +7 -7
  16. package/dist/crypto-hash.js.map +1 -1
  17. package/dist/datetime.js.map +1 -1
  18. package/dist/element-plus.js.map +1 -1
  19. package/dist/enum.js.map +1 -1
  20. package/dist/env.js.map +1 -1
  21. package/dist/event-awaiter.js.map +1 -1
  22. package/dist/general.js.map +1 -1
  23. package/dist/hash.d.ts +3 -3
  24. package/dist/hash.js +3 -3
  25. package/dist/hash.js.map +1 -1
  26. package/dist/math.js.map +1 -1
  27. package/dist/number.js.map +1 -1
  28. package/dist/object.js.map +1 -1
  29. package/dist/pino.js.map +1 -1
  30. package/dist/random.js.map +1 -1
  31. package/dist/storages/lru/keyed-store.d.ts +3 -3
  32. package/dist/storages/lru/keyed-store.js +11 -3
  33. package/dist/storages/lru/keyed-store.js.map +1 -1
  34. package/dist/storages/redis/keyed-store.d.ts +1 -1
  35. package/dist/storages/redis/keyed-store.js +1 -1
  36. package/dist/storages/redis/keyed-store.js.map +1 -1
  37. package/dist/storages/redis/msgpack.js.map +1 -1
  38. package/dist/string.d.ts +12 -6
  39. package/dist/string.d.ts.map +1 -1
  40. package/dist/string.js +9 -6
  41. package/dist/string.js.map +1 -1
  42. package/dist/time.js.map +1 -1
  43. package/dist/types/index.d.ts +30 -0
  44. package/dist/types/index.d.ts.map +1 -1
  45. package/dist/url.js.map +1 -1
  46. package/dist/vue.js.map +1 -1
  47. package/dist/web.js.map +1 -1
  48. package/package.json +15 -24
  49. package/src/classes/path.ts +9 -0
  50. package/src/classes/precision-number.ts +300 -25
  51. package/src/crypto-hash.ts +7 -7
  52. package/src/hash.ts +3 -3
  53. package/src/storages/lru/keyed-store.ts +3 -3
  54. package/src/storages/redis/keyed-store.ts +1 -1
  55. package/src/string.ts +12 -6
  56. package/src/types/index.ts +39 -0
@@ -2,15 +2,45 @@ import { FilteredKeyPath } from "./filtered-key-path.js";
2
2
  import { Blob as Blob$1, Buffer, File as File$1 } from "node:buffer";
3
3
 
4
4
  //#region src/types/index.d.ts
5
+ /**
6
+ * A generic string-keyed object for utility APIs that intentionally accept any value shape.
7
+ */
5
8
  type AnyRecord = Record<string, any>;
9
+ /**
10
+ * Binary-like inputs accepted by `toBuffer` across browser and Node.js runtimes.
11
+ */
6
12
  type BinaryInput = ArrayBuffer | Blob | Buffer | File | Blob$1 | File$1 | Uint8Array;
13
+ /**
14
+ * Boolean values represented either as booleans or common string literals.
15
+ */
7
16
  type Booleanish = 'false' | 'true' | boolean;
17
+ /**
18
+ * Accepts either a full object or a partial patch of that object.
19
+ */
8
20
  type MaybePartial<T> = Partial<T> | T;
21
+ /**
22
+ * Accepts either a mutable value or a readonly view of that value.
23
+ */
9
24
  type MaybeReadonly<T> = Readonly<T> | T;
25
+ /**
26
+ * Explicitly nullable value helper.
27
+ */
10
28
  type Nullable<T> = null | T;
29
+ /**
30
+ * Number-like value represented as a number or a numeric string.
31
+ */
11
32
  type Numberish = number | string;
33
+ /**
34
+ * A `Record` where every key is optional.
35
+ */
12
36
  type PartialRecord<K extends keyof any, T> = Partial<Record<K, T>>;
37
+ /**
38
+ * A readonly `PartialRecord`.
39
+ */
13
40
  type ReadonlyPartialRecord<K extends keyof any, T> = Readonly<PartialRecord<K, T>>;
41
+ /**
42
+ * A readonly `Record`.
43
+ */
14
44
  type ReadonlyRecord<K extends keyof any, T> = Readonly<Record<K, T>>;
15
45
  //#endregion
16
46
  export { AnyRecord, BinaryInput, Booleanish, type FilteredKeyPath, MaybePartial, MaybeReadonly, Nullable, Numberish, PartialRecord, ReadonlyPartialRecord, ReadonlyRecord };
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","names":[],"sources":["../../src/types/index.ts"],"mappings":";;;;KAQY,SAAA,GAAY,MAAA;AAAA,KACZ,WAAA,GAAc,WAAA,GAAc,IAAA,GAAO,MAAA,GAAS,IAAA,GAAO,MAAA,GAAW,MAAA,GAAW,UAAA;AAAA,KACzE,UAAA;AAAA,KACA,YAAA,MAAkB,OAAA,CAAQ,CAAA,IAAK,CAAA;AAAA,KAC/B,aAAA,MAAmB,QAAA,CAAS,CAAA,IAAK,CAAA;AAAA,KACjC,QAAA,aAAqB,CAAA;AAAA,KACrB,SAAA;AAAA,KACA,aAAA,2BAAwC,OAAA,CAAQ,MAAA,CAAO,CAAA,EAAG,CAAA;AAAA,KAC1D,qBAAA,2BAAgD,QAAA,CAAS,aAAA,CAAc,CAAA,EAAG,CAAA;AAAA,KAC1E,cAAA,2BAAyC,QAAA,CAAS,MAAA,CAAO,CAAA,EAAG,CAAA"}
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../../src/types/index.ts"],"mappings":";;;;;;AAWA;KAAY,SAAA,GAAY,MAAA;;;;KAKZ,WAAA,GAAc,WAAA,GAAc,IAAA,GAAO,MAAA,GAAS,IAAA,GAAO,MAAA,GAAW,MAAA,GAAW,UAAA;;;;KAKzE,UAAA;;;;KAKA,YAAA,MAAkB,OAAA,CAAQ,CAAA,IAAK,CAAA;;;;KAK/B,aAAA,MAAmB,QAAA,CAAS,CAAA,IAAK,CAAA;;;;KAKjC,QAAA,aAAqB,CAAA;;;;KAKrB,SAAA;AApBZ;;;AAAA,KAyBY,aAAA,2BAAwC,OAAA,CAAQ,MAAA,CAAO,CAAA,EAAG,CAAA;;AApBtE;;KAyBY,qBAAA,2BAAgD,QAAA,CAAS,aAAA,CAAc,CAAA,EAAG,CAAA;;;;KAK1E,cAAA,2BAAyC,QAAA,CAAS,MAAA,CAAO,CAAA,EAAG,CAAA"}
package/dist/url.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"url.js","names":[],"sources":["../src/url.ts"],"sourcesContent":["/**\n * Appends or updates the `redirect` query parameter on a given URL.\n *\n * Typically used to preserve the user's current path for post-login navigation.\n *\n * @param {string} url - The target URL to modify\n * @param {string} redirectPath - The path to use as the redirect destination\n * (must be an absolute path starting with '/')\n *\n * @returns {string} A new URL string with the `redirect` query parameter\n *\n * @throws {Error} If redirectPath is not a valid relative path (must start with '/')\n */\nexport function appendRedirectParamToUrl(url: string, redirectPath: string) {\n if (!redirectPath.startsWith('/')) {\n throw new Error(\n `Invalid redirect path: \"${redirectPath}\". Redirect paths must be absolute paths starting with '/'.`,\n );\n }\n\n const [baseAndHash, rawQuery = ''] = url.split('?');\n const [base, hash] = (baseAndHash || '').split('#');\n const searchParams = new URLSearchParams(rawQuery);\n searchParams.set('redirect', redirectPath);\n const queryString = searchParams.toString();\n return hash ? `${base}?${queryString}#${hash}` : `${base}?${queryString}`;\n}\n"],"mappings":";;;;;;;;;;;;;;AAaA,SAAgB,yBAAyB,KAAa,cAAsB;AACxE,KAAI,CAAC,aAAa,WAAW,IAAI,CAC7B,OAAM,IAAI,MACN,2BAA2B,aAAa,6DAC3C;CAGL,MAAM,CAAC,aAAa,WAAW,MAAM,IAAI,MAAM,IAAI;CACnD,MAAM,CAAC,MAAM,SAAS,eAAe,IAAI,MAAM,IAAI;CACnD,MAAM,eAAe,IAAI,gBAAgB,SAAS;AAClD,cAAa,IAAI,YAAY,aAAa;CAC1C,MAAM,cAAc,aAAa,UAAU;AAC3C,QAAO,OAAO,GAAG,KAAK,GAAG,YAAY,GAAG,SAAS,GAAG,KAAK,GAAG"}
1
+ {"version":3,"file":"url.js","names":[],"sources":["../src/url.ts"],"sourcesContent":["/**\n * Appends or updates the `redirect` query parameter on a given URL.\n *\n * Typically used to preserve the user's current path for post-login navigation.\n *\n * @param {string} url - The target URL to modify\n * @param {string} redirectPath - The path to use as the redirect destination\n * (must be an absolute path starting with '/')\n *\n * @returns {string} A new URL string with the `redirect` query parameter\n *\n * @throws {Error} If redirectPath is not a valid relative path (must start with '/')\n */\nexport function appendRedirectParamToUrl(url: string, redirectPath: string) {\n if (!redirectPath.startsWith('/')) {\n throw new Error(\n `Invalid redirect path: \"${redirectPath}\". Redirect paths must be absolute paths starting with '/'.`,\n );\n }\n\n const [baseAndHash, rawQuery = ''] = url.split('?');\n const [base, hash] = (baseAndHash || '').split('#');\n const searchParams = new URLSearchParams(rawQuery);\n searchParams.set('redirect', redirectPath);\n const queryString = searchParams.toString();\n return hash ? `${base}?${queryString}#${hash}` : `${base}?${queryString}`;\n}\n"],"mappings":";;;;;;;;;;;;;;AAaA,SAAgB,yBAAyB,KAAa,cAAsB;CACxE,IAAI,CAAC,aAAa,WAAW,IAAI,EAC7B,MAAM,IAAI,MACN,2BAA2B,aAAa,6DAC3C;CAGL,MAAM,CAAC,aAAa,WAAW,MAAM,IAAI,MAAM,IAAI;CACnD,MAAM,CAAC,MAAM,SAAS,eAAe,IAAI,MAAM,IAAI;CACnD,MAAM,eAAe,IAAI,gBAAgB,SAAS;CAClD,aAAa,IAAI,YAAY,aAAa;CAC1C,MAAM,cAAc,aAAa,UAAU;CAC3C,OAAO,OAAO,GAAG,KAAK,GAAG,YAAY,GAAG,SAAS,GAAG,KAAK,GAAG"}
package/dist/vue.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"vue.js","names":[],"sources":["../src/vue.ts"],"sourcesContent":["import { onActivated } from 'vue';\nimport type { Ref } from 'vue';\nimport {\n onBeforeRouteLeave,\n useRoute,\n} from 'vue-router';\n\nimport type { Nullable } from './types';\nimport { appendRedirectParamToUrl } from './url';\n\n/**\n * Appends the current Vue Router route's fullPath as the `redirect` query parameter to the given URL.\n *\n * @param {string} url - The base URL to modify\n *\n * @returns {string} A new URL with the current route fullPath as the `redirect` parameter\n */\nexport function appendRedirectParamFromCurrentRouteToUrl(url: string) {\n return appendRedirectParamToUrl(url, useRoute().fullPath);\n}\n\n/**\n * Clears an interval referenced by a Vue ref and sets it to null.\n *\n * @param {Ref<Nullable<ReturnType<typeof setInterval>>>} intervalRef - A Vue ref holding a NodeJS.Timeout or null\n */\nexport function clearIntervalRef(intervalRef: Ref<Nullable<ReturnType<typeof setInterval>>>) {\n if (intervalRef.value) clearInterval(intervalRef.value);\n intervalRef.value = null;\n}\n\n/**\n * Clears a timeout referenced by a Vue ref and sets it to null.\n *\n * @param {Ref<Nullable<ReturnType<typeof setTimeout>>>} timeoutRef - A Vue ref holding a NodeJS.Timeout or null\n */\nexport function clearTimeoutRef(timeoutRef: Ref<Nullable<ReturnType<typeof setTimeout>>>) {\n if (timeoutRef.value) clearTimeout(timeoutRef.value);\n timeoutRef.value = null;\n}\n\n/**\n * A Vue composition function that remembers and restores scroll position\n * of a scrollable container across route changes and keep-alive activation.\n *\n * Designed for use with `<KeepAlive>` components. Scroll state is saved on\n * route leave and restored when the component is activated again.\n *\n * Note: Without `<KeepAlive>`, the component state is destroyed on unmount,\n * so scroll position will not be preserved.\n *\n * @template T - The type of the scrollable element (defaults to HTMLElement)\n *\n * @param {Ref<Nullable<T>>} containerRef - A ref to the scrollable HTML element\n */\nexport function usePreserveScroll<T extends Element = HTMLElement>(containerRef: Ref<Nullable<T>>) {\n let scrollLeft = 0;\n let scrollTop = 0;\n onActivated(() => {\n if (!containerRef.value) return;\n containerRef.value.scrollLeft = scrollLeft;\n containerRef.value.scrollTop = scrollTop;\n });\n\n onBeforeRouteLeave(() => {\n scrollLeft = containerRef.value?.scrollLeft || 0;\n scrollTop = containerRef.value?.scrollTop || 0;\n });\n}\n"],"mappings":";;;;;;;;;;;AAiBA,SAAgB,yCAAyC,KAAa;AAClE,QAAO,yBAAyB,KAAK,UAAU,CAAC,SAAS;;;;;;;AAQ7D,SAAgB,iBAAiB,aAA4D;AACzF,KAAI,YAAY,MAAO,eAAc,YAAY,MAAM;AACvD,aAAY,QAAQ;;;;;;;AAQxB,SAAgB,gBAAgB,YAA0D;AACtF,KAAI,WAAW,MAAO,cAAa,WAAW,MAAM;AACpD,YAAW,QAAQ;;;;;;;;;;;;;;;;AAiBvB,SAAgB,kBAAmD,cAAgC;CAC/F,IAAI,aAAa;CACjB,IAAI,YAAY;AAChB,mBAAkB;AACd,MAAI,CAAC,aAAa,MAAO;AACzB,eAAa,MAAM,aAAa;AAChC,eAAa,MAAM,YAAY;GACjC;AAEF,0BAAyB;AACrB,eAAa,aAAa,OAAO,cAAc;AAC/C,cAAY,aAAa,OAAO,aAAa;GAC/C"}
1
+ {"version":3,"file":"vue.js","names":[],"sources":["../src/vue.ts"],"sourcesContent":["import { onActivated } from 'vue';\nimport type { Ref } from 'vue';\nimport {\n onBeforeRouteLeave,\n useRoute,\n} from 'vue-router';\n\nimport type { Nullable } from './types';\nimport { appendRedirectParamToUrl } from './url';\n\n/**\n * Appends the current Vue Router route's fullPath as the `redirect` query parameter to the given URL.\n *\n * @param {string} url - The base URL to modify\n *\n * @returns {string} A new URL with the current route fullPath as the `redirect` parameter\n */\nexport function appendRedirectParamFromCurrentRouteToUrl(url: string) {\n return appendRedirectParamToUrl(url, useRoute().fullPath);\n}\n\n/**\n * Clears an interval referenced by a Vue ref and sets it to null.\n *\n * @param {Ref<Nullable<ReturnType<typeof setInterval>>>} intervalRef - A Vue ref holding a NodeJS.Timeout or null\n */\nexport function clearIntervalRef(intervalRef: Ref<Nullable<ReturnType<typeof setInterval>>>) {\n if (intervalRef.value) clearInterval(intervalRef.value);\n intervalRef.value = null;\n}\n\n/**\n * Clears a timeout referenced by a Vue ref and sets it to null.\n *\n * @param {Ref<Nullable<ReturnType<typeof setTimeout>>>} timeoutRef - A Vue ref holding a NodeJS.Timeout or null\n */\nexport function clearTimeoutRef(timeoutRef: Ref<Nullable<ReturnType<typeof setTimeout>>>) {\n if (timeoutRef.value) clearTimeout(timeoutRef.value);\n timeoutRef.value = null;\n}\n\n/**\n * A Vue composition function that remembers and restores scroll position\n * of a scrollable container across route changes and keep-alive activation.\n *\n * Designed for use with `<KeepAlive>` components. Scroll state is saved on\n * route leave and restored when the component is activated again.\n *\n * Note: Without `<KeepAlive>`, the component state is destroyed on unmount,\n * so scroll position will not be preserved.\n *\n * @template T - The type of the scrollable element (defaults to HTMLElement)\n *\n * @param {Ref<Nullable<T>>} containerRef - A ref to the scrollable HTML element\n */\nexport function usePreserveScroll<T extends Element = HTMLElement>(containerRef: Ref<Nullable<T>>) {\n let scrollLeft = 0;\n let scrollTop = 0;\n onActivated(() => {\n if (!containerRef.value) return;\n containerRef.value.scrollLeft = scrollLeft;\n containerRef.value.scrollTop = scrollTop;\n });\n\n onBeforeRouteLeave(() => {\n scrollLeft = containerRef.value?.scrollLeft || 0;\n scrollTop = containerRef.value?.scrollTop || 0;\n });\n}\n"],"mappings":";;;;;;;;;;;AAiBA,SAAgB,yCAAyC,KAAa;CAClE,OAAO,yBAAyB,KAAK,UAAU,CAAC,SAAS;;;;;;;AAQ7D,SAAgB,iBAAiB,aAA4D;CACzF,IAAI,YAAY,OAAO,cAAc,YAAY,MAAM;CACvD,YAAY,QAAQ;;;;;;;AAQxB,SAAgB,gBAAgB,YAA0D;CACtF,IAAI,WAAW,OAAO,aAAa,WAAW,MAAM;CACpD,WAAW,QAAQ;;;;;;;;;;;;;;;;AAiBvB,SAAgB,kBAAmD,cAAgC;CAC/F,IAAI,aAAa;CACjB,IAAI,YAAY;CAChB,kBAAkB;EACd,IAAI,CAAC,aAAa,OAAO;EACzB,aAAa,MAAM,aAAa;EAChC,aAAa,MAAM,YAAY;GACjC;CAEF,yBAAyB;EACrB,aAAa,aAAa,OAAO,cAAc;EAC/C,YAAY,aAAa,OAAO,aAAa;GAC/C"}
package/dist/web.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"web.js","names":[],"sources":["../src/web.ts"],"sourcesContent":["import { appendRedirectParamToUrl } from './url';\n\n/**\n * Appends the current browser URL (including path, query, and hash) as the `redirect` query parameter to the given URL.\n *\n * @param {string} url - The base URL to modify\n *\n * @returns {string} A new URL with the current location as the `redirect` parameter\n */\nexport function appendRedirectParamFromCurrentLocationToUrl(url: string) {\n const currentPath = `${window.location.pathname}${window.location.search}${window.location.hash}`;\n return appendRedirectParamToUrl(url, currentPath);\n}\n\n/**\n * Navigates to the given URL, appending the current browser location\n * (path, query, and hash) as the `redirect` query parameter.\n *\n * Useful for redirecting to login or other gateways while preserving\n * the current location for post-auth navigation.\n *\n * @param {string} url - The destination URL to navigate to\n * @param {number} [delayMs] - Optional delay in milliseconds before navigation\n */\nexport function assignUrlWithRedirectParamFromCurrentLocation(url: string, delayMs?: number) {\n if (delayMs === undefined) window.location.assign(appendRedirectParamFromCurrentLocationToUrl(url));\n else return setTimeout(() => window.location.assign(appendRedirectParamFromCurrentLocationToUrl(url)), delayMs);\n}\n"],"mappings":";;;;;;;;;AASA,SAAgB,4CAA4C,KAAa;AAErE,QAAO,yBAAyB,KADZ,GAAG,OAAO,SAAS,WAAW,OAAO,SAAS,SAAS,OAAO,SAAS,OAC1C;;;;;;;;;;;;AAarD,SAAgB,8CAA8C,KAAa,SAAkB;AACzF,KAAI,YAAY,KAAA,EAAW,QAAO,SAAS,OAAO,4CAA4C,IAAI,CAAC;KAC9F,QAAO,iBAAiB,OAAO,SAAS,OAAO,4CAA4C,IAAI,CAAC,EAAE,QAAQ"}
1
+ {"version":3,"file":"web.js","names":[],"sources":["../src/web.ts"],"sourcesContent":["import { appendRedirectParamToUrl } from './url';\n\n/**\n * Appends the current browser URL (including path, query, and hash) as the `redirect` query parameter to the given URL.\n *\n * @param {string} url - The base URL to modify\n *\n * @returns {string} A new URL with the current location as the `redirect` parameter\n */\nexport function appendRedirectParamFromCurrentLocationToUrl(url: string) {\n const currentPath = `${window.location.pathname}${window.location.search}${window.location.hash}`;\n return appendRedirectParamToUrl(url, currentPath);\n}\n\n/**\n * Navigates to the given URL, appending the current browser location\n * (path, query, and hash) as the `redirect` query parameter.\n *\n * Useful for redirecting to login or other gateways while preserving\n * the current location for post-auth navigation.\n *\n * @param {string} url - The destination URL to navigate to\n * @param {number} [delayMs] - Optional delay in milliseconds before navigation\n */\nexport function assignUrlWithRedirectParamFromCurrentLocation(url: string, delayMs?: number) {\n if (delayMs === undefined) window.location.assign(appendRedirectParamFromCurrentLocationToUrl(url));\n else return setTimeout(() => window.location.assign(appendRedirectParamFromCurrentLocationToUrl(url)), delayMs);\n}\n"],"mappings":";;;;;;;;;AASA,SAAgB,4CAA4C,KAAa;CAErE,OAAO,yBAAyB,KAAK,GADd,OAAO,SAAS,WAAW,OAAO,SAAS,SAAS,OAAO,SAAS,OAC1C;;;;;;;;;;;;AAarD,SAAgB,8CAA8C,KAAa,SAAkB;CACzF,IAAI,YAAY,KAAA,GAAW,OAAO,SAAS,OAAO,4CAA4C,IAAI,CAAC;MAC9F,OAAO,iBAAiB,OAAO,SAAS,OAAO,4CAA4C,IAAI,CAAC,EAAE,QAAQ"}
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@kikiutils/shared",
3
3
  "type": "module",
4
- "version": "15.0.2",
4
+ "version": "15.1.0",
5
5
  "description": "A lightweight and modular utility library for modern JavaScript and TypeScript — includes secure hashing, flexible logging, datetime tools, Vue/web helpers, storage abstraction, and more.",
6
6
  "author": "kiki-kanri",
7
7
  "license": "MIT",
@@ -77,7 +77,7 @@
77
77
  "element-plus": "^2.13.7",
78
78
  "lru-cache": "^11.3.5",
79
79
  "millify": "^6.1.0",
80
- "msgpackr": "^1.11.9",
80
+ "msgpackr": "^2.0.1",
81
81
  "node-ssh": "^13.2.1",
82
82
  "pino": "^10.3.1",
83
83
  "pino-pretty": "^13.1.3",
@@ -136,12 +136,12 @@
136
136
  },
137
137
  "devDependencies": {
138
138
  "@antfu/eslint-config": "^8.2.0",
139
- "@kikiutils/eslint-config": "^9.0.1",
139
+ "@kikiutils/eslint-config": "^9.1.2",
140
140
  "@kikiutils/tsconfigs": "^6.0.0",
141
141
  "@noble/hashes": "^2.2.0",
142
- "@types/node": "^25.6.0",
142
+ "@types/node": "^25.6.2",
143
143
  "@types/ssh2": "^1.15.5",
144
- "@vitest/coverage-v8": "^4.1.4",
144
+ "@vitest/coverage-v8": "^4.1.5",
145
145
  "async-validator": "^4.2.5",
146
146
  "bson": "^7.2.0",
147
147
  "changelogen": "^0.6.2",
@@ -149,30 +149,21 @@
149
149
  "cross-env": "^10.1.0",
150
150
  "date-fns": "^4.1.0",
151
151
  "decimal.js": "^10.6.0",
152
- "element-plus": "^2.13.7",
153
- "jsdom": "^29.0.2",
154
- "knip": "^6.5.0",
155
- "lru-cache": "^11.3.5",
152
+ "element-plus": "^2.14.0",
153
+ "jsdom": "^29.1.1",
154
+ "knip": "^6.12.2",
155
+ "lru-cache": "^11.3.6",
156
156
  "millify": "^6.1.0",
157
- "msgpackr": "^1.11.10",
157
+ "msgpackr": "^2.0.1",
158
158
  "node-ssh": "^13.2.1",
159
159
  "pino": "^10.3.1",
160
160
  "pino-pretty": "^13.1.3",
161
- "publint": "^0.3.18",
162
- "tsdown": "^0.21.9",
161
+ "publint": "^0.3.20",
162
+ "tsdown": "^0.22.0",
163
163
  "typescript": "^6.0.3",
164
164
  "unplugin-unused": "^0.5.7",
165
- "vitest": "^4.1.4",
166
- "vue": "^3.5.32",
167
- "vue-router": "^5.0.4"
168
- },
169
- "pnpm": {
170
- "onlyBuiltDependencies": [
171
- "cpu-features",
172
- "esbuild",
173
- "msgpackr-extract",
174
- "ssh2",
175
- "vue-demi"
176
- ]
165
+ "vitest": "^4.1.5",
166
+ "vue": "^3.5.34",
167
+ "vue-router": "^5.0.6"
177
168
  }
178
169
  }
@@ -18,6 +18,10 @@ import {
18
18
  import type * as nodePath from 'node:path';
19
19
 
20
20
  type DropFirstParameters<T extends (...args: any) => any> = Parameters<T> extends [any, ...infer R] ? R : never;
21
+
22
+ /**
23
+ * Values accepted by the `Path` constructor and path-composition methods.
24
+ */
21
25
  export type PathLike = fs.PathLike | Path;
22
26
 
23
27
  /**
@@ -29,6 +33,11 @@ export type PathLike = fs.PathLike | Path;
29
33
  export class Path {
30
34
  readonly #value: string;
31
35
 
36
+ /**
37
+ * Creates a normalized path value by joining the provided path segments.
38
+ *
39
+ * @param {PathLike[]} paths - Path segments accepted by Node.js `path.join` or another `Path` instance
40
+ */
32
41
  constructor(...paths: PathLike[]) {
33
42
  this.#value = join(...this.#toStrings(paths));
34
43
  }
@@ -1,22 +1,28 @@
1
1
  import { Decimal } from 'decimal.js';
2
2
 
3
+ /**
4
+ * Values accepted by `PrecisionNumber` constructors and arithmetic methods.
5
+ */
3
6
  export type PrecisionNumberValue = Decimal.Value | PrecisionNumber | { toString: () => string };
4
7
 
5
8
  /**
6
- * Class representing a precision number with configurable decimal places and rounding.
9
+ * Chainable fixed-decimal wrapper around `decimal.js`.
7
10
  *
8
- * This class leverages the Decimal.js library to ensure accurate arithmetic operations
9
- * with floating point numbers. It provides methods for various arithmetic operations
10
- * (addition, subtraction, multiplication, division) as well as comparison and utility
11
- * methods to check the state of the number (e.g., if it is finite, an integer, zero, etc.).
11
+ * `PrecisionNumber` stores values as `Decimal`, rounds every mutating operation to
12
+ * the configured decimal places, and exposes both mutable and immutable arithmetic
13
+ * methods. Mutable methods (`plus`, `minus`, `times`, `dividedBy`, `modulo`,
14
+ * `pow`, `clamp`, `ceil`, `floor`, `negate`, `absoluteValue`) update the current
15
+ * instance and return `this`; immutable methods prefixed with `to` return a new
16
+ * `PrecisionNumber` with the same precision settings.
12
17
  *
13
- * The class also includes custom symbol methods to support Node.js inspection and primitive
14
- * type conversion.
18
+ * @example
19
+ * ```typescript
20
+ * import { PrecisionNumber } from '@kikiutils/shared/classes/precision-number';
15
21
  *
16
- * The class supports in-place modification methods that alter the current instance's value,
17
- * such as `plus`, `minus`, `times`, `dividedBy`, and others. Additionally, methods prefixed
18
- * with `to` (e.g., `toPlus`, `toMinus`) return a new instance of `PrecisionNumber` with the
19
- * modified value, leaving the original instance unchanged.
22
+ * const value = new PrecisionNumber('1.239', 2).plus('2.111');
23
+ * value.toString(); // '3.35'
24
+ * value.toMinus('1').toString(); // '2.35' (original value remains '3.35')
25
+ * ```
20
26
  */
21
27
  export class PrecisionNumber {
22
28
  // Private properties
@@ -24,6 +30,13 @@ export class PrecisionNumber {
24
30
  readonly #rounding: Decimal.Rounding;
25
31
  #decimal: Decimal;
26
32
 
33
+ /**
34
+ * Creates a fixed-decimal number.
35
+ *
36
+ * @param {PrecisionNumberValue} [value] - Initial value (default: `'0'`)
37
+ * @param {number} [decimalPlaces] - Decimal places retained after operations (default: `2`)
38
+ * @param {Decimal.Rounding} [rounding] - Decimal.js rounding mode (default: `Decimal.ROUND_DOWN`)
39
+ */
27
40
  constructor(
28
41
  value: PrecisionNumberValue = '0',
29
42
  decimalPlaces: number = 2,
@@ -35,10 +48,19 @@ export class PrecisionNumber {
35
48
  }
36
49
 
37
50
  // Symbols
51
+
52
+ /**
53
+ * Returns the formatted value when inspected by Node.js utilities.
54
+ */
38
55
  [Symbol.for('nodejs.util.inspect.custom')]() {
39
56
  return this.value;
40
57
  }
41
58
 
59
+ /**
60
+ * Converts to a number for numeric coercion and to the fixed string otherwise.
61
+ *
62
+ * @param {string} hint - JavaScript primitive-conversion hint
63
+ */
42
64
  [Symbol.toPrimitive](hint: string) {
43
65
  if (hint === 'number') return this.#decimal.toNumber();
44
66
  return this.value;
@@ -49,12 +71,42 @@ export class PrecisionNumber {
49
71
  return decimal.toDecimalPlaces(this.#decimalPlaces, this.#rounding);
50
72
  }
51
73
 
74
+ #valueToString(value: PrecisionNumberValue) {
75
+ return value.toString().trim();
76
+ }
77
+
52
78
  // Public getters
79
+
80
+ /**
81
+ * Decimal places retained by mutating operations and default formatting.
82
+ */
83
+ get decimalPlaces() {
84
+ return this.#decimalPlaces;
85
+ }
86
+
87
+ /**
88
+ * Decimal.js rounding mode used by mutating operations and default formatting.
89
+ */
90
+ get rounding() {
91
+ return this.#rounding;
92
+ }
93
+
94
+ /**
95
+ * Fixed-decimal string using the instance precision and rounding mode.
96
+ */
53
97
  get value() {
54
98
  return this.#decimal.toFixed(this.#decimalPlaces, this.#rounding);
55
99
  }
56
100
 
57
- // Static methods
101
+ /**
102
+ * Formats a value with decimal.js without creating a reusable instance.
103
+ *
104
+ * @param {PrecisionNumberValue} value - Value to format
105
+ * @param {number} [decimalPlaces] - Decimal places to output (default: `2`)
106
+ * @param {Decimal.Rounding} [rounding] - Decimal.js rounding mode (default: `Decimal.ROUND_DOWN`)
107
+ *
108
+ * @returns {string} Fixed-decimal string
109
+ */
58
110
  static toFixed(
59
111
  value: PrecisionNumberValue,
60
112
  decimalPlaces: number = 2,
@@ -64,117 +116,340 @@ export class PrecisionNumber {
64
116
  }
65
117
 
66
118
  // Public methods
119
+
120
+ /**
121
+ * Replaces the current value with its absolute value.
122
+ *
123
+ * @returns {this} The current instance for chaining
124
+ */
67
125
  absoluteValue() {
68
126
  this.#decimal = this.#decimalToFixedDecimal(this.#decimal.absoluteValue());
69
127
  return this;
70
128
  }
71
129
 
130
+ /**
131
+ * Rounds the current value up to the nearest integer in place.
132
+ *
133
+ * The stored value still uses this instance's configured decimal places for output.
134
+ *
135
+ * @returns {this} The current instance for chaining
136
+ */
137
+ ceil() {
138
+ this.#decimal = this.#decimalToFixedDecimal(this.#decimal.ceil());
139
+ return this;
140
+ }
141
+
142
+ /**
143
+ * Restricts the current value to the inclusive range `[min, max]` in place.
144
+ *
145
+ * @param {PrecisionNumberValue} min - Lower bound
146
+ * @param {PrecisionNumberValue} max - Upper bound
147
+ *
148
+ * @returns {this} The current instance for chaining
149
+ *
150
+ * @throws {Error} If `min` is greater than `max`
151
+ */
152
+ clamp(min: PrecisionNumberValue, max: PrecisionNumberValue) {
153
+ const minDecimal = new Decimal(this.#valueToString(min));
154
+ const maxDecimal = new Decimal(this.#valueToString(max));
155
+ if (minDecimal.gt(maxDecimal)) throw new Error('Invalid clamp range: min cannot be greater than max');
156
+ this.#decimal = this.#decimalToFixedDecimal(Decimal.min(Decimal.max(this.#decimal, minDecimal), maxDecimal));
157
+ return this;
158
+ }
159
+
160
+ /**
161
+ * Returns a new instance with the same value, decimal places, and rounding mode.
162
+ */
163
+ clone() {
164
+ return new PrecisionNumber(this.#decimal, this.#decimalPlaces, this.#rounding);
165
+ }
166
+
167
+ /**
168
+ * Divides the current value by another value in place.
169
+ *
170
+ * @param {PrecisionNumberValue} value - Divisor
171
+ *
172
+ * @returns {this} The current instance for chaining
173
+ */
72
174
  dividedBy(value: PrecisionNumberValue) {
73
- this.#decimal = this.#decimalToFixedDecimal(this.#decimal.dividedBy(value.toString().trim()));
175
+ this.#decimal = this.#decimalToFixedDecimal(this.#decimal.dividedBy(this.#valueToString(value)));
74
176
  return this;
75
177
  }
76
178
 
179
+ /**
180
+ * Checks whether the current value equals another value.
181
+ *
182
+ * @param {PrecisionNumberValue} value - Value to compare
183
+ *
184
+ * @returns {boolean} `true` when both numeric values are equal
185
+ */
77
186
  equals(value: PrecisionNumberValue) {
78
- return this.#decimal.equals(value.toString().trim());
187
+ return this.#decimal.equals(this.#valueToString(value));
188
+ }
189
+
190
+ /**
191
+ * Rounds the current value down to the nearest integer in place.
192
+ *
193
+ * The stored value still uses this instance's configured decimal places for output.
194
+ *
195
+ * @returns {this} The current instance for chaining
196
+ */
197
+ floor() {
198
+ this.#decimal = this.#decimalToFixedDecimal(this.#decimal.floor());
199
+ return this;
79
200
  }
80
201
 
202
+ /**
203
+ * Checks whether the current value is greater than another value.
204
+ */
81
205
  gt(value: PrecisionNumberValue) {
82
- return this.#decimal.gt(value.toString().trim());
206
+ return this.#decimal.gt(this.#valueToString(value));
83
207
  }
84
208
 
209
+ /**
210
+ * Checks whether the current value is greater than or equal to another value.
211
+ */
85
212
  gte(value: PrecisionNumberValue) {
86
- return this.#decimal.gte(value.toString().trim());
213
+ return this.#decimal.gte(this.#valueToString(value));
87
214
  }
88
215
 
216
+ /**
217
+ * Checks whether the current value is finite.
218
+ */
89
219
  isFinite() {
90
220
  return this.#decimal.isFinite();
91
221
  }
92
222
 
223
+ /**
224
+ * Checks whether the current value is an integer.
225
+ */
93
226
  isInteger() {
94
227
  return this.#decimal.isInteger();
95
228
  }
96
229
 
230
+ /**
231
+ * Checks whether the current value is `NaN`.
232
+ */
97
233
  isNaN() {
98
234
  return this.#decimal.isNaN();
99
235
  }
100
236
 
237
+ /**
238
+ * Checks whether the current value is negative.
239
+ */
101
240
  isNegative() {
102
241
  return this.#decimal.isNegative();
103
242
  }
104
243
 
244
+ /**
245
+ * Checks whether the current value is positive.
246
+ */
105
247
  isPositive() {
106
248
  return this.#decimal.isPositive();
107
249
  }
108
250
 
251
+ /**
252
+ * Checks whether the current value is zero.
253
+ */
109
254
  isZero() {
110
255
  return this.#decimal.isZero();
111
256
  }
112
257
 
258
+ /**
259
+ * Checks whether the current value is less than another value.
260
+ */
113
261
  lt(value: PrecisionNumberValue) {
114
- return this.#decimal.lt(value.toString().trim());
262
+ return this.#decimal.lt(this.#valueToString(value));
115
263
  }
116
264
 
265
+ /**
266
+ * Checks whether the current value is less than or equal to another value.
267
+ */
117
268
  lte(value: PrecisionNumberValue) {
118
- return this.#decimal.lte(value.toString().trim());
269
+ return this.#decimal.lte(this.#valueToString(value));
119
270
  }
120
271
 
272
+ /**
273
+ * Subtracts another value from the current value in place.
274
+ */
121
275
  minus(value: PrecisionNumberValue) {
122
- this.#decimal = this.#decimalToFixedDecimal(this.#decimal.minus(value.toString().trim()));
276
+ this.#decimal = this.#decimalToFixedDecimal(this.#decimal.minus(this.#valueToString(value)));
277
+ return this;
278
+ }
279
+
280
+ /**
281
+ * Replaces the current value with the remainder after division by another value.
282
+ *
283
+ * @param {PrecisionNumberValue} value - Divisor used to compute the remainder
284
+ *
285
+ * @returns {this} The current instance for chaining
286
+ */
287
+ modulo(value: PrecisionNumberValue) {
288
+ this.#decimal = this.#decimalToFixedDecimal(this.#decimal.modulo(this.#valueToString(value)));
123
289
  return this;
124
290
  }
125
291
 
292
+ /**
293
+ * Negates the current value in place.
294
+ */
126
295
  negate() {
127
296
  this.#decimal = this.#decimalToFixedDecimal(this.#decimal.negated());
128
297
  return this;
129
298
  }
130
299
 
300
+ /**
301
+ * Adds another value to the current value in place.
302
+ */
131
303
  plus(value: PrecisionNumberValue) {
132
- this.#decimal = this.#decimalToFixedDecimal(this.#decimal.plus(value.toString().trim()));
304
+ this.#decimal = this.#decimalToFixedDecimal(this.#decimal.plus(this.#valueToString(value)));
305
+ return this;
306
+ }
307
+
308
+ /**
309
+ * Raises the current value to an exponent in place.
310
+ *
311
+ * @param {PrecisionNumberValue} value - Exponent
312
+ *
313
+ * @returns {this} The current instance for chaining
314
+ */
315
+ pow(value: PrecisionNumberValue) {
316
+ this.#decimal = this.#decimalToFixedDecimal(this.#decimal.pow(this.#valueToString(value)));
133
317
  return this;
134
318
  }
135
319
 
320
+ /**
321
+ * Multiplies the current value by another value in place.
322
+ */
136
323
  times(value: PrecisionNumberValue) {
137
- this.#decimal = this.#decimalToFixedDecimal(this.#decimal.times(value.toString().trim()));
324
+ this.#decimal = this.#decimalToFixedDecimal(this.#decimal.times(this.#valueToString(value)));
138
325
  return this;
139
326
  }
140
327
 
328
+ /**
329
+ * Returns a new instance with the absolute value.
330
+ */
141
331
  toAbsoluteValue() {
142
332
  return new PrecisionNumber(this.#decimal.absoluteValue(), this.#decimalPlaces, this.#rounding);
143
333
  }
144
334
 
335
+ /**
336
+ * Returns a new instance rounded up to the nearest integer.
337
+ */
338
+ toCeil() {
339
+ return new PrecisionNumber(this.#decimal.ceil(), this.#decimalPlaces, this.#rounding);
340
+ }
341
+
342
+ /**
343
+ * Returns a new instance restricted to the inclusive range `[min, max]`.
344
+ *
345
+ * @param {PrecisionNumberValue} min - Lower bound
346
+ * @param {PrecisionNumberValue} max - Upper bound
347
+ *
348
+ * @throws {Error} If `min` is greater than `max`
349
+ */
350
+ toClamped(min: PrecisionNumberValue, max: PrecisionNumberValue) {
351
+ return this.clone().clamp(min, max);
352
+ }
353
+
354
+ /**
355
+ * Returns a new instance divided by another value.
356
+ */
145
357
  toDividedBy(value: PrecisionNumberValue) {
146
358
  return new PrecisionNumber(
147
- this.#decimal.dividedBy(value.toString().trim()),
359
+ this.#decimal.dividedBy(this.#valueToString(value)),
148
360
  this.#decimalPlaces,
149
361
  this.#rounding,
150
362
  );
151
363
  }
152
364
 
365
+ /**
366
+ * Returns a new instance rounded down to the nearest integer.
367
+ */
368
+ toFloor() {
369
+ return new PrecisionNumber(this.#decimal.floor(), this.#decimalPlaces, this.#rounding);
370
+ }
371
+
372
+ /**
373
+ * Serializes to the fixed-decimal string.
374
+ */
153
375
  toJSON() {
154
376
  return this.value;
155
377
  }
156
378
 
379
+ /**
380
+ * Returns a new instance with another value subtracted.
381
+ */
157
382
  toMinus(value: PrecisionNumberValue) {
158
- return new PrecisionNumber(this.#decimal.minus(value.toString().trim()), this.#decimalPlaces, this.#rounding);
383
+ return new PrecisionNumber(
384
+ this.#decimal.minus(this.#valueToString(value)),
385
+ this.#decimalPlaces,
386
+ this.#rounding,
387
+ );
159
388
  }
160
389
 
390
+ /**
391
+ * Returns a new instance with the remainder after division by another value.
392
+ */
393
+ toModulo(value: PrecisionNumberValue) {
394
+ return new PrecisionNumber(
395
+ this.#decimal.modulo(this.#valueToString(value)),
396
+ this.#decimalPlaces,
397
+ this.#rounding,
398
+ );
399
+ }
400
+
401
+ /**
402
+ * Returns a new instance with the value negated.
403
+ */
161
404
  toNegated() {
162
405
  return new PrecisionNumber(this.#decimal.negated(), this.#decimalPlaces, this.#rounding);
163
406
  }
164
407
 
408
+ /**
409
+ * Returns a new instance with another value added.
410
+ */
165
411
  toPlus(value: PrecisionNumberValue) {
166
- return new PrecisionNumber(this.#decimal.plus(value.toString().trim()), this.#decimalPlaces, this.#rounding);
412
+ return new PrecisionNumber(this.#decimal.plus(this.#valueToString(value)), this.#decimalPlaces, this.#rounding);
413
+ }
414
+
415
+ /**
416
+ * Returns a new instance raised to an exponent.
417
+ */
418
+ toPow(value: PrecisionNumberValue) {
419
+ return new PrecisionNumber(this.#decimal.pow(this.#valueToString(value)), this.#decimalPlaces, this.#rounding);
167
420
  }
168
421
 
422
+ /**
423
+ * Converts the current value to a JavaScript number.
424
+ *
425
+ * Use `toString`/`value` when preserving decimal precision is more important than native-number ergonomics.
426
+ */
427
+ toNumber() {
428
+ return this.#decimal.toNumber();
429
+ }
430
+
431
+ /**
432
+ * Converts the current value to its fixed-decimal string.
433
+ */
169
434
  toString() {
170
435
  return this.value;
171
436
  }
172
437
 
438
+ /**
439
+ * Formats the current value with custom precision and rounding for this call only.
440
+ */
173
441
  toFixed(decimalPlaces: number = this.#decimalPlaces, rounding: Decimal.Rounding = this.#rounding) {
174
442
  return this.#decimal.toFixed(decimalPlaces, rounding);
175
443
  }
176
444
 
445
+ /**
446
+ * Returns a new instance multiplied by another value.
447
+ */
177
448
  toTimes(value: PrecisionNumberValue) {
178
- return new PrecisionNumber(this.#decimal.times(value.toString().trim()), this.#decimalPlaces, this.#rounding);
449
+ return new PrecisionNumber(
450
+ this.#decimal.times(this.#valueToString(value)),
451
+ this.#decimalPlaces,
452
+ this.#rounding,
453
+ );
179
454
  }
180
455
  }