cafe-utility 19.1.0 → 19.3.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 +28 -8
  2. package/index.js +41 -10
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -234,6 +234,23 @@ declare function fromUtcString(string: string): Date;
234
234
  declare function fromMillis(millis: number): Date;
235
235
  declare function createTimeDigits(value: number): string;
236
236
  declare function humanizeTime(millis: number): string;
237
+ interface TimestampTranslations {
238
+ today: (hour: number, minute: number, pm: boolean) => string;
239
+ yesterday: () => string;
240
+ monday: () => string;
241
+ tuesday: () => string;
242
+ wednesday: () => string;
243
+ thursday: () => string;
244
+ friday: () => string;
245
+ saturday: () => string;
246
+ sunday: () => string;
247
+ weeks: (value: number) => string;
248
+ }
249
+ interface GetTimestampOptions {
250
+ now?: number;
251
+ translations?: TimestampTranslations;
252
+ }
253
+ declare function getTimestamp(date: Date | number, options?: GetTimestampOptions): string;
237
254
  interface AgoTranslations {
238
255
  now: () => string;
239
256
  seconds: (value: number) => string;
@@ -266,17 +283,17 @@ interface Progress {
266
283
  }
267
284
  declare function getProgress(startedAt: number, current: number, total: number, now?: number): Progress;
268
285
  declare const dayNumberIndex: {
269
- 0: string;
270
- 1: string;
271
- 2: string;
272
- 3: string;
273
- 4: string;
274
- 5: string;
275
- 6: string;
286
+ readonly 0: "sunday";
287
+ readonly 1: "monday";
288
+ readonly 2: "tuesday";
289
+ readonly 3: "wednesday";
290
+ readonly 4: "thursday";
291
+ readonly 5: "friday";
292
+ readonly 6: "saturday";
276
293
  };
277
294
  interface DayInfo {
278
295
  zeroBasedIndex: number;
279
- day: string;
296
+ day: 'sunday' | 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday';
280
297
  }
281
298
  declare function mapDayNumber(zeroBasedIndex: keyof typeof dayNumberIndex): DayInfo;
282
299
  declare function getDayInfoFromDate(date: Date): DayInfo;
@@ -393,6 +410,7 @@ interface Newable<T> extends Function {
393
410
  declare function findInstance<T, K extends T>(array: T[], type: Newable<K>): Optional<K>;
394
411
  declare function filterInstances<T, K extends T>(array: T[], type: Newable<K>): K[];
395
412
  declare function interleave<T, K>(arrayA: T[], arrayB: K[]): (T | K)[];
413
+ declare function toggle<T>(array: T[], value: T): T[];
396
414
  type Playbook<T> = {
397
415
  ttl: number;
398
416
  ttlMax?: number;
@@ -484,6 +502,7 @@ export declare const Arrays: {
484
502
  findInstance: typeof findInstance;
485
503
  filterInstances: typeof filterInstances;
486
504
  interleave: typeof interleave;
505
+ toggle: typeof toggle;
487
506
  };
488
507
  export declare const System: {
489
508
  sleepMillis: typeof sleepMillis;
@@ -520,6 +539,7 @@ export declare const Promises: {
520
539
  makeAsyncQueue: typeof makeAsyncQueue;
521
540
  };
522
541
  export declare const Dates: {
542
+ getTimestamp: typeof getTimestamp;
523
543
  getAgo: typeof getAgo;
524
544
  countCycles: typeof countCycles;
525
545
  isoDate: typeof isoDate;
package/index.js CHANGED
@@ -598,8 +598,7 @@ function range(n, t) {
598
598
  return e
599
599
  }
600
600
  function includesAny(n, t) {
601
- for (const e of t) if (n.includes(e)) return !0
602
- return !1
601
+ return t.some(e => n.includes(e))
603
602
  }
604
603
  function isChinese(n) {
605
604
  return /^[\u4E00-\u9FA5]+$/.test(n)
@@ -1257,6 +1256,32 @@ function humanizeTime(n) {
1257
1256
  ? `${createTimeDigits(t)}:${createTimeDigits(e)}:${createTimeDigits(r)}`
1258
1257
  : `${createTimeDigits(e)}:${createTimeDigits(r)}`
1259
1258
  }
1259
+ function absoluteDays(n) {
1260
+ return Math.floor((isDate(n) ? n.getTime() : n) / 864e5)
1261
+ }
1262
+ const DefaultTimestampTranslations = {
1263
+ today: (n, t) => createTimeDigits(n) + ':' + createTimeDigits(t),
1264
+ yesterday: () => 'Yesterday',
1265
+ monday: () => 'Mon',
1266
+ tuesday: () => 'Tue',
1267
+ wednesday: () => 'Wed',
1268
+ thursday: () => 'Thu',
1269
+ friday: () => 'Fri',
1270
+ saturday: () => 'Sat',
1271
+ sunday: () => 'Sun',
1272
+ weeks: n => `${n}w`
1273
+ }
1274
+ function getTimestamp(n, t) {
1275
+ const e = new Date(t?.now || Date.now()),
1276
+ r = t?.translations || DefaultTimestampTranslations,
1277
+ o = isDate(n) ? n : new Date(n)
1278
+ if (absoluteDays(e) === absoluteDays(o)) return r.today(o.getUTCHours(), o.getUTCMinutes(), o.getUTCHours() > 12)
1279
+ if (absoluteDays(e) - absoluteDays(o) === 1) return r.yesterday()
1280
+ const i = getDayInfoFromDate(o)
1281
+ return absoluteDays(e) - absoluteDays(o) < 7
1282
+ ? r[i.day]()
1283
+ : r.weeks(Math.round((e.getTime() - o.getTime()) / 6048e5))
1284
+ }
1260
1285
  const DefaultAgoTranslations = {
1261
1286
  now: () => 'A few seconds ago',
1262
1287
  seconds: n => `${n} seconds ago`,
@@ -1481,10 +1506,8 @@ function organiseWithLimits(n, t, e, r, o) {
1481
1506
  }
1482
1507
  function diffKeys(n, t) {
1483
1508
  const e = Object.keys(n),
1484
- r = Object.keys(t),
1485
- o = e.filter(s => !r.includes(s)),
1486
- i = r.filter(s => !e.includes(s))
1487
- return { uniqueToA: o, uniqueToB: i }
1509
+ r = Object.keys(t)
1510
+ return { uniqueToA: e.filter(o => !r.includes(o)), uniqueToB: r.filter(o => !e.includes(o)) }
1488
1511
  }
1489
1512
  function pickRandomKey(n) {
1490
1513
  const t = Object.keys(n)
@@ -1748,9 +1771,12 @@ function setMulti(n, t, e) {
1748
1771
  function group(n, t) {
1749
1772
  const e = []
1750
1773
  let r = []
1751
- n.length && (e.push(r), r.push(n[0]))
1752
- for (let o = 1; o < n.length; o++) t(n[o], n[o - 1]) || ((r = []), e.push(r)), r.push(n[o])
1753
- return e
1774
+ return (
1775
+ n.forEach((o, i) => {
1776
+ ;(i === 0 || !t(o, n[i - 1])) && ((r = []), e.push(r)), r.push(o)
1777
+ }),
1778
+ e
1779
+ )
1754
1780
  }
1755
1781
  function createBidirectionalMap() {
1756
1782
  return { map: new Map(), keys: [] }
@@ -1850,6 +1876,9 @@ function interleave(n, t) {
1850
1876
  for (let o = 0; o < r; o++) n[o] && e.push(n[o]), t[o] && e.push(t[o])
1851
1877
  return e
1852
1878
  }
1879
+ function toggle(n, t) {
1880
+ return n.includes(t) ? n.filter(e => e !== t) : [...n, t]
1881
+ }
1853
1882
  function tickPlaybook(n) {
1854
1883
  if (n.length === 0) return null
1855
1884
  const t = n[0]
@@ -2132,7 +2161,8 @@ function raycastCircle(n, t, e) {
2132
2161
  bringToFrontInPlace,
2133
2162
  findInstance,
2134
2163
  filterInstances,
2135
- interleave
2164
+ interleave,
2165
+ toggle
2136
2166
  }),
2137
2167
  (exports.System = { sleepMillis, forever, scheduleMany, waitFor, expandError }),
2138
2168
  (exports.Numbers = {
@@ -2158,6 +2188,7 @@ function raycastCircle(n, t, e) {
2158
2188
  }),
2159
2189
  (exports.Promises = { raceFulfilled, invert: invertPromise, runInParallelBatches, makeAsyncQueue }),
2160
2190
  (exports.Dates = {
2191
+ getTimestamp,
2161
2192
  getAgo,
2162
2193
  countCycles,
2163
2194
  isoDate,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "19.1.0",
3
+ "version": "19.3.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {