@kikiutils/shared 9.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +142 -0
- package/dist/consola.cjs +35 -0
- package/dist/consola.cjs.map +1 -0
- package/dist/consola.d.ts +24 -0
- package/dist/consola.d.ts.map +1 -0
- package/dist/consola.mjs +32 -0
- package/dist/consola.mjs.map +1 -0
- package/dist/crypto-hash.cjs +60 -0
- package/dist/crypto-hash.cjs.map +1 -0
- package/dist/crypto-hash.d.ts +26 -0
- package/dist/crypto-hash.d.ts.map +1 -0
- package/dist/crypto-hash.mjs +49 -0
- package/dist/crypto-hash.mjs.map +1 -0
- package/dist/datetime.cjs +131 -0
- package/dist/datetime.cjs.map +1 -0
- package/dist/datetime.d.ts +79 -0
- package/dist/datetime.d.ts.map +1 -0
- package/dist/datetime.mjs +127 -0
- package/dist/datetime.mjs.map +1 -0
- package/dist/enum.cjs +65 -0
- package/dist/enum.cjs.map +1 -0
- package/dist/enum.d.ts +43 -0
- package/dist/enum.d.ts.map +1 -0
- package/dist/enum.mjs +62 -0
- package/dist/enum.mjs.map +1 -0
- package/dist/env.cjs +54 -0
- package/dist/env.cjs.map +1 -0
- package/dist/env.d.ts +40 -0
- package/dist/env.d.ts.map +1 -0
- package/dist/env.mjs +51 -0
- package/dist/env.mjs.map +1 -0
- package/dist/general.cjs +8 -0
- package/dist/general.cjs.map +1 -0
- package/dist/general.d.ts +27 -0
- package/dist/general.d.ts.map +1 -0
- package/dist/general.mjs +6 -0
- package/dist/general.mjs.map +1 -0
- package/dist/hash.cjs +27 -0
- package/dist/hash.cjs.map +1 -0
- package/dist/hash.d.ts +17 -0
- package/dist/hash.d.ts.map +1 -0
- package/dist/hash.mjs +22 -0
- package/dist/hash.mjs.map +1 -0
- package/dist/math.cjs +37 -0
- package/dist/math.cjs.map +1 -0
- package/dist/math.d.ts +43 -0
- package/dist/math.d.ts.map +1 -0
- package/dist/math.mjs +35 -0
- package/dist/math.mjs.map +1 -0
- package/dist/number.cjs +31 -0
- package/dist/number.cjs.map +1 -0
- package/dist/number.d.ts +20 -0
- package/dist/number.d.ts.map +1 -0
- package/dist/number.mjs +29 -0
- package/dist/number.mjs.map +1 -0
- package/dist/pino.cjs +42 -0
- package/dist/pino.cjs.map +1 -0
- package/dist/pino.d.ts +24 -0
- package/dist/pino.d.ts.map +1 -0
- package/dist/pino.mjs +39 -0
- package/dist/pino.mjs.map +1 -0
- package/dist/random.cjs +30 -0
- package/dist/random.cjs.map +1 -0
- package/dist/random.d.ts +21 -0
- package/dist/random.d.ts.map +1 -0
- package/dist/random.mjs +28 -0
- package/dist/random.mjs.map +1 -0
- package/dist/string.cjs +44 -0
- package/dist/string.cjs.map +1 -0
- package/dist/string.d.ts +21 -0
- package/dist/string.d.ts.map +1 -0
- package/dist/string.mjs +42 -0
- package/dist/string.mjs.map +1 -0
- package/package.json +82 -0
- package/src/consola.ts +27 -0
- package/src/crypto-hash.ts +60 -0
- package/src/datetime.ts +154 -0
- package/src/enum.ts +60 -0
- package/src/env.ts +49 -0
- package/src/general.ts +29 -0
- package/src/hash.ts +25 -0
- package/src/math.ts +56 -0
- package/src/number.ts +29 -0
- package/src/pino.ts +37 -0
- package/src/random.ts +31 -0
- package/src/string.ts +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 kiki-kanri
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# @kikiutils/shared
|
|
2
|
+
|
|
3
|
+
[![npm version][npm-version-src]][npm-version-href]
|
|
4
|
+
[![npm downloads][npm-downloads-src]][npm-downloads-href]
|
|
5
|
+
[![codecov][codecov-src]][codecov-href]
|
|
6
|
+
[![License][license-src]][license-href]
|
|
7
|
+
|
|
8
|
+
A lightweight modular utility library for JavaScript and TypeScript, offering secure hashing, flexible logging, date utilities, Vue/web helpers, and more.
|
|
9
|
+
|
|
10
|
+
- [✨ Release Notes](./CHANGELOG.md)
|
|
11
|
+
|
|
12
|
+
## Features
|
|
13
|
+
|
|
14
|
+
- 📜 Simple and flexible logging with Consola and Pino
|
|
15
|
+
- 🔒 Secure hash utilities: MD5, SHA3-224/256/384/512
|
|
16
|
+
- 📅 Datetime utilities for formatting, ranges, and offsets
|
|
17
|
+
- 🔢 Enum helpers to extract values and keys
|
|
18
|
+
- 🌱 Environment variable checker with error handling (Node only)
|
|
19
|
+
- 📈 Math utilities like percentage formatting and rounding
|
|
20
|
+
- 💎 Number formatting (e.g. compact, currency, padding)
|
|
21
|
+
- 🔤 String tools such as random string generation and casing helpers
|
|
22
|
+
- 🌐 URL utilities for parsing and building query strings
|
|
23
|
+
- 🖥️ Web utilities using DOM APIs (e.g. `scrollToTop`) (Browser only)
|
|
24
|
+
- 🧩 Vue 3 utilities
|
|
25
|
+
- ⚙️ General-purpose utilities like value extractors and type guards
|
|
26
|
+
- 📦 Modular by design — import only what you need via `@kikiutils/shared/<module>`
|
|
27
|
+
|
|
28
|
+
## Requirements
|
|
29
|
+
|
|
30
|
+
- **Node.js** `>= 18.12.1`
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
Using [pnpm](https://pnpm.io):
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pnpm add @kikiutils/shared
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
You can also use `yarn`, `npm`, or `bun`.
|
|
41
|
+
|
|
42
|
+
> [!NOTE]
|
|
43
|
+
> This package is modular. It does not install all dependencies by default.
|
|
44
|
+
>
|
|
45
|
+
> If a utility depends on a third-party package (e.g. `date-fns`), you must install it manually.
|
|
46
|
+
|
|
47
|
+
## Usage
|
|
48
|
+
|
|
49
|
+
Import the functions or modules you want to use:
|
|
50
|
+
|
|
51
|
+
```typescript
|
|
52
|
+
import { logger } from '@kikituils/shared/consola';
|
|
53
|
+
import { extractFirstValue } from '@kikituils/shared/general';
|
|
54
|
+
|
|
55
|
+
const data = [
|
|
56
|
+
0,
|
|
57
|
+
1
|
|
58
|
+
];
|
|
59
|
+
|
|
60
|
+
const value = extractFirstValue(data);
|
|
61
|
+
console.log(value); // 0
|
|
62
|
+
logger.info(value);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Modules & Functions
|
|
66
|
+
|
|
67
|
+
Each module file includes function-level comments and usage examples.
|
|
68
|
+
|
|
69
|
+
### [consola](./src/consola.ts)
|
|
70
|
+
|
|
71
|
+
- Console logger integration
|
|
72
|
+
|
|
73
|
+
### [crypto-hash](./src/crypto-hash.ts)
|
|
74
|
+
|
|
75
|
+
- `cryptoMd5`, `cryptoMd5ToBuffer`
|
|
76
|
+
- `cryptoSha3224`, `cryptoSha3224ToBuffer`
|
|
77
|
+
- `cryptoSha3256`, `cryptoSha3256ToBuffer`
|
|
78
|
+
- `cryptoSha3384`, `cryptoSha3384ToBuffer`
|
|
79
|
+
- `cryptoSha3512`, `cryptoSha3512ToBuffer`
|
|
80
|
+
|
|
81
|
+
### [datetime](./src/datetime.ts)
|
|
82
|
+
|
|
83
|
+
- `formatDate`
|
|
84
|
+
- `getDateRangeFromDate`
|
|
85
|
+
- `getMidnightDateFromToday`
|
|
86
|
+
|
|
87
|
+
### [enum](./src/enum.ts)
|
|
88
|
+
|
|
89
|
+
- `getEnumStringValues`
|
|
90
|
+
- `getEnumNumberValues`
|
|
91
|
+
|
|
92
|
+
### [env](./src/env.ts)
|
|
93
|
+
|
|
94
|
+
- `checkAndGetEnvValue`
|
|
95
|
+
|
|
96
|
+
### [general](./src/general.ts)
|
|
97
|
+
|
|
98
|
+
- `extractFirstValue`
|
|
99
|
+
|
|
100
|
+
### [hash](./src/hash.ts)
|
|
101
|
+
|
|
102
|
+
- `sha3224`
|
|
103
|
+
- `sha3256`
|
|
104
|
+
- `sha3384`
|
|
105
|
+
- `sha3512`
|
|
106
|
+
|
|
107
|
+
### [math](./src/math.ts)
|
|
108
|
+
|
|
109
|
+
- `toPercentageString`
|
|
110
|
+
|
|
111
|
+
### [number](./src/number.ts)
|
|
112
|
+
|
|
113
|
+
- `toCompactNumberString`
|
|
114
|
+
|
|
115
|
+
### [pino](./src/pino.ts)
|
|
116
|
+
|
|
117
|
+
- Pino logger integration
|
|
118
|
+
|
|
119
|
+
### [random](./src/random.ts)
|
|
120
|
+
|
|
121
|
+
- `generateWithNestedRandomLength`
|
|
122
|
+
|
|
123
|
+
### [string](./src/string.ts)
|
|
124
|
+
|
|
125
|
+
- `randomString`
|
|
126
|
+
|
|
127
|
+
## License
|
|
128
|
+
|
|
129
|
+
[MIT License](./LICENSE)
|
|
130
|
+
|
|
131
|
+
<!-- Badges -->
|
|
132
|
+
[npm-version-href]: https://npmjs.com/package/@kikiutils/shared
|
|
133
|
+
[npm-version-src]: https://img.shields.io/npm/v/@kikiutils/shared/latest.svg?colorA=18181b&colorB=28cf8d&style=flat
|
|
134
|
+
|
|
135
|
+
[npm-downloads-href]: https://npmjs.com/package/@kikiutils/shared
|
|
136
|
+
[npm-downloads-src]: https://img.shields.io/npm/dm/@kikiutils/shared.svg?colorA=18181b&colorB=28cf8d&style=flat
|
|
137
|
+
|
|
138
|
+
[codecov-href]: https://codecov.io/gh/kikiutils/node-shared
|
|
139
|
+
[codecov-src]: https://codecov.io/gh/kikiutils/node-shared/graph/badge.svg?token=GRSQ7JO39E
|
|
140
|
+
|
|
141
|
+
[license-href]: https://github.com/kikiutils/node-shared/blob/main/LICENSE
|
|
142
|
+
[license-src]: https://img.shields.io/github/license/kikiutils/node-shared?colorA=18181b&colorB=28cf8d&style=flat
|
package/dist/consola.cjs
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const consola = require('consola');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A consola logger instance.
|
|
7
|
+
*
|
|
8
|
+
* The logger's level is determined based on the `CONSOLA_LOGGER_LEVEL` and `NODE_ENV` environment variables.
|
|
9
|
+
* If `CONSOLA_LOGGER_LEVEL` is set, it will be used; otherwise, if `NODE_ENV` is `production`,
|
|
10
|
+
* the level will be set to `0`.
|
|
11
|
+
*
|
|
12
|
+
* To manually change the level, assign the desired level to `logger.level`.
|
|
13
|
+
*
|
|
14
|
+
* See available levels [here](https://github.com/unjs/consola?tab=readme-ov-file#log-level).
|
|
15
|
+
*
|
|
16
|
+
* @example
|
|
17
|
+
* ```typescript
|
|
18
|
+
* import logger from '@kikiutils/shared/consola';
|
|
19
|
+
*
|
|
20
|
+
* logger.info('test'); // ℹ test 3:56:30 AM
|
|
21
|
+
*
|
|
22
|
+
* // Manually change the level
|
|
23
|
+
* logger.level = 3;
|
|
24
|
+
* ```
|
|
25
|
+
*/
|
|
26
|
+
const consolaLogger = consola.createConsola();
|
|
27
|
+
const logger = consolaLogger;
|
|
28
|
+
if (process.env.CONSOLA_LOGGER_LEVEL !== undefined)
|
|
29
|
+
consolaLogger.level = +process.env.CONSOLA_LOGGER_LEVEL;
|
|
30
|
+
else
|
|
31
|
+
consolaLogger.level = process.env.NODE_ENV === 'production' ? 0 : consolaLogger.level;
|
|
32
|
+
|
|
33
|
+
exports.consolaLogger = consolaLogger;
|
|
34
|
+
exports.logger = logger;
|
|
35
|
+
//# sourceMappingURL=consola.cjs.map
|
|
@@ -0,0 +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;;;;;"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A consola logger instance.
|
|
3
|
+
*
|
|
4
|
+
* The logger's level is determined based on the `CONSOLA_LOGGER_LEVEL` and `NODE_ENV` environment variables.
|
|
5
|
+
* If `CONSOLA_LOGGER_LEVEL` is set, it will be used; otherwise, if `NODE_ENV` is `production`,
|
|
6
|
+
* the level will be set to `0`.
|
|
7
|
+
*
|
|
8
|
+
* To manually change the level, assign the desired level to `logger.level`.
|
|
9
|
+
*
|
|
10
|
+
* See available levels [here](https://github.com/unjs/consola?tab=readme-ov-file#log-level).
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import logger from '@kikiutils/shared/consola';
|
|
15
|
+
*
|
|
16
|
+
* logger.info('test'); // ℹ test 3:56:30 AM
|
|
17
|
+
*
|
|
18
|
+
* // Manually change the level
|
|
19
|
+
* logger.level = 3;
|
|
20
|
+
* ```
|
|
21
|
+
*/
|
|
22
|
+
export declare const consolaLogger: import("consola").ConsolaInstance;
|
|
23
|
+
export declare const logger: import("consola").ConsolaInstance;
|
|
24
|
+
//# sourceMappingURL=consola.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"consola.d.ts","sourceRoot":"","sources":["../src/consola.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,eAAO,MAAM,aAAa,mCAAkB,CAAC;AAC7C,eAAO,MAAM,MAAM,mCAAgB,CAAC"}
|
package/dist/consola.mjs
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { createConsola } from 'consola';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A consola logger instance.
|
|
5
|
+
*
|
|
6
|
+
* The logger's level is determined based on the `CONSOLA_LOGGER_LEVEL` and `NODE_ENV` environment variables.
|
|
7
|
+
* If `CONSOLA_LOGGER_LEVEL` is set, it will be used; otherwise, if `NODE_ENV` is `production`,
|
|
8
|
+
* the level will be set to `0`.
|
|
9
|
+
*
|
|
10
|
+
* To manually change the level, assign the desired level to `logger.level`.
|
|
11
|
+
*
|
|
12
|
+
* See available levels [here](https://github.com/unjs/consola?tab=readme-ov-file#log-level).
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* import logger from '@kikiutils/shared/consola';
|
|
17
|
+
*
|
|
18
|
+
* logger.info('test'); // ℹ test 3:56:30 AM
|
|
19
|
+
*
|
|
20
|
+
* // Manually change the level
|
|
21
|
+
* logger.level = 3;
|
|
22
|
+
* ```
|
|
23
|
+
*/
|
|
24
|
+
const consolaLogger = createConsola();
|
|
25
|
+
const logger = consolaLogger;
|
|
26
|
+
if (process.env.CONSOLA_LOGGER_LEVEL !== undefined)
|
|
27
|
+
consolaLogger.level = +process.env.CONSOLA_LOGGER_LEVEL;
|
|
28
|
+
else
|
|
29
|
+
consolaLogger.level = process.env.NODE_ENV === 'production' ? 0 : consolaLogger.level;
|
|
30
|
+
|
|
31
|
+
export { consolaLogger, logger };
|
|
32
|
+
//# sourceMappingURL=consola.mjs.map
|
|
@@ -0,0 +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;;;;"}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const node_crypto = require('node:crypto');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* This file provides a set of functions for creating hash digests using different algorithms and bit lengths.
|
|
7
|
+
* It includes functions for generating SHA-3 hash digests with bit lengths of 224, 256, 384, and 512,
|
|
8
|
+
* as well as a function for generating MD5 hash digests.
|
|
9
|
+
* These functions use the Node.js crypto module to generate the hashes.
|
|
10
|
+
* Can only be used in Node.js/Deno/Bun runtimes.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* import { cryptoSha3256 } from '@kikiutils/shared/crypto-hash';
|
|
15
|
+
*
|
|
16
|
+
* console.log(cryptoSha3256('test')); // 36f028580bb02cc8272a9a020f4200e346e276ae664e45ee80745574e2f5ab80
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
function cryptoMd5(data, outputEncoding = 'hex') {
|
|
20
|
+
return node_crypto.createHash('md5').update(data).digest(outputEncoding);
|
|
21
|
+
}
|
|
22
|
+
function cryptoMd5ToBuffer(data) {
|
|
23
|
+
return node_crypto.createHash('md5').update(data).digest();
|
|
24
|
+
}
|
|
25
|
+
function cryptoSha3224(data, outputEncoding = 'hex') {
|
|
26
|
+
return node_crypto.createHash('sha3-224').update(data).digest(outputEncoding);
|
|
27
|
+
}
|
|
28
|
+
function cryptoSha3224ToBuffer(data) {
|
|
29
|
+
return node_crypto.createHash('sha3-224').update(data).digest();
|
|
30
|
+
}
|
|
31
|
+
function cryptoSha3256(data, outputEncoding = 'hex') {
|
|
32
|
+
return node_crypto.createHash('sha3-256').update(data).digest(outputEncoding);
|
|
33
|
+
}
|
|
34
|
+
function cryptoSha3256ToBuffer(data) {
|
|
35
|
+
return node_crypto.createHash('sha3-256').update(data).digest();
|
|
36
|
+
}
|
|
37
|
+
function cryptoSha3384(data, outputEncoding = 'hex') {
|
|
38
|
+
return node_crypto.createHash('sha3-384').update(data).digest(outputEncoding);
|
|
39
|
+
}
|
|
40
|
+
function cryptoSha3384ToBuffer(data) {
|
|
41
|
+
return node_crypto.createHash('sha3-384').update(data).digest();
|
|
42
|
+
}
|
|
43
|
+
function cryptoSha3512(data, outputEncoding = 'hex') {
|
|
44
|
+
return node_crypto.createHash('sha3-512').update(data).digest(outputEncoding);
|
|
45
|
+
}
|
|
46
|
+
function cryptoSha3512ToBuffer(data) {
|
|
47
|
+
return node_crypto.createHash('sha3-512').update(data).digest();
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
exports.cryptoMd5 = cryptoMd5;
|
|
51
|
+
exports.cryptoMd5ToBuffer = cryptoMd5ToBuffer;
|
|
52
|
+
exports.cryptoSha3224 = cryptoSha3224;
|
|
53
|
+
exports.cryptoSha3224ToBuffer = cryptoSha3224ToBuffer;
|
|
54
|
+
exports.cryptoSha3256 = cryptoSha3256;
|
|
55
|
+
exports.cryptoSha3256ToBuffer = cryptoSha3256ToBuffer;
|
|
56
|
+
exports.cryptoSha3384 = cryptoSha3384;
|
|
57
|
+
exports.cryptoSha3384ToBuffer = cryptoSha3384ToBuffer;
|
|
58
|
+
exports.cryptoSha3512 = cryptoSha3512;
|
|
59
|
+
exports.cryptoSha3512ToBuffer = cryptoSha3512ToBuffer;
|
|
60
|
+
//# sourceMappingURL=crypto-hash.cjs.map
|
|
@@ -0,0 +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;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This file provides a set of functions for creating hash digests using different algorithms and bit lengths.
|
|
3
|
+
* It includes functions for generating SHA-3 hash digests with bit lengths of 224, 256, 384, and 512,
|
|
4
|
+
* as well as a function for generating MD5 hash digests.
|
|
5
|
+
* These functions use the Node.js crypto module to generate the hashes.
|
|
6
|
+
* Can only be used in Node.js/Deno/Bun runtimes.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { cryptoSha3256 } from '@kikiutils/shared/crypto-hash';
|
|
11
|
+
*
|
|
12
|
+
* console.log(cryptoSha3256('test')); // 36f028580bb02cc8272a9a020f4200e346e276ae664e45ee80745574e2f5ab80
|
|
13
|
+
* ```
|
|
14
|
+
*/
|
|
15
|
+
import type { BinaryLike, BinaryToTextEncoding } from 'node:crypto';
|
|
16
|
+
export declare function cryptoMd5(data: BinaryLike, outputEncoding?: BinaryToTextEncoding): string;
|
|
17
|
+
export declare function cryptoMd5ToBuffer(data: BinaryLike): Buffer<ArrayBufferLike>;
|
|
18
|
+
export declare function cryptoSha3224(data: BinaryLike, outputEncoding?: BinaryToTextEncoding): string;
|
|
19
|
+
export declare function cryptoSha3224ToBuffer(data: BinaryLike): Buffer<ArrayBufferLike>;
|
|
20
|
+
export declare function cryptoSha3256(data: BinaryLike, outputEncoding?: BinaryToTextEncoding): string;
|
|
21
|
+
export declare function cryptoSha3256ToBuffer(data: BinaryLike): Buffer<ArrayBufferLike>;
|
|
22
|
+
export declare function cryptoSha3384(data: BinaryLike, outputEncoding?: BinaryToTextEncoding): string;
|
|
23
|
+
export declare function cryptoSha3384ToBuffer(data: BinaryLike): Buffer<ArrayBufferLike>;
|
|
24
|
+
export declare function cryptoSha3512(data: BinaryLike, outputEncoding?: BinaryToTextEncoding): string;
|
|
25
|
+
export declare function cryptoSha3512ToBuffer(data: BinaryLike): Buffer<ArrayBufferLike>;
|
|
26
|
+
//# sourceMappingURL=crypto-hash.d.ts.map
|
|
@@ -0,0 +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"}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* This file provides a set of functions for creating hash digests using different algorithms and bit lengths.
|
|
5
|
+
* It includes functions for generating SHA-3 hash digests with bit lengths of 224, 256, 384, and 512,
|
|
6
|
+
* as well as a function for generating MD5 hash digests.
|
|
7
|
+
* These functions use the Node.js crypto module to generate the hashes.
|
|
8
|
+
* Can only be used in Node.js/Deno/Bun runtimes.
|
|
9
|
+
*
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* import { cryptoSha3256 } from '@kikiutils/shared/crypto-hash';
|
|
13
|
+
*
|
|
14
|
+
* console.log(cryptoSha3256('test')); // 36f028580bb02cc8272a9a020f4200e346e276ae664e45ee80745574e2f5ab80
|
|
15
|
+
* ```
|
|
16
|
+
*/
|
|
17
|
+
function cryptoMd5(data, outputEncoding = 'hex') {
|
|
18
|
+
return createHash('md5').update(data).digest(outputEncoding);
|
|
19
|
+
}
|
|
20
|
+
function cryptoMd5ToBuffer(data) {
|
|
21
|
+
return createHash('md5').update(data).digest();
|
|
22
|
+
}
|
|
23
|
+
function cryptoSha3224(data, outputEncoding = 'hex') {
|
|
24
|
+
return createHash('sha3-224').update(data).digest(outputEncoding);
|
|
25
|
+
}
|
|
26
|
+
function cryptoSha3224ToBuffer(data) {
|
|
27
|
+
return createHash('sha3-224').update(data).digest();
|
|
28
|
+
}
|
|
29
|
+
function cryptoSha3256(data, outputEncoding = 'hex') {
|
|
30
|
+
return createHash('sha3-256').update(data).digest(outputEncoding);
|
|
31
|
+
}
|
|
32
|
+
function cryptoSha3256ToBuffer(data) {
|
|
33
|
+
return createHash('sha3-256').update(data).digest();
|
|
34
|
+
}
|
|
35
|
+
function cryptoSha3384(data, outputEncoding = 'hex') {
|
|
36
|
+
return createHash('sha3-384').update(data).digest(outputEncoding);
|
|
37
|
+
}
|
|
38
|
+
function cryptoSha3384ToBuffer(data) {
|
|
39
|
+
return createHash('sha3-384').update(data).digest();
|
|
40
|
+
}
|
|
41
|
+
function cryptoSha3512(data, outputEncoding = 'hex') {
|
|
42
|
+
return createHash('sha3-512').update(data).digest(outputEncoding);
|
|
43
|
+
}
|
|
44
|
+
function cryptoSha3512ToBuffer(data) {
|
|
45
|
+
return createHash('sha3-512').update(data).digest();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export { cryptoMd5, cryptoMd5ToBuffer, cryptoSha3224, cryptoSha3224ToBuffer, cryptoSha3256, cryptoSha3256ToBuffer, cryptoSha3384, cryptoSha3384ToBuffer, cryptoSha3512, cryptoSha3512ToBuffer };
|
|
49
|
+
//# sourceMappingURL=crypto-hash.mjs.map
|
|
@@ -0,0 +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;;;;"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const dateFns = require('date-fns');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Formats a given date, timestamp, or date string into a specified format.
|
|
7
|
+
*
|
|
8
|
+
* This function is a wrapper around `date-fns/format`.
|
|
9
|
+
*
|
|
10
|
+
* @param {DateArg<Date>} date - The input date to format. Can be a Date object, a timestamp, or a string.
|
|
11
|
+
* @param {string} [format] - The target format string.
|
|
12
|
+
* @param {FormatOptions} [options] - Optional formatting options passed to `date-fns/format`.
|
|
13
|
+
* @returns {string} The formatted date string.
|
|
14
|
+
*
|
|
15
|
+
* @example
|
|
16
|
+
* ```typescript
|
|
17
|
+
* import { formatDate } from '@kikiutils/shared/datetime';
|
|
18
|
+
*
|
|
19
|
+
* // Format a Date object
|
|
20
|
+
* console.log(formatDate(new Date(), 'yyyy-MM-dd')); // 2024-07-10
|
|
21
|
+
*
|
|
22
|
+
* // Format a timestamp
|
|
23
|
+
* console.log(formatDate(1657814400000, 'yyyy-MM-dd')); // 2022-07-15
|
|
24
|
+
*
|
|
25
|
+
* // Format a date string
|
|
26
|
+
* console.log(formatDate('2024-07-10T00:00:00Z', 'yyyy-MM-dd')); // 2024-07-10
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @see https://date-fns.org/docs/format
|
|
30
|
+
*/
|
|
31
|
+
function formatDate(date, format = 'yyyy-MM-dd HH:mm:ss', options) {
|
|
32
|
+
return dateFns.format(date, format, options);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Get the date range (start and end) based on a given date and range type.
|
|
36
|
+
*
|
|
37
|
+
* Supports common range types like 'lastMonth', 'lastWeek', 'thisMonth', 'thisWeek', 'today', and 'yesterday'.
|
|
38
|
+
*
|
|
39
|
+
* @param {Date} date - The reference date.
|
|
40
|
+
* @param {DateRangeType} type - The range type to compute.
|
|
41
|
+
* @param {object} [options] - Optional settings.
|
|
42
|
+
* @param {boolean} [options.setEndDateToNextDayStart] - If true, set `endDate` to 00:00:00.000 of the next day.
|
|
43
|
+
* @param {Day} [options.weekStartsOn] - The start day of the week (0 = Sunday, 1 = Monday, ..., 6 = Saturday).
|
|
44
|
+
* @returns {{ startDate: Date, endDate: Date }} An object with `startDate` and `endDate`.
|
|
45
|
+
*
|
|
46
|
+
* @example
|
|
47
|
+
* ```typescript
|
|
48
|
+
* import { getDateRangeFromDate } from '@kikiutils/shared/datetime';
|
|
49
|
+
*
|
|
50
|
+
* // Get the date range for last month
|
|
51
|
+
* const date = new Date('2023-07-01');
|
|
52
|
+
* console.log(getDateRangeFromDate(date, 'lastMonth'));
|
|
53
|
+
* // { startDate: 2023-06-01T00:00:00.000Z, endDate: 2023-06-30T23:59:59.999Z }
|
|
54
|
+
*
|
|
55
|
+
* // Get this week's range with Sunday as the first day
|
|
56
|
+
* console.log(getDateRangeFromDate(date, 'thisWeek', { weekStartsOn: 0 }));
|
|
57
|
+
* // { startDate: 2023-06-25T00:00:00.000Z, endDate: 2023-07-01T23:59:59.999Z }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
function getDateRangeFromDate(date, type, options) {
|
|
61
|
+
let endDate;
|
|
62
|
+
let startDate;
|
|
63
|
+
switch (type) {
|
|
64
|
+
case 'lastMonth':
|
|
65
|
+
{
|
|
66
|
+
const lastMonth = dateFns.subMonths(date, 1);
|
|
67
|
+
endDate = dateFns.endOfMonth(lastMonth);
|
|
68
|
+
startDate = dateFns.startOfMonth(lastMonth);
|
|
69
|
+
}
|
|
70
|
+
break;
|
|
71
|
+
case 'lastWeek':
|
|
72
|
+
{
|
|
73
|
+
const lastWeek = dateFns.subWeeks(date, 1);
|
|
74
|
+
endDate = dateFns.endOfWeek(lastWeek, { weekStartsOn: options?.weekStartsOn ?? 1 });
|
|
75
|
+
startDate = dateFns.startOfWeek(lastWeek, { weekStartsOn: options?.weekStartsOn ?? 1 });
|
|
76
|
+
}
|
|
77
|
+
break;
|
|
78
|
+
case 'thisMonth':
|
|
79
|
+
endDate = dateFns.endOfMonth(date);
|
|
80
|
+
startDate = dateFns.startOfMonth(date);
|
|
81
|
+
break;
|
|
82
|
+
case 'thisWeek':
|
|
83
|
+
endDate = dateFns.endOfWeek(date, { weekStartsOn: options?.weekStartsOn ?? 1 });
|
|
84
|
+
startDate = dateFns.startOfWeek(date, { weekStartsOn: options?.weekStartsOn ?? 1 });
|
|
85
|
+
break;
|
|
86
|
+
case 'today':
|
|
87
|
+
endDate = dateFns.endOfDay(date);
|
|
88
|
+
startDate = dateFns.startOfDay(date);
|
|
89
|
+
break;
|
|
90
|
+
case 'yesterday':
|
|
91
|
+
{
|
|
92
|
+
const yesterday = dateFns.subDays(date, 1);
|
|
93
|
+
endDate = dateFns.endOfDay(yesterday);
|
|
94
|
+
startDate = dateFns.startOfDay(yesterday);
|
|
95
|
+
}
|
|
96
|
+
break;
|
|
97
|
+
default: throw new Error(`Unsupported date range type: ${type}.`);
|
|
98
|
+
}
|
|
99
|
+
if (options?.setEndDateToNextDayStart)
|
|
100
|
+
endDate.setHours(24, 0, 0, 0);
|
|
101
|
+
return {
|
|
102
|
+
endDate,
|
|
103
|
+
startDate,
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Returns a `Date` object set to midnight (00:00:00) of today, with an optional day offset.
|
|
108
|
+
*
|
|
109
|
+
* @param {number} [offsetDays] - Number of days to offset from today. Can be negative.
|
|
110
|
+
* @returns {Date} A `Date` object at 00:00:00 of the offset day.
|
|
111
|
+
*
|
|
112
|
+
* @example
|
|
113
|
+
* ```typescript
|
|
114
|
+
* import { getMidnightDateFromToday } from '@kikiutils/shared/datetime';
|
|
115
|
+
*
|
|
116
|
+
* console.log(getMidnightDateFromToday()); // today at 00:00:00
|
|
117
|
+
* console.log(getMidnightDateFromToday(3)); // 3 days from today at 00:00:00
|
|
118
|
+
* console.log(getMidnightDateFromToday(-1)); // yesterday at 00:00:00
|
|
119
|
+
* ```
|
|
120
|
+
*/
|
|
121
|
+
function getMidnightDateFromToday(offsetDays = 0) {
|
|
122
|
+
const date = new Date();
|
|
123
|
+
date.setDate(date.getDate() + offsetDays);
|
|
124
|
+
date.setHours(0, 0, 0, 0);
|
|
125
|
+
return date;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
exports.formatDate = formatDate;
|
|
129
|
+
exports.getDateRangeFromDate = getDateRangeFromDate;
|
|
130
|
+
exports.getMidnightDateFromToday = getMidnightDateFromToday;
|
|
131
|
+
//# sourceMappingURL=datetime.cjs.map
|
|
@@ -0,0 +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;;;;;;"}
|