@sapphire/duration 1.2.0-pr-601.2f2c308a.0 → 1.2.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 CHANGED
@@ -2,6 +2,47 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file.
4
4
 
5
+ # [@sapphire/duration@1.2.0](https://github.com/sapphiredev/utilities/compare/@sapphire/duration@1.1.4...@sapphire/duration@1.2.0) - (2025-03-28)
6
+
7
+ ## 🐛 Bug Fixes
8
+
9
+ - **deps:** Update all non-major dependencies ([bc2f01c](https://github.com/sapphiredev/utilities/commit/bc2f01ce7d6932c9a0e02faa7cc081ecbae9640d)) ([#869](https://github.com/sapphiredev/utilities/pull/869) by @renovate[bot])
10
+
11
+ ## 🚀 Features
12
+
13
+ - **duration:** Add final separator to DurationFormatter ([75165d3](https://github.com/sapphiredev/utilities/commit/75165d3629194c4cd507fe11a6d7dc1c9f5e71c8)) ([#870](https://github.com/sapphiredev/utilities/pull/870) by @camdzic)
14
+
15
+ # [@sapphire/duration@1.1.3](https://github.com/sapphiredev/utilities/compare/@sapphire/duration@1.1.2...@sapphire/duration@1.1.3) - (2024-11-02)
16
+
17
+ ## 🐛 Bug Fixes
18
+
19
+ - Move browser imports ([100ffb0](https://github.com/sapphiredev/utilities/commit/100ffb0a2471bb9f74cc580d282d11059e1a0a68)) ([#826](https://github.com/sapphiredev/utilities/pull/826) by @kyranet)
20
+ - **deps:** Update all non-major dependencies ([083376a](https://github.com/sapphiredev/utilities/commit/083376aac55094dbeddb5194e8a8f0d794b8cceb)) ([#763](https://github.com/sapphiredev/utilities/pull/763) by @renovate[bot])
21
+ - **deps:** Update all non-major dependencies ([e7fdc5d](https://github.com/sapphiredev/utilities/commit/e7fdc5db3632a7f90292ef3978898da32730343a)) ([#752](https://github.com/sapphiredev/utilities/pull/752) by @renovate[bot])
22
+
23
+ ## 📝 Documentation
24
+
25
+ - **readme:** Fix readme table of contents ([c60e8bd](https://github.com/sapphiredev/utilities/commit/c60e8bd9fc89156cf973871597ec3f0ef0308c08))
26
+
27
+ # [@sapphire/duration@1.1.2](https://github.com/sapphiredev/utilities/compare/@sapphire/duration@1.1.2...@sapphire/duration@1.1.2) - (2024-01-19)
28
+
29
+ ## 🐛 Bug Fixes
30
+
31
+ - Fixed commonjs typings export mapping (#707) ([216ff02](https://github.com/sapphiredev/utilities/commit/216ff0260d63a9590357f9a5069f1ae2b34eaf5d))
32
+
33
+ # [@sapphire/duration@1.1.1](https://github.com/sapphiredev/utilities/compare/@sapphire/duration@1.1.1...@sapphire/duration@1.1.1) - (2023-12-04)
34
+
35
+ ## 🐛 Bug Fixes
36
+
37
+ - **duration:** Properly split CJS, ESM and IIFE ([f070fdc](https://github.com/sapphiredev/utilities/commit/f070fdc25313d090c8cdbb5313476222ed749a6d))
38
+ - Update export mapping for proper ESM/CJS split ([dd0cff8](https://github.com/sapphiredev/utilities/commit/dd0cff8e9b03a15812f25f7a1180501a92422629))
39
+ - **deps:** Update all non-major dependencies (#607) ([9cc8bd0](https://github.com/sapphiredev/utilities/commit/9cc8bd0d4b5d650deab2c913e6c3d713861bae28))
40
+ - **deps:** Update all non-major dependencies (#577) ([291dd67](https://github.com/sapphiredev/utilities/commit/291dd6783e57d8f075ce566218ba076ef6c4bbbd))
41
+
42
+ ## 🧪 Testing
43
+
44
+ - Update vitest to coverage v8 ([a4bc6e4](https://github.com/sapphiredev/utilities/commit/a4bc6e4f24ea60143a150ecc76fda6484f172ab9))
45
+
5
46
  # [@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
47
 
7
48
  ## 🏠 Refactor
package/README.md CHANGED
@@ -7,7 +7,6 @@
7
7
  **Duration utilities for JavaScript.**
8
8
 
9
9
  [![GitHub](https://img.shields.io/github/license/sapphiredev/utilities)](https://github.com/sapphiredev/utilities/blob/main/LICENSE.md)
10
- [![codecov](https://codecov.io/gh/sapphiredev/utilities/branch/main/graph/badge.svg?token=OEGIV6RFDO)](https://codecov.io/gh/sapphiredev/utilities)
11
10
  [![npm bundle size](https://img.shields.io/bundlephobia/min/@sapphire/duration?logo=webpack&style=flat-square)](https://bundlephobia.com/result?p=@sapphire/duration)
12
11
  [![npm](https://img.shields.io/npm/v/@sapphire/duration?color=crimson&logo=npm&style=flat-square)](https://www.npmjs.com/package/@sapphire/duration)
13
12
 
@@ -23,7 +22,7 @@
23
22
  - [Serializing a Duration](#serializing-a-duration)
24
23
  - [Localizing with Durations](#localizing-with-durations)
25
24
  - [Buy us some doughnuts](#buy-us-some-doughnuts)
26
- - [Contributors](#contributors-%E2%9C%A8)
25
+ - [Contributors](#contributors)
27
26
 
28
27
  ## Features
29
28
 
@@ -3,10 +3,7 @@
3
3
  var __defProp = Object.defineProperty;
4
4
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
5
5
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
6
- var __publicField = (obj, key, value) => {
7
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
8
- return value;
9
- };
6
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
10
7
 
11
8
  // src/lib/constants.ts
12
9
  var Time = /* @__PURE__ */ ((Time2) => {
@@ -64,7 +61,8 @@ var DEFAULT_UNITS = {
64
61
  };
65
62
  var DEFAULT_SEPARATORS = {
66
63
  left: " ",
67
- right: " "
64
+ right: " ",
65
+ final: " "
68
66
  };
69
67
 
70
68
  // src/lib/Duration.ts
@@ -124,7 +122,7 @@ var mappings = /* @__PURE__ */ new Map([
124
122
  [2628e6 /* Month */, "months"],
125
123
  [31536e6 /* Year */, "years"]
126
124
  ]);
127
- var _Duration = class {
125
+ var _Duration = class _Duration {
128
126
  /**
129
127
  * Create a new Duration instance
130
128
  * @param pattern The string to parse
@@ -202,20 +200,20 @@ var _Duration = class {
202
200
  return new Date(date.getTime() + this.offset);
203
201
  }
204
202
  };
205
- var Duration = _Duration;
206
- __name(Duration, "Duration");
203
+ __name(_Duration, "Duration");
207
204
  /**
208
205
  * The RegExp used for the pattern parsing
209
206
  */
210
- __publicField(Duration, "patternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zμ]*)/gi);
207
+ __publicField(_Duration, "patternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zμ]*)/gi);
211
208
  /**
212
209
  * The RegExp used for removing commas
213
210
  */
214
- __publicField(Duration, "commaRegex", /,/g);
211
+ __publicField(_Duration, "commaRegex", /,/g);
215
212
  /**
216
213
  * The RegExp used for replacing a/an with 1
217
214
  */
218
- __publicField(Duration, "aAndAnRegex", /\ban?\b/gi);
215
+ __publicField(_Duration, "aAndAnRegex", /\ban?\b/gi);
216
+ var Duration = _Duration;
219
217
 
220
218
  // src/lib/DurationFormatter.ts
221
219
  var kTimeDurations = [
@@ -228,35 +226,39 @@ var kTimeDurations = [
228
226
  ["minute" /* Minute */, 1e3 * 60],
229
227
  ["second" /* Second */, 1e3]
230
228
  ];
231
- var DurationFormatter = class {
229
+ var _DurationFormatter = class _DurationFormatter {
232
230
  constructor(units = DEFAULT_UNITS) {
233
231
  this.units = units;
234
232
  }
235
233
  format(duration, precision = 7, {
236
234
  left: leftSeparator = DEFAULT_SEPARATORS.left,
237
- right: rightSeparator = DEFAULT_SEPARATORS.right
235
+ right: rightSeparator = DEFAULT_SEPARATORS.right,
236
+ final: finalSeparator = DEFAULT_SEPARATORS.final
238
237
  } = DEFAULT_SEPARATORS) {
239
238
  const output = [];
240
239
  const negative = duration < 0;
241
- if (negative)
242
- duration *= -1;
240
+ if (negative) duration *= -1;
243
241
  for (const [type, timeDuration] of kTimeDurations) {
244
242
  const division = duration / timeDuration;
245
- if (division < 1)
246
- continue;
243
+ if (division < 1) continue;
247
244
  const floored = Math.floor(division);
248
245
  duration -= floored * timeDuration;
249
246
  output.push(addUnit(floored, this.units[type], leftSeparator));
250
- if (output.length >= precision)
251
- break;
247
+ if (output.length >= precision) break;
248
+ }
249
+ if (output.length === 0) return addUnit(0, this.units["second" /* Second */], leftSeparator);
250
+ if (negative) output[0] = `-${output[0]}`;
251
+ if (output.length > 1) {
252
+ const last = output.pop();
253
+ return `${output.join(rightSeparator)}${finalSeparator}${last}`;
252
254
  }
253
- return `${negative ? "-" : ""}${output.join(rightSeparator) || addUnit(0, this.units.second, leftSeparator)}`;
255
+ return output.join(rightSeparator);
254
256
  }
255
257
  };
256
- __name(DurationFormatter, "DurationFormatter");
258
+ __name(_DurationFormatter, "DurationFormatter");
259
+ var DurationFormatter = _DurationFormatter;
257
260
  function addUnit(time, unit, separator) {
258
- if (Reflect.has(unit, time))
259
- return `${time}${separator}${Reflect.get(unit, time)}`;
261
+ if (Reflect.has(unit, time)) return `${time}${separator}${Reflect.get(unit, time)}`;
260
262
  return `${time}${separator}${unit.DEFAULT}`;
261
263
  }
262
264
  __name(addUnit, "addUnit");
@@ -267,5 +269,5 @@ exports.Duration = Duration;
267
269
  exports.DurationFormatter = DurationFormatter;
268
270
  exports.Time = Time;
269
271
  exports.TimeTypes = TimeTypes;
270
- //# sourceMappingURL=out.js.map
271
- //# sourceMappingURL=index.js.map
272
+ //# sourceMappingURL=index.cjs.map
273
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/lib/constants.ts","../../src/lib/Duration.ts","../../src/lib/DurationFormatter.ts"],"names":["Time","TimeTypes"],"mappings":";;;;;;;;AAEY,IAAA,IAAA,qBAAAA,KAAL,KAAA;AACN,EAAAA,KAAAA,CAAAA,KAAAA,CAAA,gBAAa,IAAb,CAAA,GAAA,YAAA;AACA,EAAAA,KAAAA,CAAAA,KAAAA,CAAA,iBAAc,IAAd,CAAA,GAAA,aAAA;AACA,EAAAA,KAAAA,CAAAA,KAAAA,CAAA,iBAAc,CAAd,CAAA,GAAA,aAAA;AACA,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,CAAK,QAAQ,OAAS,EAAA,IAAA,CAAK,MAAM,IAAI,CAAA,EAAG,aAAc,CAAC,CAAA;AAG9D,MAAI,IAAA,MAAA,CAAO,UAAU,SAAW,EAAA;AAAA;AAGjC,IAAI,IAAA,MAAA,CAAO,WAAW,CAAG,EAAA,OAAO,QAAQ,CAAG,EAAA,IAAA,CAAK,KAAsB,CAAA,QAAA,cAAA,EAAG,aAAc,CAAA;AACvF,IAAA,IAAI,UAAiB,MAAA,CAAA,CAAC,IAAI,CAAI,CAAA,EAAA,MAAA,CAAO,CAAC,CAAC,CAAA,CAAA;AAEvC,IAAI,IAAA,MAAA,CAAO,SAAS,CAAG,EAAA;AACtB,MAAM,MAAA,IAAA,GAAO,OAAO,GAAI,EAAA;AACxB,MAAO,OAAA,CAAA,EAAG,OAAO,IAAK,CAAA,cAAe,CAAC,CAAG,EAAA,cAAc,GAAG,IAAI,CAAA,CAAA;AAAA;AAG/D,IAAO,OAAA,MAAA,CAAO,KAAK,cAAe,CAAA;AAAA;AAEpC,CAAA;AAtC+B,MAAA,CAAA,kBAAA,EAAA,mBAAA,CAAA;AAAxB,IAAM,iBAAN,GAAA;AA6CP,SAAS,OAAA,CAAQ,IAAc,EAAA,IAAA,EAAgC,SAAmB,EAAA;AACjF,EAAA,IAAI,OAAQ,CAAA,GAAA,CAAI,IAAM,EAAA,IAAI,GAAU,OAAA,CAAA,EAAG,IAAI,CAAA,EAAG,SAAS,CAAG,EAAA,OAAA,CAAQ,GAAI,CAAA,IAAA,EAAM,IAAI,CAAC,CAAA,CAAA;AACjF,EAAA,OAAO,GAAG,IAAI,CAAA,EAAG,SAAS,CAAA,EAAG,KAAK,OAAO,CAAA,CAAA;AAC1C;AAHS,MAAA,CAAA,OAAA,EAAA,SAAA,CAAA","file":"index.cjs","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\tfinal: ' '\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\tfinal: finalSeparator = DEFAULT_SEPARATORS.final\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\tif (output.length === 0) return addUnit(0, this.units[TimeTypes.Second], leftSeparator!);\n\t\tif (negative) output[0] = `-${output[0]}`;\n\n\t\tif (output.length > 1) {\n\t\t\tconst last = output.pop();\n\t\t\treturn `${output.join(rightSeparator!)}${finalSeparator}${last}`;\n\t\t}\n\n\t\treturn output.join(rightSeparator!);\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\tfinal?: 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"]}
@@ -6,11 +6,12 @@
6
6
  declare class DurationFormatter {
7
7
  units: DurationFormatAssetsTime;
8
8
  constructor(units?: DurationFormatAssetsTime);
9
- format(duration: number, precision?: number, { left: leftSeparator, right: rightSeparator }?: DurationFormatSeparators): string;
9
+ format(duration: number, precision?: number, { left: leftSeparator, right: rightSeparator, final: finalSeparator }?: DurationFormatSeparators): string;
10
10
  }
11
11
  interface DurationFormatSeparators {
12
12
  left?: string;
13
13
  right?: string;
14
+ final?: string;
14
15
  }
15
16
  interface DurationFormatAssetsUnit extends Record<number, string> {
16
17
  DEFAULT: string;
@@ -128,4 +129,4 @@ declare class Duration {
128
129
  private static readonly aAndAnRegex;
129
130
  }
130
131
 
131
- export { DEFAULT_SEPARATORS, DEFAULT_UNITS, Duration, DurationFormatAssetsTime, DurationFormatAssetsUnit, DurationFormatSeparators, DurationFormatter, Time, TimeTypes };
132
+ export { DEFAULT_SEPARATORS, DEFAULT_UNITS, Duration, type DurationFormatAssetsTime, type DurationFormatAssetsUnit, type DurationFormatSeparators, DurationFormatter, Time, TimeTypes };
@@ -0,0 +1,132 @@
1
+ /**
2
+ * Display the duration
3
+ * @param duration The duration in milliseconds to parse and display
4
+ * @param assets The language assets
5
+ */
6
+ declare class DurationFormatter {
7
+ units: DurationFormatAssetsTime;
8
+ constructor(units?: DurationFormatAssetsTime);
9
+ format(duration: number, precision?: number, { left: leftSeparator, right: rightSeparator, final: finalSeparator }?: DurationFormatSeparators): string;
10
+ }
11
+ interface DurationFormatSeparators {
12
+ left?: string;
13
+ right?: string;
14
+ final?: string;
15
+ }
16
+ interface DurationFormatAssetsUnit extends Record<number, string> {
17
+ DEFAULT: string;
18
+ }
19
+ interface DurationFormatAssetsTime {
20
+ [TimeTypes.Second]: DurationFormatAssetsUnit;
21
+ [TimeTypes.Minute]: DurationFormatAssetsUnit;
22
+ [TimeTypes.Hour]: DurationFormatAssetsUnit;
23
+ [TimeTypes.Day]: DurationFormatAssetsUnit;
24
+ [TimeTypes.Week]: DurationFormatAssetsUnit;
25
+ [TimeTypes.Month]: DurationFormatAssetsUnit;
26
+ [TimeTypes.Year]: DurationFormatAssetsUnit;
27
+ }
28
+
29
+ declare enum Time {
30
+ Nanosecond = 0.000001,
31
+ Microsecond = 0.001,
32
+ Millisecond = 1,
33
+ Second = 1000,
34
+ Minute = 60000,
35
+ Hour = 3600000,
36
+ Day = 86400000,
37
+ Week = 604800000,
38
+ Month = 2628000000,
39
+ Year = 31536000000
40
+ }
41
+ /**
42
+ * The supported time types
43
+ */
44
+ declare enum TimeTypes {
45
+ Second = "second",
46
+ Minute = "minute",
47
+ Hour = "hour",
48
+ Day = "day",
49
+ Week = "week",
50
+ Month = "month",
51
+ Year = "year"
52
+ }
53
+ declare const DEFAULT_UNITS: DurationFormatAssetsTime;
54
+ declare const DEFAULT_SEPARATORS: DurationFormatSeparators;
55
+
56
+ /**
57
+ * Converts duration strings into ms and future dates
58
+ */
59
+ declare class Duration {
60
+ /**
61
+ * The offset
62
+ */
63
+ offset: number;
64
+ /**
65
+ * The amount of nanoseconds extracted from the text.
66
+ */
67
+ nanoseconds: number;
68
+ /**
69
+ * The amount of microseconds extracted from the text.
70
+ */
71
+ microseconds: number;
72
+ /**
73
+ * The amount of milliseconds extracted from the text.
74
+ */
75
+ milliseconds: number;
76
+ /**
77
+ * The amount of seconds extracted from the text.
78
+ */
79
+ seconds: number;
80
+ /**
81
+ * The amount of minutes extracted from the text.
82
+ */
83
+ minutes: number;
84
+ /**
85
+ * The amount of hours extracted from the text.
86
+ */
87
+ hours: number;
88
+ /**
89
+ * The amount of days extracted from the text.
90
+ */
91
+ days: number;
92
+ /**
93
+ * The amount of weeks extracted from the text.
94
+ */
95
+ weeks: number;
96
+ /**
97
+ * The amount of months extracted from the text.
98
+ */
99
+ months: number;
100
+ /**
101
+ * The amount of years extracted from the text.
102
+ */
103
+ years: number;
104
+ /**
105
+ * Create a new Duration instance
106
+ * @param pattern The string to parse
107
+ */
108
+ constructor(pattern: string);
109
+ /**
110
+ * Get the date from now
111
+ */
112
+ get fromNow(): Date;
113
+ /**
114
+ * Get the date from
115
+ * @param date The Date instance to get the date from
116
+ */
117
+ dateFrom(date: Date): Date;
118
+ /**
119
+ * The RegExp used for the pattern parsing
120
+ */
121
+ private static readonly patternRegex;
122
+ /**
123
+ * The RegExp used for removing commas
124
+ */
125
+ private static readonly commaRegex;
126
+ /**
127
+ * The RegExp used for replacing a/an with 1
128
+ */
129
+ private static readonly aAndAnRegex;
130
+ }
131
+
132
+ export { DEFAULT_SEPARATORS, DEFAULT_UNITS, Duration, type DurationFormatAssetsTime, type DurationFormatAssetsUnit, type DurationFormatSeparators, DurationFormatter, Time, TimeTypes };
@@ -1,10 +1,7 @@
1
1
  var __defProp = Object.defineProperty;
2
2
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
3
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
4
- var __publicField = (obj, key, value) => {
5
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
6
- return value;
7
- };
4
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
8
5
 
9
6
  // src/lib/constants.ts
10
7
  var Time = /* @__PURE__ */ ((Time2) => {
@@ -62,7 +59,8 @@ var DEFAULT_UNITS = {
62
59
  };
63
60
  var DEFAULT_SEPARATORS = {
64
61
  left: " ",
65
- right: " "
62
+ right: " ",
63
+ final: " "
66
64
  };
67
65
 
68
66
  // src/lib/Duration.ts
@@ -122,7 +120,7 @@ var mappings = /* @__PURE__ */ new Map([
122
120
  [2628e6 /* Month */, "months"],
123
121
  [31536e6 /* Year */, "years"]
124
122
  ]);
125
- var _Duration = class {
123
+ var _Duration = class _Duration {
126
124
  /**
127
125
  * Create a new Duration instance
128
126
  * @param pattern The string to parse
@@ -200,20 +198,20 @@ var _Duration = class {
200
198
  return new Date(date.getTime() + this.offset);
201
199
  }
202
200
  };
203
- var Duration = _Duration;
204
- __name(Duration, "Duration");
201
+ __name(_Duration, "Duration");
205
202
  /**
206
203
  * The RegExp used for the pattern parsing
207
204
  */
208
- __publicField(Duration, "patternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zμ]*)/gi);
205
+ __publicField(_Duration, "patternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zμ]*)/gi);
209
206
  /**
210
207
  * The RegExp used for removing commas
211
208
  */
212
- __publicField(Duration, "commaRegex", /,/g);
209
+ __publicField(_Duration, "commaRegex", /,/g);
213
210
  /**
214
211
  * The RegExp used for replacing a/an with 1
215
212
  */
216
- __publicField(Duration, "aAndAnRegex", /\ban?\b/gi);
213
+ __publicField(_Duration, "aAndAnRegex", /\ban?\b/gi);
214
+ var Duration = _Duration;
217
215
 
218
216
  // src/lib/DurationFormatter.ts
219
217
  var kTimeDurations = [
@@ -226,39 +224,43 @@ var kTimeDurations = [
226
224
  ["minute" /* Minute */, 1e3 * 60],
227
225
  ["second" /* Second */, 1e3]
228
226
  ];
229
- var DurationFormatter = class {
227
+ var _DurationFormatter = class _DurationFormatter {
230
228
  constructor(units = DEFAULT_UNITS) {
231
229
  this.units = units;
232
230
  }
233
231
  format(duration, precision = 7, {
234
232
  left: leftSeparator = DEFAULT_SEPARATORS.left,
235
- right: rightSeparator = DEFAULT_SEPARATORS.right
233
+ right: rightSeparator = DEFAULT_SEPARATORS.right,
234
+ final: finalSeparator = DEFAULT_SEPARATORS.final
236
235
  } = DEFAULT_SEPARATORS) {
237
236
  const output = [];
238
237
  const negative = duration < 0;
239
- if (negative)
240
- duration *= -1;
238
+ if (negative) duration *= -1;
241
239
  for (const [type, timeDuration] of kTimeDurations) {
242
240
  const division = duration / timeDuration;
243
- if (division < 1)
244
- continue;
241
+ if (division < 1) continue;
245
242
  const floored = Math.floor(division);
246
243
  duration -= floored * timeDuration;
247
244
  output.push(addUnit(floored, this.units[type], leftSeparator));
248
- if (output.length >= precision)
249
- break;
245
+ if (output.length >= precision) break;
246
+ }
247
+ if (output.length === 0) return addUnit(0, this.units["second" /* Second */], leftSeparator);
248
+ if (negative) output[0] = `-${output[0]}`;
249
+ if (output.length > 1) {
250
+ const last = output.pop();
251
+ return `${output.join(rightSeparator)}${finalSeparator}${last}`;
250
252
  }
251
- return `${negative ? "-" : ""}${output.join(rightSeparator) || addUnit(0, this.units.second, leftSeparator)}`;
253
+ return output.join(rightSeparator);
252
254
  }
253
255
  };
254
- __name(DurationFormatter, "DurationFormatter");
256
+ __name(_DurationFormatter, "DurationFormatter");
257
+ var DurationFormatter = _DurationFormatter;
255
258
  function addUnit(time, unit, separator) {
256
- if (Reflect.has(unit, time))
257
- return `${time}${separator}${Reflect.get(unit, time)}`;
259
+ if (Reflect.has(unit, time)) return `${time}${separator}${Reflect.get(unit, time)}`;
258
260
  return `${time}${separator}${unit.DEFAULT}`;
259
261
  }
260
262
  __name(addUnit, "addUnit");
261
263
 
262
264
  export { DEFAULT_SEPARATORS, DEFAULT_UNITS, Duration, DurationFormatter, Time, TimeTypes };
263
- //# sourceMappingURL=out.js.map
265
+ //# sourceMappingURL=index.mjs.map
264
266
  //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/lib/constants.ts","../../src/lib/Duration.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AAI,CAAA,IAAA,EAAM,IAAI,CAAC,CAAA,CAAA;AACjF,EAAA,OAAO,GAAG,IAAI,CAAA,EAAG,SAAS,CAAA,EAAG,KAAK,OAAO,CAAA,CAAA;AAC1C;AAHS,MAAA,CAAA,OAAA,EAAA,SAAA,CAAA","file":"index.mjs","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\tfinal: ' '\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\tfinal: finalSeparator = DEFAULT_SEPARATORS.final\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\tif (output.length === 0) return addUnit(0, this.units[TimeTypes.Second], leftSeparator!);\n\t\tif (negative) output[0] = `-${output[0]}`;\n\n\t\tif (output.length > 1) {\n\t\t\tconst last = output.pop();\n\t\t\treturn `${output.join(rightSeparator!)}${finalSeparator}${last}`;\n\t\t}\n\n\t\treturn output.join(rightSeparator!);\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\tfinal?: 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"]}
@@ -4,10 +4,7 @@ var SapphireDuration = (function (exports) {
4
4
  var __defProp = Object.defineProperty;
5
5
  var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
6
6
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
7
- var __publicField = (obj, key, value) => {
8
- __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
9
- return value;
10
- };
7
+ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
11
8
 
12
9
  // src/lib/constants.ts
13
10
  var Time = /* @__PURE__ */ ((Time2) => {
@@ -65,7 +62,8 @@ var SapphireDuration = (function (exports) {
65
62
  };
66
63
  var DEFAULT_SEPARATORS = {
67
64
  left: " ",
68
- right: " "
65
+ right: " ",
66
+ final: " "
69
67
  };
70
68
 
71
69
  // src/lib/Duration.ts
@@ -125,7 +123,7 @@ var SapphireDuration = (function (exports) {
125
123
  [2628e6 /* Month */, "months"],
126
124
  [31536e6 /* Year */, "years"]
127
125
  ]);
128
- var _Duration = class {
126
+ var _Duration = class _Duration {
129
127
  /**
130
128
  * Create a new Duration instance
131
129
  * @param pattern The string to parse
@@ -203,20 +201,20 @@ var SapphireDuration = (function (exports) {
203
201
  return new Date(date.getTime() + this.offset);
204
202
  }
205
203
  };
206
- var Duration = _Duration;
207
- __name(Duration, "Duration");
204
+ __name(_Duration, "Duration");
208
205
  /**
209
206
  * The RegExp used for the pattern parsing
210
207
  */
211
- __publicField(Duration, "patternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zμ]*)/gi);
208
+ __publicField(_Duration, "patternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zμ]*)/gi);
212
209
  /**
213
210
  * The RegExp used for removing commas
214
211
  */
215
- __publicField(Duration, "commaRegex", /,/g);
212
+ __publicField(_Duration, "commaRegex", /,/g);
216
213
  /**
217
214
  * The RegExp used for replacing a/an with 1
218
215
  */
219
- __publicField(Duration, "aAndAnRegex", /\ban?\b/gi);
216
+ __publicField(_Duration, "aAndAnRegex", /\ban?\b/gi);
217
+ var Duration = _Duration;
220
218
 
221
219
  // src/lib/DurationFormatter.ts
222
220
  var kTimeDurations = [
@@ -229,35 +227,39 @@ var SapphireDuration = (function (exports) {
229
227
  ["minute" /* Minute */, 1e3 * 60],
230
228
  ["second" /* Second */, 1e3]
231
229
  ];
232
- var DurationFormatter = class {
230
+ var _DurationFormatter = class _DurationFormatter {
233
231
  constructor(units = DEFAULT_UNITS) {
234
232
  this.units = units;
235
233
  }
236
234
  format(duration, precision = 7, {
237
235
  left: leftSeparator = DEFAULT_SEPARATORS.left,
238
- right: rightSeparator = DEFAULT_SEPARATORS.right
236
+ right: rightSeparator = DEFAULT_SEPARATORS.right,
237
+ final: finalSeparator = DEFAULT_SEPARATORS.final
239
238
  } = DEFAULT_SEPARATORS) {
240
239
  const output = [];
241
240
  const negative = duration < 0;
242
- if (negative)
243
- duration *= -1;
241
+ if (negative) duration *= -1;
244
242
  for (const [type, timeDuration] of kTimeDurations) {
245
243
  const division = duration / timeDuration;
246
- if (division < 1)
247
- continue;
244
+ if (division < 1) continue;
248
245
  const floored = Math.floor(division);
249
246
  duration -= floored * timeDuration;
250
247
  output.push(addUnit(floored, this.units[type], leftSeparator));
251
- if (output.length >= precision)
252
- break;
248
+ if (output.length >= precision) break;
249
+ }
250
+ if (output.length === 0) return addUnit(0, this.units["second" /* Second */], leftSeparator);
251
+ if (negative) output[0] = `-${output[0]}`;
252
+ if (output.length > 1) {
253
+ const last = output.pop();
254
+ return `${output.join(rightSeparator)}${finalSeparator}${last}`;
253
255
  }
254
- return `${negative ? "-" : ""}${output.join(rightSeparator) || addUnit(0, this.units.second, leftSeparator)}`;
256
+ return output.join(rightSeparator);
255
257
  }
256
258
  };
257
- __name(DurationFormatter, "DurationFormatter");
259
+ __name(_DurationFormatter, "DurationFormatter");
260
+ var DurationFormatter = _DurationFormatter;
258
261
  function addUnit(time, unit, separator) {
259
- if (Reflect.has(unit, time))
260
- return `${time}${separator}${Reflect.get(unit, time)}`;
262
+ if (Reflect.has(unit, time)) return `${time}${separator}${Reflect.get(unit, time)}`;
261
263
  return `${time}${separator}${unit.DEFAULT}`;
262
264
  }
263
265
  __name(addUnit, "addUnit");
@@ -272,5 +274,5 @@ var SapphireDuration = (function (exports) {
272
274
  return exports;
273
275
 
274
276
  })({});
275
- //# sourceMappingURL=out.js.map
277
+ //# sourceMappingURL=index.global.js.map
276
278
  //# sourceMappingURL=index.global.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../../src/lib/constants.ts","../../src/lib/Duration.ts","../../src/lib/DurationFormatter.ts"],"names":["Time","TimeTypes"],"mappings":";;;;;;;;;AAEY,MAAA,IAA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AI,CAAA,EAAG,SAAS,CAAA,EAAG,KAAK,OAAO,CAAA,CAAA;EAC1C;EAHS,MAAA,CAAA,OAAA,EAAA,SAAA,CAAA","file":"index.global.js","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\tfinal: ' '\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\tfinal: finalSeparator = DEFAULT_SEPARATORS.final\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\tif (output.length === 0) return addUnit(0, this.units[TimeTypes.Second], leftSeparator!);\n\t\tif (negative) output[0] = `-${output[0]}`;\n\n\t\tif (output.length > 1) {\n\t\t\tconst last = output.pop();\n\t\t\treturn `${output.join(rightSeparator!)}${finalSeparator}${last}`;\n\t\t}\n\n\t\treturn output.join(rightSeparator!);\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\tfinal?: 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,29 +1,37 @@
1
1
  {
2
2
  "name": "@sapphire/duration",
3
- "version": "1.2.0-pr-601.2f2c308a.0",
3
+ "version": "1.2.0",
4
4
  "description": "A time duration utility library for JavaScript.",
5
5
  "author": "@sapphire",
6
6
  "license": "MIT",
7
- "main": "dist/index.js",
8
- "module": "dist/index.mjs",
9
- "browser": "dist/index.global.js",
10
- "unpkg": "dist/index.global.js",
11
- "types": "dist/index.d.ts",
7
+ "main": "dist/cjs/index.cjs",
8
+ "module": "dist/esm/index.mjs",
9
+ "browser": "dist/iife/index.global.js",
10
+ "unpkg": "dist/iife/index.global.js",
11
+ "types": "dist/cjs/index.d.cts",
12
12
  "exports": {
13
- "import": "./dist/index.mjs",
14
- "require": "./dist/index.js",
15
- "types": "./dist/index.d.ts"
13
+ "import": {
14
+ "types": "./dist/esm/index.d.mts",
15
+ "default": "./dist/esm/index.mjs"
16
+ },
17
+ "require": {
18
+ "types": "./dist/cjs/index.d.cts",
19
+ "default": "./dist/cjs/index.cjs"
20
+ },
21
+ "browser": "./dist/iife/index.global.js"
16
22
  },
17
23
  "sideEffects": false,
18
24
  "homepage": "https://github.com/sapphiredev/utilities/tree/main/packages/duration",
19
25
  "scripts": {
20
26
  "test": "vitest run",
21
27
  "lint": "eslint src tests --ext ts --fix -c ../../.eslintrc",
22
- "build": "tsup",
28
+ "build": "yarn gen-index && tsup && yarn build:rename-cjs-index",
29
+ "build:rename-cjs-index": "tsx ../../scripts/rename-cjs-index.cts",
23
30
  "docs": "typedoc-json-parser",
24
31
  "prepack": "yarn build",
25
32
  "bump": "cliff-jumper",
26
- "check-update": "cliff-jumper --dry-run"
33
+ "check-update": "cliff-jumper --dry-run",
34
+ "gen-index": "tsx ../../scripts/gen-index.cts duration --write"
27
35
  },
28
36
  "repository": {
29
37
  "type": "git",
@@ -31,9 +39,7 @@
31
39
  "directory": "packages/duration"
32
40
  },
33
41
  "files": [
34
- "dist/**/*.js*",
35
- "dist/**/*.mjs*",
36
- "dist/**/*.d*"
42
+ "dist/"
37
43
  ],
38
44
  "engines": {
39
45
  "node": ">=v14.0.0",
@@ -57,12 +63,13 @@
57
63
  "access": "public"
58
64
  },
59
65
  "devDependencies": {
60
- "@favware/cliff-jumper": "^2.0.1",
61
- "@vitest/coverage-c8": "^0.31.1",
62
- "tsup": "^6.7.0",
63
- "typedoc": "^0.24.7",
64
- "typedoc-json-parser": "^8.1.2",
65
- "typescript": "^5.0.4",
66
- "vitest": "^0.31.1"
66
+ "@favware/cliff-jumper": "^6.0.0",
67
+ "@vitest/coverage-v8": "^3.0.9",
68
+ "tsup": "^8.4.0",
69
+ "tsx": "^4.19.3",
70
+ "typedoc": "^0.26.11",
71
+ "typedoc-json-parser": "^10.2.0",
72
+ "typescript": "~5.4.5",
73
+ "vitest": "^3.0.9"
67
74
  }
68
75
  }
@@ -1 +0,0 @@
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,YAAS,OAAT;AACA,EAAAA,YAAA,UAAO,QAAP;AACA,EAAAA,YAAA,SAAM,SAAN;AACA,EAAAA,YAAA,UAAO,UAAP;AACA,EAAAA,YAAA,WAAQ,UAAR;AACA,EAAAA,YAAA,UAAO,WAAP;AAVW,SAAAA;AAAA,GAAA;AAgBL,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,iBAAc,GAAG;AAAA,IACjB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,mBAAe,GAAG;AAAA,IAClB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,iBAAc,GAAG;AAAA,IACjB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,eAAa,GAAG;AAAA,IAChB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,iBAAc,GAAG;AAAA,IACjB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,qBAAgB,GAAG;AAAA,IACnB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,qBAAgB,GAAG;AAAA,IACnB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAC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AK,OAAO,KAAK,cAAc,KAAK,QAAQ,GAAG,KAAK,MAAM,QAAQ,aAAc;AAAA,EAC5G;AACD;AA7Ba;AAoCb,SAAS,QAAQ,MAAc,MAAgC,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/dist/index.js.map DELETED
@@ -1 +0,0 @@
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,YAAS,OAAT;AACA,EAAAA,YAAA,UAAO,QAAP;AACA,EAAAA,YAAA,SAAM,SAAN;AACA,EAAAA,YAAA,UAAO,UAAP;AACA,EAAAA,YAAA,WAAQ,UAAR;AACA,EAAAA,YAAA,UAAO,WAAP;AAVW,SAAAA;AAAA,GAAA;AAgBL,IAAK,YAAL,kBAAKC,eAAL;AACN,EAAAA,WAAA,YAAS;AACT,EAAAA,WAAA,YAAS;AACT,EAAAA,WAAA,UAAO;AACP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oBAAiB,MAAO,KAAK,EAAE;AAAA,EAC/B,wBAAmB,MAAO,EAAE;AAAA,EAC5B,wBAAmB,GAAI;AACxB;AAOO,IAAM,oBAAN,MAAwB;AAAA,EACvB,YAAmB,QAAkC,eAAe;AAAjD;AAAA,EAAkD;AAAA,EAErE,OACN,UACA,YAAY,GACZ;AAAA,IACC,MAAM,gBAAgB,mBAAmB;AAAA,IACzC,OAAO,iBAAiB,mBAAmB;AAAA,EAC5C,IAA8B,oBAC7B;AACD,UAAM,SAAmB,CAAC;AAC1B,UAAM,WAAW,WAAW;AAC5B,QAAI;AAAU,kBAAY;AAE1B,eAAW,CAAC,MAAM,YAAY,KAAK,gBAAgB;AAClD,YAAM,WAAW,WAAW;AAC5B,UAAI,WAAW;AAAG;AAElB,YAAM,UAAU,KAAK,MAAM,QAAQ;AACnC,kBAAY,UAAU;AACtB,aAAO,KAAK,QAAQ,SAAS,KAAK,MAAM,IAAI,GAAG,aAAc,CAAC;AAG9D,UAAI,OAAO,UAAU;AAAW;AAAA,IACjC;AAEA,WAAO,GAAG,WAAW,MAAM,KAAK,OAAO,KAAK,cAAc,KAAK,QAAQ,GAAG,KAAK,MAAM,QAAQ,aAAc;AAAA,EAC5G;AACD;AA7Ba;AAoCb,SAAS,QAAQ,MAAc,MAAgC,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"]}
@@ -1 +0,0 @@
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,YAAS,OAAT;AACA,EAAAA,YAAA,UAAO,QAAP;AACA,EAAAA,YAAA,SAAM,SAAN;AACA,EAAAA,YAAA,UAAO,UAAP;AACA,EAAAA,YAAA,WAAQ,UAAR;AACA,EAAAA,YAAA,UAAO,WAAP;AAVW,SAAAA;AAAA,GAAA;AAgBL,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,iBAAc,GAAG;AAAA,IACjB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,mBAAe,GAAG;AAAA,IAClB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,iBAAc,GAAG;AAAA,IACjB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,eAAa,GAAG;AAAA,IAChB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,iBAAc,GAAG;AAAA,IACjB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,qBAAgB,GAAG;AAAA,IACnB,GAAG;AAAA,I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IAAI,GAAG,aAAc,CAAC;AAG9D,UAAI,OAAO,UAAU;AAAW;AAAA,IACjC;AAEA,WAAO,GAAG,WAAW,MAAM,KAAK,OAAO,KAAK,cAAc,KAAK,QAAQ,GAAG,KAAK,MAAM,QAAQ,aAAc;AAAA,EAC5G;AACD;AA7Ba;AAoCb,SAAS,QAAQ,MAAc,MAAgC,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"]}