@sapphire/duration 1.1.0-next.e7e9997.0 → 1.1.0-next.eac39af.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/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 kPatternRegex;
120
+ private static readonly patternRegex;
78
121
  /**
79
122
  * The RegExp used for removing commas
80
123
  */
81
- private static readonly kCommaRegex;
124
+ private static readonly commaRegex;
82
125
  /**
83
126
  * The RegExp used for replacing a/an with 1
84
127
  */
85
- private static readonly kAanRegex;
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 };
@@ -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,44 +70,60 @@ var SapphireDuration = (function (exports) {
67
70
 
68
71
  // src/lib/Duration.ts
69
72
  var tokens = /* @__PURE__ */ new Map([
70
- ["nanosecond", 1 / 1e6],
71
- ["nanoseconds", 1 / 1e6],
72
- ["ns", 1 / 1e6],
73
- ["millisecond", 1],
74
- ["milliseconds", 1],
75
- ["ms", 1],
76
- ["second", 1e3],
77
- ["seconds", 1e3],
78
- ["sec", 1e3],
79
- ["secs", 1e3],
80
- ["s", 1e3],
81
- ["minute", 1e3 * 60],
82
- ["minutes", 1e3 * 60],
83
- ["min", 1e3 * 60],
84
- ["mins", 1e3 * 60],
85
- ["m", 1e3 * 60],
86
- ["hour", 1e3 * 60 * 60],
87
- ["hours", 1e3 * 60 * 60],
88
- ["hr", 1e3 * 60 * 60],
89
- ["hrs", 1e3 * 60 * 60],
90
- ["h", 1e3 * 60 * 60],
91
- ["day", 1e3 * 60 * 60 * 24],
92
- ["days", 1e3 * 60 * 60 * 24],
93
- ["d", 1e3 * 60 * 60 * 24],
94
- ["week", 1e3 * 60 * 60 * 24 * 7],
95
- ["weeks", 1e3 * 60 * 60 * 24 * 7],
96
- ["wk", 1e3 * 60 * 60 * 24 * 7],
97
- ["wks", 1e3 * 60 * 60 * 24 * 7],
98
- ["w", 1e3 * 60 * 60 * 24 * 7],
99
- ["month", 1e3 * 60 * 60 * 24 * (365.25 / 12)],
100
- ["months", 1e3 * 60 * 60 * 24 * (365.25 / 12)],
101
- ["b", 1e3 * 60 * 60 * 24 * (365.25 / 12)],
102
- ["mo", 1e3 * 60 * 60 * 24 * (365.25 / 12)],
103
- ["year", 1e3 * 60 * 60 * 24 * 365.25],
104
- ["years", 1e3 * 60 * 60 * 24 * 365.25],
105
- ["yr", 1e3 * 60 * 60 * 24 * 365.25],
106
- ["yrs", 1e3 * 60 * 60 * 24 * 365.25],
107
- ["y", 1e3 * 60 * 60 * 24 * 365.25]
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 {
110
129
  /**
@@ -116,7 +135,59 @@ var SapphireDuration = (function (exports) {
116
135
  * The offset
117
136
  */
118
137
  __publicField(this, "offset");
119
- this.offset = _Duration.parse(pattern.toLowerCase());
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);
178
+ let result = 0;
179
+ let valid = false;
180
+ pattern.toLowerCase().replace(_Duration.commaRegex, "").replace(_Duration.aAndAnRegex, "1").replace(_Duration.patternRegex, (_, i, units) => {
181
+ const token = tokens.get(units);
182
+ if (token !== void 0) {
183
+ const n = Number(i);
184
+ result += n * token;
185
+ this[mappings.get(token)] += n;
186
+ valid = true;
187
+ }
188
+ return "";
189
+ });
190
+ this.offset = valid ? result : NaN;
120
191
  }
121
192
  /**
122
193
  * Get the date from now
@@ -131,38 +202,21 @@ var SapphireDuration = (function (exports) {
131
202
  dateFrom(date) {
132
203
  return new Date(date.getTime() + this.offset);
133
204
  }
134
- /**
135
- * Parse the pattern
136
- * @param pattern The pattern to parse
137
- */
138
- static parse(pattern) {
139
- let result = 0;
140
- let valid = false;
141
- pattern.replace(_Duration.kCommaRegex, "").replace(_Duration.kAanRegex, "1").replace(_Duration.kPatternRegex, (_, i, units) => {
142
- const token = tokens.get(units);
143
- if (token !== void 0) {
144
- result += Number(i) * token;
145
- valid = true;
146
- }
147
- return "";
148
- });
149
- return valid ? result : NaN;
150
- }
151
205
  };
152
206
  var Duration = _Duration;
153
207
  __name(Duration, "Duration");
154
208
  /**
155
209
  * The RegExp used for the pattern parsing
156
210
  */
157
- __publicField(Duration, "kPatternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zμ]*)/gi);
211
+ __publicField(Duration, "patternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zμ]*)/gi);
158
212
  /**
159
213
  * The RegExp used for removing commas
160
214
  */
161
- __publicField(Duration, "kCommaRegex", /,/g);
215
+ __publicField(Duration, "commaRegex", /,/g);
162
216
  /**
163
217
  * The RegExp used for replacing a/an with 1
164
218
  */
165
- __publicField(Duration, "kAanRegex", /\ban?\b/gi);
219
+ __publicField(Duration, "aAndAnRegex", /\ban?\b/gi);
166
220
 
167
221
  // src/lib/DurationFormatter.ts
168
222
  var kTimeDurations = [
@@ -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,iBAAc,GAAG;AAAA,IACjB,GAAG;AAAA,IACH,SAAS;AAAA,EACV;AAAA,EACA,CAAC,mBAAe,GAAG;AAAA,I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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\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,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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,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"]}
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,44 +69,60 @@ var DEFAULT_SEPARATORS = {
66
69
 
67
70
  // src/lib/Duration.ts
68
71
  var tokens = /* @__PURE__ */ new Map([
69
- ["nanosecond", 1 / 1e6],
70
- ["nanoseconds", 1 / 1e6],
71
- ["ns", 1 / 1e6],
72
- ["millisecond", 1],
73
- ["milliseconds", 1],
74
- ["ms", 1],
75
- ["second", 1e3],
76
- ["seconds", 1e3],
77
- ["sec", 1e3],
78
- ["secs", 1e3],
79
- ["s", 1e3],
80
- ["minute", 1e3 * 60],
81
- ["minutes", 1e3 * 60],
82
- ["min", 1e3 * 60],
83
- ["mins", 1e3 * 60],
84
- ["m", 1e3 * 60],
85
- ["hour", 1e3 * 60 * 60],
86
- ["hours", 1e3 * 60 * 60],
87
- ["hr", 1e3 * 60 * 60],
88
- ["hrs", 1e3 * 60 * 60],
89
- ["h", 1e3 * 60 * 60],
90
- ["day", 1e3 * 60 * 60 * 24],
91
- ["days", 1e3 * 60 * 60 * 24],
92
- ["d", 1e3 * 60 * 60 * 24],
93
- ["week", 1e3 * 60 * 60 * 24 * 7],
94
- ["weeks", 1e3 * 60 * 60 * 24 * 7],
95
- ["wk", 1e3 * 60 * 60 * 24 * 7],
96
- ["wks", 1e3 * 60 * 60 * 24 * 7],
97
- ["w", 1e3 * 60 * 60 * 24 * 7],
98
- ["month", 1e3 * 60 * 60 * 24 * (365.25 / 12)],
99
- ["months", 1e3 * 60 * 60 * 24 * (365.25 / 12)],
100
- ["b", 1e3 * 60 * 60 * 24 * (365.25 / 12)],
101
- ["mo", 1e3 * 60 * 60 * 24 * (365.25 / 12)],
102
- ["year", 1e3 * 60 * 60 * 24 * 365.25],
103
- ["years", 1e3 * 60 * 60 * 24 * 365.25],
104
- ["yr", 1e3 * 60 * 60 * 24 * 365.25],
105
- ["yrs", 1e3 * 60 * 60 * 24 * 365.25],
106
- ["y", 1e3 * 60 * 60 * 24 * 365.25]
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 {
109
128
  /**
@@ -115,7 +134,59 @@ var _Duration = class {
115
134
  * The offset
116
135
  */
117
136
  __publicField(this, "offset");
118
- this.offset = _Duration.parse(pattern.toLowerCase());
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);
177
+ let result = 0;
178
+ let valid = false;
179
+ pattern.toLowerCase().replace(_Duration.commaRegex, "").replace(_Duration.aAndAnRegex, "1").replace(_Duration.patternRegex, (_, i, units) => {
180
+ const token = tokens.get(units);
181
+ if (token !== void 0) {
182
+ const n = Number(i);
183
+ result += n * token;
184
+ this[mappings.get(token)] += n;
185
+ valid = true;
186
+ }
187
+ return "";
188
+ });
189
+ this.offset = valid ? result : NaN;
119
190
  }
120
191
  /**
121
192
  * Get the date from now
@@ -130,38 +201,21 @@ var _Duration = class {
130
201
  dateFrom(date) {
131
202
  return new Date(date.getTime() + this.offset);
132
203
  }
133
- /**
134
- * Parse the pattern
135
- * @param pattern The pattern to parse
136
- */
137
- static parse(pattern) {
138
- let result = 0;
139
- let valid = false;
140
- pattern.replace(_Duration.kCommaRegex, "").replace(_Duration.kAanRegex, "1").replace(_Duration.kPatternRegex, (_, i, units) => {
141
- const token = tokens.get(units);
142
- if (token !== void 0) {
143
- result += Number(i) * token;
144
- valid = true;
145
- }
146
- return "";
147
- });
148
- return valid ? result : NaN;
149
- }
150
204
  };
151
205
  var Duration = _Duration;
152
206
  __name(Duration, "Duration");
153
207
  /**
154
208
  * The RegExp used for the pattern parsing
155
209
  */
156
- __publicField(Duration, "kPatternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zμ]*)/gi);
210
+ __publicField(Duration, "patternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zμ]*)/gi);
157
211
  /**
158
212
  * The RegExp used for removing commas
159
213
  */
160
- __publicField(Duration, "kCommaRegex", /,/g);
214
+ __publicField(Duration, "commaRegex", /,/g);
161
215
  /**
162
216
  * The RegExp used for replacing a/an with 1
163
217
  */
164
- __publicField(Duration, "kAanRegex", /\ban?\b/gi);
218
+ __publicField(Duration, "aAndAnRegex", /\ban?\b/gi);
165
219
 
166
220
  // src/lib/DurationFormatter.ts
167
221
  var kTimeDurations = [
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,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;AACD;AAEO,IAAM,qBAA+C;AAAA,EAC3D,MA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AAgC,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\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/DurationFormat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AAK,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.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,44 +67,60 @@ var DEFAULT_SEPARATORS = {
64
67
 
65
68
  // src/lib/Duration.ts
66
69
  var tokens = /* @__PURE__ */ new Map([
67
- ["nanosecond", 1 / 1e6],
68
- ["nanoseconds", 1 / 1e6],
69
- ["ns", 1 / 1e6],
70
- ["millisecond", 1],
71
- ["milliseconds", 1],
72
- ["ms", 1],
73
- ["second", 1e3],
74
- ["seconds", 1e3],
75
- ["sec", 1e3],
76
- ["secs", 1e3],
77
- ["s", 1e3],
78
- ["minute", 1e3 * 60],
79
- ["minutes", 1e3 * 60],
80
- ["min", 1e3 * 60],
81
- ["mins", 1e3 * 60],
82
- ["m", 1e3 * 60],
83
- ["hour", 1e3 * 60 * 60],
84
- ["hours", 1e3 * 60 * 60],
85
- ["hr", 1e3 * 60 * 60],
86
- ["hrs", 1e3 * 60 * 60],
87
- ["h", 1e3 * 60 * 60],
88
- ["day", 1e3 * 60 * 60 * 24],
89
- ["days", 1e3 * 60 * 60 * 24],
90
- ["d", 1e3 * 60 * 60 * 24],
91
- ["week", 1e3 * 60 * 60 * 24 * 7],
92
- ["weeks", 1e3 * 60 * 60 * 24 * 7],
93
- ["wk", 1e3 * 60 * 60 * 24 * 7],
94
- ["wks", 1e3 * 60 * 60 * 24 * 7],
95
- ["w", 1e3 * 60 * 60 * 24 * 7],
96
- ["month", 1e3 * 60 * 60 * 24 * (365.25 / 12)],
97
- ["months", 1e3 * 60 * 60 * 24 * (365.25 / 12)],
98
- ["b", 1e3 * 60 * 60 * 24 * (365.25 / 12)],
99
- ["mo", 1e3 * 60 * 60 * 24 * (365.25 / 12)],
100
- ["year", 1e3 * 60 * 60 * 24 * 365.25],
101
- ["years", 1e3 * 60 * 60 * 24 * 365.25],
102
- ["yr", 1e3 * 60 * 60 * 24 * 365.25],
103
- ["yrs", 1e3 * 60 * 60 * 24 * 365.25],
104
- ["y", 1e3 * 60 * 60 * 24 * 365.25]
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 {
107
126
  /**
@@ -113,7 +132,59 @@ var _Duration = class {
113
132
  * The offset
114
133
  */
115
134
  __publicField(this, "offset");
116
- this.offset = _Duration.parse(pattern.toLowerCase());
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);
175
+ let result = 0;
176
+ let valid = false;
177
+ pattern.toLowerCase().replace(_Duration.commaRegex, "").replace(_Duration.aAndAnRegex, "1").replace(_Duration.patternRegex, (_, i, units) => {
178
+ const token = tokens.get(units);
179
+ if (token !== void 0) {
180
+ const n = Number(i);
181
+ result += n * token;
182
+ this[mappings.get(token)] += n;
183
+ valid = true;
184
+ }
185
+ return "";
186
+ });
187
+ this.offset = valid ? result : NaN;
117
188
  }
118
189
  /**
119
190
  * Get the date from now
@@ -128,38 +199,21 @@ var _Duration = class {
128
199
  dateFrom(date) {
129
200
  return new Date(date.getTime() + this.offset);
130
201
  }
131
- /**
132
- * Parse the pattern
133
- * @param pattern The pattern to parse
134
- */
135
- static parse(pattern) {
136
- let result = 0;
137
- let valid = false;
138
- pattern.replace(_Duration.kCommaRegex, "").replace(_Duration.kAanRegex, "1").replace(_Duration.kPatternRegex, (_, i, units) => {
139
- const token = tokens.get(units);
140
- if (token !== void 0) {
141
- result += Number(i) * token;
142
- valid = true;
143
- }
144
- return "";
145
- });
146
- return valid ? result : NaN;
147
- }
148
202
  };
149
203
  var Duration = _Duration;
150
204
  __name(Duration, "Duration");
151
205
  /**
152
206
  * The RegExp used for the pattern parsing
153
207
  */
154
- __publicField(Duration, "kPatternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zμ]*)/gi);
208
+ __publicField(Duration, "patternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zμ]*)/gi);
155
209
  /**
156
210
  * The RegExp used for removing commas
157
211
  */
158
- __publicField(Duration, "kCommaRegex", /,/g);
212
+ __publicField(Duration, "commaRegex", /,/g);
159
213
  /**
160
214
  * The RegExp used for replacing a/an with 1
161
215
  */
162
- __publicField(Duration, "kAanRegex", /\ban?\b/gi);
216
+ __publicField(Duration, "aAndAnRegex", /\ban?\b/gi);
163
217
 
164
218
  // src/lib/DurationFormatter.ts
165
219
  var kTimeDurations = [
@@ -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,EAAA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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\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,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,W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B,MAAO,KAAK,KAAK,EAAE;AAAA,EACnC,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"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sapphire/duration",
3
- "version": "1.1.0-next.e7e9997.0",
3
+ "version": "1.1.0-next.eac39af.0",
4
4
  "description": "A time duration utility library for JavaScript.",
5
5
  "author": "@sapphire",
6
6
  "license": "MIT",