@sapphire/duration 1.1.0-pr-434.77ef153.0 β 1.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.
- package/CHANGELOG.md +31 -0
- package/README.md +11 -75
- package/dist/index.d.ts +46 -8
- package/dist/index.global.js +135 -53
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +135 -53
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +135 -53
- package/dist/index.mjs.map +1 -1
- package/package.json +8 -8
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,37 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
|
|
5
|
+
# [@sapphire/duration@1.1.0](https://github.com/sapphiredev/utilities/compare/@sapphire/duration@1.0.0...@sapphire/duration@1.1.0) - (2023-03-15)
|
|
6
|
+
|
|
7
|
+
## π Refactor
|
|
8
|
+
|
|
9
|
+
- **duration:** Update Time enum ([58a6177](https://github.com/sapphiredev/utilities/commit/58a61771ce9e10341a472799ced9ccbb0ec59c94))
|
|
10
|
+
- **duration:** Reuse Time enum (#492) ([c8445e0](https://github.com/sapphiredev/utilities/commit/c8445e0587756616ffabbec1434fcb40f66d19d2))
|
|
11
|
+
|
|
12
|
+
## π Bug Fixes
|
|
13
|
+
|
|
14
|
+
- **deps:** Update all non-major dependencies (#545) ([40ca040](https://github.com/sapphiredev/utilities/commit/40ca040a21d8a0949682051a3a974538183a400e))
|
|
15
|
+
- **deps:** Update all non-major dependencies (#544) ([cc78f17](https://github.com/sapphiredev/utilities/commit/cc78f17390c7f3db08af92bf46a5a70a9c11dd5f))
|
|
16
|
+
- **deps:** Update all non-major dependencies (#532) ([8033d1f](https://github.com/sapphiredev/utilities/commit/8033d1ff7a5a1974134c61f424f171cccb2915e1))
|
|
17
|
+
- **deps:** Update all non-major dependencies (#514) ([21b07d5](https://github.com/sapphiredev/utilities/commit/21b07d5db529a0d982647a60de98e46f36f1ac93))
|
|
18
|
+
- **deps:** Update all non-major dependencies (#505) ([6178296](https://github.com/sapphiredev/utilities/commit/617829649e1e4deeee02b14533b5377cd5bc1fb3))
|
|
19
|
+
- **deps:** Update all non-major dependencies (#466) ([dc08606](https://github.com/sapphiredev/utilities/commit/dc08606a97154e47c65536123ac5f8b1262f7bd2))
|
|
20
|
+
|
|
21
|
+
## π Documentation
|
|
22
|
+
|
|
23
|
+
- Add @06000208 as a contributor ([fa3349e](https://github.com/sapphiredev/utilities/commit/fa3349e55ce4ad008785211dec7bf8e2b5d933df))
|
|
24
|
+
- Add @didinele as a contributor ([42ef7b6](https://github.com/sapphiredev/utilities/commit/42ef7b656c48fd0e720119db1d622c8bba2791e9))
|
|
25
|
+
- Add @goestav as a contributor ([0e56a92](https://github.com/sapphiredev/utilities/commit/0e56a92a4e2d0942bfa207f81a8cb03b32312034))
|
|
26
|
+
- Add @CitTheDev as a contributor ([34169ea](https://github.com/sapphiredev/utilities/commit/34169eae1dc0476ccf5a6c4f36e28602a204829e))
|
|
27
|
+
- Add @legendhimslef as a contributor ([059b6f1](https://github.com/sapphiredev/utilities/commit/059b6f1ab5362d46d58624d06c1aa39192b0716f))
|
|
28
|
+
- **duration:** Add usage (#493) ([9386c7a](https://github.com/sapphiredev/utilities/commit/9386c7aed368ffe31330c70b99130e28e823bafa))
|
|
29
|
+
- Add @r-priyam as a contributor ([fb278ba](https://github.com/sapphiredev/utilities/commit/fb278bacf627ec6fc88752eafeb12df5f3177a2c))
|
|
30
|
+
|
|
31
|
+
## π Features
|
|
32
|
+
|
|
33
|
+
- **duration:** Add units on Duration (#561) ([eac39af](https://github.com/sapphiredev/utilities/commit/eac39af4c43c4f600234fbacb5efe8fb0cbb53b8))
|
|
34
|
+
- **utilities:** Add possibility to import single functions by appending them to the import path. (#454) ([374c145](https://github.com/sapphiredev/utilities/commit/374c145a5dd329cfc1a867ed6720abf408683a88))
|
|
35
|
+
|
|
5
36
|
# [@sapphire/duration@1.0.0](https://github.com/sapphiredev/utilities/tree/@sapphire/duration@1.0.0) - (2022-09-18)
|
|
6
37
|
|
|
7
38
|
## π Refactor
|
package/README.md
CHANGED
|
@@ -190,78 +190,14 @@ We accept donations through Open Collective, Ko-fi, PayPal, Patreon and GitHub S
|
|
|
190
190
|
| Patreon | [Click Here](https://sapphirejs.dev/patreon) |
|
|
191
191
|
| PayPal | [Click Here](https://sapphirejs.dev/paypal) |
|
|
192
192
|
|
|
193
|
-
## Contributors
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
<
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
<td align="center"><a href="https://github.com/PyroTechniac"><img src="https://avatars2.githubusercontent.com/u/39341355?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Gryffon Bellish</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=PyroTechniac" title="Code">π»</a> <a href="https://github.com/sapphiredev/utilities/pulls?q=is%3Apr+reviewed-by%3APyroTechniac" title="Reviewed Pull Requests">π</a> <a href="https://github.com/sapphiredev/utilities/commits?author=PyroTechniac" title="Tests">β οΈ</a></td>
|
|
205
|
-
<td align="center"><a href="https://github.com/vladfrangu"><img src="https://avatars3.githubusercontent.com/u/17960496?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vlad Frangu</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=vladfrangu" title="Code">π»</a> <a href="https://github.com/sapphiredev/utilities/issues?q=author%3Avladfrangu" title="Bug reports">π</a> <a href="https://github.com/sapphiredev/utilities/pulls?q=is%3Apr+reviewed-by%3Avladfrangu" title="Reviewed Pull Requests">π</a> <a href="#userTesting-vladfrangu" title="User Testing">π</a> <a href="https://github.com/sapphiredev/utilities/commits?author=vladfrangu" title="Tests">β οΈ</a></td>
|
|
206
|
-
<td align="center"><a href="https://github.com/Stitch07"><img src="https://avatars0.githubusercontent.com/u/29275227?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Stitch07</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Stitch07" title="Code">π»</a> <a href="#projectManagement-Stitch07" title="Project Management">π</a> <a href="https://github.com/sapphiredev/utilities/commits?author=Stitch07" title="Tests">β οΈ</a></td>
|
|
207
|
-
<td align="center"><a href="https://github.com/apps/depfu"><img src="https://avatars3.githubusercontent.com/in/715?v=4?s=100" width="100px;" alt=""/><br /><sub><b>depfu[bot]</b></sub></a><br /><a href="#maintenance-depfu[bot]" title="Maintenance">π§</a></td>
|
|
208
|
-
<td align="center"><a href="https://github.com/apps/allcontributors"><img src="https://avatars0.githubusercontent.com/in/23186?v=4?s=100" width="100px;" alt=""/><br /><sub><b>allcontributors[bot]</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=allcontributors[bot]" title="Documentation">π</a></td>
|
|
209
|
-
</tr>
|
|
210
|
-
<tr>
|
|
211
|
-
<td align="center"><a href="https://github.com/Nytelife26"><img src="https://avatars1.githubusercontent.com/u/22531310?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Tyler J Russell</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Nytelife26" title="Documentation">π</a></td>
|
|
212
|
-
<td align="center"><a href="https://github.com/Alcremie"><img src="https://avatars0.githubusercontent.com/u/54785334?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ivan Lieder</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Alcremie" title="Code">π»</a> <a href="https://github.com/sapphiredev/utilities/issues?q=author%3AAlcremie" title="Bug reports">π</a></td>
|
|
213
|
-
<td align="center"><a href="https://github.com/RealShadowNova"><img src="https://avatars3.githubusercontent.com/u/46537907?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Hezekiah Hendry</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=RealShadowNova" title="Code">π»</a> <a href="#tool-RealShadowNova" title="Tools">π§</a></td>
|
|
214
|
-
<td align="center"><a href="https://github.com/Vetlix"><img src="https://avatars.githubusercontent.com/u/31412314?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vetlix</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Vetlix" title="Code">π»</a></td>
|
|
215
|
-
<td align="center"><a href="https://github.com/ethamitc"><img src="https://avatars.githubusercontent.com/u/27776796?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Ethan Mitchell</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=ethamitc" title="Documentation">π</a></td>
|
|
216
|
-
<td align="center"><a href="https://github.com/noftaly"><img src="https://avatars.githubusercontent.com/u/34779161?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Elliot</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=noftaly" title="Code">π»</a></td>
|
|
217
|
-
<td align="center"><a href="https://jurien.dev"><img src="https://avatars.githubusercontent.com/u/5418114?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jurien Hamaker</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=jurienhamaker" title="Code">π»</a></td>
|
|
218
|
-
</tr>
|
|
219
|
-
<tr>
|
|
220
|
-
<td align="center"><a href="https://fanoulis.dev/"><img src="https://avatars.githubusercontent.com/u/38255093?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Charalampos Fanoulis</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=cfanoulis" title="Documentation">π</a></td>
|
|
221
|
-
<td align="center"><a href="https://github.com/apps/dependabot"><img src="https://avatars.githubusercontent.com/in/29110?v=4?s=100" width="100px;" alt=""/><br /><sub><b>dependabot[bot]</b></sub></a><br /><a href="#maintenance-dependabot[bot]" title="Maintenance">π§</a></td>
|
|
222
|
-
<td align="center"><a href="https://kaname.netlify.app/"><img src="https://avatars.githubusercontent.com/u/56084970?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Kaname</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=kaname-png" title="Code">π»</a></td>
|
|
223
|
-
<td align="center"><a href="https://github.com/nandhagk"><img src="https://avatars.githubusercontent.com/u/62976649?v=4?s=100" width="100px;" alt=""/><br /><sub><b>nandhagk</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/issues?q=author%3Anandhagk" title="Bug reports">π</a></td>
|
|
224
|
-
<td align="center"><a href="https://megatank58.me/"><img src="https://avatars.githubusercontent.com/u/51410502?v=4?s=100" width="100px;" alt=""/><br /><sub><b>megatank58</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=megatank58" title="Code">π»</a></td>
|
|
225
|
-
<td align="center"><a href="https://github.com/UndiedGamer"><img src="https://avatars.githubusercontent.com/u/84702365?v=4?s=100" width="100px;" alt=""/><br /><sub><b>UndiedGamer</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=UndiedGamer" title="Code">π»</a></td>
|
|
226
|
-
<td align="center"><a href="https://github.com/Lioness100"><img src="https://avatars.githubusercontent.com/u/65814829?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Lioness100</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Lioness100" title="Documentation">π</a> <a href="https://github.com/sapphiredev/utilities/commits?author=Lioness100" title="Code">π»</a></td>
|
|
227
|
-
</tr>
|
|
228
|
-
<tr>
|
|
229
|
-
<td align="center"><a href="https://gitlab.com/DavidPH/"><img src="https://avatars.githubusercontent.com/u/44669930?v=4?s=100" width="100px;" alt=""/><br /><sub><b>David</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=DavidPHH" title="Code">π»</a></td>
|
|
230
|
-
<td align="center"><a href="https://github.com/apps/renovate"><img src="https://avatars.githubusercontent.com/in/2740?v=4?s=100" width="100px;" alt=""/><br /><sub><b>renovate[bot]</b></sub></a><br /><a href="#maintenance-renovate[bot]" title="Maintenance">π§</a></td>
|
|
231
|
-
<td align="center"><a href="https://renovate.whitesourcesoftware.com/"><img src="https://avatars.githubusercontent.com/u/25180681?v=4?s=100" width="100px;" alt=""/><br /><sub><b>WhiteSource Renovate</b></sub></a><br /><a href="#maintenance-renovate-bot" title="Maintenance">π§</a></td>
|
|
232
|
-
<td align="center"><a href="https://fc5570.me/"><img src="https://avatars.githubusercontent.com/u/68158483?v=4?s=100" width="100px;" alt=""/><br /><sub><b>FC</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=FC5570" title="Code">π»</a></td>
|
|
233
|
-
<td align="center"><a href="https://github.com/Tokipudi"><img src="https://avatars.githubusercontent.com/u/29551076?v=4?s=100" width="100px;" alt=""/><br /><sub><b>JΓ©rΓ©my de Saint Denis</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Tokipudi" title="Code">π»</a></td>
|
|
234
|
-
<td align="center"><a href="https://github.com/ItsMrCube"><img src="https://avatars.githubusercontent.com/u/25201357?v=4?s=100" width="100px;" alt=""/><br /><sub><b>MrCube</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=ItsMrCube" title="Code">π»</a></td>
|
|
235
|
-
<td align="center"><a href="https://github.com/bitomic"><img src="https://avatars.githubusercontent.com/u/35199700?v=4?s=100" width="100px;" alt=""/><br /><sub><b>bitomic</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=bitomic" title="Code">π»</a></td>
|
|
236
|
-
</tr>
|
|
237
|
-
<tr>
|
|
238
|
-
<td align="center"><a href="https://c43721.dev/"><img src="https://avatars.githubusercontent.com/u/55610086?v=4?s=100" width="100px;" alt=""/><br /><sub><b>c43721</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=c43721" title="Code">π»</a></td>
|
|
239
|
-
<td align="center"><a href="https://commandtechno.com/"><img src="https://avatars.githubusercontent.com/u/68407783?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Commandtechno</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Commandtechno" title="Code">π»</a></td>
|
|
240
|
-
<td align="center"><a href="https://github.com/dhruv-kaushikk"><img src="https://avatars.githubusercontent.com/u/73697546?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Aura</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=dhruv-kaushikk" title="Code">π»</a></td>
|
|
241
|
-
<td align="center"><a href="https://axis.moe/"><img src="https://avatars.githubusercontent.com/u/54381371?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Jonathan</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=axisiscool" title="Code">π»</a></td>
|
|
242
|
-
<td align="center"><a href="https://github.com/imranbarbhuiya"><img src="https://avatars.githubusercontent.com/u/74945038?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Parbez</b></sub></a><br /><a href="#maintenance-imranbarbhuiya" title="Maintenance">π§</a></td>
|
|
243
|
-
<td align="center"><a href="https://github.com/NotKaskus"><img src="https://avatars.githubusercontent.com/u/75168528?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Paul Andrew</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=NotKaskus" title="Documentation">π</a></td>
|
|
244
|
-
<td align="center"><a href="https://linktr.ee/mzato0001"><img src="https://avatars.githubusercontent.com/u/62367547?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Mzato</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=Mzato0001" title="Code">π»</a> <a href="https://github.com/sapphiredev/utilities/issues?q=author%3AMzato0001" title="Bug reports">π</a></td>
|
|
245
|
-
</tr>
|
|
246
|
-
<tr>
|
|
247
|
-
<td align="center"><a href="https://github.com/MajesticString"><img src="https://avatars.githubusercontent.com/u/66224939?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Harry Allen</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=MajesticString" title="Documentation">π</a></td>
|
|
248
|
-
<td align="center"><a href="https://github.com/EvolutionX-10"><img src="https://avatars.githubusercontent.com/u/85353424?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Evo</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=EvolutionX-10" title="Code">π»</a></td>
|
|
249
|
-
<td align="center"><a href="https://enes.ovh/"><img src="https://avatars.githubusercontent.com/u/61084101?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Enes GenΓ§</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=enxg" title="Code">π»</a></td>
|
|
250
|
-
<td align="center"><a href="https://github.com/muchnameless"><img src="https://avatars.githubusercontent.com/u/12682826?v=4?s=100" width="100px;" alt=""/><br /><sub><b>muchnameless</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=muchnameless" title="Code">π»</a></td>
|
|
251
|
-
<td align="center"><a href="https://github.com/r-priyam"><img src="https://avatars.githubusercontent.com/u/50884372?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Priyam</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=r-priyam" title="Code">π»</a></td>
|
|
252
|
-
<td align="center"><a href="https://github.com/legendhimslef"><img src="https://avatars.githubusercontent.com/u/69213593?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Voxelli</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=legendhimslef" title="Code">π»</a></td>
|
|
253
|
-
<td align="center"><a href="https://github.com/CitTheDev"><img src="https://avatars.githubusercontent.com/u/94020875?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Cit The Dev</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=CitTheDev" title="Code">π»</a></td>
|
|
254
|
-
</tr>
|
|
255
|
-
<tr>
|
|
256
|
-
<td align="center"><a href="https://www.goestav.com/"><img src="https://avatars.githubusercontent.com/u/27970303?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Goestav</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=goestav" title="Code">π»</a></td>
|
|
257
|
-
<td align="center"><a href="https://github.com/didinele"><img src="https://avatars.githubusercontent.com/u/27137376?v=4?s=100" width="100px;" alt=""/><br /><sub><b>DD</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=didinele" title="Code">π»</a></td>
|
|
258
|
-
<td align="center"><a href="https://steamcommunity.com/id/06000208"><img src="https://avatars.githubusercontent.com/u/52764066?v=4?s=100" width="100px;" alt=""/><br /><sub><b>amber</b></sub></a><br /><a href="https://github.com/sapphiredev/utilities/commits?author=06000208" title="Code">π»</a></td>
|
|
259
|
-
</tr>
|
|
260
|
-
</table>
|
|
261
|
-
|
|
262
|
-
<!-- markdownlint-restore -->
|
|
263
|
-
<!-- prettier-ignore-end -->
|
|
264
|
-
|
|
265
|
-
<!-- ALL-CONTRIBUTORS-LIST:END -->
|
|
266
|
-
|
|
267
|
-
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind welcome!
|
|
193
|
+
## Contributors
|
|
194
|
+
|
|
195
|
+
Please make sure to read the [Contributing Guide][contributing] before making a pull request.
|
|
196
|
+
|
|
197
|
+
Thank you to all the people who already contributed to Sapphire!
|
|
198
|
+
|
|
199
|
+
<a href="https://github.com/sapphiredev/utilities/graphs/contributors">
|
|
200
|
+
<img src="https://contrib.rocks/image?repo=sapphiredev/utilities" />
|
|
201
|
+
</a>
|
|
202
|
+
|
|
203
|
+
[contributing]: https://github.com/sapphiredev/.github/blob/main/.github/CONTRIBUTING.md
|
package/dist/index.d.ts
CHANGED
|
@@ -26,11 +26,14 @@ interface DurationFormatAssetsTime {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
declare enum Time {
|
|
29
|
+
Nanosecond = 0.000001,
|
|
30
|
+
Microsecond = 0.001,
|
|
29
31
|
Millisecond = 1,
|
|
30
32
|
Second = 1000,
|
|
31
33
|
Minute = 60000,
|
|
32
34
|
Hour = 3600000,
|
|
33
35
|
Day = 86400000,
|
|
36
|
+
Week = 604800000,
|
|
34
37
|
Month = 2628000000,
|
|
35
38
|
Year = 31536000000
|
|
36
39
|
}
|
|
@@ -57,6 +60,46 @@ declare class Duration {
|
|
|
57
60
|
* The offset
|
|
58
61
|
*/
|
|
59
62
|
offset: number;
|
|
63
|
+
/**
|
|
64
|
+
* The amount of nanoseconds extracted from the text.
|
|
65
|
+
*/
|
|
66
|
+
nanoseconds: number;
|
|
67
|
+
/**
|
|
68
|
+
* The amount of microseconds extracted from the text.
|
|
69
|
+
*/
|
|
70
|
+
microseconds: number;
|
|
71
|
+
/**
|
|
72
|
+
* The amount of milliseconds extracted from the text.
|
|
73
|
+
*/
|
|
74
|
+
milliseconds: number;
|
|
75
|
+
/**
|
|
76
|
+
* The amount of seconds extracted from the text.
|
|
77
|
+
*/
|
|
78
|
+
seconds: number;
|
|
79
|
+
/**
|
|
80
|
+
* The amount of minutes extracted from the text.
|
|
81
|
+
*/
|
|
82
|
+
minutes: number;
|
|
83
|
+
/**
|
|
84
|
+
* The amount of hours extracted from the text.
|
|
85
|
+
*/
|
|
86
|
+
hours: number;
|
|
87
|
+
/**
|
|
88
|
+
* The amount of days extracted from the text.
|
|
89
|
+
*/
|
|
90
|
+
days: number;
|
|
91
|
+
/**
|
|
92
|
+
* The amount of weeks extracted from the text.
|
|
93
|
+
*/
|
|
94
|
+
weeks: number;
|
|
95
|
+
/**
|
|
96
|
+
* The amount of months extracted from the text.
|
|
97
|
+
*/
|
|
98
|
+
months: number;
|
|
99
|
+
/**
|
|
100
|
+
* The amount of years extracted from the text.
|
|
101
|
+
*/
|
|
102
|
+
years: number;
|
|
60
103
|
/**
|
|
61
104
|
* Create a new Duration instance
|
|
62
105
|
* @param pattern The string to parse
|
|
@@ -74,20 +117,15 @@ declare class Duration {
|
|
|
74
117
|
/**
|
|
75
118
|
* The RegExp used for the pattern parsing
|
|
76
119
|
*/
|
|
77
|
-
private static readonly
|
|
120
|
+
private static readonly patternRegex;
|
|
78
121
|
/**
|
|
79
122
|
* The RegExp used for removing commas
|
|
80
123
|
*/
|
|
81
|
-
private static readonly
|
|
124
|
+
private static readonly commaRegex;
|
|
82
125
|
/**
|
|
83
126
|
* The RegExp used for replacing a/an with 1
|
|
84
127
|
*/
|
|
85
|
-
private static readonly
|
|
86
|
-
/**
|
|
87
|
-
* Parse the pattern
|
|
88
|
-
* @param pattern The pattern to parse
|
|
89
|
-
*/
|
|
90
|
-
private static parse;
|
|
128
|
+
private static readonly aAndAnRegex;
|
|
91
129
|
}
|
|
92
130
|
|
|
93
131
|
export { DEFAULT_SEPARATORS, DEFAULT_UNITS, Duration, DurationFormatAssetsTime, DurationFormatAssetsUnit, DurationFormatSeparators, DurationFormatter, Time, TimeTypes };
|
package/dist/index.global.js
CHANGED
|
@@ -11,11 +11,14 @@ var SapphireDuration = (function (exports) {
|
|
|
11
11
|
|
|
12
12
|
// src/lib/constants.ts
|
|
13
13
|
var Time = /* @__PURE__ */ ((Time2) => {
|
|
14
|
+
Time2[Time2["Nanosecond"] = 1e-6] = "Nanosecond";
|
|
15
|
+
Time2[Time2["Microsecond"] = 1e-3] = "Microsecond";
|
|
14
16
|
Time2[Time2["Millisecond"] = 1] = "Millisecond";
|
|
15
17
|
Time2[Time2["Second"] = 1e3] = "Second";
|
|
16
18
|
Time2[Time2["Minute"] = 6e4] = "Minute";
|
|
17
19
|
Time2[Time2["Hour"] = 36e5] = "Hour";
|
|
18
20
|
Time2[Time2["Day"] = 864e5] = "Day";
|
|
21
|
+
Time2[Time2["Week"] = 6048e5] = "Week";
|
|
19
22
|
Time2[Time2["Month"] = 2628e6] = "Month";
|
|
20
23
|
Time2[Time2["Year"] = 31536e6] = "Year";
|
|
21
24
|
return Time2;
|
|
@@ -67,79 +70,158 @@ var SapphireDuration = (function (exports) {
|
|
|
67
70
|
|
|
68
71
|
// src/lib/Duration.ts
|
|
69
72
|
var tokens = /* @__PURE__ */ new Map([
|
|
70
|
-
["nanosecond",
|
|
71
|
-
["nanoseconds",
|
|
72
|
-
["ns",
|
|
73
|
-
["
|
|
74
|
-
["
|
|
75
|
-
["
|
|
76
|
-
["
|
|
77
|
-
["
|
|
78
|
-
["
|
|
79
|
-
["
|
|
80
|
-
["
|
|
81
|
-
["
|
|
82
|
-
["
|
|
83
|
-
["
|
|
84
|
-
["
|
|
85
|
-
["
|
|
86
|
-
["
|
|
87
|
-
["
|
|
88
|
-
["
|
|
89
|
-
["
|
|
90
|
-
["
|
|
91
|
-
["
|
|
92
|
-
["
|
|
93
|
-
["
|
|
94
|
-
["
|
|
95
|
-
["
|
|
96
|
-
["
|
|
97
|
-
["
|
|
98
|
-
["
|
|
99
|
-
["
|
|
100
|
-
["
|
|
101
|
-
["
|
|
102
|
-
["
|
|
103
|
-
["
|
|
104
|
-
["
|
|
105
|
-
["
|
|
106
|
-
["
|
|
107
|
-
["
|
|
73
|
+
["nanosecond", 1e-6 /* Nanosecond */],
|
|
74
|
+
["nanoseconds", 1e-6 /* Nanosecond */],
|
|
75
|
+
["ns", 1e-6 /* Nanosecond */],
|
|
76
|
+
["microsecond", 1e-3 /* Microsecond */],
|
|
77
|
+
["microseconds", 1e-3 /* Microsecond */],
|
|
78
|
+
["\u03BCs", 1e-3 /* Microsecond */],
|
|
79
|
+
["us", 1e-3 /* Microsecond */],
|
|
80
|
+
["millisecond", 1 /* Millisecond */],
|
|
81
|
+
["milliseconds", 1 /* Millisecond */],
|
|
82
|
+
["ms", 1 /* Millisecond */],
|
|
83
|
+
["second", 1e3 /* Second */],
|
|
84
|
+
["seconds", 1e3 /* Second */],
|
|
85
|
+
["sec", 1e3 /* Second */],
|
|
86
|
+
["secs", 1e3 /* Second */],
|
|
87
|
+
["s", 1e3 /* Second */],
|
|
88
|
+
["minute", 6e4 /* Minute */],
|
|
89
|
+
["minutes", 6e4 /* Minute */],
|
|
90
|
+
["min", 6e4 /* Minute */],
|
|
91
|
+
["mins", 6e4 /* Minute */],
|
|
92
|
+
["m", 6e4 /* Minute */],
|
|
93
|
+
["hour", 36e5 /* Hour */],
|
|
94
|
+
["hours", 36e5 /* Hour */],
|
|
95
|
+
["hr", 36e5 /* Hour */],
|
|
96
|
+
["hrs", 36e5 /* Hour */],
|
|
97
|
+
["h", 36e5 /* Hour */],
|
|
98
|
+
["day", 864e5 /* Day */],
|
|
99
|
+
["days", 864e5 /* Day */],
|
|
100
|
+
["d", 864e5 /* Day */],
|
|
101
|
+
["week", 6048e5 /* Week */],
|
|
102
|
+
["weeks", 6048e5 /* Week */],
|
|
103
|
+
["wk", 6048e5 /* Week */],
|
|
104
|
+
["wks", 6048e5 /* Week */],
|
|
105
|
+
["w", 6048e5 /* Week */],
|
|
106
|
+
["month", 2628e6 /* Month */],
|
|
107
|
+
["months", 2628e6 /* Month */],
|
|
108
|
+
["b", 2628e6 /* Month */],
|
|
109
|
+
["mo", 2628e6 /* Month */],
|
|
110
|
+
["year", 31536e6 /* Year */],
|
|
111
|
+
["years", 31536e6 /* Year */],
|
|
112
|
+
["yr", 31536e6 /* Year */],
|
|
113
|
+
["yrs", 31536e6 /* Year */],
|
|
114
|
+
["y", 31536e6 /* Year */]
|
|
115
|
+
]);
|
|
116
|
+
var mappings = /* @__PURE__ */ new Map([
|
|
117
|
+
[1e-6 /* Nanosecond */, "nanoseconds"],
|
|
118
|
+
[1e-3 /* Microsecond */, "microseconds"],
|
|
119
|
+
[1 /* Millisecond */, "milliseconds"],
|
|
120
|
+
[1e3 /* Second */, "seconds"],
|
|
121
|
+
[6e4 /* Minute */, "minutes"],
|
|
122
|
+
[36e5 /* Hour */, "hours"],
|
|
123
|
+
[864e5 /* Day */, "days"],
|
|
124
|
+
[6048e5 /* Week */, "weeks"],
|
|
125
|
+
[2628e6 /* Month */, "months"],
|
|
126
|
+
[31536e6 /* Year */, "years"]
|
|
108
127
|
]);
|
|
109
128
|
var _Duration = class {
|
|
129
|
+
/**
|
|
130
|
+
* Create a new Duration instance
|
|
131
|
+
* @param pattern The string to parse
|
|
132
|
+
*/
|
|
110
133
|
constructor(pattern) {
|
|
134
|
+
/**
|
|
135
|
+
* The offset
|
|
136
|
+
*/
|
|
111
137
|
__publicField(this, "offset");
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
138
|
+
/**
|
|
139
|
+
* The amount of nanoseconds extracted from the text.
|
|
140
|
+
*/
|
|
141
|
+
__publicField(this, "nanoseconds", 0);
|
|
142
|
+
/**
|
|
143
|
+
* The amount of microseconds extracted from the text.
|
|
144
|
+
*/
|
|
145
|
+
__publicField(this, "microseconds", 0);
|
|
146
|
+
/**
|
|
147
|
+
* The amount of milliseconds extracted from the text.
|
|
148
|
+
*/
|
|
149
|
+
__publicField(this, "milliseconds", 0);
|
|
150
|
+
/**
|
|
151
|
+
* The amount of seconds extracted from the text.
|
|
152
|
+
*/
|
|
153
|
+
__publicField(this, "seconds", 0);
|
|
154
|
+
/**
|
|
155
|
+
* The amount of minutes extracted from the text.
|
|
156
|
+
*/
|
|
157
|
+
__publicField(this, "minutes", 0);
|
|
158
|
+
/**
|
|
159
|
+
* The amount of hours extracted from the text.
|
|
160
|
+
*/
|
|
161
|
+
__publicField(this, "hours", 0);
|
|
162
|
+
/**
|
|
163
|
+
* The amount of days extracted from the text.
|
|
164
|
+
*/
|
|
165
|
+
__publicField(this, "days", 0);
|
|
166
|
+
/**
|
|
167
|
+
* The amount of weeks extracted from the text.
|
|
168
|
+
*/
|
|
169
|
+
__publicField(this, "weeks", 0);
|
|
170
|
+
/**
|
|
171
|
+
* The amount of months extracted from the text.
|
|
172
|
+
*/
|
|
173
|
+
__publicField(this, "months", 0);
|
|
174
|
+
/**
|
|
175
|
+
* The amount of years extracted from the text.
|
|
176
|
+
*/
|
|
177
|
+
__publicField(this, "years", 0);
|
|
121
178
|
let result = 0;
|
|
122
179
|
let valid = false;
|
|
123
|
-
pattern.replace(_Duration.
|
|
180
|
+
pattern.toLowerCase().replace(_Duration.commaRegex, "").replace(_Duration.aAndAnRegex, "1").replace(_Duration.patternRegex, (_, i, units) => {
|
|
124
181
|
const token = tokens.get(units);
|
|
125
182
|
if (token !== void 0) {
|
|
126
|
-
|
|
183
|
+
const n = Number(i);
|
|
184
|
+
result += n * token;
|
|
185
|
+
this[mappings.get(token)] += n;
|
|
127
186
|
valid = true;
|
|
128
187
|
}
|
|
129
188
|
return "";
|
|
130
189
|
});
|
|
131
|
-
|
|
190
|
+
this.offset = valid ? result : NaN;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Get the date from now
|
|
194
|
+
*/
|
|
195
|
+
get fromNow() {
|
|
196
|
+
return this.dateFrom(/* @__PURE__ */ new Date());
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Get the date from
|
|
200
|
+
* @param date The Date instance to get the date from
|
|
201
|
+
*/
|
|
202
|
+
dateFrom(date) {
|
|
203
|
+
return new Date(date.getTime() + this.offset);
|
|
132
204
|
}
|
|
133
205
|
};
|
|
134
206
|
var Duration = _Duration;
|
|
135
207
|
__name(Duration, "Duration");
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
208
|
+
/**
|
|
209
|
+
* The RegExp used for the pattern parsing
|
|
210
|
+
*/
|
|
211
|
+
__publicField(Duration, "patternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zΞΌ]*)/gi);
|
|
212
|
+
/**
|
|
213
|
+
* The RegExp used for removing commas
|
|
214
|
+
*/
|
|
215
|
+
__publicField(Duration, "commaRegex", /,/g);
|
|
216
|
+
/**
|
|
217
|
+
* The RegExp used for replacing a/an with 1
|
|
218
|
+
*/
|
|
219
|
+
__publicField(Duration, "aAndAnRegex", /\ban?\b/gi);
|
|
139
220
|
|
|
140
221
|
// src/lib/DurationFormatter.ts
|
|
141
222
|
var kTimeDurations = [
|
|
142
223
|
["year" /* Year */, 31536e6],
|
|
224
|
+
// 29.53059 days is the official duration of a month: https://en.wikipedia.org/wiki/Month
|
|
143
225
|
["month" /* Month */, 2628e6],
|
|
144
226
|
["week" /* Week */, 1e3 * 60 * 60 * 24 * 7],
|
|
145
227
|
["day" /* Day */, 1e3 * 60 * 60 * 24],
|
package/dist/index.global.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/lib/constants.ts","../src/lib/Duration.ts","../src/lib/DurationFormatter.ts"],"names":["Time","TimeTypes"],"mappings":";;;;;;;;;AAEO,IAAK,OAAL,kBAAKA,UAAL;AACN,EAAAA,YAAA,iBAAc,KAAd;AACA,EAAAA,YAAA,YAAS,OAAT;AACA,EAAAA,YAAA,YAAS,OAAT;AACA,EAAAA,YAAA,UAAO,QAAP;AACA,EAAAA,YAAA,SAAM,SAAN;AACA,EAAAA,YAAA,WAAQ,UAAR;AACA,EAAAA,YAAA,UAAO,WAAP;AAPW,SAAAA;AAAA,GAAA;AAaL,IAAK,YAAL,kBAAKC,eAAL;AACN,EAAAA,WAAA,YAAS;AACT,EAAAA,WAAA,YAAS;AACT,EAAAA,WAAA,UAAO;AACP,EAAAA,WAAA,SAAM;AACN,EAAAA,WAAA,UAAO;AACP,EAAAA,WAAA,WAAQ;AACR,EAAAA,WAAA,UAAO;AAPI,SAAAA;AAAA,GAAA;AAUL,IAAM,gBAA0C;AAAA,EACtD,CAAC,oBAAiB;AAAA,IACjB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,sBAAkB;AAAA,IAClB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,oBAAiB;AAAA,IACjB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,kBAAgB;AAAA,IAChB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,oBAAiB;AAAA,IACjB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,wBAAmB;AAAA,IACnB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,wBAAmB;AAAA,IACnB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AACD;AAEO,IAAM,qBAA+C;AAAA,EAC3D,MAAM;AAAA,EACN,OAAO;AACR;;;AC3DA,IAAM,SAAS,oBAAI,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econd = 1,\n\tSecond = 1000,\n\tMinute = 1000 * 60,\n\tHour = 1000 * 60 * 60,\n\tDay = 1000 * 60 * 60 * 24,\n\tMonth = 1000 * 60 * 60 * 24 * (365 / 12),\n\tYear = 1000 * 60 * 60 * 24 * 365\n}\n\n/**\n * The supported time types\n */\nexport enum TimeTypes {\n\tSecond = 'second',\n\tMinute = 'minute',\n\tHour = 'hour',\n\tDay = 'day',\n\tWeek = 'week',\n\tMonth = 'month',\n\tYear = 'year'\n}\n\nexport const DEFAULT_UNITS: DurationFormatAssetsTime = {\n\t[TimeTypes.Year]: {\n\t\t1: 'year',\n\t\tDEFAULT: 'years'\n\t},\n\t[TimeTypes.Month]: {\n\t\t1: 'month',\n\t\tDEFAULT: 'months'\n\t},\n\t[TimeTypes.Week]: {\n\t\t1: 'week',\n\t\tDEFAULT: 'weeks'\n\t},\n\t[TimeTypes.Day]: {\n\t\t1: 'day',\n\t\tDEFAULT: 'days'\n\t},\n\t[TimeTypes.Hour]: {\n\t\t1: 'hour',\n\t\tDEFAULT: 'hours'\n\t},\n\t[TimeTypes.Minute]: {\n\t\t1: 'minute',\n\t\tDEFAULT: 'minutes'\n\t},\n\t[TimeTypes.Second]: {\n\t\t1: 'second',\n\t\tDEFAULT: 'seconds'\n\t}\n};\n\nexport const DEFAULT_SEPARATORS: DurationFormatSeparators = {\n\tleft: ' ',\n\tright: ' '\n};\n","const tokens = new Map([\n\t['nanosecond', 1 / 1e6],\n\t['nanoseconds', 1 / 1e6],\n\t['ns', 1 / 1e6],\n\n\t['millisecond', 1],\n\t['milliseconds', 1],\n\t['ms', 1],\n\n\t['second', 1000],\n\t['seconds', 1000],\n\t['sec', 1000],\n\t['secs', 1000],\n\t['s', 1000],\n\n\t['minute', 1000 * 60],\n\t['minutes', 1000 * 60],\n\t['min', 1000 * 60],\n\t['mins', 1000 * 60],\n\t['m', 1000 * 60],\n\n\t['hour', 1000 * 60 * 60],\n\t['hours', 1000 * 60 * 60],\n\t['hr', 1000 * 60 * 60],\n\t['hrs', 1000 * 60 * 60],\n\t['h', 1000 * 60 * 60],\n\n\t['day', 1000 * 60 * 60 * 24],\n\t['days', 1000 * 60 * 60 * 24],\n\t['d', 1000 * 60 * 60 * 24],\n\n\t['week', 1000 * 60 * 60 * 24 * 7],\n\t['weeks', 1000 * 60 * 60 * 24 * 7],\n\t['wk', 1000 * 60 * 60 * 24 * 7],\n\t['wks', 1000 * 60 * 60 * 24 * 7],\n\t['w', 1000 * 60 * 60 * 24 * 7],\n\n\t['month', 1000 * 60 * 60 * 24 * (365.25 / 12)],\n\t['months', 1000 * 60 * 60 * 24 * (365.25 / 12)],\n\t['b', 1000 * 60 * 60 * 24 * (365.25 / 12)],\n\t['mo', 1000 * 60 * 60 * 24 * (365.25 / 12)],\n\n\t['year', 1000 * 60 * 60 * 24 * 365.25],\n\t['years', 1000 * 60 * 60 * 24 * 365.25],\n\t['yr', 1000 * 60 * 60 * 24 * 365.25],\n\t['yrs', 1000 * 60 * 60 * 24 * 365.25],\n\t['y', 1000 * 60 * 60 * 24 * 365.25]\n]);\n\n/**\n * Converts duration strings into ms and future dates\n */\nexport class Duration {\n\t/**\n\t * The offset\n\t */\n\tpublic offset: number;\n\n\t/**\n\t * Create a new Duration instance\n\t * @param pattern The string to parse\n\t */\n\tpublic constructor(pattern: string) {\n\t\tthis.offset = Duration.parse(pattern.toLowerCase());\n\t}\n\n\t/**\n\t * Get the date from now\n\t */\n\tpublic get fromNow(): Date {\n\t\treturn this.dateFrom(new Date());\n\t}\n\n\t/**\n\t * Get the date from\n\t * @param date The Date instance to get the date from\n\t */\n\tpublic dateFrom(date: Date): Date {\n\t\treturn new Date(date.getTime() + this.offset);\n\t}\n\n\t/**\n\t * The RegExp used for the pattern parsing\n\t */\n\tprivate static readonly kPatternRegex = /(-?\\d*\\.?\\d+(?:e[-+]?\\d+)?)\\s*([a-zΞΌ]*)/gi;\n\n\t/**\n\t * The RegExp used for removing commas\n\t */\n\tprivate static readonly kCommaRegex = /,/g;\n\n\t/**\n\t * The RegExp used for replacing a/an with 1\n\t */\n\tprivate static readonly kAanRegex = /\\ban?\\b/gi;\n\n\t/**\n\t * Parse the pattern\n\t * @param pattern The pattern to parse\n\t */\n\tprivate static parse(pattern: string): number {\n\t\tlet result = 0;\n\t\tlet valid = false;\n\n\t\tpattern\n\t\t\t// ignore commas\n\t\t\t.replace(Duration.kCommaRegex, '')\n\t\t\t// a / an = 1\n\t\t\t.replace(Duration.kAanRegex, '1')\n\t\t\t// do math\n\t\t\t.replace(Duration.kPatternRegex, (_, i, units) => {\n\t\t\t\tconst token = tokens.get(units);\n\t\t\t\tif (token !== undefined) {\n\t\t\t\t\tresult += Number(i) * token;\n\t\t\t\t\tvalid = true;\n\t\t\t\t}\n\t\t\t\treturn '';\n\t\t\t});\n\n\t\treturn valid ? result : NaN;\n\t}\n}\n","import { DEFAULT_SEPARATORS, DEFAULT_UNITS, TimeTypes } from './constants';\n\n/**\n * The duration of each time type in milliseconds\n */\nconst kTimeDurations: readonly [TimeTypes, number][] = [\n\t[TimeTypes.Year, 31536000000],\n\t// 29.53059 days is the official duration of a month: https://en.wikipedia.org/wiki/Month\n\t[TimeTypes.Month, 2628000000],\n\t[TimeTypes.Week, 1000 * 60 * 60 * 24 * 7],\n\t[TimeTypes.Day, 1000 * 60 * 60 * 24],\n\t[TimeTypes.Hour, 1000 * 60 * 60],\n\t[TimeTypes.Minute, 1000 * 60],\n\t[TimeTypes.Second, 1000]\n];\n\n/**\n * Display the duration\n * @param duration The duration in milliseconds to parse and display\n * @param assets The language assets\n */\nexport class DurationFormatter {\n\tpublic constructor(public units: DurationFormatAssetsTime = DEFAULT_UNITS) {}\n\n\tpublic format(\n\t\tduration: number,\n\t\tprecision = 7,\n\t\t{\n\t\t\tleft: leftSeparator = DEFAULT_SEPARATORS.left,\n\t\t\tright: rightSeparator = DEFAULT_SEPARATORS.right\n\t\t}: DurationFormatSeparators = DEFAULT_SEPARATORS\n\t) {\n\t\tconst output: string[] = [];\n\t\tconst negative = duration < 0;\n\t\tif (negative) duration *= -1;\n\n\t\tfor (const [type, timeDuration] of kTimeDurations) {\n\t\t\tconst division = duration / timeDuration;\n\t\t\tif (division < 1) continue;\n\n\t\t\tconst floored = Math.floor(division);\n\t\t\tduration -= floored * timeDuration;\n\t\t\toutput.push(addUnit(floored, this.units[type], leftSeparator!));\n\n\t\t\t// If the output has enough precision, break\n\t\t\tif (output.length >= precision) break;\n\t\t}\n\n\t\treturn `${negative ? '-' : ''}${output.join(rightSeparator) || addUnit(0, this.units.second, leftSeparator!)}`;\n\t}\n}\n\n/**\n * Adds an unit, if non zero\n * @param time The duration of said unit\n * @param unit The unit language assets\n */\nfunction addUnit(time: number, unit: DurationFormatAssetsUnit, separator: string) {\n\tif (Reflect.has(unit, time)) return `${time}${separator}${Reflect.get(unit, time)}`;\n\treturn `${time}${separator}${unit.DEFAULT}`;\n}\n\nexport interface DurationFormatSeparators {\n\tleft?: string;\n\tright?: string;\n}\n\nexport interface DurationFormatAssetsUnit extends Record<number, string> {\n\tDEFAULT: string;\n}\n\nexport interface DurationFormatAssetsTime {\n\t[TimeTypes.Second]: DurationFormatAssetsUnit;\n\t[TimeTypes.Minute]: DurationFormatAssetsUnit;\n\t[TimeTypes.Hour]: DurationFormatAssetsUnit;\n\t[TimeTypes.Day]: DurationFormatAssetsUnit;\n\t[TimeTypes.Week]: DurationFormatAssetsUnit;\n\t[TimeTypes.Month]: DurationFormatAssetsUnit;\n\t[TimeTypes.Year]: DurationFormatAssetsUnit;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/lib/constants.ts","../src/lib/Duration.ts","../src/lib/DurationFormatter.ts"],"names":["Time","TimeTypes"],"mappings":";;;;;;;;;AAEO,IAAK,OAAL,kBAAKA,UAAL;AACN,EAAAA,YAAA,gBAAa,QAAb;AACA,EAAAA,YAAA,iBAAc,QAAd;AACA,EAAAA,YAAA,iBAAc,KAAd;AACA,EAAAA,YAAA,YAAS,OAAT;AACA,EAAAA,YAAA,YA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;AACnC;AAHS","sourcesContent":["import type { DurationFormatAssetsTime, DurationFormatSeparators } from './DurationFormatter';\n\nexport enum Time {\n\tNanosecond = 1 / 1_000_000,\n\tMicrosecond = 1 / 1000,\n\tMillisecond = 1,\n\tSecond = 1000,\n\tMinute = Second * 60,\n\tHour = Minute * 60,\n\tDay = Hour * 24,\n\tWeek = Day * 7,\n\tMonth = Day * (365 / 12),\n\tYear = Day * 365\n}\n\n/**\n * The supported time types\n */\nexport enum TimeTypes {\n\tSecond = 'second',\n\tMinute = 'minute',\n\tHour = 'hour',\n\tDay = 'day',\n\tWeek = 'week',\n\tMonth = 'month',\n\tYear = 'year'\n}\n\nexport const DEFAULT_UNITS: DurationFormatAssetsTime = {\n\t[TimeTypes.Year]: {\n\t\t1: 'year',\n\t\tDEFAULT: 'years'\n\t},\n\t[TimeTypes.Month]: {\n\t\t1: 'month',\n\t\tDEFAULT: 'months'\n\t},\n\t[TimeTypes.Week]: {\n\t\t1: 'week',\n\t\tDEFAULT: 'weeks'\n\t},\n\t[TimeTypes.Day]: {\n\t\t1: 'day',\n\t\tDEFAULT: 'days'\n\t},\n\t[TimeTypes.Hour]: {\n\t\t1: 'hour',\n\t\tDEFAULT: 'hours'\n\t},\n\t[TimeTypes.Minute]: {\n\t\t1: 'minute',\n\t\tDEFAULT: 'minutes'\n\t},\n\t[TimeTypes.Second]: {\n\t\t1: 'second',\n\t\tDEFAULT: 'seconds'\n\t}\n};\n\nexport const DEFAULT_SEPARATORS: DurationFormatSeparators = {\n\tleft: ' ',\n\tright: ' '\n};\n","import { Time } from './constants';\n\nconst tokens = new Map([\n\t['nanosecond', Time.Nanosecond],\n\t['nanoseconds', Time.Nanosecond],\n\t['ns', Time.Nanosecond],\n\n\t['microsecond', Time.Microsecond],\n\t['microseconds', Time.Microsecond],\n\t['ΞΌs', Time.Microsecond],\n\t['us', Time.Microsecond],\n\n\t['millisecond', Time.Millisecond],\n\t['milliseconds', Time.Millisecond],\n\t['ms', Time.Millisecond],\n\n\t['second', Time.Second],\n\t['seconds', Time.Second],\n\t['sec', Time.Second],\n\t['secs', Time.Second],\n\t['s', Time.Second],\n\n\t['minute', Time.Minute],\n\t['minutes', Time.Minute],\n\t['min', Time.Minute],\n\t['mins', Time.Minute],\n\t['m', Time.Minute],\n\n\t['hour', Time.Hour],\n\t['hours', Time.Hour],\n\t['hr', Time.Hour],\n\t['hrs', Time.Hour],\n\t['h', Time.Hour],\n\n\t['day', Time.Day],\n\t['days', Time.Day],\n\t['d', Time.Day],\n\n\t['week', Time.Week],\n\t['weeks', Time.Week],\n\t['wk', Time.Week],\n\t['wks', Time.Week],\n\t['w', Time.Week],\n\n\t['month', Time.Month],\n\t['months', Time.Month],\n\t['b', Time.Month],\n\t['mo', Time.Month],\n\n\t['year', Time.Year],\n\t['years', Time.Year],\n\t['yr', Time.Year],\n\t['yrs', Time.Year],\n\t['y', Time.Year]\n]);\n\nconst mappings = new Map([\n\t[Time.Nanosecond, 'nanoseconds'],\n\t[Time.Microsecond, 'microseconds'],\n\t[Time.Millisecond, 'milliseconds'],\n\t[Time.Second, 'seconds'],\n\t[Time.Minute, 'minutes'],\n\t[Time.Hour, 'hours'],\n\t[Time.Day, 'days'],\n\t[Time.Week, 'weeks'],\n\t[Time.Month, 'months'],\n\t[Time.Year, 'years']\n] as const);\n\n/**\n * Converts duration strings into ms and future dates\n */\nexport class Duration {\n\t/**\n\t * The offset\n\t */\n\tpublic offset: number;\n\n\t/**\n\t * The amount of nanoseconds extracted from the text.\n\t */\n\tpublic nanoseconds = 0;\n\n\t/**\n\t * The amount of microseconds extracted from the text.\n\t */\n\tpublic microseconds = 0;\n\n\t/**\n\t * The amount of milliseconds extracted from the text.\n\t */\n\tpublic milliseconds = 0;\n\n\t/**\n\t * The amount of seconds extracted from the text.\n\t */\n\tpublic seconds = 0;\n\n\t/**\n\t * The amount of minutes extracted from the text.\n\t */\n\tpublic minutes = 0;\n\n\t/**\n\t * The amount of hours extracted from the text.\n\t */\n\tpublic hours = 0;\n\n\t/**\n\t * The amount of days extracted from the text.\n\t */\n\tpublic days = 0;\n\n\t/**\n\t * The amount of weeks extracted from the text.\n\t */\n\tpublic weeks = 0;\n\n\t/**\n\t * The amount of months extracted from the text.\n\t */\n\tpublic months = 0;\n\n\t/**\n\t * The amount of years extracted from the text.\n\t */\n\tpublic years = 0;\n\n\t/**\n\t * Create a new Duration instance\n\t * @param pattern The string to parse\n\t */\n\tpublic constructor(pattern: string) {\n\t\tlet result = 0;\n\t\tlet valid = false;\n\n\t\tpattern\n\t\t\t.toLowerCase()\n\t\t\t// ignore commas\n\t\t\t.replace(Duration.commaRegex, '')\n\t\t\t// a / an = 1\n\t\t\t.replace(Duration.aAndAnRegex, '1')\n\t\t\t// do math\n\t\t\t.replace(Duration.patternRegex, (_, i, units) => {\n\t\t\t\tconst token = tokens.get(units);\n\t\t\t\tif (token !== undefined) {\n\t\t\t\t\tconst n = Number(i);\n\t\t\t\t\tresult += n * token;\n\t\t\t\t\tthis[mappings.get(token)!] += n;\n\t\t\t\t\tvalid = true;\n\t\t\t\t}\n\t\t\t\treturn '';\n\t\t\t});\n\n\t\tthis.offset = valid ? result : NaN;\n\t}\n\n\t/**\n\t * Get the date from now\n\t */\n\tpublic get fromNow(): Date {\n\t\treturn this.dateFrom(new Date());\n\t}\n\n\t/**\n\t * Get the date from\n\t * @param date The Date instance to get the date from\n\t */\n\tpublic dateFrom(date: Date): Date {\n\t\treturn new Date(date.getTime() + this.offset);\n\t}\n\n\t/**\n\t * The RegExp used for the pattern parsing\n\t */\n\tprivate static readonly patternRegex = /(-?\\d*\\.?\\d+(?:e[-+]?\\d+)?)\\s*([a-zΞΌ]*)/gi;\n\n\t/**\n\t * The RegExp used for removing commas\n\t */\n\tprivate static readonly commaRegex = /,/g;\n\n\t/**\n\t * The RegExp used for replacing a/an with 1\n\t */\n\tprivate static readonly aAndAnRegex = /\\ban?\\b/gi;\n}\n","import { DEFAULT_SEPARATORS, DEFAULT_UNITS, TimeTypes } from './constants';\n\n/**\n * The duration of each time type in milliseconds\n */\nconst kTimeDurations: readonly [TimeTypes, number][] = [\n\t[TimeTypes.Year, 31536000000],\n\t// 29.53059 days is the official duration of a month: https://en.wikipedia.org/wiki/Month\n\t[TimeTypes.Month, 2628000000],\n\t[TimeTypes.Week, 1000 * 60 * 60 * 24 * 7],\n\t[TimeTypes.Day, 1000 * 60 * 60 * 24],\n\t[TimeTypes.Hour, 1000 * 60 * 60],\n\t[TimeTypes.Minute, 1000 * 60],\n\t[TimeTypes.Second, 1000]\n];\n\n/**\n * Display the duration\n * @param duration The duration in milliseconds to parse and display\n * @param assets The language assets\n */\nexport class DurationFormatter {\n\tpublic constructor(public units: DurationFormatAssetsTime = DEFAULT_UNITS) {}\n\n\tpublic format(\n\t\tduration: number,\n\t\tprecision = 7,\n\t\t{\n\t\t\tleft: leftSeparator = DEFAULT_SEPARATORS.left,\n\t\t\tright: rightSeparator = DEFAULT_SEPARATORS.right\n\t\t}: DurationFormatSeparators = DEFAULT_SEPARATORS\n\t) {\n\t\tconst output: string[] = [];\n\t\tconst negative = duration < 0;\n\t\tif (negative) duration *= -1;\n\n\t\tfor (const [type, timeDuration] of kTimeDurations) {\n\t\t\tconst division = duration / timeDuration;\n\t\t\tif (division < 1) continue;\n\n\t\t\tconst floored = Math.floor(division);\n\t\t\tduration -= floored * timeDuration;\n\t\t\toutput.push(addUnit(floored, this.units[type], leftSeparator!));\n\n\t\t\t// If the output has enough precision, break\n\t\t\tif (output.length >= precision) break;\n\t\t}\n\n\t\treturn `${negative ? '-' : ''}${output.join(rightSeparator) || addUnit(0, this.units.second, leftSeparator!)}`;\n\t}\n}\n\n/**\n * Adds an unit, if non zero\n * @param time The duration of said unit\n * @param unit The unit language assets\n */\nfunction addUnit(time: number, unit: DurationFormatAssetsUnit, separator: string) {\n\tif (Reflect.has(unit, time)) return `${time}${separator}${Reflect.get(unit, time)}`;\n\treturn `${time}${separator}${unit.DEFAULT}`;\n}\n\nexport interface DurationFormatSeparators {\n\tleft?: string;\n\tright?: string;\n}\n\nexport interface DurationFormatAssetsUnit extends Record<number, string> {\n\tDEFAULT: string;\n}\n\nexport interface DurationFormatAssetsTime {\n\t[TimeTypes.Second]: DurationFormatAssetsUnit;\n\t[TimeTypes.Minute]: DurationFormatAssetsUnit;\n\t[TimeTypes.Hour]: DurationFormatAssetsUnit;\n\t[TimeTypes.Day]: DurationFormatAssetsUnit;\n\t[TimeTypes.Week]: DurationFormatAssetsUnit;\n\t[TimeTypes.Month]: DurationFormatAssetsUnit;\n\t[TimeTypes.Year]: DurationFormatAssetsUnit;\n}\n"]}
|
package/dist/index.js
CHANGED
|
@@ -10,11 +10,14 @@ var __publicField = (obj, key, value) => {
|
|
|
10
10
|
|
|
11
11
|
// src/lib/constants.ts
|
|
12
12
|
var Time = /* @__PURE__ */ ((Time2) => {
|
|
13
|
+
Time2[Time2["Nanosecond"] = 1e-6] = "Nanosecond";
|
|
14
|
+
Time2[Time2["Microsecond"] = 1e-3] = "Microsecond";
|
|
13
15
|
Time2[Time2["Millisecond"] = 1] = "Millisecond";
|
|
14
16
|
Time2[Time2["Second"] = 1e3] = "Second";
|
|
15
17
|
Time2[Time2["Minute"] = 6e4] = "Minute";
|
|
16
18
|
Time2[Time2["Hour"] = 36e5] = "Hour";
|
|
17
19
|
Time2[Time2["Day"] = 864e5] = "Day";
|
|
20
|
+
Time2[Time2["Week"] = 6048e5] = "Week";
|
|
18
21
|
Time2[Time2["Month"] = 2628e6] = "Month";
|
|
19
22
|
Time2[Time2["Year"] = 31536e6] = "Year";
|
|
20
23
|
return Time2;
|
|
@@ -66,79 +69,158 @@ var DEFAULT_SEPARATORS = {
|
|
|
66
69
|
|
|
67
70
|
// src/lib/Duration.ts
|
|
68
71
|
var tokens = /* @__PURE__ */ new Map([
|
|
69
|
-
["nanosecond",
|
|
70
|
-
["nanoseconds",
|
|
71
|
-
["ns",
|
|
72
|
-
["
|
|
73
|
-
["
|
|
74
|
-
["
|
|
75
|
-
["
|
|
76
|
-
["
|
|
77
|
-
["
|
|
78
|
-
["
|
|
79
|
-
["
|
|
80
|
-
["
|
|
81
|
-
["
|
|
82
|
-
["
|
|
83
|
-
["
|
|
84
|
-
["
|
|
85
|
-
["
|
|
86
|
-
["
|
|
87
|
-
["
|
|
88
|
-
["
|
|
89
|
-
["
|
|
90
|
-
["
|
|
91
|
-
["
|
|
92
|
-
["
|
|
93
|
-
["
|
|
94
|
-
["
|
|
95
|
-
["
|
|
96
|
-
["
|
|
97
|
-
["
|
|
98
|
-
["
|
|
99
|
-
["
|
|
100
|
-
["
|
|
101
|
-
["
|
|
102
|
-
["
|
|
103
|
-
["
|
|
104
|
-
["
|
|
105
|
-
["
|
|
106
|
-
["
|
|
72
|
+
["nanosecond", 1e-6 /* Nanosecond */],
|
|
73
|
+
["nanoseconds", 1e-6 /* Nanosecond */],
|
|
74
|
+
["ns", 1e-6 /* Nanosecond */],
|
|
75
|
+
["microsecond", 1e-3 /* Microsecond */],
|
|
76
|
+
["microseconds", 1e-3 /* Microsecond */],
|
|
77
|
+
["\u03BCs", 1e-3 /* Microsecond */],
|
|
78
|
+
["us", 1e-3 /* Microsecond */],
|
|
79
|
+
["millisecond", 1 /* Millisecond */],
|
|
80
|
+
["milliseconds", 1 /* Millisecond */],
|
|
81
|
+
["ms", 1 /* Millisecond */],
|
|
82
|
+
["second", 1e3 /* Second */],
|
|
83
|
+
["seconds", 1e3 /* Second */],
|
|
84
|
+
["sec", 1e3 /* Second */],
|
|
85
|
+
["secs", 1e3 /* Second */],
|
|
86
|
+
["s", 1e3 /* Second */],
|
|
87
|
+
["minute", 6e4 /* Minute */],
|
|
88
|
+
["minutes", 6e4 /* Minute */],
|
|
89
|
+
["min", 6e4 /* Minute */],
|
|
90
|
+
["mins", 6e4 /* Minute */],
|
|
91
|
+
["m", 6e4 /* Minute */],
|
|
92
|
+
["hour", 36e5 /* Hour */],
|
|
93
|
+
["hours", 36e5 /* Hour */],
|
|
94
|
+
["hr", 36e5 /* Hour */],
|
|
95
|
+
["hrs", 36e5 /* Hour */],
|
|
96
|
+
["h", 36e5 /* Hour */],
|
|
97
|
+
["day", 864e5 /* Day */],
|
|
98
|
+
["days", 864e5 /* Day */],
|
|
99
|
+
["d", 864e5 /* Day */],
|
|
100
|
+
["week", 6048e5 /* Week */],
|
|
101
|
+
["weeks", 6048e5 /* Week */],
|
|
102
|
+
["wk", 6048e5 /* Week */],
|
|
103
|
+
["wks", 6048e5 /* Week */],
|
|
104
|
+
["w", 6048e5 /* Week */],
|
|
105
|
+
["month", 2628e6 /* Month */],
|
|
106
|
+
["months", 2628e6 /* Month */],
|
|
107
|
+
["b", 2628e6 /* Month */],
|
|
108
|
+
["mo", 2628e6 /* Month */],
|
|
109
|
+
["year", 31536e6 /* Year */],
|
|
110
|
+
["years", 31536e6 /* Year */],
|
|
111
|
+
["yr", 31536e6 /* Year */],
|
|
112
|
+
["yrs", 31536e6 /* Year */],
|
|
113
|
+
["y", 31536e6 /* Year */]
|
|
114
|
+
]);
|
|
115
|
+
var mappings = /* @__PURE__ */ new Map([
|
|
116
|
+
[1e-6 /* Nanosecond */, "nanoseconds"],
|
|
117
|
+
[1e-3 /* Microsecond */, "microseconds"],
|
|
118
|
+
[1 /* Millisecond */, "milliseconds"],
|
|
119
|
+
[1e3 /* Second */, "seconds"],
|
|
120
|
+
[6e4 /* Minute */, "minutes"],
|
|
121
|
+
[36e5 /* Hour */, "hours"],
|
|
122
|
+
[864e5 /* Day */, "days"],
|
|
123
|
+
[6048e5 /* Week */, "weeks"],
|
|
124
|
+
[2628e6 /* Month */, "months"],
|
|
125
|
+
[31536e6 /* Year */, "years"]
|
|
107
126
|
]);
|
|
108
127
|
var _Duration = class {
|
|
128
|
+
/**
|
|
129
|
+
* Create a new Duration instance
|
|
130
|
+
* @param pattern The string to parse
|
|
131
|
+
*/
|
|
109
132
|
constructor(pattern) {
|
|
133
|
+
/**
|
|
134
|
+
* The offset
|
|
135
|
+
*/
|
|
110
136
|
__publicField(this, "offset");
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
137
|
+
/**
|
|
138
|
+
* The amount of nanoseconds extracted from the text.
|
|
139
|
+
*/
|
|
140
|
+
__publicField(this, "nanoseconds", 0);
|
|
141
|
+
/**
|
|
142
|
+
* The amount of microseconds extracted from the text.
|
|
143
|
+
*/
|
|
144
|
+
__publicField(this, "microseconds", 0);
|
|
145
|
+
/**
|
|
146
|
+
* The amount of milliseconds extracted from the text.
|
|
147
|
+
*/
|
|
148
|
+
__publicField(this, "milliseconds", 0);
|
|
149
|
+
/**
|
|
150
|
+
* The amount of seconds extracted from the text.
|
|
151
|
+
*/
|
|
152
|
+
__publicField(this, "seconds", 0);
|
|
153
|
+
/**
|
|
154
|
+
* The amount of minutes extracted from the text.
|
|
155
|
+
*/
|
|
156
|
+
__publicField(this, "minutes", 0);
|
|
157
|
+
/**
|
|
158
|
+
* The amount of hours extracted from the text.
|
|
159
|
+
*/
|
|
160
|
+
__publicField(this, "hours", 0);
|
|
161
|
+
/**
|
|
162
|
+
* The amount of days extracted from the text.
|
|
163
|
+
*/
|
|
164
|
+
__publicField(this, "days", 0);
|
|
165
|
+
/**
|
|
166
|
+
* The amount of weeks extracted from the text.
|
|
167
|
+
*/
|
|
168
|
+
__publicField(this, "weeks", 0);
|
|
169
|
+
/**
|
|
170
|
+
* The amount of months extracted from the text.
|
|
171
|
+
*/
|
|
172
|
+
__publicField(this, "months", 0);
|
|
173
|
+
/**
|
|
174
|
+
* The amount of years extracted from the text.
|
|
175
|
+
*/
|
|
176
|
+
__publicField(this, "years", 0);
|
|
120
177
|
let result = 0;
|
|
121
178
|
let valid = false;
|
|
122
|
-
pattern.replace(_Duration.
|
|
179
|
+
pattern.toLowerCase().replace(_Duration.commaRegex, "").replace(_Duration.aAndAnRegex, "1").replace(_Duration.patternRegex, (_, i, units) => {
|
|
123
180
|
const token = tokens.get(units);
|
|
124
181
|
if (token !== void 0) {
|
|
125
|
-
|
|
182
|
+
const n = Number(i);
|
|
183
|
+
result += n * token;
|
|
184
|
+
this[mappings.get(token)] += n;
|
|
126
185
|
valid = true;
|
|
127
186
|
}
|
|
128
187
|
return "";
|
|
129
188
|
});
|
|
130
|
-
|
|
189
|
+
this.offset = valid ? result : NaN;
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Get the date from now
|
|
193
|
+
*/
|
|
194
|
+
get fromNow() {
|
|
195
|
+
return this.dateFrom(/* @__PURE__ */ new Date());
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Get the date from
|
|
199
|
+
* @param date The Date instance to get the date from
|
|
200
|
+
*/
|
|
201
|
+
dateFrom(date) {
|
|
202
|
+
return new Date(date.getTime() + this.offset);
|
|
131
203
|
}
|
|
132
204
|
};
|
|
133
205
|
var Duration = _Duration;
|
|
134
206
|
__name(Duration, "Duration");
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
207
|
+
/**
|
|
208
|
+
* The RegExp used for the pattern parsing
|
|
209
|
+
*/
|
|
210
|
+
__publicField(Duration, "patternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zΞΌ]*)/gi);
|
|
211
|
+
/**
|
|
212
|
+
* The RegExp used for removing commas
|
|
213
|
+
*/
|
|
214
|
+
__publicField(Duration, "commaRegex", /,/g);
|
|
215
|
+
/**
|
|
216
|
+
* The RegExp used for replacing a/an with 1
|
|
217
|
+
*/
|
|
218
|
+
__publicField(Duration, "aAndAnRegex", /\ban?\b/gi);
|
|
138
219
|
|
|
139
220
|
// src/lib/DurationFormatter.ts
|
|
140
221
|
var kTimeDurations = [
|
|
141
222
|
["year" /* Year */, 31536e6],
|
|
223
|
+
// 29.53059 days is the official duration of a month: https://en.wikipedia.org/wiki/Month
|
|
142
224
|
["month" /* Month */, 2628e6],
|
|
143
225
|
["week" /* Week */, 1e3 * 60 * 60 * 24 * 7],
|
|
144
226
|
["day" /* Day */, 1e3 * 60 * 60 * 24],
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/lib/constants.ts","../src/lib/Duration.ts","../src/lib/DurationFormatter.ts"],"names":["Time","TimeTypes"],"mappings":";;;;;;;;;AAEO,IAAK,OAAL,kBAAKA,UAAL;AACN,EAAAA,YAAA,iBAAc,KAAd;AACA,EAAAA,YAAA,YAAS,OAAT;AACA,EAAAA,YAAA,YAAS,OAAT;AACA,EAAAA,YAAA,UAAO,QAAP;AACA,EAAAA,YAAA,SAAM,SAAN;AACA,EAAAA,YAAA,WAAQ,UAAR;AACA,EAAAA,YAAA,UAAO,WAAP;AAPW,SAAAA;AAAA,GAAA;AAaL,IAAK,YAAL,kBAAKC,eAAL;AACN,EAAAA,WAAA,YAAS;AACT,EAAAA,WAAA,YAAS;AACT,EAAAA,WAAA,UAAO;AACP,EAAAA,WAAA,SAAM;AACN,EAAAA,WAAA,UAAO;AACP,EAAAA,WAAA,WAAQ;AACR,EAAAA,WAAA,UAAO;AAPI,SAAAA;AAAA,GAAA;AAUL,IAAM,gBAA0C;AAAA,EACtD,CAAC,oBAAiB;AAAA,IACjB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,sBAAkB;AAAA,IAClB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,oBAAiB;AAAA,IACjB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,kBAAgB;AAAA,IAChB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,oBAAiB;AAAA,IACjB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,wBAAmB;AAAA,IACnB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,wBAAmB;AAAA,IACnB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AACD;AAEO,IAAM,qBAA+C;AAAA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type { DurationFormatAssetsTime, DurationFormatSeparators } from './DurationFormatter';\n\nexport enum Time {\n\tMillisecond = 1,\n\tSecond = 1000,\n\tMinute = 1000 * 60,\n\tHour = 1000 * 60 * 60,\n\tDay = 1000 * 60 * 60 * 24,\n\tMonth = 1000 * 60 * 60 * 24 * (365 / 12),\n\tYear = 1000 * 60 * 60 * 24 * 365\n}\n\n/**\n * The supported time types\n */\nexport enum TimeTypes {\n\tSecond = 'second',\n\tMinute = 'minute',\n\tHour = 'hour',\n\tDay = 'day',\n\tWeek = 'week',\n\tMonth = 'month',\n\tYear = 'year'\n}\n\nexport const DEFAULT_UNITS: DurationFormatAssetsTime = {\n\t[TimeTypes.Year]: {\n\t\t1: 'year',\n\t\tDEFAULT: 'years'\n\t},\n\t[TimeTypes.Month]: {\n\t\t1: 'month',\n\t\tDEFAULT: 'months'\n\t},\n\t[TimeTypes.Week]: {\n\t\t1: 'week',\n\t\tDEFAULT: 'weeks'\n\t},\n\t[TimeTypes.Day]: {\n\t\t1: 'day',\n\t\tDEFAULT: 'days'\n\t},\n\t[TimeTypes.Hour]: {\n\t\t1: 'hour',\n\t\tDEFAULT: 'hours'\n\t},\n\t[TimeTypes.Minute]: {\n\t\t1: 'minute',\n\t\tDEFAULT: 'minutes'\n\t},\n\t[TimeTypes.Second]: {\n\t\t1: 'second',\n\t\tDEFAULT: 'seconds'\n\t}\n};\n\nexport const DEFAULT_SEPARATORS: DurationFormatSeparators = {\n\tleft: ' ',\n\tright: ' '\n};\n","const tokens = new Map([\n\t['nanosecond', 1 / 1e6],\n\t['nanoseconds', 1 / 1e6],\n\t['ns', 1 / 1e6],\n\n\t['millisecond', 1],\n\t['milliseconds', 1],\n\t['ms', 1],\n\n\t['second', 1000],\n\t['seconds', 1000],\n\t['sec', 1000],\n\t['secs', 1000],\n\t['s', 1000],\n\n\t['minute', 1000 * 60],\n\t['minutes', 1000 * 60],\n\t['min', 1000 * 60],\n\t['mins', 1000 * 60],\n\t['m', 1000 * 60],\n\n\t['hour', 1000 * 60 * 60],\n\t['hours', 1000 * 60 * 60],\n\t['hr', 1000 * 60 * 60],\n\t['hrs', 1000 * 60 * 60],\n\t['h', 1000 * 60 * 60],\n\n\t['day', 1000 * 60 * 60 * 24],\n\t['days', 1000 * 60 * 60 * 24],\n\t['d', 1000 * 60 * 60 * 24],\n\n\t['week', 1000 * 60 * 60 * 24 * 7],\n\t['weeks', 1000 * 60 * 60 * 24 * 7],\n\t['wk', 1000 * 60 * 60 * 24 * 7],\n\t['wks', 1000 * 60 * 60 * 24 * 7],\n\t['w', 1000 * 60 * 60 * 24 * 7],\n\n\t['month', 1000 * 60 * 60 * 24 * (365.25 / 12)],\n\t['months', 1000 * 60 * 60 * 24 * (365.25 / 12)],\n\t['b', 1000 * 60 * 60 * 24 * (365.25 / 12)],\n\t['mo', 1000 * 60 * 60 * 24 * (365.25 / 12)],\n\n\t['year', 1000 * 60 * 60 * 24 * 365.25],\n\t['years', 1000 * 60 * 60 * 24 * 365.25],\n\t['yr', 1000 * 60 * 60 * 24 * 365.25],\n\t['yrs', 1000 * 60 * 60 * 24 * 365.25],\n\t['y', 1000 * 60 * 60 * 24 * 365.25]\n]);\n\n/**\n * Converts duration strings into ms and future dates\n */\nexport class Duration {\n\t/**\n\t * The offset\n\t */\n\tpublic offset: number;\n\n\t/**\n\t * Create a new Duration instance\n\t * @param pattern The string to parse\n\t */\n\tpublic constructor(pattern: string) {\n\t\tthis.offset = Duration.parse(pattern.toLowerCase());\n\t}\n\n\t/**\n\t * Get the date from now\n\t */\n\tpublic get fromNow(): Date {\n\t\treturn this.dateFrom(new Date());\n\t}\n\n\t/**\n\t * Get the date from\n\t * @param date The Date instance to get the date from\n\t */\n\tpublic dateFrom(date: Date): Date {\n\t\treturn new Date(date.getTime() + this.offset);\n\t}\n\n\t/**\n\t * The RegExp used for the pattern parsing\n\t */\n\tprivate static readonly kPatternRegex = /(-?\\d*\\.?\\d+(?:e[-+]?\\d+)?)\\s*([a-zΞΌ]*)/gi;\n\n\t/**\n\t * The RegExp used for removing commas\n\t */\n\tprivate static readonly kCommaRegex = /,/g;\n\n\t/**\n\t * The RegExp used for replacing a/an with 1\n\t */\n\tprivate static readonly kAanRegex = /\\ban?\\b/gi;\n\n\t/**\n\t * Parse the pattern\n\t * @param pattern The pattern to parse\n\t */\n\tprivate static parse(pattern: string): number {\n\t\tlet result = 0;\n\t\tlet valid = false;\n\n\t\tpattern\n\t\t\t// ignore commas\n\t\t\t.replace(Duration.kCommaRegex, '')\n\t\t\t// a / an = 1\n\t\t\t.replace(Duration.kAanRegex, '1')\n\t\t\t// do math\n\t\t\t.replace(Duration.kPatternRegex, (_, i, units) => {\n\t\t\t\tconst token = tokens.get(units);\n\t\t\t\tif (token !== undefined) {\n\t\t\t\t\tresult += Number(i) * token;\n\t\t\t\t\tvalid = true;\n\t\t\t\t}\n\t\t\t\treturn '';\n\t\t\t});\n\n\t\treturn valid ? result : NaN;\n\t}\n}\n","import { DEFAULT_SEPARATORS, DEFAULT_UNITS, TimeTypes } from './constants';\n\n/**\n * The duration of each time type in milliseconds\n */\nconst kTimeDurations: readonly [TimeTypes, number][] = [\n\t[TimeTypes.Year, 31536000000],\n\t// 29.53059 days is the official duration of a month: https://en.wikipedia.org/wiki/Month\n\t[TimeTypes.Month, 2628000000],\n\t[TimeTypes.Week, 1000 * 60 * 60 * 24 * 7],\n\t[TimeTypes.Day, 1000 * 60 * 60 * 24],\n\t[TimeTypes.Hour, 1000 * 60 * 60],\n\t[TimeTypes.Minute, 1000 * 60],\n\t[TimeTypes.Second, 1000]\n];\n\n/**\n * Display the duration\n * @param duration The duration in milliseconds to parse and display\n * @param assets The language assets\n */\nexport class DurationFormatter {\n\tpublic constructor(public units: DurationFormatAssetsTime = DEFAULT_UNITS) {}\n\n\tpublic format(\n\t\tduration: number,\n\t\tprecision = 7,\n\t\t{\n\t\t\tleft: leftSeparator = DEFAULT_SEPARATORS.left,\n\t\t\tright: rightSeparator = DEFAULT_SEPARATORS.right\n\t\t}: DurationFormatSeparators = DEFAULT_SEPARATORS\n\t) {\n\t\tconst output: string[] = [];\n\t\tconst negative = duration < 0;\n\t\tif (negative) duration *= -1;\n\n\t\tfor (const [type, timeDuration] of kTimeDurations) {\n\t\t\tconst division = duration / timeDuration;\n\t\t\tif (division < 1) continue;\n\n\t\t\tconst floored = Math.floor(division);\n\t\t\tduration -= floored * timeDuration;\n\t\t\toutput.push(addUnit(floored, this.units[type], leftSeparator!));\n\n\t\t\t// If the output has enough precision, break\n\t\t\tif (output.length >= precision) break;\n\t\t}\n\n\t\treturn `${negative ? '-' : ''}${output.join(rightSeparator) || addUnit(0, this.units.second, leftSeparator!)}`;\n\t}\n}\n\n/**\n * Adds an unit, if non zero\n * @param time The duration of said unit\n * @param unit The unit language assets\n */\nfunction addUnit(time: number, unit: DurationFormatAssetsUnit, separator: string) {\n\tif (Reflect.has(unit, time)) return `${time}${separator}${Reflect.get(unit, time)}`;\n\treturn `${time}${separator}${unit.DEFAULT}`;\n}\n\nexport interface DurationFormatSeparators {\n\tleft?: string;\n\tright?: string;\n}\n\nexport interface DurationFormatAssetsUnit extends Record<number, string> {\n\tDEFAULT: string;\n}\n\nexport interface DurationFormatAssetsTime {\n\t[TimeTypes.Second]: DurationFormatAssetsUnit;\n\t[TimeTypes.Minute]: DurationFormatAssetsUnit;\n\t[TimeTypes.Hour]: DurationFormatAssetsUnit;\n\t[TimeTypes.Day]: DurationFormatAssetsUnit;\n\t[TimeTypes.Week]: DurationFormatAssetsUnit;\n\t[TimeTypes.Month]: DurationFormatAssetsUnit;\n\t[TimeTypes.Year]: DurationFormatAssetsUnit;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/lib/constants.ts","../src/lib/Duration.ts","../src/lib/DurationFormatter.ts"],"names":["Time","TimeTypes"],"mappings":";;;;;;;;;AAEO,IAAK,OAAL,kBAAKA,UAAL;AACN,EAAAA,YAAA,gBAAa,QAAb;AACA,EAAAA,YAAA,iBAAc,QAAd;AACA,EAAAA,YAAA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AAY,QAAQ,IAAI,MAAM,IAAI;AAChF,SAAO,GAAG,OAAO,YAAY,KAAK;AACnC;AAHS","sourcesContent":["import type { DurationFormatAssetsTime, DurationFormatSeparators } from './DurationFormatter';\n\nexport enum Time {\n\tNanosecond = 1 / 1_000_000,\n\tMicrosecond = 1 / 1000,\n\tMillisecond = 1,\n\tSecond = 1000,\n\tMinute = Second * 60,\n\tHour = Minute * 60,\n\tDay = Hour * 24,\n\tWeek = Day * 7,\n\tMonth = Day * (365 / 12),\n\tYear = Day * 365\n}\n\n/**\n * The supported time types\n */\nexport enum TimeTypes {\n\tSecond = 'second',\n\tMinute = 'minute',\n\tHour = 'hour',\n\tDay = 'day',\n\tWeek = 'week',\n\tMonth = 'month',\n\tYear = 'year'\n}\n\nexport const DEFAULT_UNITS: DurationFormatAssetsTime = {\n\t[TimeTypes.Year]: {\n\t\t1: 'year',\n\t\tDEFAULT: 'years'\n\t},\n\t[TimeTypes.Month]: {\n\t\t1: 'month',\n\t\tDEFAULT: 'months'\n\t},\n\t[TimeTypes.Week]: {\n\t\t1: 'week',\n\t\tDEFAULT: 'weeks'\n\t},\n\t[TimeTypes.Day]: {\n\t\t1: 'day',\n\t\tDEFAULT: 'days'\n\t},\n\t[TimeTypes.Hour]: {\n\t\t1: 'hour',\n\t\tDEFAULT: 'hours'\n\t},\n\t[TimeTypes.Minute]: {\n\t\t1: 'minute',\n\t\tDEFAULT: 'minutes'\n\t},\n\t[TimeTypes.Second]: {\n\t\t1: 'second',\n\t\tDEFAULT: 'seconds'\n\t}\n};\n\nexport const DEFAULT_SEPARATORS: DurationFormatSeparators = {\n\tleft: ' ',\n\tright: ' '\n};\n","import { Time } from './constants';\n\nconst tokens = new Map([\n\t['nanosecond', Time.Nanosecond],\n\t['nanoseconds', Time.Nanosecond],\n\t['ns', Time.Nanosecond],\n\n\t['microsecond', Time.Microsecond],\n\t['microseconds', Time.Microsecond],\n\t['ΞΌs', Time.Microsecond],\n\t['us', Time.Microsecond],\n\n\t['millisecond', Time.Millisecond],\n\t['milliseconds', Time.Millisecond],\n\t['ms', Time.Millisecond],\n\n\t['second', Time.Second],\n\t['seconds', Time.Second],\n\t['sec', Time.Second],\n\t['secs', Time.Second],\n\t['s', Time.Second],\n\n\t['minute', Time.Minute],\n\t['minutes', Time.Minute],\n\t['min', Time.Minute],\n\t['mins', Time.Minute],\n\t['m', Time.Minute],\n\n\t['hour', Time.Hour],\n\t['hours', Time.Hour],\n\t['hr', Time.Hour],\n\t['hrs', Time.Hour],\n\t['h', Time.Hour],\n\n\t['day', Time.Day],\n\t['days', Time.Day],\n\t['d', Time.Day],\n\n\t['week', Time.Week],\n\t['weeks', Time.Week],\n\t['wk', Time.Week],\n\t['wks', Time.Week],\n\t['w', Time.Week],\n\n\t['month', Time.Month],\n\t['months', Time.Month],\n\t['b', Time.Month],\n\t['mo', Time.Month],\n\n\t['year', Time.Year],\n\t['years', Time.Year],\n\t['yr', Time.Year],\n\t['yrs', Time.Year],\n\t['y', Time.Year]\n]);\n\nconst mappings = new Map([\n\t[Time.Nanosecond, 'nanoseconds'],\n\t[Time.Microsecond, 'microseconds'],\n\t[Time.Millisecond, 'milliseconds'],\n\t[Time.Second, 'seconds'],\n\t[Time.Minute, 'minutes'],\n\t[Time.Hour, 'hours'],\n\t[Time.Day, 'days'],\n\t[Time.Week, 'weeks'],\n\t[Time.Month, 'months'],\n\t[Time.Year, 'years']\n] as const);\n\n/**\n * Converts duration strings into ms and future dates\n */\nexport class Duration {\n\t/**\n\t * The offset\n\t */\n\tpublic offset: number;\n\n\t/**\n\t * The amount of nanoseconds extracted from the text.\n\t */\n\tpublic nanoseconds = 0;\n\n\t/**\n\t * The amount of microseconds extracted from the text.\n\t */\n\tpublic microseconds = 0;\n\n\t/**\n\t * The amount of milliseconds extracted from the text.\n\t */\n\tpublic milliseconds = 0;\n\n\t/**\n\t * The amount of seconds extracted from the text.\n\t */\n\tpublic seconds = 0;\n\n\t/**\n\t * The amount of minutes extracted from the text.\n\t */\n\tpublic minutes = 0;\n\n\t/**\n\t * The amount of hours extracted from the text.\n\t */\n\tpublic hours = 0;\n\n\t/**\n\t * The amount of days extracted from the text.\n\t */\n\tpublic days = 0;\n\n\t/**\n\t * The amount of weeks extracted from the text.\n\t */\n\tpublic weeks = 0;\n\n\t/**\n\t * The amount of months extracted from the text.\n\t */\n\tpublic months = 0;\n\n\t/**\n\t * The amount of years extracted from the text.\n\t */\n\tpublic years = 0;\n\n\t/**\n\t * Create a new Duration instance\n\t * @param pattern The string to parse\n\t */\n\tpublic constructor(pattern: string) {\n\t\tlet result = 0;\n\t\tlet valid = false;\n\n\t\tpattern\n\t\t\t.toLowerCase()\n\t\t\t// ignore commas\n\t\t\t.replace(Duration.commaRegex, '')\n\t\t\t// a / an = 1\n\t\t\t.replace(Duration.aAndAnRegex, '1')\n\t\t\t// do math\n\t\t\t.replace(Duration.patternRegex, (_, i, units) => {\n\t\t\t\tconst token = tokens.get(units);\n\t\t\t\tif (token !== undefined) {\n\t\t\t\t\tconst n = Number(i);\n\t\t\t\t\tresult += n * token;\n\t\t\t\t\tthis[mappings.get(token)!] += n;\n\t\t\t\t\tvalid = true;\n\t\t\t\t}\n\t\t\t\treturn '';\n\t\t\t});\n\n\t\tthis.offset = valid ? result : NaN;\n\t}\n\n\t/**\n\t * Get the date from now\n\t */\n\tpublic get fromNow(): Date {\n\t\treturn this.dateFrom(new Date());\n\t}\n\n\t/**\n\t * Get the date from\n\t * @param date The Date instance to get the date from\n\t */\n\tpublic dateFrom(date: Date): Date {\n\t\treturn new Date(date.getTime() + this.offset);\n\t}\n\n\t/**\n\t * The RegExp used for the pattern parsing\n\t */\n\tprivate static readonly patternRegex = /(-?\\d*\\.?\\d+(?:e[-+]?\\d+)?)\\s*([a-zΞΌ]*)/gi;\n\n\t/**\n\t * The RegExp used for removing commas\n\t */\n\tprivate static readonly commaRegex = /,/g;\n\n\t/**\n\t * The RegExp used for replacing a/an with 1\n\t */\n\tprivate static readonly aAndAnRegex = /\\ban?\\b/gi;\n}\n","import { DEFAULT_SEPARATORS, DEFAULT_UNITS, TimeTypes } from './constants';\n\n/**\n * The duration of each time type in milliseconds\n */\nconst kTimeDurations: readonly [TimeTypes, number][] = [\n\t[TimeTypes.Year, 31536000000],\n\t// 29.53059 days is the official duration of a month: https://en.wikipedia.org/wiki/Month\n\t[TimeTypes.Month, 2628000000],\n\t[TimeTypes.Week, 1000 * 60 * 60 * 24 * 7],\n\t[TimeTypes.Day, 1000 * 60 * 60 * 24],\n\t[TimeTypes.Hour, 1000 * 60 * 60],\n\t[TimeTypes.Minute, 1000 * 60],\n\t[TimeTypes.Second, 1000]\n];\n\n/**\n * Display the duration\n * @param duration The duration in milliseconds to parse and display\n * @param assets The language assets\n */\nexport class DurationFormatter {\n\tpublic constructor(public units: DurationFormatAssetsTime = DEFAULT_UNITS) {}\n\n\tpublic format(\n\t\tduration: number,\n\t\tprecision = 7,\n\t\t{\n\t\t\tleft: leftSeparator = DEFAULT_SEPARATORS.left,\n\t\t\tright: rightSeparator = DEFAULT_SEPARATORS.right\n\t\t}: DurationFormatSeparators = DEFAULT_SEPARATORS\n\t) {\n\t\tconst output: string[] = [];\n\t\tconst negative = duration < 0;\n\t\tif (negative) duration *= -1;\n\n\t\tfor (const [type, timeDuration] of kTimeDurations) {\n\t\t\tconst division = duration / timeDuration;\n\t\t\tif (division < 1) continue;\n\n\t\t\tconst floored = Math.floor(division);\n\t\t\tduration -= floored * timeDuration;\n\t\t\toutput.push(addUnit(floored, this.units[type], leftSeparator!));\n\n\t\t\t// If the output has enough precision, break\n\t\t\tif (output.length >= precision) break;\n\t\t}\n\n\t\treturn `${negative ? '-' : ''}${output.join(rightSeparator) || addUnit(0, this.units.second, leftSeparator!)}`;\n\t}\n}\n\n/**\n * Adds an unit, if non zero\n * @param time The duration of said unit\n * @param unit The unit language assets\n */\nfunction addUnit(time: number, unit: DurationFormatAssetsUnit, separator: string) {\n\tif (Reflect.has(unit, time)) return `${time}${separator}${Reflect.get(unit, time)}`;\n\treturn `${time}${separator}${unit.DEFAULT}`;\n}\n\nexport interface DurationFormatSeparators {\n\tleft?: string;\n\tright?: string;\n}\n\nexport interface DurationFormatAssetsUnit extends Record<number, string> {\n\tDEFAULT: string;\n}\n\nexport interface DurationFormatAssetsTime {\n\t[TimeTypes.Second]: DurationFormatAssetsUnit;\n\t[TimeTypes.Minute]: DurationFormatAssetsUnit;\n\t[TimeTypes.Hour]: DurationFormatAssetsUnit;\n\t[TimeTypes.Day]: DurationFormatAssetsUnit;\n\t[TimeTypes.Week]: DurationFormatAssetsUnit;\n\t[TimeTypes.Month]: DurationFormatAssetsUnit;\n\t[TimeTypes.Year]: DurationFormatAssetsUnit;\n}\n"]}
|
package/dist/index.mjs
CHANGED
|
@@ -8,11 +8,14 @@ var __publicField = (obj, key, value) => {
|
|
|
8
8
|
|
|
9
9
|
// src/lib/constants.ts
|
|
10
10
|
var Time = /* @__PURE__ */ ((Time2) => {
|
|
11
|
+
Time2[Time2["Nanosecond"] = 1e-6] = "Nanosecond";
|
|
12
|
+
Time2[Time2["Microsecond"] = 1e-3] = "Microsecond";
|
|
11
13
|
Time2[Time2["Millisecond"] = 1] = "Millisecond";
|
|
12
14
|
Time2[Time2["Second"] = 1e3] = "Second";
|
|
13
15
|
Time2[Time2["Minute"] = 6e4] = "Minute";
|
|
14
16
|
Time2[Time2["Hour"] = 36e5] = "Hour";
|
|
15
17
|
Time2[Time2["Day"] = 864e5] = "Day";
|
|
18
|
+
Time2[Time2["Week"] = 6048e5] = "Week";
|
|
16
19
|
Time2[Time2["Month"] = 2628e6] = "Month";
|
|
17
20
|
Time2[Time2["Year"] = 31536e6] = "Year";
|
|
18
21
|
return Time2;
|
|
@@ -64,79 +67,158 @@ var DEFAULT_SEPARATORS = {
|
|
|
64
67
|
|
|
65
68
|
// src/lib/Duration.ts
|
|
66
69
|
var tokens = /* @__PURE__ */ new Map([
|
|
67
|
-
["nanosecond",
|
|
68
|
-
["nanoseconds",
|
|
69
|
-
["ns",
|
|
70
|
-
["
|
|
71
|
-
["
|
|
72
|
-
["
|
|
73
|
-
["
|
|
74
|
-
["
|
|
75
|
-
["
|
|
76
|
-
["
|
|
77
|
-
["
|
|
78
|
-
["
|
|
79
|
-
["
|
|
80
|
-
["
|
|
81
|
-
["
|
|
82
|
-
["
|
|
83
|
-
["
|
|
84
|
-
["
|
|
85
|
-
["
|
|
86
|
-
["
|
|
87
|
-
["
|
|
88
|
-
["
|
|
89
|
-
["
|
|
90
|
-
["
|
|
91
|
-
["
|
|
92
|
-
["
|
|
93
|
-
["
|
|
94
|
-
["
|
|
95
|
-
["
|
|
96
|
-
["
|
|
97
|
-
["
|
|
98
|
-
["
|
|
99
|
-
["
|
|
100
|
-
["
|
|
101
|
-
["
|
|
102
|
-
["
|
|
103
|
-
["
|
|
104
|
-
["
|
|
70
|
+
["nanosecond", 1e-6 /* Nanosecond */],
|
|
71
|
+
["nanoseconds", 1e-6 /* Nanosecond */],
|
|
72
|
+
["ns", 1e-6 /* Nanosecond */],
|
|
73
|
+
["microsecond", 1e-3 /* Microsecond */],
|
|
74
|
+
["microseconds", 1e-3 /* Microsecond */],
|
|
75
|
+
["\u03BCs", 1e-3 /* Microsecond */],
|
|
76
|
+
["us", 1e-3 /* Microsecond */],
|
|
77
|
+
["millisecond", 1 /* Millisecond */],
|
|
78
|
+
["milliseconds", 1 /* Millisecond */],
|
|
79
|
+
["ms", 1 /* Millisecond */],
|
|
80
|
+
["second", 1e3 /* Second */],
|
|
81
|
+
["seconds", 1e3 /* Second */],
|
|
82
|
+
["sec", 1e3 /* Second */],
|
|
83
|
+
["secs", 1e3 /* Second */],
|
|
84
|
+
["s", 1e3 /* Second */],
|
|
85
|
+
["minute", 6e4 /* Minute */],
|
|
86
|
+
["minutes", 6e4 /* Minute */],
|
|
87
|
+
["min", 6e4 /* Minute */],
|
|
88
|
+
["mins", 6e4 /* Minute */],
|
|
89
|
+
["m", 6e4 /* Minute */],
|
|
90
|
+
["hour", 36e5 /* Hour */],
|
|
91
|
+
["hours", 36e5 /* Hour */],
|
|
92
|
+
["hr", 36e5 /* Hour */],
|
|
93
|
+
["hrs", 36e5 /* Hour */],
|
|
94
|
+
["h", 36e5 /* Hour */],
|
|
95
|
+
["day", 864e5 /* Day */],
|
|
96
|
+
["days", 864e5 /* Day */],
|
|
97
|
+
["d", 864e5 /* Day */],
|
|
98
|
+
["week", 6048e5 /* Week */],
|
|
99
|
+
["weeks", 6048e5 /* Week */],
|
|
100
|
+
["wk", 6048e5 /* Week */],
|
|
101
|
+
["wks", 6048e5 /* Week */],
|
|
102
|
+
["w", 6048e5 /* Week */],
|
|
103
|
+
["month", 2628e6 /* Month */],
|
|
104
|
+
["months", 2628e6 /* Month */],
|
|
105
|
+
["b", 2628e6 /* Month */],
|
|
106
|
+
["mo", 2628e6 /* Month */],
|
|
107
|
+
["year", 31536e6 /* Year */],
|
|
108
|
+
["years", 31536e6 /* Year */],
|
|
109
|
+
["yr", 31536e6 /* Year */],
|
|
110
|
+
["yrs", 31536e6 /* Year */],
|
|
111
|
+
["y", 31536e6 /* Year */]
|
|
112
|
+
]);
|
|
113
|
+
var mappings = /* @__PURE__ */ new Map([
|
|
114
|
+
[1e-6 /* Nanosecond */, "nanoseconds"],
|
|
115
|
+
[1e-3 /* Microsecond */, "microseconds"],
|
|
116
|
+
[1 /* Millisecond */, "milliseconds"],
|
|
117
|
+
[1e3 /* Second */, "seconds"],
|
|
118
|
+
[6e4 /* Minute */, "minutes"],
|
|
119
|
+
[36e5 /* Hour */, "hours"],
|
|
120
|
+
[864e5 /* Day */, "days"],
|
|
121
|
+
[6048e5 /* Week */, "weeks"],
|
|
122
|
+
[2628e6 /* Month */, "months"],
|
|
123
|
+
[31536e6 /* Year */, "years"]
|
|
105
124
|
]);
|
|
106
125
|
var _Duration = class {
|
|
126
|
+
/**
|
|
127
|
+
* Create a new Duration instance
|
|
128
|
+
* @param pattern The string to parse
|
|
129
|
+
*/
|
|
107
130
|
constructor(pattern) {
|
|
131
|
+
/**
|
|
132
|
+
* The offset
|
|
133
|
+
*/
|
|
108
134
|
__publicField(this, "offset");
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
135
|
+
/**
|
|
136
|
+
* The amount of nanoseconds extracted from the text.
|
|
137
|
+
*/
|
|
138
|
+
__publicField(this, "nanoseconds", 0);
|
|
139
|
+
/**
|
|
140
|
+
* The amount of microseconds extracted from the text.
|
|
141
|
+
*/
|
|
142
|
+
__publicField(this, "microseconds", 0);
|
|
143
|
+
/**
|
|
144
|
+
* The amount of milliseconds extracted from the text.
|
|
145
|
+
*/
|
|
146
|
+
__publicField(this, "milliseconds", 0);
|
|
147
|
+
/**
|
|
148
|
+
* The amount of seconds extracted from the text.
|
|
149
|
+
*/
|
|
150
|
+
__publicField(this, "seconds", 0);
|
|
151
|
+
/**
|
|
152
|
+
* The amount of minutes extracted from the text.
|
|
153
|
+
*/
|
|
154
|
+
__publicField(this, "minutes", 0);
|
|
155
|
+
/**
|
|
156
|
+
* The amount of hours extracted from the text.
|
|
157
|
+
*/
|
|
158
|
+
__publicField(this, "hours", 0);
|
|
159
|
+
/**
|
|
160
|
+
* The amount of days extracted from the text.
|
|
161
|
+
*/
|
|
162
|
+
__publicField(this, "days", 0);
|
|
163
|
+
/**
|
|
164
|
+
* The amount of weeks extracted from the text.
|
|
165
|
+
*/
|
|
166
|
+
__publicField(this, "weeks", 0);
|
|
167
|
+
/**
|
|
168
|
+
* The amount of months extracted from the text.
|
|
169
|
+
*/
|
|
170
|
+
__publicField(this, "months", 0);
|
|
171
|
+
/**
|
|
172
|
+
* The amount of years extracted from the text.
|
|
173
|
+
*/
|
|
174
|
+
__publicField(this, "years", 0);
|
|
118
175
|
let result = 0;
|
|
119
176
|
let valid = false;
|
|
120
|
-
pattern.replace(_Duration.
|
|
177
|
+
pattern.toLowerCase().replace(_Duration.commaRegex, "").replace(_Duration.aAndAnRegex, "1").replace(_Duration.patternRegex, (_, i, units) => {
|
|
121
178
|
const token = tokens.get(units);
|
|
122
179
|
if (token !== void 0) {
|
|
123
|
-
|
|
180
|
+
const n = Number(i);
|
|
181
|
+
result += n * token;
|
|
182
|
+
this[mappings.get(token)] += n;
|
|
124
183
|
valid = true;
|
|
125
184
|
}
|
|
126
185
|
return "";
|
|
127
186
|
});
|
|
128
|
-
|
|
187
|
+
this.offset = valid ? result : NaN;
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* Get the date from now
|
|
191
|
+
*/
|
|
192
|
+
get fromNow() {
|
|
193
|
+
return this.dateFrom(/* @__PURE__ */ new Date());
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Get the date from
|
|
197
|
+
* @param date The Date instance to get the date from
|
|
198
|
+
*/
|
|
199
|
+
dateFrom(date) {
|
|
200
|
+
return new Date(date.getTime() + this.offset);
|
|
129
201
|
}
|
|
130
202
|
};
|
|
131
203
|
var Duration = _Duration;
|
|
132
204
|
__name(Duration, "Duration");
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
205
|
+
/**
|
|
206
|
+
* The RegExp used for the pattern parsing
|
|
207
|
+
*/
|
|
208
|
+
__publicField(Duration, "patternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zΞΌ]*)/gi);
|
|
209
|
+
/**
|
|
210
|
+
* The RegExp used for removing commas
|
|
211
|
+
*/
|
|
212
|
+
__publicField(Duration, "commaRegex", /,/g);
|
|
213
|
+
/**
|
|
214
|
+
* The RegExp used for replacing a/an with 1
|
|
215
|
+
*/
|
|
216
|
+
__publicField(Duration, "aAndAnRegex", /\ban?\b/gi);
|
|
136
217
|
|
|
137
218
|
// src/lib/DurationFormatter.ts
|
|
138
219
|
var kTimeDurations = [
|
|
139
220
|
["year" /* Year */, 31536e6],
|
|
221
|
+
// 29.53059 days is the official duration of a month: https://en.wikipedia.org/wiki/Month
|
|
140
222
|
["month" /* Month */, 2628e6],
|
|
141
223
|
["week" /* Week */, 1e3 * 60 * 60 * 24 * 7],
|
|
142
224
|
["day" /* Day */, 1e3 * 60 * 60 * 24],
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/lib/constants.ts","../src/lib/Duration.ts","../src/lib/DurationFormatter.ts"],"names":["Time","TimeTypes"],"mappings":";;;;;;;;;AAEO,IAAK,OAAL,kBAAKA,UAAL;AACN,EAAAA,YAAA,iBAAc,KAAd;AACA,EAAAA,YAAA,YAAS,OAAT;AACA,EAAAA,YAAA,YAAS,OAAT;AACA,EAAAA,YAAA,UAAO,QAAP;AACA,EAAAA,YAAA,SAAM,SAAN;AACA,EAAAA,YAAA,WAAQ,UAAR;AACA,EAAAA,YAAA,UAAO,WAAP;AAPW,SAAAA;AAAA,GAAA;AAaL,IAAK,YAAL,kBAAKC,eAAL;AACN,EAAAA,WAAA,YAAS;AACT,EAAAA,WAAA,YAAS;AACT,EAAAA,WAAA,UAAO;AACP,EAAAA,WAAA,SAAM;AACN,EAAAA,WAAA,UAAO;AACP,EAAAA,WAAA,WAAQ;AACR,EAAAA,WAAA,UAAO;AAPI,SAAAA;AAAA,GAAA;AAUL,IAAM,gBAA0C;AAAA,EACtD,CAAC,oBAAiB;AAAA,IACjB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,sBAAkB;AAAA,IAClB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,oBAAiB;AAAA,IACjB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,kBAAgB;AAAA,IAChB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,oBAAiB;AAAA,IACjB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,wBAAmB;AAAA,IACnB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,wBAAmB;AAAA,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AAM,IAAI;AAChF,SAAO,GAAG,OAAO,YAAY,KAAK;AACnC;AAHS","sourcesContent":["import type { DurationFormatAssetsTime, DurationFormatSeparators } from './DurationFormatter';\n\nexport enum Time {\n\tMillisecond = 1,\n\tSecond = 1000,\n\tMinute = 1000 * 60,\n\tHour = 1000 * 60 * 60,\n\tDay = 1000 * 60 * 60 * 24,\n\tMonth = 1000 * 60 * 60 * 24 * (365 / 12),\n\tYear = 1000 * 60 * 60 * 24 * 365\n}\n\n/**\n * The supported time types\n */\nexport enum TimeTypes {\n\tSecond = 'second',\n\tMinute = 'minute',\n\tHour = 'hour',\n\tDay = 'day',\n\tWeek = 'week',\n\tMonth = 'month',\n\tYear = 'year'\n}\n\nexport const DEFAULT_UNITS: DurationFormatAssetsTime = {\n\t[TimeTypes.Year]: {\n\t\t1: 'year',\n\t\tDEFAULT: 'years'\n\t},\n\t[TimeTypes.Month]: {\n\t\t1: 'month',\n\t\tDEFAULT: 'months'\n\t},\n\t[TimeTypes.Week]: {\n\t\t1: 'week',\n\t\tDEFAULT: 'weeks'\n\t},\n\t[TimeTypes.Day]: {\n\t\t1: 'day',\n\t\tDEFAULT: 'days'\n\t},\n\t[TimeTypes.Hour]: {\n\t\t1: 'hour',\n\t\tDEFAULT: 'hours'\n\t},\n\t[TimeTypes.Minute]: {\n\t\t1: 'minute',\n\t\tDEFAULT: 'minutes'\n\t},\n\t[TimeTypes.Second]: {\n\t\t1: 'second',\n\t\tDEFAULT: 'seconds'\n\t}\n};\n\nexport const DEFAULT_SEPARATORS: DurationFormatSeparators = {\n\tleft: ' ',\n\tright: ' '\n};\n","const tokens = new Map([\n\t['nanosecond', 1 / 1e6],\n\t['nanoseconds', 1 / 1e6],\n\t['ns', 1 / 1e6],\n\n\t['millisecond', 1],\n\t['milliseconds', 1],\n\t['ms', 1],\n\n\t['second', 1000],\n\t['seconds', 1000],\n\t['sec', 1000],\n\t['secs', 1000],\n\t['s', 1000],\n\n\t['minute', 1000 * 60],\n\t['minutes', 1000 * 60],\n\t['min', 1000 * 60],\n\t['mins', 1000 * 60],\n\t['m', 1000 * 60],\n\n\t['hour', 1000 * 60 * 60],\n\t['hours', 1000 * 60 * 60],\n\t['hr', 1000 * 60 * 60],\n\t['hrs', 1000 * 60 * 60],\n\t['h', 1000 * 60 * 60],\n\n\t['day', 1000 * 60 * 60 * 24],\n\t['days', 1000 * 60 * 60 * 24],\n\t['d', 1000 * 60 * 60 * 24],\n\n\t['week', 1000 * 60 * 60 * 24 * 7],\n\t['weeks', 1000 * 60 * 60 * 24 * 7],\n\t['wk', 1000 * 60 * 60 * 24 * 7],\n\t['wks', 1000 * 60 * 60 * 24 * 7],\n\t['w', 1000 * 60 * 60 * 24 * 7],\n\n\t['month', 1000 * 60 * 60 * 24 * (365.25 / 12)],\n\t['months', 1000 * 60 * 60 * 24 * (365.25 / 12)],\n\t['b', 1000 * 60 * 60 * 24 * (365.25 / 12)],\n\t['mo', 1000 * 60 * 60 * 24 * (365.25 / 12)],\n\n\t['year', 1000 * 60 * 60 * 24 * 365.25],\n\t['years', 1000 * 60 * 60 * 24 * 365.25],\n\t['yr', 1000 * 60 * 60 * 24 * 365.25],\n\t['yrs', 1000 * 60 * 60 * 24 * 365.25],\n\t['y', 1000 * 60 * 60 * 24 * 365.25]\n]);\n\n/**\n * Converts duration strings into ms and future dates\n */\nexport class Duration {\n\t/**\n\t * The offset\n\t */\n\tpublic offset: number;\n\n\t/**\n\t * Create a new Duration instance\n\t * @param pattern The string to parse\n\t */\n\tpublic constructor(pattern: string) {\n\t\tthis.offset = Duration.parse(pattern.toLowerCase());\n\t}\n\n\t/**\n\t * Get the date from now\n\t */\n\tpublic get fromNow(): Date {\n\t\treturn this.dateFrom(new Date());\n\t}\n\n\t/**\n\t * Get the date from\n\t * @param date The Date instance to get the date from\n\t */\n\tpublic dateFrom(date: Date): Date {\n\t\treturn new Date(date.getTime() + this.offset);\n\t}\n\n\t/**\n\t * The RegExp used for the pattern parsing\n\t */\n\tprivate static readonly kPatternRegex = /(-?\\d*\\.?\\d+(?:e[-+]?\\d+)?)\\s*([a-zΞΌ]*)/gi;\n\n\t/**\n\t * The RegExp used for removing commas\n\t */\n\tprivate static readonly kCommaRegex = /,/g;\n\n\t/**\n\t * The RegExp used for replacing a/an with 1\n\t */\n\tprivate static readonly kAanRegex = /\\ban?\\b/gi;\n\n\t/**\n\t * Parse the pattern\n\t * @param pattern The pattern to parse\n\t */\n\tprivate static parse(pattern: string): number {\n\t\tlet result = 0;\n\t\tlet valid = false;\n\n\t\tpattern\n\t\t\t// ignore commas\n\t\t\t.replace(Duration.kCommaRegex, '')\n\t\t\t// a / an = 1\n\t\t\t.replace(Duration.kAanRegex, '1')\n\t\t\t// do math\n\t\t\t.replace(Duration.kPatternRegex, (_, i, units) => {\n\t\t\t\tconst token = tokens.get(units);\n\t\t\t\tif (token !== undefined) {\n\t\t\t\t\tresult += Number(i) * token;\n\t\t\t\t\tvalid = true;\n\t\t\t\t}\n\t\t\t\treturn '';\n\t\t\t});\n\n\t\treturn valid ? result : NaN;\n\t}\n}\n","import { DEFAULT_SEPARATORS, DEFAULT_UNITS, TimeTypes } from './constants';\n\n/**\n * The duration of each time type in milliseconds\n */\nconst kTimeDurations: readonly [TimeTypes, number][] = [\n\t[TimeTypes.Year, 31536000000],\n\t// 29.53059 days is the official duration of a month: https://en.wikipedia.org/wiki/Month\n\t[TimeTypes.Month, 2628000000],\n\t[TimeTypes.Week, 1000 * 60 * 60 * 24 * 7],\n\t[TimeTypes.Day, 1000 * 60 * 60 * 24],\n\t[TimeTypes.Hour, 1000 * 60 * 60],\n\t[TimeTypes.Minute, 1000 * 60],\n\t[TimeTypes.Second, 1000]\n];\n\n/**\n * Display the duration\n * @param duration The duration in milliseconds to parse and display\n * @param assets The language assets\n */\nexport class DurationFormatter {\n\tpublic constructor(public units: DurationFormatAssetsTime = DEFAULT_UNITS) {}\n\n\tpublic format(\n\t\tduration: number,\n\t\tprecision = 7,\n\t\t{\n\t\t\tleft: leftSeparator = DEFAULT_SEPARATORS.left,\n\t\t\tright: rightSeparator = DEFAULT_SEPARATORS.right\n\t\t}: DurationFormatSeparators = DEFAULT_SEPARATORS\n\t) {\n\t\tconst output: string[] = [];\n\t\tconst negative = duration < 0;\n\t\tif (negative) duration *= -1;\n\n\t\tfor (const [type, timeDuration] of kTimeDurations) {\n\t\t\tconst division = duration / timeDuration;\n\t\t\tif (division < 1) continue;\n\n\t\t\tconst floored = Math.floor(division);\n\t\t\tduration -= floored * timeDuration;\n\t\t\toutput.push(addUnit(floored, this.units[type], leftSeparator!));\n\n\t\t\t// If the output has enough precision, break\n\t\t\tif (output.length >= precision) break;\n\t\t}\n\n\t\treturn `${negative ? '-' : ''}${output.join(rightSeparator) || addUnit(0, this.units.second, leftSeparator!)}`;\n\t}\n}\n\n/**\n * Adds an unit, if non zero\n * @param time The duration of said unit\n * @param unit The unit language assets\n */\nfunction addUnit(time: number, unit: DurationFormatAssetsUnit, separator: string) {\n\tif (Reflect.has(unit, time)) return `${time}${separator}${Reflect.get(unit, time)}`;\n\treturn `${time}${separator}${unit.DEFAULT}`;\n}\n\nexport interface DurationFormatSeparators {\n\tleft?: string;\n\tright?: string;\n}\n\nexport interface DurationFormatAssetsUnit extends Record<number, string> {\n\tDEFAULT: string;\n}\n\nexport interface DurationFormatAssetsTime {\n\t[TimeTypes.Second]: DurationFormatAssetsUnit;\n\t[TimeTypes.Minute]: DurationFormatAssetsUnit;\n\t[TimeTypes.Hour]: DurationFormatAssetsUnit;\n\t[TimeTypes.Day]: DurationFormatAssetsUnit;\n\t[TimeTypes.Week]: DurationFormatAssetsUnit;\n\t[TimeTypes.Month]: DurationFormatAssetsUnit;\n\t[TimeTypes.Year]: DurationFormatAssetsUnit;\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../src/lib/constants.ts","../src/lib/Duration.ts","../src/lib/DurationFormatter.ts"],"names":["Time","TimeTypes"],"mappings":";;;;;;;;;AAEO,IAAK,OAAL,kBAAKA,UAAL;AACN,EAAA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WAAmB;AACjF,MAAI,QAAQ,IAAI,MAAM,IAAI;AAAG,WAAO,GAAG,OAAO,YAAY,QAAQ,IAAI,MAAM,IAAI;AAChF,SAAO,GAAG,OAAO,YAAY,KAAK;AACnC;AAHS","sourcesContent":["import type { DurationFormatAssetsTime, DurationFormatSeparators } from './DurationFormatter';\n\nexport enum Time {\n\tNanosecond = 1 / 1_000_000,\n\tMicrosecond = 1 / 1000,\n\tMillisecond = 1,\n\tSecond = 1000,\n\tMinute = Second * 60,\n\tHour = Minute * 60,\n\tDay = Hour * 24,\n\tWeek = Day * 7,\n\tMonth = Day * (365 / 12),\n\tYear = Day * 365\n}\n\n/**\n * The supported time types\n */\nexport enum TimeTypes {\n\tSecond = 'second',\n\tMinute = 'minute',\n\tHour = 'hour',\n\tDay = 'day',\n\tWeek = 'week',\n\tMonth = 'month',\n\tYear = 'year'\n}\n\nexport const DEFAULT_UNITS: DurationFormatAssetsTime = {\n\t[TimeTypes.Year]: {\n\t\t1: 'year',\n\t\tDEFAULT: 'years'\n\t},\n\t[TimeTypes.Month]: {\n\t\t1: 'month',\n\t\tDEFAULT: 'months'\n\t},\n\t[TimeTypes.Week]: {\n\t\t1: 'week',\n\t\tDEFAULT: 'weeks'\n\t},\n\t[TimeTypes.Day]: {\n\t\t1: 'day',\n\t\tDEFAULT: 'days'\n\t},\n\t[TimeTypes.Hour]: {\n\t\t1: 'hour',\n\t\tDEFAULT: 'hours'\n\t},\n\t[TimeTypes.Minute]: {\n\t\t1: 'minute',\n\t\tDEFAULT: 'minutes'\n\t},\n\t[TimeTypes.Second]: {\n\t\t1: 'second',\n\t\tDEFAULT: 'seconds'\n\t}\n};\n\nexport const DEFAULT_SEPARATORS: DurationFormatSeparators = {\n\tleft: ' ',\n\tright: ' '\n};\n","import { Time } from './constants';\n\nconst tokens = new Map([\n\t['nanosecond', Time.Nanosecond],\n\t['nanoseconds', Time.Nanosecond],\n\t['ns', Time.Nanosecond],\n\n\t['microsecond', Time.Microsecond],\n\t['microseconds', Time.Microsecond],\n\t['ΞΌs', Time.Microsecond],\n\t['us', Time.Microsecond],\n\n\t['millisecond', Time.Millisecond],\n\t['milliseconds', Time.Millisecond],\n\t['ms', Time.Millisecond],\n\n\t['second', Time.Second],\n\t['seconds', Time.Second],\n\t['sec', Time.Second],\n\t['secs', Time.Second],\n\t['s', Time.Second],\n\n\t['minute', Time.Minute],\n\t['minutes', Time.Minute],\n\t['min', Time.Minute],\n\t['mins', Time.Minute],\n\t['m', Time.Minute],\n\n\t['hour', Time.Hour],\n\t['hours', Time.Hour],\n\t['hr', Time.Hour],\n\t['hrs', Time.Hour],\n\t['h', Time.Hour],\n\n\t['day', Time.Day],\n\t['days', Time.Day],\n\t['d', Time.Day],\n\n\t['week', Time.Week],\n\t['weeks', Time.Week],\n\t['wk', Time.Week],\n\t['wks', Time.Week],\n\t['w', Time.Week],\n\n\t['month', Time.Month],\n\t['months', Time.Month],\n\t['b', Time.Month],\n\t['mo', Time.Month],\n\n\t['year', Time.Year],\n\t['years', Time.Year],\n\t['yr', Time.Year],\n\t['yrs', Time.Year],\n\t['y', Time.Year]\n]);\n\nconst mappings = new Map([\n\t[Time.Nanosecond, 'nanoseconds'],\n\t[Time.Microsecond, 'microseconds'],\n\t[Time.Millisecond, 'milliseconds'],\n\t[Time.Second, 'seconds'],\n\t[Time.Minute, 'minutes'],\n\t[Time.Hour, 'hours'],\n\t[Time.Day, 'days'],\n\t[Time.Week, 'weeks'],\n\t[Time.Month, 'months'],\n\t[Time.Year, 'years']\n] as const);\n\n/**\n * Converts duration strings into ms and future dates\n */\nexport class Duration {\n\t/**\n\t * The offset\n\t */\n\tpublic offset: number;\n\n\t/**\n\t * The amount of nanoseconds extracted from the text.\n\t */\n\tpublic nanoseconds = 0;\n\n\t/**\n\t * The amount of microseconds extracted from the text.\n\t */\n\tpublic microseconds = 0;\n\n\t/**\n\t * The amount of milliseconds extracted from the text.\n\t */\n\tpublic milliseconds = 0;\n\n\t/**\n\t * The amount of seconds extracted from the text.\n\t */\n\tpublic seconds = 0;\n\n\t/**\n\t * The amount of minutes extracted from the text.\n\t */\n\tpublic minutes = 0;\n\n\t/**\n\t * The amount of hours extracted from the text.\n\t */\n\tpublic hours = 0;\n\n\t/**\n\t * The amount of days extracted from the text.\n\t */\n\tpublic days = 0;\n\n\t/**\n\t * The amount of weeks extracted from the text.\n\t */\n\tpublic weeks = 0;\n\n\t/**\n\t * The amount of months extracted from the text.\n\t */\n\tpublic months = 0;\n\n\t/**\n\t * The amount of years extracted from the text.\n\t */\n\tpublic years = 0;\n\n\t/**\n\t * Create a new Duration instance\n\t * @param pattern The string to parse\n\t */\n\tpublic constructor(pattern: string) {\n\t\tlet result = 0;\n\t\tlet valid = false;\n\n\t\tpattern\n\t\t\t.toLowerCase()\n\t\t\t// ignore commas\n\t\t\t.replace(Duration.commaRegex, '')\n\t\t\t// a / an = 1\n\t\t\t.replace(Duration.aAndAnRegex, '1')\n\t\t\t// do math\n\t\t\t.replace(Duration.patternRegex, (_, i, units) => {\n\t\t\t\tconst token = tokens.get(units);\n\t\t\t\tif (token !== undefined) {\n\t\t\t\t\tconst n = Number(i);\n\t\t\t\t\tresult += n * token;\n\t\t\t\t\tthis[mappings.get(token)!] += n;\n\t\t\t\t\tvalid = true;\n\t\t\t\t}\n\t\t\t\treturn '';\n\t\t\t});\n\n\t\tthis.offset = valid ? result : NaN;\n\t}\n\n\t/**\n\t * Get the date from now\n\t */\n\tpublic get fromNow(): Date {\n\t\treturn this.dateFrom(new Date());\n\t}\n\n\t/**\n\t * Get the date from\n\t * @param date The Date instance to get the date from\n\t */\n\tpublic dateFrom(date: Date): Date {\n\t\treturn new Date(date.getTime() + this.offset);\n\t}\n\n\t/**\n\t * The RegExp used for the pattern parsing\n\t */\n\tprivate static readonly patternRegex = /(-?\\d*\\.?\\d+(?:e[-+]?\\d+)?)\\s*([a-zΞΌ]*)/gi;\n\n\t/**\n\t * The RegExp used for removing commas\n\t */\n\tprivate static readonly commaRegex = /,/g;\n\n\t/**\n\t * The RegExp used for replacing a/an with 1\n\t */\n\tprivate static readonly aAndAnRegex = /\\ban?\\b/gi;\n}\n","import { DEFAULT_SEPARATORS, DEFAULT_UNITS, TimeTypes } from './constants';\n\n/**\n * The duration of each time type in milliseconds\n */\nconst kTimeDurations: readonly [TimeTypes, number][] = [\n\t[TimeTypes.Year, 31536000000],\n\t// 29.53059 days is the official duration of a month: https://en.wikipedia.org/wiki/Month\n\t[TimeTypes.Month, 2628000000],\n\t[TimeTypes.Week, 1000 * 60 * 60 * 24 * 7],\n\t[TimeTypes.Day, 1000 * 60 * 60 * 24],\n\t[TimeTypes.Hour, 1000 * 60 * 60],\n\t[TimeTypes.Minute, 1000 * 60],\n\t[TimeTypes.Second, 1000]\n];\n\n/**\n * Display the duration\n * @param duration The duration in milliseconds to parse and display\n * @param assets The language assets\n */\nexport class DurationFormatter {\n\tpublic constructor(public units: DurationFormatAssetsTime = DEFAULT_UNITS) {}\n\n\tpublic format(\n\t\tduration: number,\n\t\tprecision = 7,\n\t\t{\n\t\t\tleft: leftSeparator = DEFAULT_SEPARATORS.left,\n\t\t\tright: rightSeparator = DEFAULT_SEPARATORS.right\n\t\t}: DurationFormatSeparators = DEFAULT_SEPARATORS\n\t) {\n\t\tconst output: string[] = [];\n\t\tconst negative = duration < 0;\n\t\tif (negative) duration *= -1;\n\n\t\tfor (const [type, timeDuration] of kTimeDurations) {\n\t\t\tconst division = duration / timeDuration;\n\t\t\tif (division < 1) continue;\n\n\t\t\tconst floored = Math.floor(division);\n\t\t\tduration -= floored * timeDuration;\n\t\t\toutput.push(addUnit(floored, this.units[type], leftSeparator!));\n\n\t\t\t// If the output has enough precision, break\n\t\t\tif (output.length >= precision) break;\n\t\t}\n\n\t\treturn `${negative ? '-' : ''}${output.join(rightSeparator) || addUnit(0, this.units.second, leftSeparator!)}`;\n\t}\n}\n\n/**\n * Adds an unit, if non zero\n * @param time The duration of said unit\n * @param unit The unit language assets\n */\nfunction addUnit(time: number, unit: DurationFormatAssetsUnit, separator: string) {\n\tif (Reflect.has(unit, time)) return `${time}${separator}${Reflect.get(unit, time)}`;\n\treturn `${time}${separator}${unit.DEFAULT}`;\n}\n\nexport interface DurationFormatSeparators {\n\tleft?: string;\n\tright?: string;\n}\n\nexport interface DurationFormatAssetsUnit extends Record<number, string> {\n\tDEFAULT: string;\n}\n\nexport interface DurationFormatAssetsTime {\n\t[TimeTypes.Second]: DurationFormatAssetsUnit;\n\t[TimeTypes.Minute]: DurationFormatAssetsUnit;\n\t[TimeTypes.Hour]: DurationFormatAssetsUnit;\n\t[TimeTypes.Day]: DurationFormatAssetsUnit;\n\t[TimeTypes.Week]: DurationFormatAssetsUnit;\n\t[TimeTypes.Month]: DurationFormatAssetsUnit;\n\t[TimeTypes.Year]: DurationFormatAssetsUnit;\n}\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sapphire/duration",
|
|
3
|
-
"version": "1.1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "A time duration utility library for JavaScript.",
|
|
5
5
|
"author": "@sapphire",
|
|
6
6
|
"license": "MIT",
|
|
@@ -57,12 +57,12 @@
|
|
|
57
57
|
"access": "public"
|
|
58
58
|
},
|
|
59
59
|
"devDependencies": {
|
|
60
|
-
"@favware/cliff-jumper": "^
|
|
61
|
-
"@vitest/coverage-c8": "^0.
|
|
62
|
-
"tsup": "^6.
|
|
63
|
-
"typedoc": "^0.23.
|
|
64
|
-
"typedoc-json-parser": "^7.0
|
|
65
|
-
"typescript": "^4.9.
|
|
66
|
-
"vitest": "^0.
|
|
60
|
+
"@favware/cliff-jumper": "^2.0.0",
|
|
61
|
+
"@vitest/coverage-c8": "^0.29.2",
|
|
62
|
+
"tsup": "^6.6.3",
|
|
63
|
+
"typedoc": "^0.23.26",
|
|
64
|
+
"typedoc-json-parser": "^7.2.0",
|
|
65
|
+
"typescript": "^4.9.5",
|
|
66
|
+
"vitest": "^0.29.2"
|
|
67
67
|
}
|
|
68
68
|
}
|