cafe-utility 21.5.0 → 22.0.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 +9 -7
  2. package/index.js +32 -27
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -240,7 +240,7 @@ declare function fromUtcString(string: string): Date;
240
240
  declare function fromMillis(millis: number): Date;
241
241
  declare function createTimeDigits(value: number): string;
242
242
  declare function humanizeTime(millis: number): string;
243
- interface TimestampTranslations {
243
+ interface TimestampLabels {
244
244
  today: (hour: number, minute: number, pm: boolean) => string;
245
245
  yesterday: () => string;
246
246
  monday: () => string;
@@ -254,10 +254,10 @@ interface TimestampTranslations {
254
254
  }
255
255
  interface GetTimestampOptions {
256
256
  now?: number;
257
- translations?: TimestampTranslations;
257
+ labels?: TimestampLabels;
258
258
  }
259
259
  declare function getTimestamp(date: Date | number, options?: GetTimestampOptions): string;
260
- interface AgoTranslations {
260
+ interface TimeDeltaLabels {
261
261
  now: () => string;
262
262
  seconds: (value: number) => string;
263
263
  minutes: (value: number) => string;
@@ -265,11 +265,12 @@ interface AgoTranslations {
265
265
  days: (value: number) => string;
266
266
  weeks: (value: number) => string;
267
267
  }
268
- interface GetAgoOptions {
268
+ interface TimeDeltaOptions {
269
269
  now?: number;
270
- translations?: AgoTranslations;
270
+ labels?: TimeDeltaLabels;
271
271
  }
272
- declare function getAgo(date: Date | number, options?: GetAgoOptions): string;
272
+ declare function getTimeDelta(date: Date | number, options?: TimeDeltaOptions): string;
273
+ declare function secondsToHumanTime(seconds: number, labels?: TimeDeltaLabels): string;
273
274
  type CountCyclesOptions = {
274
275
  precision?: number;
275
276
  now?: number;
@@ -613,7 +614,8 @@ export declare const Promises: {
613
614
  };
614
615
  export declare const Dates: {
615
616
  getTimestamp: typeof getTimestamp;
616
- getAgo: typeof getAgo;
617
+ getTimeDelta: typeof getTimeDelta;
618
+ secondsToHumanTime: typeof secondsToHumanTime;
617
619
  countCycles: typeof countCycles;
618
620
  isoDate: typeof isoDate;
619
621
  throttle: typeof throttle;
package/index.js CHANGED
@@ -1296,7 +1296,7 @@ function humanizeTime(n) {
1296
1296
  function absoluteDays(n) {
1297
1297
  return Math.floor((isDate(n) ? n.getTime() : n) / 864e5)
1298
1298
  }
1299
- const DefaultTimestampTranslations = {
1299
+ const DefaultTimestampLabels = {
1300
1300
  today: (n, t) => createTimeDigits(n) + ':' + createTimeDigits(t),
1301
1301
  yesterday: () => 'Yesterday',
1302
1302
  monday: () => 'Mon',
@@ -1310,7 +1310,7 @@ const DefaultTimestampTranslations = {
1310
1310
  }
1311
1311
  function getTimestamp(n, t) {
1312
1312
  const e = new Date(t?.now || Date.now()),
1313
- r = t?.translations || DefaultTimestampTranslations,
1313
+ r = t?.labels || DefaultTimestampLabels,
1314
1314
  o = isDate(n) ? n : new Date(n)
1315
1315
  if (absoluteDays(e) === absoluteDays(o)) return r.today(o.getUTCHours(), o.getUTCMinutes(), o.getUTCHours() > 12)
1316
1316
  if (absoluteDays(e) - absoluteDays(o) === 1) return r.yesterday()
@@ -1319,30 +1319,34 @@ function getTimestamp(n, t) {
1319
1319
  ? r[i.day]()
1320
1320
  : r.weeks(Math.round((e.getTime() - o.getTime()) / 6048e5))
1321
1321
  }
1322
- const DefaultAgoTranslations = {
1323
- now: () => 'A few seconds ago',
1324
- seconds: n => `${n} seconds ago`,
1325
- minutes: n => `${n} minutes ago`,
1326
- hours: n => `${n} hours ago`,
1327
- days: n => `${n} days ago`,
1328
- weeks: n => `${n} weeks ago`
1329
- }
1330
- function getAgo(n, t) {
1331
- const e = t?.now || Date.now(),
1332
- r = t?.translations || DefaultAgoTranslations,
1333
- o = exports.Types.isDate(n) ? n.getTime() : n
1334
- let i = (e - o) / 1e3
1335
- return i < 10
1336
- ? r.now()
1337
- : i < 120
1338
- ? r.seconds(Math.floor(i))
1339
- : ((i /= 60),
1340
- i < 120
1341
- ? r.minutes(Math.floor(i))
1342
- : ((i /= 60),
1343
- i < 48
1344
- ? r.hours(Math.floor(i))
1345
- : ((i /= 24), i < 14 ? r.days(Math.floor(i)) : ((i /= 7), r.weeks(Math.floor(i))))))
1322
+ const DefaultTimeDeltaLabels = {
1323
+ now: () => 'A few seconds',
1324
+ seconds: n => `${n} seconds`,
1325
+ minutes: n => `${n} minutes`,
1326
+ hours: n => `${n} hours`,
1327
+ days: n => `${n} days`,
1328
+ weeks: n => `${n} weeks`
1329
+ }
1330
+ function getTimeDelta(n, t) {
1331
+ var e
1332
+ const r = (e = t?.now) !== null && e !== void 0 ? e : Date.now(),
1333
+ o = t?.labels || DefaultTimeDeltaLabels,
1334
+ i = exports.Types.isDate(n) ? n.getTime() : n
1335
+ let s = (r - i) / 1e3
1336
+ return s < 10
1337
+ ? o.now()
1338
+ : s < 120
1339
+ ? o.seconds(Math.floor(s))
1340
+ : ((s /= 60),
1341
+ s < 120
1342
+ ? o.minutes(Math.floor(s))
1343
+ : ((s /= 60),
1344
+ s < 48
1345
+ ? o.hours(Math.floor(s))
1346
+ : ((s /= 24), s < 14 ? o.days(Math.floor(s)) : ((s /= 7), o.weeks(Math.floor(s))))))
1347
+ }
1348
+ function secondsToHumanTime(n, t = DefaultTimeDeltaLabels) {
1349
+ return getTimeDelta(0, { now: n * 1e3, labels: t })
1346
1350
  }
1347
1351
  function countCycles(n, t, e) {
1348
1352
  var r, o, i
@@ -2401,7 +2405,8 @@ function raycastCircle(n, t, e) {
2401
2405
  (exports.Promises = { raceFulfilled, invert: invertPromise, runInParallelBatches, makeAsyncQueue }),
2402
2406
  (exports.Dates = {
2403
2407
  getTimestamp,
2404
- getAgo,
2408
+ getTimeDelta,
2409
+ secondsToHumanTime,
2405
2410
  countCycles,
2406
2411
  isoDate,
2407
2412
  throttle,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "21.5.0",
3
+ "version": "22.0.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {