@kikiutils/shared 9.0.0 → 9.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 (96) hide show
  1. package/README.md +15 -0
  2. package/dist/consola.cjs +1 -1
  3. package/dist/consola.cjs.map +1 -1
  4. package/dist/consola.d.ts +1 -1
  5. package/dist/consola.mjs +1 -1
  6. package/dist/consola.mjs.map +1 -1
  7. package/dist/crypto-hash.cjs +1 -0
  8. package/dist/crypto-hash.cjs.map +1 -1
  9. package/dist/crypto-hash.d.ts +1 -0
  10. package/dist/crypto-hash.d.ts.map +1 -1
  11. package/dist/crypto-hash.mjs +1 -0
  12. package/dist/crypto-hash.mjs.map +1 -1
  13. package/dist/datetime.cjs +3 -0
  14. package/dist/datetime.cjs.map +1 -1
  15. package/dist/datetime.d.ts +3 -0
  16. package/dist/datetime.d.ts.map +1 -1
  17. package/dist/datetime.mjs +3 -0
  18. package/dist/datetime.mjs.map +1 -1
  19. package/dist/enum.cjs +2 -0
  20. package/dist/enum.cjs.map +1 -1
  21. package/dist/enum.d.ts +2 -0
  22. package/dist/enum.d.ts.map +1 -1
  23. package/dist/enum.mjs +2 -0
  24. package/dist/enum.mjs.map +1 -1
  25. package/dist/env.cjs +2 -0
  26. package/dist/env.cjs.map +1 -1
  27. package/dist/env.d.ts +2 -0
  28. package/dist/env.d.ts.map +1 -1
  29. package/dist/env.mjs +2 -0
  30. package/dist/env.mjs.map +1 -1
  31. package/dist/general.cjs.map +1 -1
  32. package/dist/general.d.ts +1 -0
  33. package/dist/general.d.ts.map +1 -1
  34. package/dist/general.mjs.map +1 -1
  35. package/dist/math.cjs +1 -0
  36. package/dist/math.cjs.map +1 -1
  37. package/dist/math.d.ts +3 -0
  38. package/dist/math.d.ts.map +1 -1
  39. package/dist/math.mjs +1 -0
  40. package/dist/math.mjs.map +1 -1
  41. package/dist/number.cjs +1 -0
  42. package/dist/number.cjs.map +1 -1
  43. package/dist/number.d.ts +1 -0
  44. package/dist/number.d.ts.map +1 -1
  45. package/dist/number.mjs +1 -0
  46. package/dist/number.mjs.map +1 -1
  47. package/dist/pino.cjs +1 -1
  48. package/dist/pino.cjs.map +1 -1
  49. package/dist/pino.d.ts +1 -1
  50. package/dist/pino.mjs +1 -1
  51. package/dist/pino.mjs.map +1 -1
  52. package/dist/random.cjs +8 -7
  53. package/dist/random.cjs.map +1 -1
  54. package/dist/random.d.ts +8 -7
  55. package/dist/random.d.ts.map +1 -1
  56. package/dist/random.mjs +8 -7
  57. package/dist/random.mjs.map +1 -1
  58. package/dist/string.cjs +1 -0
  59. package/dist/string.cjs.map +1 -1
  60. package/dist/string.d.ts +1 -0
  61. package/dist/string.d.ts.map +1 -1
  62. package/dist/string.mjs +1 -0
  63. package/dist/string.mjs.map +1 -1
  64. package/dist/url.cjs +21 -0
  65. package/dist/url.cjs.map +1 -0
  66. package/dist/url.d.ts +12 -0
  67. package/dist/url.d.ts.map +1 -0
  68. package/dist/url.mjs +19 -0
  69. package/dist/url.mjs.map +1 -0
  70. package/dist/vue.cjs +64 -0
  71. package/dist/vue.cjs.map +1 -0
  72. package/dist/vue.d.ts +31 -0
  73. package/dist/vue.d.ts.map +1 -0
  74. package/dist/vue.mjs +59 -0
  75. package/dist/vue.mjs.map +1 -0
  76. package/dist/web.cjs +18 -0
  77. package/dist/web.cjs.map +1 -0
  78. package/dist/web.d.ts +9 -0
  79. package/dist/web.d.ts.map +1 -0
  80. package/dist/web.mjs +16 -0
  81. package/dist/web.mjs.map +1 -0
  82. package/package.json +5 -2
  83. package/src/consola.ts +1 -1
  84. package/src/crypto-hash.ts +1 -0
  85. package/src/datetime.ts +3 -0
  86. package/src/enum.ts +2 -0
  87. package/src/env.ts +2 -0
  88. package/src/general.ts +1 -0
  89. package/src/math.ts +3 -0
  90. package/src/number.ts +1 -0
  91. package/src/pino.ts +1 -1
  92. package/src/random.ts +8 -7
  93. package/src/string.ts +1 -0
  94. package/src/url.ts +16 -0
  95. package/src/vue.ts +62 -0
  96. package/src/web.ts +13 -0
package/README.md CHANGED
@@ -124,6 +124,21 @@ Each module file includes function-level comments and usage examples.
124
124
 
125
125
  - `randomString`
126
126
 
127
+ ### [url](./src/url.ts)
128
+
129
+ - `appendRedirectParamToUrl`
130
+
131
+ ### [vue](./src/vue.ts)
132
+
133
+ - `appendRedirectParamFromCurrentRouteToUrl`
134
+ - `clearIntervalRef`
135
+ - `clearTimeoutRef`
136
+ - `usePreserveScroll`
137
+
138
+ ### [web](./src/web.ts)
139
+
140
+ - `appendRedirectParamFromCurrentLocationToUrl`
141
+
127
142
  ## License
128
143
 
129
144
  [MIT License](./LICENSE)
package/dist/consola.cjs CHANGED
@@ -15,7 +15,7 @@ const consola = require('consola');
15
15
  *
16
16
  * @example
17
17
  * ```typescript
18
- * import logger from '@kikiutils/shared/consola';
18
+ * import { logger } from '@kikiutils/shared/consola';
19
19
  *
20
20
  * logger.info('test'); // ℹ test 3:56:30 AM
21
21
  *
@@ -1 +1 @@
1
- {"version":3,"file":"consola.cjs","sources":["../src/consola.ts"],"sourcesContent":["import { createConsola } from 'consola';\n\n/**\n * A consola logger instance.\n *\n * The logger's level is determined based on the `CONSOLA_LOGGER_LEVEL` and `NODE_ENV` environment variables.\n * If `CONSOLA_LOGGER_LEVEL` is set, it will be used; otherwise, if `NODE_ENV` is `production`,\n * the level will be set to `0`.\n *\n * To manually change the level, assign the desired level to `logger.level`.\n *\n * See available levels [here](https://github.com/unjs/consola?tab=readme-ov-file#log-level).\n *\n * @example\n * ```typescript\n * import logger from '@kikiutils/shared/consola';\n *\n * logger.info('test'); // ℹ test 3:56:30 AM\n *\n * // Manually change the level\n * logger.level = 3;\n * ```\n */\nexport const consolaLogger = createConsola();\nexport const logger = consolaLogger;\nif (process.env.CONSOLA_LOGGER_LEVEL !== undefined) consolaLogger.level = +process.env.CONSOLA_LOGGER_LEVEL;\nelse consolaLogger.level = process.env.NODE_ENV === 'production' ? 0 : consolaLogger.level;\n"],"names":["createConsola"],"mappings":";;;;AAEA;;;;;;;;;;;;;;;;;;;;AAoBG;AACU,MAAA,aAAa,GAAGA,qBAAa;AACnC,MAAM,MAAM,GAAG;AACtB,IAAI,OAAO,CAAC,GAAG,CAAC,oBAAoB,KAAK,SAAS;IAAE,aAAa,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,oBAAoB;;IACtG,aAAa,CAAC,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,GAAG,CAAC,GAAG,aAAa,CAAC,KAAK;;;;;"}
1
+ {"version":3,"file":"consola.cjs","sources":["../src/consola.ts"],"sourcesContent":["import { createConsola } from 'consola';\n\n/**\n * A consola logger instance.\n *\n * The logger's level is determined based on the `CONSOLA_LOGGER_LEVEL` and `NODE_ENV` environment variables.\n * If `CONSOLA_LOGGER_LEVEL` is set, it will be used; otherwise, if `NODE_ENV` is `production`,\n * the level will be set to `0`.\n *\n * To manually change the level, assign the desired level to `logger.level`.\n *\n * See available levels [here](https://github.com/unjs/consola?tab=readme-ov-file#log-level).\n *\n * @example\n * ```typescript\n * import { logger } from '@kikiutils/shared/consola';\n *\n * logger.info('test'); // ℹ test 3:56:30 AM\n *\n * // Manually change the level\n * logger.level = 3;\n * ```\n */\nexport const consolaLogger = createConsola();\nexport const logger = consolaLogger;\nif (process.env.CONSOLA_LOGGER_LEVEL !== undefined) consolaLogger.level = +process.env.CONSOLA_LOGGER_LEVEL;\nelse consolaLogger.level = process.env.NODE_ENV === 'production' ? 0 : consolaLogger.level;\n"],"names":["createConsola"],"mappings":";;;;AAEA;;;;;;;;;;;;;;;;;;;;AAoBG;AACU,MAAA,aAAa,GAAGA,qBAAa;AACnC,MAAM,MAAM,GAAG;AACtB,IAAI,OAAO,CAAC,GAAG,CAAC,oBAAoB,KAAK,SAAS;IAAE,aAAa,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,oBAAoB;;IACtG,aAAa,CAAC,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,GAAG,CAAC,GAAG,aAAa,CAAC,KAAK;;;;;"}
package/dist/consola.d.ts CHANGED
@@ -11,7 +11,7 @@
11
11
  *
12
12
  * @example
13
13
  * ```typescript
14
- * import logger from '@kikiutils/shared/consola';
14
+ * import { logger } from '@kikiutils/shared/consola';
15
15
  *
16
16
  * logger.info('test'); // ℹ test 3:56:30 AM
17
17
  *
package/dist/consola.mjs CHANGED
@@ -13,7 +13,7 @@ import { createConsola } from 'consola';
13
13
  *
14
14
  * @example
15
15
  * ```typescript
16
- * import logger from '@kikiutils/shared/consola';
16
+ * import { logger } from '@kikiutils/shared/consola';
17
17
  *
18
18
  * logger.info('test'); // ℹ test 3:56:30 AM
19
19
  *
@@ -1 +1 @@
1
- {"version":3,"file":"consola.mjs","sources":["../src/consola.ts"],"sourcesContent":["import { createConsola } from 'consola';\n\n/**\n * A consola logger instance.\n *\n * The logger's level is determined based on the `CONSOLA_LOGGER_LEVEL` and `NODE_ENV` environment variables.\n * If `CONSOLA_LOGGER_LEVEL` is set, it will be used; otherwise, if `NODE_ENV` is `production`,\n * the level will be set to `0`.\n *\n * To manually change the level, assign the desired level to `logger.level`.\n *\n * See available levels [here](https://github.com/unjs/consola?tab=readme-ov-file#log-level).\n *\n * @example\n * ```typescript\n * import logger from '@kikiutils/shared/consola';\n *\n * logger.info('test'); // ℹ test 3:56:30 AM\n *\n * // Manually change the level\n * logger.level = 3;\n * ```\n */\nexport const consolaLogger = createConsola();\nexport const logger = consolaLogger;\nif (process.env.CONSOLA_LOGGER_LEVEL !== undefined) consolaLogger.level = +process.env.CONSOLA_LOGGER_LEVEL;\nelse consolaLogger.level = process.env.NODE_ENV === 'production' ? 0 : consolaLogger.level;\n"],"names":[],"mappings":";;AAEA;;;;;;;;;;;;;;;;;;;;AAoBG;AACU,MAAA,aAAa,GAAG,aAAa;AACnC,MAAM,MAAM,GAAG;AACtB,IAAI,OAAO,CAAC,GAAG,CAAC,oBAAoB,KAAK,SAAS;IAAE,aAAa,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,oBAAoB;;IACtG,aAAa,CAAC,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,GAAG,CAAC,GAAG,aAAa,CAAC,KAAK;;;;"}
1
+ {"version":3,"file":"consola.mjs","sources":["../src/consola.ts"],"sourcesContent":["import { createConsola } from 'consola';\n\n/**\n * A consola logger instance.\n *\n * The logger's level is determined based on the `CONSOLA_LOGGER_LEVEL` and `NODE_ENV` environment variables.\n * If `CONSOLA_LOGGER_LEVEL` is set, it will be used; otherwise, if `NODE_ENV` is `production`,\n * the level will be set to `0`.\n *\n * To manually change the level, assign the desired level to `logger.level`.\n *\n * See available levels [here](https://github.com/unjs/consola?tab=readme-ov-file#log-level).\n *\n * @example\n * ```typescript\n * import { logger } from '@kikiutils/shared/consola';\n *\n * logger.info('test'); // ℹ test 3:56:30 AM\n *\n * // Manually change the level\n * logger.level = 3;\n * ```\n */\nexport const consolaLogger = createConsola();\nexport const logger = consolaLogger;\nif (process.env.CONSOLA_LOGGER_LEVEL !== undefined) consolaLogger.level = +process.env.CONSOLA_LOGGER_LEVEL;\nelse consolaLogger.level = process.env.NODE_ENV === 'production' ? 0 : consolaLogger.level;\n"],"names":[],"mappings":";;AAEA;;;;;;;;;;;;;;;;;;;;AAoBG;AACU,MAAA,aAAa,GAAG,aAAa;AACnC,MAAM,MAAM,GAAG;AACtB,IAAI,OAAO,CAAC,GAAG,CAAC,oBAAoB,KAAK,SAAS;IAAE,aAAa,CAAC,KAAK,GAAG,CAAC,OAAO,CAAC,GAAG,CAAC,oBAAoB;;IACtG,aAAa,CAAC,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,QAAQ,KAAK,YAAY,GAAG,CAAC,GAAG,aAAa,CAAC,KAAK;;;;"}
@@ -6,6 +6,7 @@ const node_crypto = require('node:crypto');
6
6
  * This file provides a set of functions for creating hash digests using different algorithms and bit lengths.
7
7
  * It includes functions for generating SHA-3 hash digests with bit lengths of 224, 256, 384, and 512,
8
8
  * as well as a function for generating MD5 hash digests.
9
+ *
9
10
  * These functions use the Node.js crypto module to generate the hashes.
10
11
  * Can only be used in Node.js/Deno/Bun runtimes.
11
12
  *
@@ -1 +1 @@
1
- {"version":3,"file":"crypto-hash.cjs","sources":["../src/crypto-hash.ts"],"sourcesContent":["/**\n * This file provides a set of functions for creating hash digests using different algorithms and bit lengths.\n * It includes functions for generating SHA-3 hash digests with bit lengths of 224, 256, 384, and 512,\n * as well as a function for generating MD5 hash digests.\n * These functions use the Node.js crypto module to generate the hashes.\n * Can only be used in Node.js/Deno/Bun runtimes.\n *\n * @example\n * ```typescript\n * import { cryptoSha3256 } from '@kikiutils/shared/crypto-hash';\n *\n * console.log(cryptoSha3256('test')); // 36f028580bb02cc8272a9a020f4200e346e276ae664e45ee80745574e2f5ab80\n * ```\n */\n\nimport { createHash } from 'node:crypto';\nimport type {\n BinaryLike,\n BinaryToTextEncoding,\n} from 'node:crypto';\n\nexport function cryptoMd5(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('md5').update(data).digest(outputEncoding);\n}\n\nexport function cryptoMd5ToBuffer(data: BinaryLike) {\n return createHash('md5').update(data).digest();\n}\n\nexport function cryptoSha3224(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('sha3-224').update(data).digest(outputEncoding);\n}\n\nexport function cryptoSha3224ToBuffer(data: BinaryLike) {\n return createHash('sha3-224').update(data).digest();\n}\n\nexport function cryptoSha3256(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('sha3-256').update(data).digest(outputEncoding);\n}\n\nexport function cryptoSha3256ToBuffer(data: BinaryLike) {\n return createHash('sha3-256').update(data).digest();\n}\n\nexport function cryptoSha3384(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('sha3-384').update(data).digest(outputEncoding);\n}\n\nexport function cryptoSha3384ToBuffer(data: BinaryLike) {\n return createHash('sha3-384').update(data).digest();\n}\n\nexport function cryptoSha3512(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('sha3-512').update(data).digest(outputEncoding);\n}\n\nexport function cryptoSha3512ToBuffer(data: BinaryLike) {\n return createHash('sha3-512').update(data).digest();\n}\n"],"names":["createHash"],"mappings":";;;;AAAA;;;;;;;;;;;;;AAaG;SAQa,SAAS,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACpF,IAAA,OAAOA,sBAAU,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AAChE;AAEM,SAAU,iBAAiB,CAAC,IAAgB,EAAA;AAC9C,IAAA,OAAOA,sBAAU,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AAClD;SAEgB,aAAa,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACxF,IAAA,OAAOA,sBAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AACrE;AAEM,SAAU,qBAAqB,CAAC,IAAgB,EAAA;AAClD,IAAA,OAAOA,sBAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AACvD;SAEgB,aAAa,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACxF,IAAA,OAAOA,sBAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AACrE;AAEM,SAAU,qBAAqB,CAAC,IAAgB,EAAA;AAClD,IAAA,OAAOA,sBAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AACvD;SAEgB,aAAa,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACxF,IAAA,OAAOA,sBAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AACrE;AAEM,SAAU,qBAAqB,CAAC,IAAgB,EAAA;AAClD,IAAA,OAAOA,sBAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AACvD;SAEgB,aAAa,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACxF,IAAA,OAAOA,sBAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AACrE;AAEM,SAAU,qBAAqB,CAAC,IAAgB,EAAA;AAClD,IAAA,OAAOA,sBAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AACvD;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"crypto-hash.cjs","sources":["../src/crypto-hash.ts"],"sourcesContent":["/**\n * This file provides a set of functions for creating hash digests using different algorithms and bit lengths.\n * It includes functions for generating SHA-3 hash digests with bit lengths of 224, 256, 384, and 512,\n * as well as a function for generating MD5 hash digests.\n *\n * These functions use the Node.js crypto module to generate the hashes.\n * Can only be used in Node.js/Deno/Bun runtimes.\n *\n * @example\n * ```typescript\n * import { cryptoSha3256 } from '@kikiutils/shared/crypto-hash';\n *\n * console.log(cryptoSha3256('test')); // 36f028580bb02cc8272a9a020f4200e346e276ae664e45ee80745574e2f5ab80\n * ```\n */\n\nimport { createHash } from 'node:crypto';\nimport type {\n BinaryLike,\n BinaryToTextEncoding,\n} from 'node:crypto';\n\nexport function cryptoMd5(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('md5').update(data).digest(outputEncoding);\n}\n\nexport function cryptoMd5ToBuffer(data: BinaryLike) {\n return createHash('md5').update(data).digest();\n}\n\nexport function cryptoSha3224(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('sha3-224').update(data).digest(outputEncoding);\n}\n\nexport function cryptoSha3224ToBuffer(data: BinaryLike) {\n return createHash('sha3-224').update(data).digest();\n}\n\nexport function cryptoSha3256(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('sha3-256').update(data).digest(outputEncoding);\n}\n\nexport function cryptoSha3256ToBuffer(data: BinaryLike) {\n return createHash('sha3-256').update(data).digest();\n}\n\nexport function cryptoSha3384(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('sha3-384').update(data).digest(outputEncoding);\n}\n\nexport function cryptoSha3384ToBuffer(data: BinaryLike) {\n return createHash('sha3-384').update(data).digest();\n}\n\nexport function cryptoSha3512(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('sha3-512').update(data).digest(outputEncoding);\n}\n\nexport function cryptoSha3512ToBuffer(data: BinaryLike) {\n return createHash('sha3-512').update(data).digest();\n}\n"],"names":["createHash"],"mappings":";;;;AAAA;;;;;;;;;;;;;;AAcG;SAQa,SAAS,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACpF,IAAA,OAAOA,sBAAU,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AAChE;AAEM,SAAU,iBAAiB,CAAC,IAAgB,EAAA;AAC9C,IAAA,OAAOA,sBAAU,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AAClD;SAEgB,aAAa,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACxF,IAAA,OAAOA,sBAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AACrE;AAEM,SAAU,qBAAqB,CAAC,IAAgB,EAAA;AAClD,IAAA,OAAOA,sBAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AACvD;SAEgB,aAAa,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACxF,IAAA,OAAOA,sBAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AACrE;AAEM,SAAU,qBAAqB,CAAC,IAAgB,EAAA;AAClD,IAAA,OAAOA,sBAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AACvD;SAEgB,aAAa,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACxF,IAAA,OAAOA,sBAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AACrE;AAEM,SAAU,qBAAqB,CAAC,IAAgB,EAAA;AAClD,IAAA,OAAOA,sBAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AACvD;SAEgB,aAAa,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACxF,IAAA,OAAOA,sBAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AACrE;AAEM,SAAU,qBAAqB,CAAC,IAAgB,EAAA;AAClD,IAAA,OAAOA,sBAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AACvD;;;;;;;;;;;;;"}
@@ -2,6 +2,7 @@
2
2
  * This file provides a set of functions for creating hash digests using different algorithms and bit lengths.
3
3
  * It includes functions for generating SHA-3 hash digests with bit lengths of 224, 256, 384, and 512,
4
4
  * as well as a function for generating MD5 hash digests.
5
+ *
5
6
  * These functions use the Node.js crypto module to generate the hashes.
6
7
  * Can only be used in Node.js/Deno/Bun runtimes.
7
8
  *
@@ -1 +1 @@
1
- {"version":3,"file":"crypto-hash.d.ts","sourceRoot":"","sources":["../src/crypto-hash.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;GAaG;AAGH,OAAO,KAAK,EACR,UAAU,EACV,oBAAoB,EACvB,MAAM,aAAa,CAAC;AAErB,wBAAgB,SAAS,CAAC,IAAI,EAAE,UAAU,EAAE,cAAc,GAAE,oBAA4B,UAEvF;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,UAAU,2BAEjD;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,UAAU,EAAE,cAAc,GAAE,oBAA4B,UAE3F;AAED,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,UAAU,2BAErD;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,UAAU,EAAE,cAAc,GAAE,oBAA4B,UAE3F;AAED,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,UAAU,2BAErD;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,UAAU,EAAE,cAAc,GAAE,oBAA4B,UAE3F;AAED,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,UAAU,2BAErD;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,UAAU,EAAE,cAAc,GAAE,oBAA4B,UAE3F;AAED,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,UAAU,2BAErD"}
1
+ {"version":3,"file":"crypto-hash.d.ts","sourceRoot":"","sources":["../src/crypto-hash.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AAGH,OAAO,KAAK,EACR,UAAU,EACV,oBAAoB,EACvB,MAAM,aAAa,CAAC;AAErB,wBAAgB,SAAS,CAAC,IAAI,EAAE,UAAU,EAAE,cAAc,GAAE,oBAA4B,UAEvF;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,UAAU,2BAEjD;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,UAAU,EAAE,cAAc,GAAE,oBAA4B,UAE3F;AAED,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,UAAU,2BAErD;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,UAAU,EAAE,cAAc,GAAE,oBAA4B,UAE3F;AAED,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,UAAU,2BAErD;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,UAAU,EAAE,cAAc,GAAE,oBAA4B,UAE3F;AAED,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,UAAU,2BAErD;AAED,wBAAgB,aAAa,CAAC,IAAI,EAAE,UAAU,EAAE,cAAc,GAAE,oBAA4B,UAE3F;AAED,wBAAgB,qBAAqB,CAAC,IAAI,EAAE,UAAU,2BAErD"}
@@ -4,6 +4,7 @@ import { createHash } from 'node:crypto';
4
4
  * This file provides a set of functions for creating hash digests using different algorithms and bit lengths.
5
5
  * It includes functions for generating SHA-3 hash digests with bit lengths of 224, 256, 384, and 512,
6
6
  * as well as a function for generating MD5 hash digests.
7
+ *
7
8
  * These functions use the Node.js crypto module to generate the hashes.
8
9
  * Can only be used in Node.js/Deno/Bun runtimes.
9
10
  *
@@ -1 +1 @@
1
- {"version":3,"file":"crypto-hash.mjs","sources":["../src/crypto-hash.ts"],"sourcesContent":["/**\n * This file provides a set of functions for creating hash digests using different algorithms and bit lengths.\n * It includes functions for generating SHA-3 hash digests with bit lengths of 224, 256, 384, and 512,\n * as well as a function for generating MD5 hash digests.\n * These functions use the Node.js crypto module to generate the hashes.\n * Can only be used in Node.js/Deno/Bun runtimes.\n *\n * @example\n * ```typescript\n * import { cryptoSha3256 } from '@kikiutils/shared/crypto-hash';\n *\n * console.log(cryptoSha3256('test')); // 36f028580bb02cc8272a9a020f4200e346e276ae664e45ee80745574e2f5ab80\n * ```\n */\n\nimport { createHash } from 'node:crypto';\nimport type {\n BinaryLike,\n BinaryToTextEncoding,\n} from 'node:crypto';\n\nexport function cryptoMd5(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('md5').update(data).digest(outputEncoding);\n}\n\nexport function cryptoMd5ToBuffer(data: BinaryLike) {\n return createHash('md5').update(data).digest();\n}\n\nexport function cryptoSha3224(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('sha3-224').update(data).digest(outputEncoding);\n}\n\nexport function cryptoSha3224ToBuffer(data: BinaryLike) {\n return createHash('sha3-224').update(data).digest();\n}\n\nexport function cryptoSha3256(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('sha3-256').update(data).digest(outputEncoding);\n}\n\nexport function cryptoSha3256ToBuffer(data: BinaryLike) {\n return createHash('sha3-256').update(data).digest();\n}\n\nexport function cryptoSha3384(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('sha3-384').update(data).digest(outputEncoding);\n}\n\nexport function cryptoSha3384ToBuffer(data: BinaryLike) {\n return createHash('sha3-384').update(data).digest();\n}\n\nexport function cryptoSha3512(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('sha3-512').update(data).digest(outputEncoding);\n}\n\nexport function cryptoSha3512ToBuffer(data: BinaryLike) {\n return createHash('sha3-512').update(data).digest();\n}\n"],"names":[],"mappings":";;AAAA;;;;;;;;;;;;;AAaG;SAQa,SAAS,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACpF,IAAA,OAAO,UAAU,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AAChE;AAEM,SAAU,iBAAiB,CAAC,IAAgB,EAAA;AAC9C,IAAA,OAAO,UAAU,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AAClD;SAEgB,aAAa,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACxF,IAAA,OAAO,UAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AACrE;AAEM,SAAU,qBAAqB,CAAC,IAAgB,EAAA;AAClD,IAAA,OAAO,UAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AACvD;SAEgB,aAAa,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACxF,IAAA,OAAO,UAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AACrE;AAEM,SAAU,qBAAqB,CAAC,IAAgB,EAAA;AAClD,IAAA,OAAO,UAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AACvD;SAEgB,aAAa,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACxF,IAAA,OAAO,UAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AACrE;AAEM,SAAU,qBAAqB,CAAC,IAAgB,EAAA;AAClD,IAAA,OAAO,UAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AACvD;SAEgB,aAAa,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACxF,IAAA,OAAO,UAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AACrE;AAEM,SAAU,qBAAqB,CAAC,IAAgB,EAAA;AAClD,IAAA,OAAO,UAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AACvD;;;;"}
1
+ {"version":3,"file":"crypto-hash.mjs","sources":["../src/crypto-hash.ts"],"sourcesContent":["/**\n * This file provides a set of functions for creating hash digests using different algorithms and bit lengths.\n * It includes functions for generating SHA-3 hash digests with bit lengths of 224, 256, 384, and 512,\n * as well as a function for generating MD5 hash digests.\n *\n * These functions use the Node.js crypto module to generate the hashes.\n * Can only be used in Node.js/Deno/Bun runtimes.\n *\n * @example\n * ```typescript\n * import { cryptoSha3256 } from '@kikiutils/shared/crypto-hash';\n *\n * console.log(cryptoSha3256('test')); // 36f028580bb02cc8272a9a020f4200e346e276ae664e45ee80745574e2f5ab80\n * ```\n */\n\nimport { createHash } from 'node:crypto';\nimport type {\n BinaryLike,\n BinaryToTextEncoding,\n} from 'node:crypto';\n\nexport function cryptoMd5(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('md5').update(data).digest(outputEncoding);\n}\n\nexport function cryptoMd5ToBuffer(data: BinaryLike) {\n return createHash('md5').update(data).digest();\n}\n\nexport function cryptoSha3224(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('sha3-224').update(data).digest(outputEncoding);\n}\n\nexport function cryptoSha3224ToBuffer(data: BinaryLike) {\n return createHash('sha3-224').update(data).digest();\n}\n\nexport function cryptoSha3256(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('sha3-256').update(data).digest(outputEncoding);\n}\n\nexport function cryptoSha3256ToBuffer(data: BinaryLike) {\n return createHash('sha3-256').update(data).digest();\n}\n\nexport function cryptoSha3384(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('sha3-384').update(data).digest(outputEncoding);\n}\n\nexport function cryptoSha3384ToBuffer(data: BinaryLike) {\n return createHash('sha3-384').update(data).digest();\n}\n\nexport function cryptoSha3512(data: BinaryLike, outputEncoding: BinaryToTextEncoding = 'hex') {\n return createHash('sha3-512').update(data).digest(outputEncoding);\n}\n\nexport function cryptoSha3512ToBuffer(data: BinaryLike) {\n return createHash('sha3-512').update(data).digest();\n}\n"],"names":[],"mappings":";;AAAA;;;;;;;;;;;;;;AAcG;SAQa,SAAS,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACpF,IAAA,OAAO,UAAU,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AAChE;AAEM,SAAU,iBAAiB,CAAC,IAAgB,EAAA;AAC9C,IAAA,OAAO,UAAU,CAAC,KAAK,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AAClD;SAEgB,aAAa,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACxF,IAAA,OAAO,UAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AACrE;AAEM,SAAU,qBAAqB,CAAC,IAAgB,EAAA;AAClD,IAAA,OAAO,UAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AACvD;SAEgB,aAAa,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACxF,IAAA,OAAO,UAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AACrE;AAEM,SAAU,qBAAqB,CAAC,IAAgB,EAAA;AAClD,IAAA,OAAO,UAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AACvD;SAEgB,aAAa,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACxF,IAAA,OAAO,UAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AACrE;AAEM,SAAU,qBAAqB,CAAC,IAAgB,EAAA;AAClD,IAAA,OAAO,UAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AACvD;SAEgB,aAAa,CAAC,IAAgB,EAAE,iBAAuC,KAAK,EAAA;AACxF,IAAA,OAAO,UAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,cAAc,CAAC;AACrE;AAEM,SAAU,qBAAqB,CAAC,IAAgB,EAAA;AAClD,IAAA,OAAO,UAAU,CAAC,UAAU,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE;AACvD;;;;"}
package/dist/datetime.cjs CHANGED
@@ -10,6 +10,7 @@ const dateFns = require('date-fns');
10
10
  * @param {DateArg<Date>} date - The input date to format. Can be a Date object, a timestamp, or a string.
11
11
  * @param {string} [format] - The target format string.
12
12
  * @param {FormatOptions} [options] - Optional formatting options passed to `date-fns/format`.
13
+ *
13
14
  * @returns {string} The formatted date string.
14
15
  *
15
16
  * @example
@@ -41,6 +42,7 @@ function formatDate(date, format = 'yyyy-MM-dd HH:mm:ss', options) {
41
42
  * @param {object} [options] - Optional settings.
42
43
  * @param {boolean} [options.setEndDateToNextDayStart] - If true, set `endDate` to 00:00:00.000 of the next day.
43
44
  * @param {Day} [options.weekStartsOn] - The start day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday).
45
+ *
44
46
  * @returns {{ startDate: Date, endDate: Date }} An object with `startDate` and `endDate`.
45
47
  *
46
48
  * @example
@@ -107,6 +109,7 @@ function getDateRangeFromDate(date, type, options) {
107
109
  * Returns a `Date` object set to midnight (00:00:00) of today, with an optional day offset.
108
110
  *
109
111
  * @param {number} [offsetDays] - Number of days to offset from today. Can be negative.
112
+ *
110
113
  * @returns {Date} A `Date` object at 00:00:00 of the offset day.
111
114
  *
112
115
  * @example
@@ -1 +1 @@
1
- {"version":3,"file":"datetime.cjs","sources":["../src/datetime.ts"],"sourcesContent":["import {\n format as dateFnsFormat,\n endOfDay,\n endOfMonth,\n endOfWeek,\n startOfDay,\n startOfMonth,\n startOfWeek,\n subDays,\n subMonths,\n subWeeks,\n} from 'date-fns';\nimport type {\n DateArg,\n Day,\n FormatOptions,\n} from 'date-fns';\n\nexport type DateRangeType = 'lastMonth' | 'lastWeek' | 'thisMonth' | 'thisWeek' | 'today' | 'yesterday';\n\n/**\n * Formats a given date, timestamp, or date string into a specified format.\n *\n * This function is a wrapper around `date-fns/format`.\n *\n * @param {DateArg<Date>} date - The input date to format. Can be a Date object, a timestamp, or a string.\n * @param {string} [format] - The target format string.\n * @param {FormatOptions} [options] - Optional formatting options passed to `date-fns/format`.\n * @returns {string} The formatted date string.\n *\n * @example\n * ```typescript\n * import { formatDate } from '@kikiutils/shared/datetime';\n *\n * // Format a Date object\n * console.log(formatDate(new Date(), 'yyyy-MM-dd')); // 2024-07-10\n *\n * // Format a timestamp\n * console.log(formatDate(1657814400000, 'yyyy-MM-dd')); // 2022-07-15\n *\n * // Format a date string\n * console.log(formatDate('2024-07-10T00:00:00Z', 'yyyy-MM-dd')); // 2024-07-10\n * ```\n *\n * @see https://date-fns.org/docs/format\n */\nexport function formatDate(date: DateArg<Date> & {}, format: string = 'yyyy-MM-dd HH:mm:ss', options?: FormatOptions) {\n return dateFnsFormat(date, format, options);\n}\n\n/**\n * Get the date range (start and end) based on a given date and range type.\n *\n * Supports common range types like 'lastMonth', 'lastWeek', 'thisMonth', 'thisWeek', 'today', and 'yesterday'.\n *\n * @param {Date} date - The reference date.\n * @param {DateRangeType} type - The range type to compute.\n * @param {object} [options] - Optional settings.\n * @param {boolean} [options.setEndDateToNextDayStart] - If true, set `endDate` to 00:00:00.000 of the next day.\n * @param {Day} [options.weekStartsOn] - The start day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday).\n * @returns {{ startDate: Date, endDate: Date }} An object with `startDate` and `endDate`.\n *\n * @example\n * ```typescript\n * import { getDateRangeFromDate } from '@kikiutils/shared/datetime';\n *\n * // Get the date range for last month\n * const date = new Date('2023-07-01');\n * console.log(getDateRangeFromDate(date, 'lastMonth'));\n * // { startDate: 2023-06-01T00:00:00.000Z, endDate: 2023-06-30T23:59:59.999Z }\n *\n * // Get this week's range with Sunday as the first day\n * console.log(getDateRangeFromDate(date, 'thisWeek', { weekStartsOn: 0 }));\n * // { startDate: 2023-06-25T00:00:00.000Z, endDate: 2023-07-01T23:59:59.999Z }\n * ```\n */\nexport function getDateRangeFromDate(\n date: Date,\n type: DateRangeType,\n options?: {\n setEndDateToNextDayStart?: boolean;\n weekStartsOn?: Day;\n },\n) {\n let endDate: Date;\n let startDate: Date;\n switch (type) {\n case 'lastMonth':\n {\n const lastMonth = subMonths(date, 1);\n endDate = endOfMonth(lastMonth);\n startDate = startOfMonth(lastMonth);\n }\n\n break;\n case 'lastWeek':\n {\n const lastWeek = subWeeks(date, 1);\n endDate = endOfWeek(lastWeek, { weekStartsOn: options?.weekStartsOn ?? 1 });\n startDate = startOfWeek(lastWeek, { weekStartsOn: options?.weekStartsOn ?? 1 });\n }\n\n break;\n case 'thisMonth':\n endDate = endOfMonth(date);\n startDate = startOfMonth(date);\n break;\n case 'thisWeek':\n endDate = endOfWeek(date, { weekStartsOn: options?.weekStartsOn ?? 1 });\n startDate = startOfWeek(date, { weekStartsOn: options?.weekStartsOn ?? 1 });\n break;\n case 'today':\n endDate = endOfDay(date);\n startDate = startOfDay(date);\n break;\n case 'yesterday':\n {\n const yesterday = subDays(date, 1);\n endDate = endOfDay(yesterday);\n startDate = startOfDay(yesterday);\n }\n\n break;\n default: throw new Error(`Unsupported date range type: ${type}.`);\n }\n\n if (options?.setEndDateToNextDayStart) endDate.setHours(24, 0, 0, 0);\n return {\n endDate,\n startDate,\n };\n}\n\n/**\n * Returns a `Date` object set to midnight (00:00:00) of today, with an optional day offset.\n *\n * @param {number} [offsetDays] - Number of days to offset from today. Can be negative.\n * @returns {Date} A `Date` object at 00:00:00 of the offset day.\n *\n * @example\n * ```typescript\n * import { getMidnightDateFromToday } from '@kikiutils/shared/datetime';\n *\n * console.log(getMidnightDateFromToday()); // today at 00:00:00\n * console.log(getMidnightDateFromToday(3)); // 3 days from today at 00:00:00\n * console.log(getMidnightDateFromToday(-1)); // yesterday at 00:00:00\n * ```\n */\nexport function getMidnightDateFromToday(offsetDays: number = 0) {\n const date = new Date();\n date.setDate(date.getDate() + offsetDays);\n date.setHours(0, 0, 0, 0);\n return date;\n}\n"],"names":["dateFnsFormat","subMonths","endOfMonth","startOfMonth","subWeeks","endOfWeek","startOfWeek","endOfDay","startOfDay","subDays"],"mappings":";;;;AAoBA;;;;;;;;;;;;;;;;;;;;;;;;;AAyBG;AACG,SAAU,UAAU,CAAC,IAAwB,EAAE,MAAiB,GAAA,qBAAqB,EAAE,OAAuB,EAAA;IAChH,OAAOA,cAAa,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC;AAC/C;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;AAyBG;SACa,oBAAoB,CAChC,IAAU,EACV,IAAmB,EACnB,OAGC,EAAA;AAED,IAAA,IAAI,OAAa;AACjB,IAAA,IAAI,SAAe;IACnB,QAAQ,IAAI;AACR,QAAA,KAAK,WAAW;YACZ;gBACI,MAAM,SAAS,GAAGC,iBAAS,CAAC,IAAI,EAAE,CAAC,CAAC;AACpC,gBAAA,OAAO,GAAGC,kBAAU,CAAC,SAAS,CAAC;AAC/B,gBAAA,SAAS,GAAGC,oBAAY,CAAC,SAAS,CAAC;;YAGvC;AACJ,QAAA,KAAK,UAAU;YACX;gBACI,MAAM,QAAQ,GAAGC,gBAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;AAClC,gBAAA,OAAO,GAAGC,iBAAS,CAAC,QAAQ,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;AAC3E,gBAAA,SAAS,GAAGC,mBAAW,CAAC,QAAQ,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;;YAGnF;AACJ,QAAA,KAAK,WAAW;AACZ,YAAA,OAAO,GAAGJ,kBAAU,CAAC,IAAI,CAAC;AAC1B,YAAA,SAAS,GAAGC,oBAAY,CAAC,IAAI,CAAC;YAC9B;AACJ,QAAA,KAAK,UAAU;AACX,YAAA,OAAO,GAAGE,iBAAS,CAAC,IAAI,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;AACvE,YAAA,SAAS,GAAGC,mBAAW,CAAC,IAAI,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;YAC3E;AACJ,QAAA,KAAK,OAAO;AACR,YAAA,OAAO,GAAGC,gBAAQ,CAAC,IAAI,CAAC;AACxB,YAAA,SAAS,GAAGC,kBAAU,CAAC,IAAI,CAAC;YAC5B;AACJ,QAAA,KAAK,WAAW;YACZ;gBACI,MAAM,SAAS,GAAGC,eAAO,CAAC,IAAI,EAAE,CAAC,CAAC;AAClC,gBAAA,OAAO,GAAGF,gBAAQ,CAAC,SAAS,CAAC;AAC7B,gBAAA,SAAS,GAAGC,kBAAU,CAAC,SAAS,CAAC;;YAGrC;QACJ,SAAS,MAAM,IAAI,KAAK,CAAC,CAAgC,6BAAA,EAAA,IAAI,CAAG,CAAA,CAAA,CAAC;;IAGrE,IAAI,OAAO,EAAE,wBAAwB;QAAE,OAAO,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IACpE,OAAO;QACH,OAAO;QACP,SAAS;KACZ;AACL;AAEA;;;;;;;;;;;;;;AAcG;AACa,SAAA,wBAAwB,CAAC,UAAA,GAAqB,CAAC,EAAA;AAC3D,IAAA,MAAM,IAAI,GAAG,IAAI,IAAI,EAAE;IACvB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,UAAU,CAAC;IACzC,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;AACzB,IAAA,OAAO,IAAI;AACf;;;;;;"}
1
+ {"version":3,"file":"datetime.cjs","sources":["../src/datetime.ts"],"sourcesContent":["import {\n format as dateFnsFormat,\n endOfDay,\n endOfMonth,\n endOfWeek,\n startOfDay,\n startOfMonth,\n startOfWeek,\n subDays,\n subMonths,\n subWeeks,\n} from 'date-fns';\nimport type {\n DateArg,\n Day,\n FormatOptions,\n} from 'date-fns';\n\nexport type DateRangeType = 'lastMonth' | 'lastWeek' | 'thisMonth' | 'thisWeek' | 'today' | 'yesterday';\n\n/**\n * Formats a given date, timestamp, or date string into a specified format.\n *\n * This function is a wrapper around `date-fns/format`.\n *\n * @param {DateArg<Date>} date - The input date to format. Can be a Date object, a timestamp, or a string.\n * @param {string} [format] - The target format string.\n * @param {FormatOptions} [options] - Optional formatting options passed to `date-fns/format`.\n *\n * @returns {string} The formatted date string.\n *\n * @example\n * ```typescript\n * import { formatDate } from '@kikiutils/shared/datetime';\n *\n * // Format a Date object\n * console.log(formatDate(new Date(), 'yyyy-MM-dd')); // 2024-07-10\n *\n * // Format a timestamp\n * console.log(formatDate(1657814400000, 'yyyy-MM-dd')); // 2022-07-15\n *\n * // Format a date string\n * console.log(formatDate('2024-07-10T00:00:00Z', 'yyyy-MM-dd')); // 2024-07-10\n * ```\n *\n * @see https://date-fns.org/docs/format\n */\nexport function formatDate(date: DateArg<Date> & {}, format: string = 'yyyy-MM-dd HH:mm:ss', options?: FormatOptions) {\n return dateFnsFormat(date, format, options);\n}\n\n/**\n * Get the date range (start and end) based on a given date and range type.\n *\n * Supports common range types like 'lastMonth', 'lastWeek', 'thisMonth', 'thisWeek', 'today', and 'yesterday'.\n *\n * @param {Date} date - The reference date.\n * @param {DateRangeType} type - The range type to compute.\n * @param {object} [options] - Optional settings.\n * @param {boolean} [options.setEndDateToNextDayStart] - If true, set `endDate` to 00:00:00.000 of the next day.\n * @param {Day} [options.weekStartsOn] - The start day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday).\n *\n * @returns {{ startDate: Date, endDate: Date }} An object with `startDate` and `endDate`.\n *\n * @example\n * ```typescript\n * import { getDateRangeFromDate } from '@kikiutils/shared/datetime';\n *\n * // Get the date range for last month\n * const date = new Date('2023-07-01');\n * console.log(getDateRangeFromDate(date, 'lastMonth'));\n * // { startDate: 2023-06-01T00:00:00.000Z, endDate: 2023-06-30T23:59:59.999Z }\n *\n * // Get this week's range with Sunday as the first day\n * console.log(getDateRangeFromDate(date, 'thisWeek', { weekStartsOn: 0 }));\n * // { startDate: 2023-06-25T00:00:00.000Z, endDate: 2023-07-01T23:59:59.999Z }\n * ```\n */\nexport function getDateRangeFromDate(\n date: Date,\n type: DateRangeType,\n options?: {\n setEndDateToNextDayStart?: boolean;\n weekStartsOn?: Day;\n },\n) {\n let endDate: Date;\n let startDate: Date;\n switch (type) {\n case 'lastMonth':\n {\n const lastMonth = subMonths(date, 1);\n endDate = endOfMonth(lastMonth);\n startDate = startOfMonth(lastMonth);\n }\n\n break;\n case 'lastWeek':\n {\n const lastWeek = subWeeks(date, 1);\n endDate = endOfWeek(lastWeek, { weekStartsOn: options?.weekStartsOn ?? 1 });\n startDate = startOfWeek(lastWeek, { weekStartsOn: options?.weekStartsOn ?? 1 });\n }\n\n break;\n case 'thisMonth':\n endDate = endOfMonth(date);\n startDate = startOfMonth(date);\n break;\n case 'thisWeek':\n endDate = endOfWeek(date, { weekStartsOn: options?.weekStartsOn ?? 1 });\n startDate = startOfWeek(date, { weekStartsOn: options?.weekStartsOn ?? 1 });\n break;\n case 'today':\n endDate = endOfDay(date);\n startDate = startOfDay(date);\n break;\n case 'yesterday':\n {\n const yesterday = subDays(date, 1);\n endDate = endOfDay(yesterday);\n startDate = startOfDay(yesterday);\n }\n\n break;\n default: throw new Error(`Unsupported date range type: ${type}.`);\n }\n\n if (options?.setEndDateToNextDayStart) endDate.setHours(24, 0, 0, 0);\n return {\n endDate,\n startDate,\n };\n}\n\n/**\n * Returns a `Date` object set to midnight (00:00:00) of today, with an optional day offset.\n *\n * @param {number} [offsetDays] - Number of days to offset from today. Can be negative.\n *\n * @returns {Date} A `Date` object at 00:00:00 of the offset day.\n *\n * @example\n * ```typescript\n * import { getMidnightDateFromToday } from '@kikiutils/shared/datetime';\n *\n * console.log(getMidnightDateFromToday()); // today at 00:00:00\n * console.log(getMidnightDateFromToday(3)); // 3 days from today at 00:00:00\n * console.log(getMidnightDateFromToday(-1)); // yesterday at 00:00:00\n * ```\n */\nexport function getMidnightDateFromToday(offsetDays: number = 0) {\n const date = new Date();\n date.setDate(date.getDate() + offsetDays);\n date.setHours(0, 0, 0, 0);\n return date;\n}\n"],"names":["dateFnsFormat","subMonths","endOfMonth","startOfMonth","subWeeks","endOfWeek","startOfWeek","endOfDay","startOfDay","subDays"],"mappings":";;;;AAoBA;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BG;AACG,SAAU,UAAU,CAAC,IAAwB,EAAE,MAAiB,GAAA,qBAAqB,EAAE,OAAuB,EAAA;IAChH,OAAOA,cAAa,CAAC,IAAI,EAAE,MAAM,EAAE,OAAO,CAAC;AAC/C;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BG;SACa,oBAAoB,CAChC,IAAU,EACV,IAAmB,EACnB,OAGC,EAAA;AAED,IAAA,IAAI,OAAa;AACjB,IAAA,IAAI,SAAe;IACnB,QAAQ,IAAI;AACR,QAAA,KAAK,WAAW;YACZ;gBACI,MAAM,SAAS,GAAGC,iBAAS,CAAC,IAAI,EAAE,CAAC,CAAC;AACpC,gBAAA,OAAO,GAAGC,kBAAU,CAAC,SAAS,CAAC;AAC/B,gBAAA,SAAS,GAAGC,oBAAY,CAAC,SAAS,CAAC;;YAGvC;AACJ,QAAA,KAAK,UAAU;YACX;gBACI,MAAM,QAAQ,GAAGC,gBAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;AAClC,gBAAA,OAAO,GAAGC,iBAAS,CAAC,QAAQ,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;AAC3E,gBAAA,SAAS,GAAGC,mBAAW,CAAC,QAAQ,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;;YAGnF;AACJ,QAAA,KAAK,WAAW;AACZ,YAAA,OAAO,GAAGJ,kBAAU,CAAC,IAAI,CAAC;AAC1B,YAAA,SAAS,GAAGC,oBAAY,CAAC,IAAI,CAAC;YAC9B;AACJ,QAAA,KAAK,UAAU;AACX,YAAA,OAAO,GAAGE,iBAAS,CAAC,IAAI,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;AACvE,YAAA,SAAS,GAAGC,mBAAW,CAAC,IAAI,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;YAC3E;AACJ,QAAA,KAAK,OAAO;AACR,YAAA,OAAO,GAAGC,gBAAQ,CAAC,IAAI,CAAC;AACxB,YAAA,SAAS,GAAGC,kBAAU,CAAC,IAAI,CAAC;YAC5B;AACJ,QAAA,KAAK,WAAW;YACZ;gBACI,MAAM,SAAS,GAAGC,eAAO,CAAC,IAAI,EAAE,CAAC,CAAC;AAClC,gBAAA,OAAO,GAAGF,gBAAQ,CAAC,SAAS,CAAC;AAC7B,gBAAA,SAAS,GAAGC,kBAAU,CAAC,SAAS,CAAC;;YAGrC;QACJ,SAAS,MAAM,IAAI,KAAK,CAAC,CAAgC,6BAAA,EAAA,IAAI,CAAG,CAAA,CAAA,CAAC;;IAGrE,IAAI,OAAO,EAAE,wBAAwB;QAAE,OAAO,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IACpE,OAAO;QACH,OAAO;QACP,SAAS;KACZ;AACL;AAEA;;;;;;;;;;;;;;;AAeG;AACa,SAAA,wBAAwB,CAAC,UAAA,GAAqB,CAAC,EAAA;AAC3D,IAAA,MAAM,IAAI,GAAG,IAAI,IAAI,EAAE;IACvB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,UAAU,CAAC;IACzC,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;AACzB,IAAA,OAAO,IAAI;AACf;;;;;;"}
@@ -8,6 +8,7 @@ export type DateRangeType = 'lastMonth' | 'lastWeek' | 'thisMonth' | 'thisWeek'
8
8
  * @param {DateArg<Date>} date - The input date to format. Can be a Date object, a timestamp, or a string.
9
9
  * @param {string} [format] - The target format string.
10
10
  * @param {FormatOptions} [options] - Optional formatting options passed to `date-fns/format`.
11
+ *
11
12
  * @returns {string} The formatted date string.
12
13
  *
13
14
  * @example
@@ -37,6 +38,7 @@ export declare function formatDate(date: DateArg<Date> & {}, format?: string, op
37
38
  * @param {object} [options] - Optional settings.
38
39
  * @param {boolean} [options.setEndDateToNextDayStart] - If true, set `endDate` to 00:00:00.000 of the next day.
39
40
  * @param {Day} [options.weekStartsOn] - The start day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday).
41
+ *
40
42
  * @returns {{ startDate: Date, endDate: Date }} An object with `startDate` and `endDate`.
41
43
  *
42
44
  * @example
@@ -64,6 +66,7 @@ export declare function getDateRangeFromDate(date: Date, type: DateRangeType, op
64
66
  * Returns a `Date` object set to midnight (00:00:00) of today, with an optional day offset.
65
67
  *
66
68
  * @param {number} [offsetDays] - Number of days to offset from today. Can be negative.
69
+ *
67
70
  * @returns {Date} A `Date` object at 00:00:00 of the offset day.
68
71
  *
69
72
  * @example
@@ -1 +1 @@
1
- {"version":3,"file":"datetime.d.ts","sourceRoot":"","sources":["../src/datetime.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EACR,OAAO,EACP,GAAG,EACH,aAAa,EAChB,MAAM,UAAU,CAAC;AAElB,MAAM,MAAM,aAAa,GAAG,WAAW,GAAG,UAAU,GAAG,WAAW,GAAG,UAAU,GAAG,OAAO,GAAG,WAAW,CAAC;AAExG;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,EAAE,MAAM,GAAE,MAA8B,EAAE,OAAO,CAAC,EAAE,aAAa,UAEnH;AAED;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,wBAAgB,oBAAoB,CAChC,IAAI,EAAE,IAAI,EACV,IAAI,EAAE,aAAa,EACnB,OAAO,CAAC,EAAE;IACN,wBAAwB,CAAC,EAAE,OAAO,CAAC;IACnC,YAAY,CAAC,EAAE,GAAG,CAAC;CACtB;;;EAiDJ;AAED;;;;;;;;;;;;;;GAcG;AACH,wBAAgB,wBAAwB,CAAC,UAAU,GAAE,MAAU,QAK9D"}
1
+ {"version":3,"file":"datetime.d.ts","sourceRoot":"","sources":["../src/datetime.ts"],"names":[],"mappings":"AAYA,OAAO,KAAK,EACR,OAAO,EACP,GAAG,EACH,aAAa,EAChB,MAAM,UAAU,CAAC;AAElB,MAAM,MAAM,aAAa,GAAG,WAAW,GAAG,UAAU,GAAG,WAAW,GAAG,UAAU,GAAG,OAAO,GAAG,WAAW,CAAC;AAExG;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,UAAU,CAAC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,EAAE,MAAM,GAAE,MAA8B,EAAE,OAAO,CAAC,EAAE,aAAa,UAEnH;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,oBAAoB,CAChC,IAAI,EAAE,IAAI,EACV,IAAI,EAAE,aAAa,EACnB,OAAO,CAAC,EAAE;IACN,wBAAwB,CAAC,EAAE,OAAO,CAAC;IACnC,YAAY,CAAC,EAAE,GAAG,CAAC;CACtB;;;EAiDJ;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,wBAAwB,CAAC,UAAU,GAAE,MAAU,QAK9D"}
package/dist/datetime.mjs CHANGED
@@ -8,6 +8,7 @@ import { format, subDays, endOfDay, startOfDay, endOfWeek, startOfWeek, endOfMon
8
8
  * @param {DateArg<Date>} date - The input date to format. Can be a Date object, a timestamp, or a string.
9
9
  * @param {string} [format] - The target format string.
10
10
  * @param {FormatOptions} [options] - Optional formatting options passed to `date-fns/format`.
11
+ *
11
12
  * @returns {string} The formatted date string.
12
13
  *
13
14
  * @example
@@ -39,6 +40,7 @@ function formatDate(date, format$1 = 'yyyy-MM-dd HH:mm:ss', options) {
39
40
  * @param {object} [options] - Optional settings.
40
41
  * @param {boolean} [options.setEndDateToNextDayStart] - If true, set `endDate` to 00:00:00.000 of the next day.
41
42
  * @param {Day} [options.weekStartsOn] - The start day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday).
43
+ *
42
44
  * @returns {{ startDate: Date, endDate: Date }} An object with `startDate` and `endDate`.
43
45
  *
44
46
  * @example
@@ -105,6 +107,7 @@ function getDateRangeFromDate(date, type, options) {
105
107
  * Returns a `Date` object set to midnight (00:00:00) of today, with an optional day offset.
106
108
  *
107
109
  * @param {number} [offsetDays] - Number of days to offset from today. Can be negative.
110
+ *
108
111
  * @returns {Date} A `Date` object at 00:00:00 of the offset day.
109
112
  *
110
113
  * @example
@@ -1 +1 @@
1
- {"version":3,"file":"datetime.mjs","sources":["../src/datetime.ts"],"sourcesContent":["import {\n format as dateFnsFormat,\n endOfDay,\n endOfMonth,\n endOfWeek,\n startOfDay,\n startOfMonth,\n startOfWeek,\n subDays,\n subMonths,\n subWeeks,\n} from 'date-fns';\nimport type {\n DateArg,\n Day,\n FormatOptions,\n} from 'date-fns';\n\nexport type DateRangeType = 'lastMonth' | 'lastWeek' | 'thisMonth' | 'thisWeek' | 'today' | 'yesterday';\n\n/**\n * Formats a given date, timestamp, or date string into a specified format.\n *\n * This function is a wrapper around `date-fns/format`.\n *\n * @param {DateArg<Date>} date - The input date to format. Can be a Date object, a timestamp, or a string.\n * @param {string} [format] - The target format string.\n * @param {FormatOptions} [options] - Optional formatting options passed to `date-fns/format`.\n * @returns {string} The formatted date string.\n *\n * @example\n * ```typescript\n * import { formatDate } from '@kikiutils/shared/datetime';\n *\n * // Format a Date object\n * console.log(formatDate(new Date(), 'yyyy-MM-dd')); // 2024-07-10\n *\n * // Format a timestamp\n * console.log(formatDate(1657814400000, 'yyyy-MM-dd')); // 2022-07-15\n *\n * // Format a date string\n * console.log(formatDate('2024-07-10T00:00:00Z', 'yyyy-MM-dd')); // 2024-07-10\n * ```\n *\n * @see https://date-fns.org/docs/format\n */\nexport function formatDate(date: DateArg<Date> & {}, format: string = 'yyyy-MM-dd HH:mm:ss', options?: FormatOptions) {\n return dateFnsFormat(date, format, options);\n}\n\n/**\n * Get the date range (start and end) based on a given date and range type.\n *\n * Supports common range types like 'lastMonth', 'lastWeek', 'thisMonth', 'thisWeek', 'today', and 'yesterday'.\n *\n * @param {Date} date - The reference date.\n * @param {DateRangeType} type - The range type to compute.\n * @param {object} [options] - Optional settings.\n * @param {boolean} [options.setEndDateToNextDayStart] - If true, set `endDate` to 00:00:00.000 of the next day.\n * @param {Day} [options.weekStartsOn] - The start day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday).\n * @returns {{ startDate: Date, endDate: Date }} An object with `startDate` and `endDate`.\n *\n * @example\n * ```typescript\n * import { getDateRangeFromDate } from '@kikiutils/shared/datetime';\n *\n * // Get the date range for last month\n * const date = new Date('2023-07-01');\n * console.log(getDateRangeFromDate(date, 'lastMonth'));\n * // { startDate: 2023-06-01T00:00:00.000Z, endDate: 2023-06-30T23:59:59.999Z }\n *\n * // Get this week's range with Sunday as the first day\n * console.log(getDateRangeFromDate(date, 'thisWeek', { weekStartsOn: 0 }));\n * // { startDate: 2023-06-25T00:00:00.000Z, endDate: 2023-07-01T23:59:59.999Z }\n * ```\n */\nexport function getDateRangeFromDate(\n date: Date,\n type: DateRangeType,\n options?: {\n setEndDateToNextDayStart?: boolean;\n weekStartsOn?: Day;\n },\n) {\n let endDate: Date;\n let startDate: Date;\n switch (type) {\n case 'lastMonth':\n {\n const lastMonth = subMonths(date, 1);\n endDate = endOfMonth(lastMonth);\n startDate = startOfMonth(lastMonth);\n }\n\n break;\n case 'lastWeek':\n {\n const lastWeek = subWeeks(date, 1);\n endDate = endOfWeek(lastWeek, { weekStartsOn: options?.weekStartsOn ?? 1 });\n startDate = startOfWeek(lastWeek, { weekStartsOn: options?.weekStartsOn ?? 1 });\n }\n\n break;\n case 'thisMonth':\n endDate = endOfMonth(date);\n startDate = startOfMonth(date);\n break;\n case 'thisWeek':\n endDate = endOfWeek(date, { weekStartsOn: options?.weekStartsOn ?? 1 });\n startDate = startOfWeek(date, { weekStartsOn: options?.weekStartsOn ?? 1 });\n break;\n case 'today':\n endDate = endOfDay(date);\n startDate = startOfDay(date);\n break;\n case 'yesterday':\n {\n const yesterday = subDays(date, 1);\n endDate = endOfDay(yesterday);\n startDate = startOfDay(yesterday);\n }\n\n break;\n default: throw new Error(`Unsupported date range type: ${type}.`);\n }\n\n if (options?.setEndDateToNextDayStart) endDate.setHours(24, 0, 0, 0);\n return {\n endDate,\n startDate,\n };\n}\n\n/**\n * Returns a `Date` object set to midnight (00:00:00) of today, with an optional day offset.\n *\n * @param {number} [offsetDays] - Number of days to offset from today. Can be negative.\n * @returns {Date} A `Date` object at 00:00:00 of the offset day.\n *\n * @example\n * ```typescript\n * import { getMidnightDateFromToday } from '@kikiutils/shared/datetime';\n *\n * console.log(getMidnightDateFromToday()); // today at 00:00:00\n * console.log(getMidnightDateFromToday(3)); // 3 days from today at 00:00:00\n * console.log(getMidnightDateFromToday(-1)); // yesterday at 00:00:00\n * ```\n */\nexport function getMidnightDateFromToday(offsetDays: number = 0) {\n const date = new Date();\n date.setDate(date.getDate() + offsetDays);\n date.setHours(0, 0, 0, 0);\n return date;\n}\n"],"names":["format","dateFnsFormat"],"mappings":";;AAoBA;;;;;;;;;;;;;;;;;;;;;;;;;AAyBG;AACG,SAAU,UAAU,CAAC,IAAwB,EAAEA,QAAiB,GAAA,qBAAqB,EAAE,OAAuB,EAAA;IAChH,OAAOC,MAAa,CAAC,IAAI,EAAED,QAAM,EAAE,OAAO,CAAC;AAC/C;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;AAyBG;SACa,oBAAoB,CAChC,IAAU,EACV,IAAmB,EACnB,OAGC,EAAA;AAED,IAAA,IAAI,OAAa;AACjB,IAAA,IAAI,SAAe;IACnB,QAAQ,IAAI;AACR,QAAA,KAAK,WAAW;YACZ;gBACI,MAAM,SAAS,GAAG,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC;AACpC,gBAAA,OAAO,GAAG,UAAU,CAAC,SAAS,CAAC;AAC/B,gBAAA,SAAS,GAAG,YAAY,CAAC,SAAS,CAAC;;YAGvC;AACJ,QAAA,KAAK,UAAU;YACX;gBACI,MAAM,QAAQ,GAAG,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;AAClC,gBAAA,OAAO,GAAG,SAAS,CAAC,QAAQ,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;AAC3E,gBAAA,SAAS,GAAG,WAAW,CAAC,QAAQ,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;;YAGnF;AACJ,QAAA,KAAK,WAAW;AACZ,YAAA,OAAO,GAAG,UAAU,CAAC,IAAI,CAAC;AAC1B,YAAA,SAAS,GAAG,YAAY,CAAC,IAAI,CAAC;YAC9B;AACJ,QAAA,KAAK,UAAU;AACX,YAAA,OAAO,GAAG,SAAS,CAAC,IAAI,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;AACvE,YAAA,SAAS,GAAG,WAAW,CAAC,IAAI,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;YAC3E;AACJ,QAAA,KAAK,OAAO;AACR,YAAA,OAAO,GAAG,QAAQ,CAAC,IAAI,CAAC;AACxB,YAAA,SAAS,GAAG,UAAU,CAAC,IAAI,CAAC;YAC5B;AACJ,QAAA,KAAK,WAAW;YACZ;gBACI,MAAM,SAAS,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;AAClC,gBAAA,OAAO,GAAG,QAAQ,CAAC,SAAS,CAAC;AAC7B,gBAAA,SAAS,GAAG,UAAU,CAAC,SAAS,CAAC;;YAGrC;QACJ,SAAS,MAAM,IAAI,KAAK,CAAC,CAAgC,6BAAA,EAAA,IAAI,CAAG,CAAA,CAAA,CAAC;;IAGrE,IAAI,OAAO,EAAE,wBAAwB;QAAE,OAAO,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IACpE,OAAO;QACH,OAAO;QACP,SAAS;KACZ;AACL;AAEA;;;;;;;;;;;;;;AAcG;AACa,SAAA,wBAAwB,CAAC,UAAA,GAAqB,CAAC,EAAA;AAC3D,IAAA,MAAM,IAAI,GAAG,IAAI,IAAI,EAAE;IACvB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,UAAU,CAAC;IACzC,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;AACzB,IAAA,OAAO,IAAI;AACf;;;;"}
1
+ {"version":3,"file":"datetime.mjs","sources":["../src/datetime.ts"],"sourcesContent":["import {\n format as dateFnsFormat,\n endOfDay,\n endOfMonth,\n endOfWeek,\n startOfDay,\n startOfMonth,\n startOfWeek,\n subDays,\n subMonths,\n subWeeks,\n} from 'date-fns';\nimport type {\n DateArg,\n Day,\n FormatOptions,\n} from 'date-fns';\n\nexport type DateRangeType = 'lastMonth' | 'lastWeek' | 'thisMonth' | 'thisWeek' | 'today' | 'yesterday';\n\n/**\n * Formats a given date, timestamp, or date string into a specified format.\n *\n * This function is a wrapper around `date-fns/format`.\n *\n * @param {DateArg<Date>} date - The input date to format. Can be a Date object, a timestamp, or a string.\n * @param {string} [format] - The target format string.\n * @param {FormatOptions} [options] - Optional formatting options passed to `date-fns/format`.\n *\n * @returns {string} The formatted date string.\n *\n * @example\n * ```typescript\n * import { formatDate } from '@kikiutils/shared/datetime';\n *\n * // Format a Date object\n * console.log(formatDate(new Date(), 'yyyy-MM-dd')); // 2024-07-10\n *\n * // Format a timestamp\n * console.log(formatDate(1657814400000, 'yyyy-MM-dd')); // 2022-07-15\n *\n * // Format a date string\n * console.log(formatDate('2024-07-10T00:00:00Z', 'yyyy-MM-dd')); // 2024-07-10\n * ```\n *\n * @see https://date-fns.org/docs/format\n */\nexport function formatDate(date: DateArg<Date> & {}, format: string = 'yyyy-MM-dd HH:mm:ss', options?: FormatOptions) {\n return dateFnsFormat(date, format, options);\n}\n\n/**\n * Get the date range (start and end) based on a given date and range type.\n *\n * Supports common range types like 'lastMonth', 'lastWeek', 'thisMonth', 'thisWeek', 'today', and 'yesterday'.\n *\n * @param {Date} date - The reference date.\n * @param {DateRangeType} type - The range type to compute.\n * @param {object} [options] - Optional settings.\n * @param {boolean} [options.setEndDateToNextDayStart] - If true, set `endDate` to 00:00:00.000 of the next day.\n * @param {Day} [options.weekStartsOn] - The start day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday).\n *\n * @returns {{ startDate: Date, endDate: Date }} An object with `startDate` and `endDate`.\n *\n * @example\n * ```typescript\n * import { getDateRangeFromDate } from '@kikiutils/shared/datetime';\n *\n * // Get the date range for last month\n * const date = new Date('2023-07-01');\n * console.log(getDateRangeFromDate(date, 'lastMonth'));\n * // { startDate: 2023-06-01T00:00:00.000Z, endDate: 2023-06-30T23:59:59.999Z }\n *\n * // Get this week's range with Sunday as the first day\n * console.log(getDateRangeFromDate(date, 'thisWeek', { weekStartsOn: 0 }));\n * // { startDate: 2023-06-25T00:00:00.000Z, endDate: 2023-07-01T23:59:59.999Z }\n * ```\n */\nexport function getDateRangeFromDate(\n date: Date,\n type: DateRangeType,\n options?: {\n setEndDateToNextDayStart?: boolean;\n weekStartsOn?: Day;\n },\n) {\n let endDate: Date;\n let startDate: Date;\n switch (type) {\n case 'lastMonth':\n {\n const lastMonth = subMonths(date, 1);\n endDate = endOfMonth(lastMonth);\n startDate = startOfMonth(lastMonth);\n }\n\n break;\n case 'lastWeek':\n {\n const lastWeek = subWeeks(date, 1);\n endDate = endOfWeek(lastWeek, { weekStartsOn: options?.weekStartsOn ?? 1 });\n startDate = startOfWeek(lastWeek, { weekStartsOn: options?.weekStartsOn ?? 1 });\n }\n\n break;\n case 'thisMonth':\n endDate = endOfMonth(date);\n startDate = startOfMonth(date);\n break;\n case 'thisWeek':\n endDate = endOfWeek(date, { weekStartsOn: options?.weekStartsOn ?? 1 });\n startDate = startOfWeek(date, { weekStartsOn: options?.weekStartsOn ?? 1 });\n break;\n case 'today':\n endDate = endOfDay(date);\n startDate = startOfDay(date);\n break;\n case 'yesterday':\n {\n const yesterday = subDays(date, 1);\n endDate = endOfDay(yesterday);\n startDate = startOfDay(yesterday);\n }\n\n break;\n default: throw new Error(`Unsupported date range type: ${type}.`);\n }\n\n if (options?.setEndDateToNextDayStart) endDate.setHours(24, 0, 0, 0);\n return {\n endDate,\n startDate,\n };\n}\n\n/**\n * Returns a `Date` object set to midnight (00:00:00) of today, with an optional day offset.\n *\n * @param {number} [offsetDays] - Number of days to offset from today. Can be negative.\n *\n * @returns {Date} A `Date` object at 00:00:00 of the offset day.\n *\n * @example\n * ```typescript\n * import { getMidnightDateFromToday } from '@kikiutils/shared/datetime';\n *\n * console.log(getMidnightDateFromToday()); // today at 00:00:00\n * console.log(getMidnightDateFromToday(3)); // 3 days from today at 00:00:00\n * console.log(getMidnightDateFromToday(-1)); // yesterday at 00:00:00\n * ```\n */\nexport function getMidnightDateFromToday(offsetDays: number = 0) {\n const date = new Date();\n date.setDate(date.getDate() + offsetDays);\n date.setHours(0, 0, 0, 0);\n return date;\n}\n"],"names":["format","dateFnsFormat"],"mappings":";;AAoBA;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BG;AACG,SAAU,UAAU,CAAC,IAAwB,EAAEA,QAAiB,GAAA,qBAAqB,EAAE,OAAuB,EAAA;IAChH,OAAOC,MAAa,CAAC,IAAI,EAAED,QAAM,EAAE,OAAO,CAAC;AAC/C;AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;AA0BG;SACa,oBAAoB,CAChC,IAAU,EACV,IAAmB,EACnB,OAGC,EAAA;AAED,IAAA,IAAI,OAAa;AACjB,IAAA,IAAI,SAAe;IACnB,QAAQ,IAAI;AACR,QAAA,KAAK,WAAW;YACZ;gBACI,MAAM,SAAS,GAAG,SAAS,CAAC,IAAI,EAAE,CAAC,CAAC;AACpC,gBAAA,OAAO,GAAG,UAAU,CAAC,SAAS,CAAC;AAC/B,gBAAA,SAAS,GAAG,YAAY,CAAC,SAAS,CAAC;;YAGvC;AACJ,QAAA,KAAK,UAAU;YACX;gBACI,MAAM,QAAQ,GAAG,QAAQ,CAAC,IAAI,EAAE,CAAC,CAAC;AAClC,gBAAA,OAAO,GAAG,SAAS,CAAC,QAAQ,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;AAC3E,gBAAA,SAAS,GAAG,WAAW,CAAC,QAAQ,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;;YAGnF;AACJ,QAAA,KAAK,WAAW;AACZ,YAAA,OAAO,GAAG,UAAU,CAAC,IAAI,CAAC;AAC1B,YAAA,SAAS,GAAG,YAAY,CAAC,IAAI,CAAC;YAC9B;AACJ,QAAA,KAAK,UAAU;AACX,YAAA,OAAO,GAAG,SAAS,CAAC,IAAI,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;AACvE,YAAA,SAAS,GAAG,WAAW,CAAC,IAAI,EAAE,EAAE,YAAY,EAAE,OAAO,EAAE,YAAY,IAAI,CAAC,EAAE,CAAC;YAC3E;AACJ,QAAA,KAAK,OAAO;AACR,YAAA,OAAO,GAAG,QAAQ,CAAC,IAAI,CAAC;AACxB,YAAA,SAAS,GAAG,UAAU,CAAC,IAAI,CAAC;YAC5B;AACJ,QAAA,KAAK,WAAW;YACZ;gBACI,MAAM,SAAS,GAAG,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;AAClC,gBAAA,OAAO,GAAG,QAAQ,CAAC,SAAS,CAAC;AAC7B,gBAAA,SAAS,GAAG,UAAU,CAAC,SAAS,CAAC;;YAGrC;QACJ,SAAS,MAAM,IAAI,KAAK,CAAC,CAAgC,6BAAA,EAAA,IAAI,CAAG,CAAA,CAAA,CAAC;;IAGrE,IAAI,OAAO,EAAE,wBAAwB;QAAE,OAAO,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;IACpE,OAAO;QACH,OAAO;QACP,SAAS;KACZ;AACL;AAEA;;;;;;;;;;;;;;;AAeG;AACa,SAAA,wBAAwB,CAAC,UAAA,GAAqB,CAAC,EAAA;AAC3D,IAAA,MAAM,IAAI,GAAG,IAAI,IAAI,EAAE;IACvB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,EAAE,GAAG,UAAU,CAAC;IACzC,IAAI,CAAC,QAAQ,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,CAAC;AACzB,IAAA,OAAO,IAAI;AACf;;;;"}
package/dist/enum.cjs CHANGED
@@ -5,6 +5,7 @@
5
5
  *
6
6
  * @param {Record<number | string, number | string>} data - The enumeration-like object to extract numeric values from.
7
7
  * The keys can be numbers or strings, and the values can be numbers or strings.
8
+ *
8
9
  * @returns {number[]} An array of numeric values extracted from the object.
9
10
  *
10
11
  * @example
@@ -28,6 +29,7 @@ function getEnumNumberValues(data) {
28
29
  *
29
30
  * @param {Record<number | string, number | string>} data - The enumeration-like object to extract string values from.
30
31
  * The keys can be numbers or strings, and the values can be numbers or strings.
32
+ *
31
33
  * @returns {string[]} An array of string values extracted from the object.
32
34
  *
33
35
  * @example
package/dist/enum.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"enum.cjs","sources":["../src/enum.ts"],"sourcesContent":["/**\n * Extracts the numeric values from an enumeration-like object.\n *\n * @param {Record<number | string, number | string>} data - The enumeration-like object to extract numeric values from.\n * The keys can be numbers or strings, and the values can be numbers or strings.\n * @returns {number[]} An array of numeric values extracted from the object.\n *\n * @example\n * ```typescript\n * import { getEnumNumberValues } from '@kikiutils/shared/enum';\n *\n * enum RecordType {\n * Receive = 0,\n * Send = 1,\n * Unknown = 'unknown'\n * }\n *\n * console.log(getEnumNumberValues(RecordType)); // [0, 1]\n * ```\n */\nexport function getEnumNumberValues(data: Record<number | string, number | string>) {\n return Object.values(data).filter((value) => typeof value === 'number');\n}\n\n/**\n * Extracts the string values from an enumeration-like object.\n *\n * @param {Record<number | string, number | string>} data - The enumeration-like object to extract string values from.\n * The keys can be numbers or strings, and the values can be numbers or strings.\n * @returns {string[]} An array of string values extracted from the object.\n *\n * @example\n * ```typescript\n * import { getEnumStringValues } from '@kikiutils/shared/enum';\n *\n * enum RecordType {\n * Receive = 0,\n * Send = 1,\n * Unknown = 'unknown'\n * }\n *\n * console.log(getEnumStringValues(RecordType)); // ['unknown']\n * ```\n */\nexport function getEnumStringValues(data: Record<number | string, number | string>) {\n const keys: string[] = [];\n const keysCount: Record<string, number> = {};\n const values: any[] = [];\n Object.entries(data).forEach(([key, value]) => {\n keys.push(key);\n values.push(value);\n if (typeof value !== 'string') return;\n keysCount[key] = (keysCount[key] ?? 0) + 1;\n keysCount[value] = (keysCount[value] ?? 0) + 1;\n });\n\n return values.filter((value) => {\n return typeof value === 'string' && (!keys.includes(value) || (keysCount[value] && keysCount[value] > 1));\n });\n}\n"],"names":[],"mappings":";;AAAA;;;;;;;;;;;;;;;;;;;AAmBG;AACG,SAAU,mBAAmB,CAAC,IAA8C,EAAA;AAC9E,IAAA,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,KAAK,OAAO,KAAK,KAAK,QAAQ,CAAC;AAC3E;AAEA;;;;;;;;;;;;;;;;;;;AAmBG;AACG,SAAU,mBAAmB,CAAC,IAA8C,EAAA;IAC9E,MAAM,IAAI,GAAa,EAAE;IACzB,MAAM,SAAS,GAA2B,EAAE;IAC5C,MAAM,MAAM,GAAU,EAAE;AACxB,IAAA,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,KAAI;AAC1C,QAAA,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC;AACd,QAAA,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC;QAClB,IAAI,OAAO,KAAK,KAAK,QAAQ;YAAE;AAC/B,QAAA,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC;AAC1C,QAAA,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC;AAClD,KAAC,CAAC;AAEF,IAAA,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,KAAI;AAC3B,QAAA,OAAO,OAAO,KAAK,KAAK,QAAQ,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,SAAS,CAAC,KAAK,CAAC,IAAI,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;AAC7G,KAAC,CAAC;AACN;;;;;"}
1
+ {"version":3,"file":"enum.cjs","sources":["../src/enum.ts"],"sourcesContent":["/**\n * Extracts the numeric values from an enumeration-like object.\n *\n * @param {Record<number | string, number | string>} data - The enumeration-like object to extract numeric values from.\n * The keys can be numbers or strings, and the values can be numbers or strings.\n *\n * @returns {number[]} An array of numeric values extracted from the object.\n *\n * @example\n * ```typescript\n * import { getEnumNumberValues } from '@kikiutils/shared/enum';\n *\n * enum RecordType {\n * Receive = 0,\n * Send = 1,\n * Unknown = 'unknown'\n * }\n *\n * console.log(getEnumNumberValues(RecordType)); // [0, 1]\n * ```\n */\nexport function getEnumNumberValues(data: Record<number | string, number | string>) {\n return Object.values(data).filter((value) => typeof value === 'number');\n}\n\n/**\n * Extracts the string values from an enumeration-like object.\n *\n * @param {Record<number | string, number | string>} data - The enumeration-like object to extract string values from.\n * The keys can be numbers or strings, and the values can be numbers or strings.\n *\n * @returns {string[]} An array of string values extracted from the object.\n *\n * @example\n * ```typescript\n * import { getEnumStringValues } from '@kikiutils/shared/enum';\n *\n * enum RecordType {\n * Receive = 0,\n * Send = 1,\n * Unknown = 'unknown'\n * }\n *\n * console.log(getEnumStringValues(RecordType)); // ['unknown']\n * ```\n */\nexport function getEnumStringValues(data: Record<number | string, number | string>) {\n const keys: string[] = [];\n const keysCount: Record<string, number> = {};\n const values: any[] = [];\n Object.entries(data).forEach(([key, value]) => {\n keys.push(key);\n values.push(value);\n if (typeof value !== 'string') return;\n keysCount[key] = (keysCount[key] ?? 0) + 1;\n keysCount[value] = (keysCount[value] ?? 0) + 1;\n });\n\n return values.filter((value) => {\n return typeof value === 'string' && (!keys.includes(value) || (keysCount[value] && keysCount[value] > 1));\n });\n}\n"],"names":[],"mappings":";;AAAA;;;;;;;;;;;;;;;;;;;;AAoBG;AACG,SAAU,mBAAmB,CAAC,IAA8C,EAAA;AAC9E,IAAA,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,KAAK,OAAO,KAAK,KAAK,QAAQ,CAAC;AAC3E;AAEA;;;;;;;;;;;;;;;;;;;;AAoBG;AACG,SAAU,mBAAmB,CAAC,IAA8C,EAAA;IAC9E,MAAM,IAAI,GAAa,EAAE;IACzB,MAAM,SAAS,GAA2B,EAAE;IAC5C,MAAM,MAAM,GAAU,EAAE;AACxB,IAAA,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,KAAI;AAC1C,QAAA,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC;AACd,QAAA,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC;QAClB,IAAI,OAAO,KAAK,KAAK,QAAQ;YAAE;AAC/B,QAAA,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC;AAC1C,QAAA,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC;AAClD,KAAC,CAAC;AAEF,IAAA,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,KAAI;AAC3B,QAAA,OAAO,OAAO,KAAK,KAAK,QAAQ,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,SAAS,CAAC,KAAK,CAAC,IAAI,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;AAC7G,KAAC,CAAC;AACN;;;;;"}
package/dist/enum.d.ts CHANGED
@@ -3,6 +3,7 @@
3
3
  *
4
4
  * @param {Record<number | string, number | string>} data - The enumeration-like object to extract numeric values from.
5
5
  * The keys can be numbers or strings, and the values can be numbers or strings.
6
+ *
6
7
  * @returns {number[]} An array of numeric values extracted from the object.
7
8
  *
8
9
  * @example
@@ -24,6 +25,7 @@ export declare function getEnumNumberValues(data: Record<number | string, number
24
25
  *
25
26
  * @param {Record<number | string, number | string>} data - The enumeration-like object to extract string values from.
26
27
  * The keys can be numbers or strings, and the values can be numbers or strings.
28
+ *
27
29
  * @returns {string[]} An array of string values extracted from the object.
28
30
  *
29
31
  * @example
@@ -1 +1 @@
1
- {"version":3,"file":"enum.d.ts","sourceRoot":"","sources":["../src/enum.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,YAEjF;AAED;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,SAejF"}
1
+ {"version":3,"file":"enum.d.ts","sourceRoot":"","sources":["../src/enum.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,YAEjF;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,GAAG,MAAM,EAAE,MAAM,GAAG,MAAM,CAAC,SAejF"}
package/dist/enum.mjs CHANGED
@@ -3,6 +3,7 @@
3
3
  *
4
4
  * @param {Record<number | string, number | string>} data - The enumeration-like object to extract numeric values from.
5
5
  * The keys can be numbers or strings, and the values can be numbers or strings.
6
+ *
6
7
  * @returns {number[]} An array of numeric values extracted from the object.
7
8
  *
8
9
  * @example
@@ -26,6 +27,7 @@ function getEnumNumberValues(data) {
26
27
  *
27
28
  * @param {Record<number | string, number | string>} data - The enumeration-like object to extract string values from.
28
29
  * The keys can be numbers or strings, and the values can be numbers or strings.
30
+ *
29
31
  * @returns {string[]} An array of string values extracted from the object.
30
32
  *
31
33
  * @example
package/dist/enum.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"enum.mjs","sources":["../src/enum.ts"],"sourcesContent":["/**\n * Extracts the numeric values from an enumeration-like object.\n *\n * @param {Record<number | string, number | string>} data - The enumeration-like object to extract numeric values from.\n * The keys can be numbers or strings, and the values can be numbers or strings.\n * @returns {number[]} An array of numeric values extracted from the object.\n *\n * @example\n * ```typescript\n * import { getEnumNumberValues } from '@kikiutils/shared/enum';\n *\n * enum RecordType {\n * Receive = 0,\n * Send = 1,\n * Unknown = 'unknown'\n * }\n *\n * console.log(getEnumNumberValues(RecordType)); // [0, 1]\n * ```\n */\nexport function getEnumNumberValues(data: Record<number | string, number | string>) {\n return Object.values(data).filter((value) => typeof value === 'number');\n}\n\n/**\n * Extracts the string values from an enumeration-like object.\n *\n * @param {Record<number | string, number | string>} data - The enumeration-like object to extract string values from.\n * The keys can be numbers or strings, and the values can be numbers or strings.\n * @returns {string[]} An array of string values extracted from the object.\n *\n * @example\n * ```typescript\n * import { getEnumStringValues } from '@kikiutils/shared/enum';\n *\n * enum RecordType {\n * Receive = 0,\n * Send = 1,\n * Unknown = 'unknown'\n * }\n *\n * console.log(getEnumStringValues(RecordType)); // ['unknown']\n * ```\n */\nexport function getEnumStringValues(data: Record<number | string, number | string>) {\n const keys: string[] = [];\n const keysCount: Record<string, number> = {};\n const values: any[] = [];\n Object.entries(data).forEach(([key, value]) => {\n keys.push(key);\n values.push(value);\n if (typeof value !== 'string') return;\n keysCount[key] = (keysCount[key] ?? 0) + 1;\n keysCount[value] = (keysCount[value] ?? 0) + 1;\n });\n\n return values.filter((value) => {\n return typeof value === 'string' && (!keys.includes(value) || (keysCount[value] && keysCount[value] > 1));\n });\n}\n"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;AAmBG;AACG,SAAU,mBAAmB,CAAC,IAA8C,EAAA;AAC9E,IAAA,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,KAAK,OAAO,KAAK,KAAK,QAAQ,CAAC;AAC3E;AAEA;;;;;;;;;;;;;;;;;;;AAmBG;AACG,SAAU,mBAAmB,CAAC,IAA8C,EAAA;IAC9E,MAAM,IAAI,GAAa,EAAE;IACzB,MAAM,SAAS,GAA2B,EAAE;IAC5C,MAAM,MAAM,GAAU,EAAE;AACxB,IAAA,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,KAAI;AAC1C,QAAA,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC;AACd,QAAA,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC;QAClB,IAAI,OAAO,KAAK,KAAK,QAAQ;YAAE;AAC/B,QAAA,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC;AAC1C,QAAA,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC;AAClD,KAAC,CAAC;AAEF,IAAA,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,KAAI;AAC3B,QAAA,OAAO,OAAO,KAAK,KAAK,QAAQ,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,SAAS,CAAC,KAAK,CAAC,IAAI,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;AAC7G,KAAC,CAAC;AACN;;;;"}
1
+ {"version":3,"file":"enum.mjs","sources":["../src/enum.ts"],"sourcesContent":["/**\n * Extracts the numeric values from an enumeration-like object.\n *\n * @param {Record<number | string, number | string>} data - The enumeration-like object to extract numeric values from.\n * The keys can be numbers or strings, and the values can be numbers or strings.\n *\n * @returns {number[]} An array of numeric values extracted from the object.\n *\n * @example\n * ```typescript\n * import { getEnumNumberValues } from '@kikiutils/shared/enum';\n *\n * enum RecordType {\n * Receive = 0,\n * Send = 1,\n * Unknown = 'unknown'\n * }\n *\n * console.log(getEnumNumberValues(RecordType)); // [0, 1]\n * ```\n */\nexport function getEnumNumberValues(data: Record<number | string, number | string>) {\n return Object.values(data).filter((value) => typeof value === 'number');\n}\n\n/**\n * Extracts the string values from an enumeration-like object.\n *\n * @param {Record<number | string, number | string>} data - The enumeration-like object to extract string values from.\n * The keys can be numbers or strings, and the values can be numbers or strings.\n *\n * @returns {string[]} An array of string values extracted from the object.\n *\n * @example\n * ```typescript\n * import { getEnumStringValues } from '@kikiutils/shared/enum';\n *\n * enum RecordType {\n * Receive = 0,\n * Send = 1,\n * Unknown = 'unknown'\n * }\n *\n * console.log(getEnumStringValues(RecordType)); // ['unknown']\n * ```\n */\nexport function getEnumStringValues(data: Record<number | string, number | string>) {\n const keys: string[] = [];\n const keysCount: Record<string, number> = {};\n const values: any[] = [];\n Object.entries(data).forEach(([key, value]) => {\n keys.push(key);\n values.push(value);\n if (typeof value !== 'string') return;\n keysCount[key] = (keysCount[key] ?? 0) + 1;\n keysCount[value] = (keysCount[value] ?? 0) + 1;\n });\n\n return values.filter((value) => {\n return typeof value === 'string' && (!keys.includes(value) || (keysCount[value] && keysCount[value] > 1));\n });\n}\n"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;AAoBG;AACG,SAAU,mBAAmB,CAAC,IAA8C,EAAA;AAC9E,IAAA,OAAO,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,KAAK,OAAO,KAAK,KAAK,QAAQ,CAAC;AAC3E;AAEA;;;;;;;;;;;;;;;;;;;;AAoBG;AACG,SAAU,mBAAmB,CAAC,IAA8C,EAAA;IAC9E,MAAM,IAAI,GAAa,EAAE;IACzB,MAAM,SAAS,GAA2B,EAAE;IAC5C,MAAM,MAAM,GAAU,EAAE;AACxB,IAAA,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,EAAE,KAAK,CAAC,KAAI;AAC1C,QAAA,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC;AACd,QAAA,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC;QAClB,IAAI,OAAO,KAAK,KAAK,QAAQ;YAAE;AAC/B,QAAA,SAAS,CAAC,GAAG,CAAC,GAAG,CAAC,SAAS,CAAC,GAAG,CAAC,IAAI,CAAC,IAAI,CAAC;AAC1C,QAAA,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC;AAClD,KAAC,CAAC;AAEF,IAAA,OAAO,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,KAAI;AAC3B,QAAA,OAAO,OAAO,KAAK,KAAK,QAAQ,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,KAAK,CAAC,KAAK,SAAS,CAAC,KAAK,CAAC,IAAI,SAAS,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;AAC7G,KAAC,CAAC;AACN;;;;"}
package/dist/env.cjs CHANGED
@@ -25,7 +25,9 @@ class EnvironmentNotFoundError extends Error {
25
25
  * Retrieves the value of an environment variable, or throws an error if not set.
26
26
  *
27
27
  * @param {string} key - The environment variable key to check.
28
+ *
28
29
  * @returns {string} The value of the environment variable.
30
+ *
29
31
  * @throws {EnvironmentNotFoundError} If the environment variable is not defined.
30
32
  *
31
33
  * @example
package/dist/env.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"env.cjs","sources":["../src/env.ts"],"sourcesContent":["/**\n * Custom error class for handling missing environment variables.\n *\n * Extends the built-in `Error` class and includes the missing key.\n *\n * @extends {Error}\n */\nexport class EnvironmentNotFoundError extends Error {\n readonly key: string;\n\n /**\n * Creates a new EnvironmentNotFoundError.\n *\n * @param {string} key - The missing environment variable key.\n */\n constructor(key: string) {\n super(`Missing environment variable: ${key}`);\n this.key = key;\n this.name = this.constructor.name;\n Error.captureStackTrace?.(this, this.constructor);\n }\n}\n\n/**\n * Retrieves the value of an environment variable, or throws an error if not set.\n *\n * @param {string} key - The environment variable key to check.\n * @returns {string} The value of the environment variable.\n * @throws {EnvironmentNotFoundError} If the environment variable is not defined.\n *\n * @example\n * ```typescript\n * import { checkAndGetEnvValue } from '@kikiutils/shared/env';\n *\n * // When the environment variable 'API_KEY' is set:\n * console.log(checkAndGetEnvValue('API_KEY')); // value of API_KEY\n *\n * // When the environment variable 'API_KEY' is not set:\n * try {\n * const apiKey = checkAndGetEnvValue('API_KEY');\n * } catch (error) {\n * console.error(error); // Missing environment variable: API_KEY\n * }\n * ```\n */\nexport function checkAndGetEnvValue(key: string): string {\n if (!process.env[key]) throw new EnvironmentNotFoundError(key);\n return process.env[key];\n}\n"],"names":[],"mappings":";;AAAA;;;;;;AAMG;AACG,MAAO,wBAAyB,SAAQ,KAAK,CAAA;AACtC,IAAA,GAAG;AAEZ;;;;AAIG;AACH,IAAA,WAAA,CAAY,GAAW,EAAA;AACnB,QAAA,KAAK,CAAC,CAAA,8BAAA,EAAiC,GAAG,CAAA,CAAE,CAAC;AAC7C,QAAA,IAAI,CAAC,GAAG,GAAG,GAAG;QACd,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI;QACjC,KAAK,CAAC,iBAAiB,GAAG,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC;;AAExD;AAED;;;;;;;;;;;;;;;;;;;;;AAqBG;AACG,SAAU,mBAAmB,CAAC,GAAW,EAAA;AAC3C,IAAA,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;AAAE,QAAA,MAAM,IAAI,wBAAwB,CAAC,GAAG,CAAC;AAC9D,IAAA,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;AAC3B;;;;;"}
1
+ {"version":3,"file":"env.cjs","sources":["../src/env.ts"],"sourcesContent":["/**\n * Custom error class for handling missing environment variables.\n *\n * Extends the built-in `Error` class and includes the missing key.\n *\n * @extends {Error}\n */\nexport class EnvironmentNotFoundError extends Error {\n readonly key: string;\n\n /**\n * Creates a new EnvironmentNotFoundError.\n *\n * @param {string} key - The missing environment variable key.\n */\n constructor(key: string) {\n super(`Missing environment variable: ${key}`);\n this.key = key;\n this.name = this.constructor.name;\n Error.captureStackTrace?.(this, this.constructor);\n }\n}\n\n/**\n * Retrieves the value of an environment variable, or throws an error if not set.\n *\n * @param {string} key - The environment variable key to check.\n *\n * @returns {string} The value of the environment variable.\n *\n * @throws {EnvironmentNotFoundError} If the environment variable is not defined.\n *\n * @example\n * ```typescript\n * import { checkAndGetEnvValue } from '@kikiutils/shared/env';\n *\n * // When the environment variable 'API_KEY' is set:\n * console.log(checkAndGetEnvValue('API_KEY')); // value of API_KEY\n *\n * // When the environment variable 'API_KEY' is not set:\n * try {\n * const apiKey = checkAndGetEnvValue('API_KEY');\n * } catch (error) {\n * console.error(error); // Missing environment variable: API_KEY\n * }\n * ```\n */\nexport function checkAndGetEnvValue(key: string): string {\n if (!process.env[key]) throw new EnvironmentNotFoundError(key);\n return process.env[key];\n}\n"],"names":[],"mappings":";;AAAA;;;;;;AAMG;AACG,MAAO,wBAAyB,SAAQ,KAAK,CAAA;AACtC,IAAA,GAAG;AAEZ;;;;AAIG;AACH,IAAA,WAAA,CAAY,GAAW,EAAA;AACnB,QAAA,KAAK,CAAC,CAAA,8BAAA,EAAiC,GAAG,CAAA,CAAE,CAAC;AAC7C,QAAA,IAAI,CAAC,GAAG,GAAG,GAAG;QACd,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI;QACjC,KAAK,CAAC,iBAAiB,GAAG,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC;;AAExD;AAED;;;;;;;;;;;;;;;;;;;;;;;AAuBG;AACG,SAAU,mBAAmB,CAAC,GAAW,EAAA;AAC3C,IAAA,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;AAAE,QAAA,MAAM,IAAI,wBAAwB,CAAC,GAAG,CAAC;AAC9D,IAAA,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;AAC3B;;;;;"}
package/dist/env.d.ts CHANGED
@@ -18,7 +18,9 @@ export declare class EnvironmentNotFoundError extends Error {
18
18
  * Retrieves the value of an environment variable, or throws an error if not set.
19
19
  *
20
20
  * @param {string} key - The environment variable key to check.
21
+ *
21
22
  * @returns {string} The value of the environment variable.
23
+ *
22
24
  * @throws {EnvironmentNotFoundError} If the environment variable is not defined.
23
25
  *
24
26
  * @example
package/dist/env.d.ts.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,qBAAa,wBAAyB,SAAQ,KAAK;IAC/C,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAErB;;;;OAIG;gBACS,GAAG,EAAE,MAAM;CAM1B;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAGvD"}
1
+ {"version":3,"file":"env.d.ts","sourceRoot":"","sources":["../src/env.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,qBAAa,wBAAyB,SAAQ,KAAK;IAC/C,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;IAErB;;;;OAIG;gBACS,GAAG,EAAE,MAAM;CAM1B;AAED;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,mBAAmB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAGvD"}
package/dist/env.mjs CHANGED
@@ -23,7 +23,9 @@ class EnvironmentNotFoundError extends Error {
23
23
  * Retrieves the value of an environment variable, or throws an error if not set.
24
24
  *
25
25
  * @param {string} key - The environment variable key to check.
26
+ *
26
27
  * @returns {string} The value of the environment variable.
28
+ *
27
29
  * @throws {EnvironmentNotFoundError} If the environment variable is not defined.
28
30
  *
29
31
  * @example
package/dist/env.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"env.mjs","sources":["../src/env.ts"],"sourcesContent":["/**\n * Custom error class for handling missing environment variables.\n *\n * Extends the built-in `Error` class and includes the missing key.\n *\n * @extends {Error}\n */\nexport class EnvironmentNotFoundError extends Error {\n readonly key: string;\n\n /**\n * Creates a new EnvironmentNotFoundError.\n *\n * @param {string} key - The missing environment variable key.\n */\n constructor(key: string) {\n super(`Missing environment variable: ${key}`);\n this.key = key;\n this.name = this.constructor.name;\n Error.captureStackTrace?.(this, this.constructor);\n }\n}\n\n/**\n * Retrieves the value of an environment variable, or throws an error if not set.\n *\n * @param {string} key - The environment variable key to check.\n * @returns {string} The value of the environment variable.\n * @throws {EnvironmentNotFoundError} If the environment variable is not defined.\n *\n * @example\n * ```typescript\n * import { checkAndGetEnvValue } from '@kikiutils/shared/env';\n *\n * // When the environment variable 'API_KEY' is set:\n * console.log(checkAndGetEnvValue('API_KEY')); // value of API_KEY\n *\n * // When the environment variable 'API_KEY' is not set:\n * try {\n * const apiKey = checkAndGetEnvValue('API_KEY');\n * } catch (error) {\n * console.error(error); // Missing environment variable: API_KEY\n * }\n * ```\n */\nexport function checkAndGetEnvValue(key: string): string {\n if (!process.env[key]) throw new EnvironmentNotFoundError(key);\n return process.env[key];\n}\n"],"names":[],"mappings":"AAAA;;;;;;AAMG;AACG,MAAO,wBAAyB,SAAQ,KAAK,CAAA;AACtC,IAAA,GAAG;AAEZ;;;;AAIG;AACH,IAAA,WAAA,CAAY,GAAW,EAAA;AACnB,QAAA,KAAK,CAAC,CAAA,8BAAA,EAAiC,GAAG,CAAA,CAAE,CAAC;AAC7C,QAAA,IAAI,CAAC,GAAG,GAAG,GAAG;QACd,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI;QACjC,KAAK,CAAC,iBAAiB,GAAG,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC;;AAExD;AAED;;;;;;;;;;;;;;;;;;;;;AAqBG;AACG,SAAU,mBAAmB,CAAC,GAAW,EAAA;AAC3C,IAAA,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;AAAE,QAAA,MAAM,IAAI,wBAAwB,CAAC,GAAG,CAAC;AAC9D,IAAA,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;AAC3B;;;;"}
1
+ {"version":3,"file":"env.mjs","sources":["../src/env.ts"],"sourcesContent":["/**\n * Custom error class for handling missing environment variables.\n *\n * Extends the built-in `Error` class and includes the missing key.\n *\n * @extends {Error}\n */\nexport class EnvironmentNotFoundError extends Error {\n readonly key: string;\n\n /**\n * Creates a new EnvironmentNotFoundError.\n *\n * @param {string} key - The missing environment variable key.\n */\n constructor(key: string) {\n super(`Missing environment variable: ${key}`);\n this.key = key;\n this.name = this.constructor.name;\n Error.captureStackTrace?.(this, this.constructor);\n }\n}\n\n/**\n * Retrieves the value of an environment variable, or throws an error if not set.\n *\n * @param {string} key - The environment variable key to check.\n *\n * @returns {string} The value of the environment variable.\n *\n * @throws {EnvironmentNotFoundError} If the environment variable is not defined.\n *\n * @example\n * ```typescript\n * import { checkAndGetEnvValue } from '@kikiutils/shared/env';\n *\n * // When the environment variable 'API_KEY' is set:\n * console.log(checkAndGetEnvValue('API_KEY')); // value of API_KEY\n *\n * // When the environment variable 'API_KEY' is not set:\n * try {\n * const apiKey = checkAndGetEnvValue('API_KEY');\n * } catch (error) {\n * console.error(error); // Missing environment variable: API_KEY\n * }\n * ```\n */\nexport function checkAndGetEnvValue(key: string): string {\n if (!process.env[key]) throw new EnvironmentNotFoundError(key);\n return process.env[key];\n}\n"],"names":[],"mappings":"AAAA;;;;;;AAMG;AACG,MAAO,wBAAyB,SAAQ,KAAK,CAAA;AACtC,IAAA,GAAG;AAEZ;;;;AAIG;AACH,IAAA,WAAA,CAAY,GAAW,EAAA;AACnB,QAAA,KAAK,CAAC,CAAA,8BAAA,EAAiC,GAAG,CAAA,CAAE,CAAC;AAC7C,QAAA,IAAI,CAAC,GAAG,GAAG,GAAG;QACd,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC,WAAW,CAAC,IAAI;QACjC,KAAK,CAAC,iBAAiB,GAAG,IAAI,EAAE,IAAI,CAAC,WAAW,CAAC;;AAExD;AAED;;;;;;;;;;;;;;;;;;;;;;;AAuBG;AACG,SAAU,mBAAmB,CAAC,GAAW,EAAA;AAC3C,IAAA,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;AAAE,QAAA,MAAM,IAAI,wBAAwB,CAAC,GAAG,CAAC;AAC9D,IAAA,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC;AAC3B;;;;"}
@@ -1 +1 @@
1
- {"version":3,"file":"general.cjs","sources":["../src/general.ts"],"sourcesContent":["/**\n * Extracts the first value from an array or returns the value itself if it's not an array.\n *\n * - If `value` is an array, returns the first element.\n * - If `value` is not an array, returns `value` directly.\n * - If the result is `null` or `undefined`, and `defaultValue` is provided, returns `defaultValue` instead.\n *\n * @template T - The type of the input value(s).\n * @template D - The type of the default value (if provided).\n *\n * @param {T | T[]} value - A single value or an array of values.\n * @param {D} [defaultValue] - A fallback value if the result is `null` or `undefined`.\n * @returns {T | D | undefined} The first value or the fallback.\n *\n * @example\n * ```typescript\n * import { extractFirstValue } from '@kikiutils/shared/general';\n *\n * console.log(extractFirstValue([1, 2, 3])); // 1\n * console.log(extractFirstValue('hello')); // hello\n * console.log(extractFirstValue([], 'default')); // default\n * console.log(extractFirstValue(undefined, 'fallback')); // fallback\n * ```\n */\nexport function extractFirstValue<T>(value: T | T[]): T | undefined;\nexport function extractFirstValue<T, D>(value: T | T[], defaultValue: D): D | NonNullable<T>;\nexport function extractFirstValue<T, D>(value: T | T[], defaultValue?: D) {\n return (Array.isArray(value) ? value[0] : value) ?? defaultValue;\n}\n"],"names":[],"mappings":";;AA0BgB,SAAA,iBAAiB,CAAO,KAAc,EAAE,YAAgB,EAAA;IACpE,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,KAAK,YAAY;AACpE;;;;"}
1
+ {"version":3,"file":"general.cjs","sources":["../src/general.ts"],"sourcesContent":["/**\n * Extracts the first value from an array or returns the value itself if it's not an array.\n *\n * - If `value` is an array, returns the first element.\n * - If `value` is not an array, returns `value` directly.\n * - If the result is `null` or `undefined`, and `defaultValue` is provided, returns `defaultValue` instead.\n *\n * @template T - The type of the input value(s).\n * @template D - The type of the default value (if provided).\n *\n * @param {T | T[]} value - A single value or an array of values.\n * @param {D} [defaultValue] - A fallback value if the result is `null` or `undefined`.\n *\n * @returns {T | D | undefined} The first value or the fallback.\n *\n * @example\n * ```typescript\n * import { extractFirstValue } from '@kikiutils/shared/general';\n *\n * console.log(extractFirstValue([1, 2, 3])); // 1\n * console.log(extractFirstValue('hello')); // hello\n * console.log(extractFirstValue([], 'default')); // default\n * console.log(extractFirstValue(undefined, 'fallback')); // fallback\n * ```\n */\nexport function extractFirstValue<T>(value: T | T[]): T | undefined;\nexport function extractFirstValue<T, D>(value: T | T[], defaultValue: D): D | NonNullable<T>;\nexport function extractFirstValue<T, D>(value: T | T[], defaultValue?: D) {\n return (Array.isArray(value) ? value[0] : value) ?? defaultValue;\n}\n"],"names":[],"mappings":";;AA2BgB,SAAA,iBAAiB,CAAO,KAAc,EAAE,YAAgB,EAAA;IACpE,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,KAAK,YAAY;AACpE;;;;"}
package/dist/general.d.ts CHANGED
@@ -10,6 +10,7 @@
10
10
  *
11
11
  * @param {T | T[]} value - A single value or an array of values.
12
12
  * @param {D} [defaultValue] - A fallback value if the result is `null` or `undefined`.
13
+ *
13
14
  * @returns {T | D | undefined} The first value or the fallback.
14
15
  *
15
16
  * @example
@@ -1 +1 @@
1
- {"version":3,"file":"general.d.ts","sourceRoot":"","sources":["../src/general.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,GAAG,SAAS,CAAC;AACpE,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,EAAE,EAAE,YAAY,EAAE,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC"}
1
+ {"version":3,"file":"general.d.ts","sourceRoot":"","sources":["../src/general.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,EAAE,GAAG,CAAC,GAAG,SAAS,CAAC;AACpE,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,EAAE,EAAE,YAAY,EAAE,CAAC,GAAG,CAAC,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"general.mjs","sources":["../src/general.ts"],"sourcesContent":["/**\n * Extracts the first value from an array or returns the value itself if it's not an array.\n *\n * - If `value` is an array, returns the first element.\n * - If `value` is not an array, returns `value` directly.\n * - If the result is `null` or `undefined`, and `defaultValue` is provided, returns `defaultValue` instead.\n *\n * @template T - The type of the input value(s).\n * @template D - The type of the default value (if provided).\n *\n * @param {T | T[]} value - A single value or an array of values.\n * @param {D} [defaultValue] - A fallback value if the result is `null` or `undefined`.\n * @returns {T | D | undefined} The first value or the fallback.\n *\n * @example\n * ```typescript\n * import { extractFirstValue } from '@kikiutils/shared/general';\n *\n * console.log(extractFirstValue([1, 2, 3])); // 1\n * console.log(extractFirstValue('hello')); // hello\n * console.log(extractFirstValue([], 'default')); // default\n * console.log(extractFirstValue(undefined, 'fallback')); // fallback\n * ```\n */\nexport function extractFirstValue<T>(value: T | T[]): T | undefined;\nexport function extractFirstValue<T, D>(value: T | T[], defaultValue: D): D | NonNullable<T>;\nexport function extractFirstValue<T, D>(value: T | T[], defaultValue?: D) {\n return (Array.isArray(value) ? value[0] : value) ?? defaultValue;\n}\n"],"names":[],"mappings":"AA0BgB,SAAA,iBAAiB,CAAO,KAAc,EAAE,YAAgB,EAAA;IACpE,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,KAAK,YAAY;AACpE;;;;"}
1
+ {"version":3,"file":"general.mjs","sources":["../src/general.ts"],"sourcesContent":["/**\n * Extracts the first value from an array or returns the value itself if it's not an array.\n *\n * - If `value` is an array, returns the first element.\n * - If `value` is not an array, returns `value` directly.\n * - If the result is `null` or `undefined`, and `defaultValue` is provided, returns `defaultValue` instead.\n *\n * @template T - The type of the input value(s).\n * @template D - The type of the default value (if provided).\n *\n * @param {T | T[]} value - A single value or an array of values.\n * @param {D} [defaultValue] - A fallback value if the result is `null` or `undefined`.\n *\n * @returns {T | D | undefined} The first value or the fallback.\n *\n * @example\n * ```typescript\n * import { extractFirstValue } from '@kikiutils/shared/general';\n *\n * console.log(extractFirstValue([1, 2, 3])); // 1\n * console.log(extractFirstValue('hello')); // hello\n * console.log(extractFirstValue([], 'default')); // default\n * console.log(extractFirstValue(undefined, 'fallback')); // fallback\n * ```\n */\nexport function extractFirstValue<T>(value: T | T[]): T | undefined;\nexport function extractFirstValue<T, D>(value: T | T[], defaultValue: D): D | NonNullable<T>;\nexport function extractFirstValue<T, D>(value: T | T[], defaultValue?: D) {\n return (Array.isArray(value) ? value[0] : value) ?? defaultValue;\n}\n"],"names":[],"mappings":"AA2BgB,SAAA,iBAAiB,CAAO,KAAc,EAAE,YAAgB,EAAA;IACpE,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,KAAK,CAAC,CAAC,CAAC,GAAG,KAAK,KAAK,YAAY;AACpE;;;;"}
package/dist/math.cjs CHANGED
@@ -12,6 +12,7 @@ const decimal_js = require('decimal.js');
12
12
  * @param {CalculableValue} molecular - The numerator of the fraction.
13
13
  * @param {CalculableValue} denominator - The denominator of the fraction.
14
14
  * @param {ToPercentageStringOptions} [options] - Optional output settings.
15
+ *
15
16
  * @returns {string} Formatted percentage string.
16
17
  *
17
18
  * @example
package/dist/math.cjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"math.cjs","sources":["../src/math.ts"],"sourcesContent":["import { Decimal } from 'decimal.js';\n\ntype CalculableValue = Decimal.Value | { toString: () => string };\n\n/**\n * Options for configuring the output of `toPercentageString`.\n */\nexport interface ToPercentageStringOptions {\n /**\n * Number of decimal places to include in the result.\n * @default 2\n */\n decimalPlaces?: number;\n\n /**\n * Whether to include the '%' symbol in the result.\n * @default true\n */\n withSymbol?: boolean;\n}\n\n/**\n * Converts a fraction (numerator / denominator) into a percentage string.\n *\n * - Uses `decimal.js` for precise decimal calculations.\n * - Supports custom decimal places and optional percentage symbol.\n * - Returns `'0.00%'` if result is `NaN` or division is invalid.\n *\n * @param {CalculableValue} molecular - The numerator of the fraction.\n * @param {CalculableValue} denominator - The denominator of the fraction.\n * @param {ToPercentageStringOptions} [options] - Optional output settings.\n * @returns {string} Formatted percentage string.\n *\n * @example\n * ```typescript\n * import { toPercentageString } from '@kikiutils/shared/math';\n *\n * console.log(toPercentageString(50, 200)); // 25.00%\n * console.log(toPercentageString(50, 200, { withSymbol: false })); // 25.00\n * console.log(toPercentageString(50, 200, { decimalPlaces: 1 })); // 25.0%\n * ```\n */\nexport function toPercentageString(\n molecular: CalculableValue,\n denominator: CalculableValue,\n options?: ToPercentageStringOptions,\n) {\n const molecularDecimal = new Decimal(molecular.toString());\n const denominatorDecimal = new Decimal(denominator.toString());\n const calculationResult = molecularDecimal.div(denominatorDecimal);\n const result = calculationResult.isNaN()\n ? '0.00'\n : calculationResult.times(100).toFixed(options?.decimalPlaces ?? 2);\n\n return options?.withSymbol ?? true ? `${result}%` : result;\n}\n"],"names":["Decimal"],"mappings":";;;;AAqBA;;;;;;;;;;;;;;;;;;;;AAoBG;SACa,kBAAkB,CAC9B,SAA0B,EAC1B,WAA4B,EAC5B,OAAmC,EAAA;IAEnC,MAAM,gBAAgB,GAAG,IAAIA,kBAAO,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC;IAC1D,MAAM,kBAAkB,GAAG,IAAIA,kBAAO,CAAC,WAAW,CAAC,QAAQ,EAAE,CAAC;IAC9D,MAAM,iBAAiB,GAAG,gBAAgB,CAAC,GAAG,CAAC,kBAAkB,CAAC;AAClE,IAAA,MAAM,MAAM,GAAG,iBAAiB,CAAC,KAAK;AAClC,UAAE;AACF,UAAE,iBAAiB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,aAAa,IAAI,CAAC,CAAC;AAEvE,IAAA,OAAO,OAAO,EAAE,UAAU,IAAI,IAAI,GAAG,CAAA,EAAG,MAAM,CAAG,CAAA,CAAA,GAAG,MAAM;AAC9D;;;;"}
1
+ {"version":3,"file":"math.cjs","sources":["../src/math.ts"],"sourcesContent":["import { Decimal } from 'decimal.js';\n\ntype CalculableValue = Decimal.Value | { toString: () => string };\n\n/**\n * Options for configuring the output of `toPercentageString`.\n */\nexport interface ToPercentageStringOptions {\n /**\n * Number of decimal places to include in the result.\n *\n * @default 2\n */\n decimalPlaces?: number;\n\n /**\n * Whether to include the '%' symbol in the result.\n *\n * @default true\n */\n withSymbol?: boolean;\n}\n\n/**\n * Converts a fraction (numerator / denominator) into a percentage string.\n *\n * - Uses `decimal.js` for precise decimal calculations.\n * - Supports custom decimal places and optional percentage symbol.\n * - Returns `'0.00%'` if result is `NaN` or division is invalid.\n *\n * @param {CalculableValue} molecular - The numerator of the fraction.\n * @param {CalculableValue} denominator - The denominator of the fraction.\n * @param {ToPercentageStringOptions} [options] - Optional output settings.\n *\n * @returns {string} Formatted percentage string.\n *\n * @example\n * ```typescript\n * import { toPercentageString } from '@kikiutils/shared/math';\n *\n * console.log(toPercentageString(50, 200)); // 25.00%\n * console.log(toPercentageString(50, 200, { withSymbol: false })); // 25.00\n * console.log(toPercentageString(50, 200, { decimalPlaces: 1 })); // 25.0%\n * ```\n */\nexport function toPercentageString(\n molecular: CalculableValue,\n denominator: CalculableValue,\n options?: ToPercentageStringOptions,\n) {\n const molecularDecimal = new Decimal(molecular.toString());\n const denominatorDecimal = new Decimal(denominator.toString());\n const calculationResult = molecularDecimal.div(denominatorDecimal);\n const result = calculationResult.isNaN()\n ? '0.00'\n : calculationResult.times(100).toFixed(options?.decimalPlaces ?? 2);\n\n return options?.withSymbol ?? true ? `${result}%` : result;\n}\n"],"names":["Decimal"],"mappings":";;;;AAuBA;;;;;;;;;;;;;;;;;;;;;AAqBG;SACa,kBAAkB,CAC9B,SAA0B,EAC1B,WAA4B,EAC5B,OAAmC,EAAA;IAEnC,MAAM,gBAAgB,GAAG,IAAIA,kBAAO,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC;IAC1D,MAAM,kBAAkB,GAAG,IAAIA,kBAAO,CAAC,WAAW,CAAC,QAAQ,EAAE,CAAC;IAC9D,MAAM,iBAAiB,GAAG,gBAAgB,CAAC,GAAG,CAAC,kBAAkB,CAAC;AAClE,IAAA,MAAM,MAAM,GAAG,iBAAiB,CAAC,KAAK;AAClC,UAAE;AACF,UAAE,iBAAiB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,aAAa,IAAI,CAAC,CAAC;AAEvE,IAAA,OAAO,OAAO,EAAE,UAAU,IAAI,IAAI,GAAG,CAAA,EAAG,MAAM,CAAG,CAAA,CAAA,GAAG,MAAM;AAC9D;;;;"}
package/dist/math.d.ts CHANGED
@@ -8,11 +8,13 @@ type CalculableValue = Decimal.Value | {
8
8
  export interface ToPercentageStringOptions {
9
9
  /**
10
10
  * Number of decimal places to include in the result.
11
+ *
11
12
  * @default 2
12
13
  */
13
14
  decimalPlaces?: number;
14
15
  /**
15
16
  * Whether to include the '%' symbol in the result.
17
+ *
16
18
  * @default true
17
19
  */
18
20
  withSymbol?: boolean;
@@ -27,6 +29,7 @@ export interface ToPercentageStringOptions {
27
29
  * @param {CalculableValue} molecular - The numerator of the fraction.
28
30
  * @param {CalculableValue} denominator - The denominator of the fraction.
29
31
  * @param {ToPercentageStringOptions} [options] - Optional output settings.
32
+ *
30
33
  * @returns {string} Formatted percentage string.
31
34
  *
32
35
  * @example
@@ -1 +1 @@
1
- {"version":3,"file":"math.d.ts","sourceRoot":"","sources":["../src/math.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAErC,KAAK,eAAe,GAAG,OAAO,CAAC,KAAK,GAAG;IAAE,QAAQ,EAAE,MAAM,MAAM,CAAA;CAAE,CAAC;AAElE;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACtC;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB;;;OAGG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;CACxB;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,kBAAkB,CAC9B,SAAS,EAAE,eAAe,EAC1B,WAAW,EAAE,eAAe,EAC5B,OAAO,CAAC,EAAE,yBAAyB,UAUtC"}
1
+ {"version":3,"file":"math.d.ts","sourceRoot":"","sources":["../src/math.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,YAAY,CAAC;AAErC,KAAK,eAAe,GAAG,OAAO,CAAC,KAAK,GAAG;IAAE,QAAQ,EAAE,MAAM,MAAM,CAAA;CAAE,CAAC;AAElE;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACtC;;;;OAIG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IAEvB;;;;OAIG;IACH,UAAU,CAAC,EAAE,OAAO,CAAC;CACxB;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,kBAAkB,CAC9B,SAAS,EAAE,eAAe,EAC1B,WAAW,EAAE,eAAe,EAC5B,OAAO,CAAC,EAAE,yBAAyB,UAUtC"}
package/dist/math.mjs CHANGED
@@ -10,6 +10,7 @@ import { Decimal } from 'decimal.js';
10
10
  * @param {CalculableValue} molecular - The numerator of the fraction.
11
11
  * @param {CalculableValue} denominator - The denominator of the fraction.
12
12
  * @param {ToPercentageStringOptions} [options] - Optional output settings.
13
+ *
13
14
  * @returns {string} Formatted percentage string.
14
15
  *
15
16
  * @example
package/dist/math.mjs.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"math.mjs","sources":["../src/math.ts"],"sourcesContent":["import { Decimal } from 'decimal.js';\n\ntype CalculableValue = Decimal.Value | { toString: () => string };\n\n/**\n * Options for configuring the output of `toPercentageString`.\n */\nexport interface ToPercentageStringOptions {\n /**\n * Number of decimal places to include in the result.\n * @default 2\n */\n decimalPlaces?: number;\n\n /**\n * Whether to include the '%' symbol in the result.\n * @default true\n */\n withSymbol?: boolean;\n}\n\n/**\n * Converts a fraction (numerator / denominator) into a percentage string.\n *\n * - Uses `decimal.js` for precise decimal calculations.\n * - Supports custom decimal places and optional percentage symbol.\n * - Returns `'0.00%'` if result is `NaN` or division is invalid.\n *\n * @param {CalculableValue} molecular - The numerator of the fraction.\n * @param {CalculableValue} denominator - The denominator of the fraction.\n * @param {ToPercentageStringOptions} [options] - Optional output settings.\n * @returns {string} Formatted percentage string.\n *\n * @example\n * ```typescript\n * import { toPercentageString } from '@kikiutils/shared/math';\n *\n * console.log(toPercentageString(50, 200)); // 25.00%\n * console.log(toPercentageString(50, 200, { withSymbol: false })); // 25.00\n * console.log(toPercentageString(50, 200, { decimalPlaces: 1 })); // 25.0%\n * ```\n */\nexport function toPercentageString(\n molecular: CalculableValue,\n denominator: CalculableValue,\n options?: ToPercentageStringOptions,\n) {\n const molecularDecimal = new Decimal(molecular.toString());\n const denominatorDecimal = new Decimal(denominator.toString());\n const calculationResult = molecularDecimal.div(denominatorDecimal);\n const result = calculationResult.isNaN()\n ? '0.00'\n : calculationResult.times(100).toFixed(options?.decimalPlaces ?? 2);\n\n return options?.withSymbol ?? true ? `${result}%` : result;\n}\n"],"names":[],"mappings":";;AAqBA;;;;;;;;;;;;;;;;;;;;AAoBG;SACa,kBAAkB,CAC9B,SAA0B,EAC1B,WAA4B,EAC5B,OAAmC,EAAA;IAEnC,MAAM,gBAAgB,GAAG,IAAI,OAAO,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC;IAC1D,MAAM,kBAAkB,GAAG,IAAI,OAAO,CAAC,WAAW,CAAC,QAAQ,EAAE,CAAC;IAC9D,MAAM,iBAAiB,GAAG,gBAAgB,CAAC,GAAG,CAAC,kBAAkB,CAAC;AAClE,IAAA,MAAM,MAAM,GAAG,iBAAiB,CAAC,KAAK;AAClC,UAAE;AACF,UAAE,iBAAiB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,aAAa,IAAI,CAAC,CAAC;AAEvE,IAAA,OAAO,OAAO,EAAE,UAAU,IAAI,IAAI,GAAG,CAAA,EAAG,MAAM,CAAG,CAAA,CAAA,GAAG,MAAM;AAC9D;;;;"}
1
+ {"version":3,"file":"math.mjs","sources":["../src/math.ts"],"sourcesContent":["import { Decimal } from 'decimal.js';\n\ntype CalculableValue = Decimal.Value | { toString: () => string };\n\n/**\n * Options for configuring the output of `toPercentageString`.\n */\nexport interface ToPercentageStringOptions {\n /**\n * Number of decimal places to include in the result.\n *\n * @default 2\n */\n decimalPlaces?: number;\n\n /**\n * Whether to include the '%' symbol in the result.\n *\n * @default true\n */\n withSymbol?: boolean;\n}\n\n/**\n * Converts a fraction (numerator / denominator) into a percentage string.\n *\n * - Uses `decimal.js` for precise decimal calculations.\n * - Supports custom decimal places and optional percentage symbol.\n * - Returns `'0.00%'` if result is `NaN` or division is invalid.\n *\n * @param {CalculableValue} molecular - The numerator of the fraction.\n * @param {CalculableValue} denominator - The denominator of the fraction.\n * @param {ToPercentageStringOptions} [options] - Optional output settings.\n *\n * @returns {string} Formatted percentage string.\n *\n * @example\n * ```typescript\n * import { toPercentageString } from '@kikiutils/shared/math';\n *\n * console.log(toPercentageString(50, 200)); // 25.00%\n * console.log(toPercentageString(50, 200, { withSymbol: false })); // 25.00\n * console.log(toPercentageString(50, 200, { decimalPlaces: 1 })); // 25.0%\n * ```\n */\nexport function toPercentageString(\n molecular: CalculableValue,\n denominator: CalculableValue,\n options?: ToPercentageStringOptions,\n) {\n const molecularDecimal = new Decimal(molecular.toString());\n const denominatorDecimal = new Decimal(denominator.toString());\n const calculationResult = molecularDecimal.div(denominatorDecimal);\n const result = calculationResult.isNaN()\n ? '0.00'\n : calculationResult.times(100).toFixed(options?.decimalPlaces ?? 2);\n\n return options?.withSymbol ?? true ? `${result}%` : result;\n}\n"],"names":[],"mappings":";;AAuBA;;;;;;;;;;;;;;;;;;;;;AAqBG;SACa,kBAAkB,CAC9B,SAA0B,EAC1B,WAA4B,EAC5B,OAAmC,EAAA;IAEnC,MAAM,gBAAgB,GAAG,IAAI,OAAO,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC;IAC1D,MAAM,kBAAkB,GAAG,IAAI,OAAO,CAAC,WAAW,CAAC,QAAQ,EAAE,CAAC;IAC9D,MAAM,iBAAiB,GAAG,gBAAgB,CAAC,GAAG,CAAC,kBAAkB,CAAC;AAClE,IAAA,MAAM,MAAM,GAAG,iBAAiB,CAAC,KAAK;AAClC,UAAE;AACF,UAAE,iBAAiB,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC,OAAO,EAAE,aAAa,IAAI,CAAC,CAAC;AAEvE,IAAA,OAAO,OAAO,EAAE,UAAU,IAAI,IAAI,GAAG,CAAA,EAAG,MAAM,CAAG,CAAA,CAAA,GAAG,MAAM;AAC9D;;;;"}
package/dist/number.cjs CHANGED
@@ -9,6 +9,7 @@ const millify = require('millify');
9
9
  *
10
10
  * @param {number} value - The number to format.
11
11
  * @param {Parameters<typeof millify>[1]} [options] - Optional configuration passed to `millify`.
12
+ *
12
13
  * @returns {string} The compact number string.
13
14
  *
14
15
  * @example