@sapphire/duration 1.1.1-next.cb20bb4b.0 → 1.1.1-next.cf8dd7ee.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/README.md +1 -1
- package/dist/index.d.mts +131 -0
- package/dist/index.global.js +9 -8
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +9 -8
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +9 -8
- package/dist/index.mjs.map +1 -1
- package/package.json +16 -11
package/README.md
CHANGED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
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 }?: DurationFormatSeparators): string;
|
|
10
|
+
}
|
|
11
|
+
interface DurationFormatSeparators {
|
|
12
|
+
left?: string;
|
|
13
|
+
right?: string;
|
|
14
|
+
}
|
|
15
|
+
interface DurationFormatAssetsUnit extends Record<number, string> {
|
|
16
|
+
DEFAULT: string;
|
|
17
|
+
}
|
|
18
|
+
interface DurationFormatAssetsTime {
|
|
19
|
+
[TimeTypes.Second]: DurationFormatAssetsUnit;
|
|
20
|
+
[TimeTypes.Minute]: DurationFormatAssetsUnit;
|
|
21
|
+
[TimeTypes.Hour]: DurationFormatAssetsUnit;
|
|
22
|
+
[TimeTypes.Day]: DurationFormatAssetsUnit;
|
|
23
|
+
[TimeTypes.Week]: DurationFormatAssetsUnit;
|
|
24
|
+
[TimeTypes.Month]: DurationFormatAssetsUnit;
|
|
25
|
+
[TimeTypes.Year]: DurationFormatAssetsUnit;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
declare enum Time {
|
|
29
|
+
Nanosecond = 0.000001,
|
|
30
|
+
Microsecond = 0.001,
|
|
31
|
+
Millisecond = 1,
|
|
32
|
+
Second = 1000,
|
|
33
|
+
Minute = 60000,
|
|
34
|
+
Hour = 3600000,
|
|
35
|
+
Day = 86400000,
|
|
36
|
+
Week = 604800000,
|
|
37
|
+
Month = 2628000000,
|
|
38
|
+
Year = 31536000000
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* The supported time types
|
|
42
|
+
*/
|
|
43
|
+
declare enum TimeTypes {
|
|
44
|
+
Second = "second",
|
|
45
|
+
Minute = "minute",
|
|
46
|
+
Hour = "hour",
|
|
47
|
+
Day = "day",
|
|
48
|
+
Week = "week",
|
|
49
|
+
Month = "month",
|
|
50
|
+
Year = "year"
|
|
51
|
+
}
|
|
52
|
+
declare const DEFAULT_UNITS: DurationFormatAssetsTime;
|
|
53
|
+
declare const DEFAULT_SEPARATORS: DurationFormatSeparators;
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Converts duration strings into ms and future dates
|
|
57
|
+
*/
|
|
58
|
+
declare class Duration {
|
|
59
|
+
/**
|
|
60
|
+
* The offset
|
|
61
|
+
*/
|
|
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;
|
|
103
|
+
/**
|
|
104
|
+
* Create a new Duration instance
|
|
105
|
+
* @param pattern The string to parse
|
|
106
|
+
*/
|
|
107
|
+
constructor(pattern: string);
|
|
108
|
+
/**
|
|
109
|
+
* Get the date from now
|
|
110
|
+
*/
|
|
111
|
+
get fromNow(): Date;
|
|
112
|
+
/**
|
|
113
|
+
* Get the date from
|
|
114
|
+
* @param date The Date instance to get the date from
|
|
115
|
+
*/
|
|
116
|
+
dateFrom(date: Date): Date;
|
|
117
|
+
/**
|
|
118
|
+
* The RegExp used for the pattern parsing
|
|
119
|
+
*/
|
|
120
|
+
private static readonly patternRegex;
|
|
121
|
+
/**
|
|
122
|
+
* The RegExp used for removing commas
|
|
123
|
+
*/
|
|
124
|
+
private static readonly commaRegex;
|
|
125
|
+
/**
|
|
126
|
+
* The RegExp used for replacing a/an with 1
|
|
127
|
+
*/
|
|
128
|
+
private static readonly aAndAnRegex;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
export { DEFAULT_SEPARATORS, DEFAULT_UNITS, Duration, DurationFormatAssetsTime, DurationFormatAssetsUnit, DurationFormatSeparators, DurationFormatter, Time, TimeTypes };
|
package/dist/index.global.js
CHANGED
|
@@ -125,7 +125,7 @@ var SapphireDuration = (function (exports) {
|
|
|
125
125
|
[2628e6 /* Month */, "months"],
|
|
126
126
|
[31536e6 /* Year */, "years"]
|
|
127
127
|
]);
|
|
128
|
-
var _Duration = class {
|
|
128
|
+
var _Duration = class _Duration {
|
|
129
129
|
/**
|
|
130
130
|
* Create a new Duration instance
|
|
131
131
|
* @param pattern The string to parse
|
|
@@ -203,20 +203,20 @@ var SapphireDuration = (function (exports) {
|
|
|
203
203
|
return new Date(date.getTime() + this.offset);
|
|
204
204
|
}
|
|
205
205
|
};
|
|
206
|
-
|
|
207
|
-
__name(Duration, "Duration");
|
|
206
|
+
__name(_Duration, "Duration");
|
|
208
207
|
/**
|
|
209
208
|
* The RegExp used for the pattern parsing
|
|
210
209
|
*/
|
|
211
|
-
__publicField(
|
|
210
|
+
__publicField(_Duration, "patternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zμ]*)/gi);
|
|
212
211
|
/**
|
|
213
212
|
* The RegExp used for removing commas
|
|
214
213
|
*/
|
|
215
|
-
__publicField(
|
|
214
|
+
__publicField(_Duration, "commaRegex", /,/g);
|
|
216
215
|
/**
|
|
217
216
|
* The RegExp used for replacing a/an with 1
|
|
218
217
|
*/
|
|
219
|
-
__publicField(
|
|
218
|
+
__publicField(_Duration, "aAndAnRegex", /\ban?\b/gi);
|
|
219
|
+
var Duration = _Duration;
|
|
220
220
|
|
|
221
221
|
// src/lib/DurationFormatter.ts
|
|
222
222
|
var kTimeDurations = [
|
|
@@ -229,7 +229,7 @@ var SapphireDuration = (function (exports) {
|
|
|
229
229
|
["minute" /* Minute */, 1e3 * 60],
|
|
230
230
|
["second" /* Second */, 1e3]
|
|
231
231
|
];
|
|
232
|
-
var
|
|
232
|
+
var _DurationFormatter = class _DurationFormatter {
|
|
233
233
|
constructor(units = DEFAULT_UNITS) {
|
|
234
234
|
this.units = units;
|
|
235
235
|
}
|
|
@@ -254,7 +254,8 @@ var SapphireDuration = (function (exports) {
|
|
|
254
254
|
return `${negative ? "-" : ""}${output.join(rightSeparator) || addUnit(0, this.units.second, leftSeparator)}`;
|
|
255
255
|
}
|
|
256
256
|
};
|
|
257
|
-
__name(
|
|
257
|
+
__name(_DurationFormatter, "DurationFormatter");
|
|
258
|
+
var DurationFormatter = _DurationFormatter;
|
|
258
259
|
function addUnit(time, unit, separator) {
|
|
259
260
|
if (Reflect.has(unit, time))
|
|
260
261
|
return `${time}${separator}${Reflect.get(unit, time)}`;
|
package/dist/index.global.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/lib/constants.ts","../src/lib/Duration.ts","../src/lib/DurationFormatter.ts"],"names":["Time","TimeTypes"],"mappings":";;;;;;;;;AAEO,IAAK,OAAL,kBAAKA,UAAL;AACN,EAAAA,YAAA,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,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5B,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
|
+
{"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;A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cesContent":["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
|
@@ -124,7 +124,7 @@ var mappings = /* @__PURE__ */ new Map([
|
|
|
124
124
|
[2628e6 /* Month */, "months"],
|
|
125
125
|
[31536e6 /* Year */, "years"]
|
|
126
126
|
]);
|
|
127
|
-
var _Duration = class {
|
|
127
|
+
var _Duration = class _Duration {
|
|
128
128
|
/**
|
|
129
129
|
* Create a new Duration instance
|
|
130
130
|
* @param pattern The string to parse
|
|
@@ -202,20 +202,20 @@ var _Duration = class {
|
|
|
202
202
|
return new Date(date.getTime() + this.offset);
|
|
203
203
|
}
|
|
204
204
|
};
|
|
205
|
-
|
|
206
|
-
__name(Duration, "Duration");
|
|
205
|
+
__name(_Duration, "Duration");
|
|
207
206
|
/**
|
|
208
207
|
* The RegExp used for the pattern parsing
|
|
209
208
|
*/
|
|
210
|
-
__publicField(
|
|
209
|
+
__publicField(_Duration, "patternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zμ]*)/gi);
|
|
211
210
|
/**
|
|
212
211
|
* The RegExp used for removing commas
|
|
213
212
|
*/
|
|
214
|
-
__publicField(
|
|
213
|
+
__publicField(_Duration, "commaRegex", /,/g);
|
|
215
214
|
/**
|
|
216
215
|
* The RegExp used for replacing a/an with 1
|
|
217
216
|
*/
|
|
218
|
-
__publicField(
|
|
217
|
+
__publicField(_Duration, "aAndAnRegex", /\ban?\b/gi);
|
|
218
|
+
var Duration = _Duration;
|
|
219
219
|
|
|
220
220
|
// src/lib/DurationFormatter.ts
|
|
221
221
|
var kTimeDurations = [
|
|
@@ -228,7 +228,7 @@ var kTimeDurations = [
|
|
|
228
228
|
["minute" /* Minute */, 1e3 * 60],
|
|
229
229
|
["second" /* Second */, 1e3]
|
|
230
230
|
];
|
|
231
|
-
var
|
|
231
|
+
var _DurationFormatter = class _DurationFormatter {
|
|
232
232
|
constructor(units = DEFAULT_UNITS) {
|
|
233
233
|
this.units = units;
|
|
234
234
|
}
|
|
@@ -253,7 +253,8 @@ var DurationFormatter = class {
|
|
|
253
253
|
return `${negative ? "-" : ""}${output.join(rightSeparator) || addUnit(0, this.units.second, leftSeparator)}`;
|
|
254
254
|
}
|
|
255
255
|
};
|
|
256
|
-
__name(
|
|
256
|
+
__name(_DurationFormatter, "DurationFormatter");
|
|
257
|
+
var DurationFormatter = _DurationFormatter;
|
|
257
258
|
function addUnit(time, unit, separator) {
|
|
258
259
|
if (Reflect.has(unit, time))
|
|
259
260
|
return `${time}${separator}${Reflect.get(unit, time)}`;
|
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,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,EAAA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AAgB,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"]}
|
|
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,qBAAg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UAAU;AACtB,aAAO,KAAK,QAAQ,SAAS,KAAK,MAAM,IAAI,GAAG,aAAc,CAAC;AAG9D,UAAI,OAAO,UAAU;AAAW;AAAA,IACjC;AAEA,WAAO,GAAG,WAAW,MAAM,EAAE,GAAG,OAAO,KAAK,cAAc,KAAK,QAAQ,GAAG,KAAK,MAAM,QAAQ,aAAc,CAAC;AAAA,EAC7G;AACD;AA7B+B;AAAxB,IAAM,oBAAN;AAoCP,SAAS,QAAQ,MAAc,MAAgC,WAAmB;AACjF,MAAI,QAAQ,IAAI,MAAM,IAAI;AAAG,WAAO,GAAG,IAAI,GAAG,SAAS,GAAG,QAAQ,IAAI,MAAM,IAAI,CAAC;AACjF,SAAO,GAAG,IAAI,GAAG,SAAS,GAAG,KAAK,OAAO;AAC1C;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
|
@@ -122,7 +122,7 @@ var mappings = /* @__PURE__ */ new Map([
|
|
|
122
122
|
[2628e6 /* Month */, "months"],
|
|
123
123
|
[31536e6 /* Year */, "years"]
|
|
124
124
|
]);
|
|
125
|
-
var _Duration = class {
|
|
125
|
+
var _Duration = class _Duration {
|
|
126
126
|
/**
|
|
127
127
|
* Create a new Duration instance
|
|
128
128
|
* @param pattern The string to parse
|
|
@@ -200,20 +200,20 @@ var _Duration = class {
|
|
|
200
200
|
return new Date(date.getTime() + this.offset);
|
|
201
201
|
}
|
|
202
202
|
};
|
|
203
|
-
|
|
204
|
-
__name(Duration, "Duration");
|
|
203
|
+
__name(_Duration, "Duration");
|
|
205
204
|
/**
|
|
206
205
|
* The RegExp used for the pattern parsing
|
|
207
206
|
*/
|
|
208
|
-
__publicField(
|
|
207
|
+
__publicField(_Duration, "patternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zμ]*)/gi);
|
|
209
208
|
/**
|
|
210
209
|
* The RegExp used for removing commas
|
|
211
210
|
*/
|
|
212
|
-
__publicField(
|
|
211
|
+
__publicField(_Duration, "commaRegex", /,/g);
|
|
213
212
|
/**
|
|
214
213
|
* The RegExp used for replacing a/an with 1
|
|
215
214
|
*/
|
|
216
|
-
__publicField(
|
|
215
|
+
__publicField(_Duration, "aAndAnRegex", /\ban?\b/gi);
|
|
216
|
+
var Duration = _Duration;
|
|
217
217
|
|
|
218
218
|
// src/lib/DurationFormatter.ts
|
|
219
219
|
var kTimeDurations = [
|
|
@@ -226,7 +226,7 @@ var kTimeDurations = [
|
|
|
226
226
|
["minute" /* Minute */, 1e3 * 60],
|
|
227
227
|
["second" /* Second */, 1e3]
|
|
228
228
|
];
|
|
229
|
-
var
|
|
229
|
+
var _DurationFormatter = class _DurationFormatter {
|
|
230
230
|
constructor(units = DEFAULT_UNITS) {
|
|
231
231
|
this.units = units;
|
|
232
232
|
}
|
|
@@ -251,7 +251,8 @@ var DurationFormatter = class {
|
|
|
251
251
|
return `${negative ? "-" : ""}${output.join(rightSeparator) || addUnit(0, this.units.second, leftSeparator)}`;
|
|
252
252
|
}
|
|
253
253
|
};
|
|
254
|
-
__name(
|
|
254
|
+
__name(_DurationFormatter, "DurationFormatter");
|
|
255
|
+
var DurationFormatter = _DurationFormatter;
|
|
255
256
|
function addUnit(time, unit, separator) {
|
|
256
257
|
if (Reflect.has(unit, time))
|
|
257
258
|
return `${time}${separator}${Reflect.get(unit, time)}`;
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/lib/constants.ts","../src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,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
|
+
{"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,UA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EAC/B,wBAAmB,MAAO,EAAE;AAAA,EAC5B,wBAAmB,GAAI;AACxB;AAOO,IAAM,qBAAN,MAAM,mBAAkB;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,EAAE,GAAG,OAAO,KAAK,cAAc,KAAK,QAAQ,GAAG,KAAK,MAAM,QAAQ,aAAc,CAAC;AAAA,EAC7G;AACD;AA7B+B;AAAxB,IAAM,oBAAN;AAoCP,SAAS,QAAQ,MAAc,MAAgC,WAAmB;AACjF,MAAI,QAAQ,IAAI,MAAM,IAAI;AAAG,WAAO,GAAG,IAAI,GAAG,SAAS,GAAG,QAAQ,IAAI,MAAM,IAAI,CAAC;AACjF,SAAO,GAAG,IAAI,GAAG,SAAS,GAAG,KAAK,OAAO;AAC1C;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.1-next.
|
|
3
|
+
"version": "1.1.1-next.cf8dd7ee.0",
|
|
4
4
|
"description": "A time duration utility library for JavaScript.",
|
|
5
5
|
"author": "@sapphire",
|
|
6
6
|
"license": "MIT",
|
|
@@ -10,9 +10,14 @@
|
|
|
10
10
|
"unpkg": "dist/index.global.js",
|
|
11
11
|
"types": "dist/index.d.ts",
|
|
12
12
|
"exports": {
|
|
13
|
-
"import":
|
|
14
|
-
|
|
15
|
-
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.mjs"
|
|
16
|
+
},
|
|
17
|
+
"require": {
|
|
18
|
+
"types": "./dist/index.d.ts",
|
|
19
|
+
"default": "./dist/index.js"
|
|
20
|
+
}
|
|
16
21
|
},
|
|
17
22
|
"sideEffects": false,
|
|
18
23
|
"homepage": "https://github.com/sapphiredev/utilities/tree/main/packages/duration",
|
|
@@ -57,12 +62,12 @@
|
|
|
57
62
|
"access": "public"
|
|
58
63
|
},
|
|
59
64
|
"devDependencies": {
|
|
60
|
-
"@favware/cliff-jumper": "^2.
|
|
61
|
-
"@vitest/coverage-
|
|
62
|
-
"tsup": "^
|
|
63
|
-
"typedoc": "^0.
|
|
64
|
-
"typedoc-json-parser": "^
|
|
65
|
-
"typescript": "^
|
|
66
|
-
"vitest": "^0.
|
|
65
|
+
"@favware/cliff-jumper": "^2.1.1",
|
|
66
|
+
"@vitest/coverage-v8": "^0.34.3",
|
|
67
|
+
"tsup": "^7.2.0",
|
|
68
|
+
"typedoc": "^0.25.0",
|
|
69
|
+
"typedoc-json-parser": "^8.2.0",
|
|
70
|
+
"typescript": "^5.2.2",
|
|
71
|
+
"vitest": "^0.34.3"
|
|
67
72
|
}
|
|
68
73
|
}
|