cafe-utility 10.5.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 +43 -4
  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
  }
@@ -1577,7 +1614,7 @@ function fromObjectString(string) {
1577
1614
  return JSON.parse(string)
1578
1615
  }
1579
1616
 
1580
- const thresholds = [1e3, 1e6, 1e9, 1e12, 1e15, 1e18, 1e21, 1e24, 1e27, 1e30, 1e16, 1e18, 1e18, 1e18, 1e33]
1617
+ const thresholds = [1e3, 1e6, 1e9, 1e12, 1e15, 1e18, 1e21, 1e24, 1e27, 1e30, 1e9, 1e16, 1e18, 1e18, 1e18, 1e33]
1581
1618
  const longNumberUnits = [
1582
1619
  'thousand',
1583
1620
  'million',
@@ -1589,13 +1626,14 @@ const longNumberUnits = [
1589
1626
  'septillion',
1590
1627
  'octillion',
1591
1628
  'nonillion',
1629
+ 'gwei',
1592
1630
  'bzz',
1593
1631
  'btc',
1594
1632
  'eth',
1595
1633
  'dai',
1596
1634
  'decillion'
1597
1635
  ]
1598
- const shortNumberUnits = ['K', 'M', 'B', 'T', 'Qa', 'Qi', 'Sx', 'Sp', 'O', 'N', 'bzz', 'eth', 'btc', 'dai', 'D']
1636
+ const shortNumberUnits = ['K', 'M', 'B', 'T', 'Qa', 'Qi', 'Sx', 'Sp', 'O', 'N', 'gwei', 'bzz', 'eth', 'btc', 'dai', 'D']
1599
1637
  function formatNumber(number, options) {
1600
1638
  var _a, _b
1601
1639
  const longFormat =
@@ -1622,7 +1660,7 @@ function makeNumber(numberWithUnit) {
1622
1660
  if (isNaN(number)) {
1623
1661
  throw 'makeNumber got NaN for input'
1624
1662
  }
1625
- const unit = numberWithUnit.replace(/^-?[0-9.]+/, '')
1663
+ const unit = numberWithUnit.replace(/^-?[0-9.]+/, '').trim()
1626
1664
  const index = shortNumberUnits.findIndex(value => value.toLowerCase() === unit.toLowerCase())
1627
1665
  if (index === -1) {
1628
1666
  return number
@@ -2497,7 +2535,8 @@ exports.Dates = {
2497
2535
  getDayInfoFromDateTimeString,
2498
2536
  seconds,
2499
2537
  minutes,
2500
- hours
2538
+ hours,
2539
+ make: makeDate
2501
2540
  }
2502
2541
 
2503
2542
  exports.Objects = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "10.5.0",
3
+ "version": "10.7.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {