cafe-utility 10.6.0 → 10.7.1

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 +3 -1
  2. package/index.js +46 -8
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -178,7 +178,7 @@ declare function fromMillis(millis: number): Date;
178
178
  declare function createTimeDigits(value: number): string;
179
179
  declare function humanizeTime(millis: number): string;
180
180
  declare function getAgo(date: Date, now?: number): string;
181
- declare function getAgoStructured(date: Date, now?: number): {
181
+ declare function getAgoStructured(dateOrTimestamp: Date | number, now?: number): {
182
182
  value: number;
183
183
  unit: string;
184
184
  };
@@ -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
@@ -1293,25 +1293,25 @@ function getAgo(date, now) {
1293
1293
  return delta.toFixed(0) + ' days ago'
1294
1294
  }
1295
1295
 
1296
- function getAgoStructured(date, now) {
1296
+ function getAgoStructured(dateOrTimestamp, now) {
1297
1297
  if (!now) {
1298
1298
  now = Date.now()
1299
1299
  }
1300
- const then = date.getTime()
1300
+ const then = typeof dateOrTimestamp === 'number' ? dateOrTimestamp : dateOrTimestamp.getTime()
1301
1301
  let delta = (now - then) / 1000
1302
1302
  if (delta < 120) {
1303
- return { value: delta, unit: 'second' }
1303
+ return { value: Math.floor(delta), unit: 'second' }
1304
1304
  }
1305
1305
  delta /= 60
1306
1306
  if (delta < 120) {
1307
- return { value: delta, unit: 'minute' }
1307
+ return { value: Math.floor(delta), unit: 'minute' }
1308
1308
  }
1309
1309
  delta /= 60
1310
1310
  if (delta < 48) {
1311
- return { value: delta, unit: 'hour' }
1311
+ return { value: Math.floor(delta), unit: 'hour' }
1312
1312
  }
1313
1313
  delta /= 24
1314
- return { value: delta, unit: 'day' }
1314
+ return { value: Math.floor(delta), unit: 'day' }
1315
1315
  }
1316
1316
 
1317
1317
  const throttleTimers = {}
@@ -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.1",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {