@sapphire/duration 1.1.1-next.e501bf55.0 → 1.1.1-next.eb4dd234.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.
@@ -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
- var Duration = _Duration;
207
- __name(Duration, "Duration");
206
+ __name(_Duration, "Duration");
208
207
  /**
209
208
  * The RegExp used for the pattern parsing
210
209
  */
211
- __publicField(Duration, "patternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zμ]*)/gi);
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(Duration, "commaRegex", /,/g);
214
+ __publicField(_Duration, "commaRegex", /,/g);
216
215
  /**
217
216
  * The RegExp used for replacing a/an with 1
218
217
  */
219
- __publicField(Duration, "aAndAnRegex", /\ban?\b/gi);
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 DurationFormatter = class {
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(DurationFormatter, "DurationFormatter");
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)}`;
@@ -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;A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,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,Y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esContent":["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
- var Duration = _Duration;
206
- __name(Duration, "Duration");
205
+ __name(_Duration, "Duration");
207
206
  /**
208
207
  * The RegExp used for the pattern parsing
209
208
  */
210
- __publicField(Duration, "patternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zμ]*)/gi);
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(Duration, "commaRegex", /,/g);
213
+ __publicField(_Duration, "commaRegex", /,/g);
215
214
  /**
216
215
  * The RegExp used for replacing a/an with 1
217
216
  */
218
- __publicField(Duration, "aAndAnRegex", /\ban?\b/gi);
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 DurationFormatter = class {
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(DurationFormatter, "DurationFormatter");
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,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;AA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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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AC5G;AACD;AA7B+B;AAAxB,IAAM,oBAAN;AAoCP,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
@@ -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
- var Duration = _Duration;
204
- __name(Duration, "Duration");
203
+ __name(_Duration, "Duration");
205
204
  /**
206
205
  * The RegExp used for the pattern parsing
207
206
  */
208
- __publicField(Duration, "patternRegex", /(-?\d*\.?\d+(?:e[-+]?\d+)?)\s*([a-zμ]*)/gi);
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(Duration, "commaRegex", /,/g);
211
+ __publicField(_Duration, "commaRegex", /,/g);
213
212
  /**
214
213
  * The RegExp used for replacing a/an with 1
215
214
  */
216
- __publicField(Duration, "aAndAnRegex", /\ban?\b/gi);
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 DurationFormatter = class {
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(DurationFormatter, "DurationFormatter");
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)}`;
@@ -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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AAO,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,qBAAgB,GAAG;AAAA,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AO,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;AA7B+B;AAAxB,IAAM,oBAAN;AAoCP,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.1-next.e501bf55.0",
3
+ "version": "1.1.1-next.eb4dd234.0",
4
4
  "description": "A time duration utility library for JavaScript.",
5
5
  "author": "@sapphire",
6
6
  "license": "MIT",
@@ -57,12 +57,12 @@
57
57
  "access": "public"
58
58
  },
59
59
  "devDependencies": {
60
- "@favware/cliff-jumper": "^2.0.0",
61
- "@vitest/coverage-c8": "^0.31.0",
62
- "tsup": "^6.7.0",
63
- "typedoc": "^0.24.7",
64
- "typedoc-json-parser": "^7.4.0",
65
- "typescript": "^5.0.4",
66
- "vitest": "^0.31.0"
60
+ "@favware/cliff-jumper": "^2.1.1",
61
+ "@vitest/coverage-v8": "^0.32.0",
62
+ "tsup": "^7.0.0",
63
+ "typedoc": "^0.24.8",
64
+ "typedoc-json-parser": "^8.1.2",
65
+ "typescript": "^5.1.3",
66
+ "vitest": "^0.32.0"
67
67
  }
68
68
  }