cafe-utility 10.6.0 → 10.7.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.
Files changed (3) hide show
  1. package/index.d.ts +2 -0
  2. package/index.js +40 -2
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -211,6 +211,7 @@ declare function getDayInfoFromDateTimeString(dateTimeString: string): DayInfo;
211
211
  declare function seconds(value: number): number;
212
212
  declare function minutes(value: number): number;
213
213
  declare function hours(value: number): number;
214
+ declare function makeDate(numberWithUnit: string): number;
214
215
  declare function getPreLine(string: string): string;
215
216
  declare function containsWord(string: string, word: string): boolean;
216
217
  declare function containsWords(string: string, words: string[]): boolean;
@@ -431,6 +432,7 @@ export declare const Dates: {
431
432
  seconds: typeof seconds;
432
433
  minutes: typeof minutes;
433
434
  hours: typeof hours;
435
+ make: typeof makeDate;
434
436
  };
435
437
  export declare const Objects: {
436
438
  safeParse: typeof safeParse;
package/index.js CHANGED
@@ -1391,6 +1391,43 @@ function hours(value) {
1391
1391
  return value * 3600000
1392
1392
  }
1393
1393
 
1394
+ const dateUnits = [
1395
+ ['ms', 1],
1396
+ ['milli', 1],
1397
+ ['millis', 1],
1398
+ ['millisecond', 1],
1399
+ ['milliseconds', 1],
1400
+ ['s', 1000],
1401
+ ['sec', 1000],
1402
+ ['second', 1000],
1403
+ ['seconds', 1000],
1404
+ ['m', 60000],
1405
+ ['min', 60000],
1406
+ ['minute', 60000],
1407
+ ['minutes', 60000],
1408
+ ['h', 3600000],
1409
+ ['hour', 3600000],
1410
+ ['hours', 3600000],
1411
+ ['d', 86400000],
1412
+ ['day', 86400000],
1413
+ ['days', 86400000],
1414
+ ['w', 604800000],
1415
+ ['week', 604800000],
1416
+ ['weeks', 604800000]
1417
+ ]
1418
+ function makeDate(numberWithUnit) {
1419
+ const number = parseFloat(numberWithUnit)
1420
+ if (isNaN(number)) {
1421
+ throw 'makeDate got NaN for input'
1422
+ }
1423
+ const unit = numberWithUnit.replace(/^-?[0-9.]+/, '').trim()
1424
+ const index = dateUnits.findIndex(value => value[0] === unit.toLowerCase())
1425
+ if (index === -1) {
1426
+ return number
1427
+ }
1428
+ return number * dateUnits[index][1]
1429
+ }
1430
+
1394
1431
  function getPreLine(string) {
1395
1432
  return string.replace(/ +/g, ' ').replace(/^ /gm, '')
1396
1433
  }
@@ -1623,7 +1660,7 @@ function makeNumber(numberWithUnit) {
1623
1660
  if (isNaN(number)) {
1624
1661
  throw 'makeNumber got NaN for input'
1625
1662
  }
1626
- const unit = numberWithUnit.replace(/^-?[0-9.]+/, '')
1663
+ const unit = numberWithUnit.replace(/^-?[0-9.]+/, '').trim()
1627
1664
  const index = shortNumberUnits.findIndex(value => value.toLowerCase() === unit.toLowerCase())
1628
1665
  if (index === -1) {
1629
1666
  return number
@@ -2498,7 +2535,8 @@ exports.Dates = {
2498
2535
  getDayInfoFromDateTimeString,
2499
2536
  seconds,
2500
2537
  minutes,
2501
- hours
2538
+ hours,
2539
+ make: makeDate
2502
2540
  }
2503
2541
 
2504
2542
  exports.Objects = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "10.6.0",
3
+ "version": "10.7.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {