cafe-utility 10.8.0 → 10.9.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 +10 -5
  2. package/index.js +21 -1
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -182,6 +182,14 @@ declare function getAgoStructured(dateOrTimestamp: Date | number, now?: number):
182
182
  value: number;
183
183
  unit: string;
184
184
  };
185
+ declare type CountCyclesOptions = {
186
+ precision?: number;
187
+ now?: number;
188
+ };
189
+ declare function countCycles(since: number, cycleLength: number, options?: CountCyclesOptions): {
190
+ cycles: number;
191
+ remaining: number;
192
+ };
185
193
  declare function throttle(identifier: string, millis: number): boolean;
186
194
  declare function timeSince(unit: 's' | 'm' | 'h' | 'd', a: Date | number, optionalB?: Date | number): number;
187
195
  interface Progress {
@@ -281,17 +289,13 @@ declare function transformToArray(objectOfArrays: CafeObject<unknown[]>): CafeOb
281
289
  declare function incrementMulti<T>(objects: T[], key: keyof T, step?: number): void;
282
290
  declare function setMulti<T, K extends keyof T>(objects: T[], key: K, value: T[K]): void;
283
291
  declare function group<T>(array: T[], groupFn: (current: T, previous: T) => boolean): T[][];
284
- interface FastIndexItem<T> {
285
- validUntil: number;
286
- data: T;
287
- }
288
292
  interface FastIndex<T> {
289
293
  index: CafeObject<T>;
290
294
  keys: string[];
291
295
  }
292
296
  declare function createFastIndex<T>(): FastIndex<T>;
293
297
  declare function pushToFastIndex<T>(object: FastIndex<T>, key: string, item: T, limit?: number): void;
294
- declare function pushToFastIndexWithExpiracy<T>(object: FastIndex<FastIndexItem<T>>, key: string, item: T, expiration: number, limit?: number): void;
298
+ declare function pushToFastIndexWithExpiracy<T>(object: FastIndex<T>, key: string, item: T, expiration: number, limit?: number): void;
295
299
  declare function getFromFastIndexWithExpiracy<T>(object: FastIndex<T>, key: string): T | null;
296
300
  declare function makeAsyncQueue(concurrency?: number): {
297
301
  enqueue(fn: () => Promise<void>): void;
@@ -419,6 +423,7 @@ export declare const Promises: {
419
423
  export declare const Dates: {
420
424
  getAgo: typeof getAgo;
421
425
  getAgoStructured: typeof getAgoStructured;
426
+ countCycles: typeof countCycles;
422
427
  isoDate: typeof isoDate;
423
428
  throttle: typeof throttle;
424
429
  timeSince: typeof timeSince;
package/index.js CHANGED
@@ -536,7 +536,11 @@ function deepMergeInPlace(target, source) {
536
536
  deepMergeInPlace(target[key], source[key])
537
537
  } else if (Array.isArray(source[key])) {
538
538
  target[key] = [...source[key]]
539
- } else {
539
+ } else if (
540
+ (source[key] !== null && source[key] !== undefined) ||
541
+ target[key] === null ||
542
+ target[key] === undefined
543
+ ) {
540
544
  target[key] = source[key]
541
545
  }
542
546
  }
@@ -1314,6 +1318,21 @@ function getAgoStructured(dateOrTimestamp, now) {
1314
1318
  return { value: Math.floor(delta), unit: 'day' }
1315
1319
  }
1316
1320
 
1321
+ function countCycles(since, cycleLength, options) {
1322
+ var _a, _b
1323
+ const now =
1324
+ (_a = options === null || options === void 0 ? void 0 : options.now) !== null && _a !== void 0 ? _a : Date.now()
1325
+ const delta = now - since
1326
+ const cycles = Math.floor(delta / cycleLength)
1327
+ const remaining = Math.ceil(
1328
+ (delta % cycleLength) /
1329
+ ((_b = options === null || options === void 0 ? void 0 : options.precision) !== null && _b !== void 0
1330
+ ? _b
1331
+ : 1)
1332
+ )
1333
+ return { cycles, remaining }
1334
+ }
1335
+
1317
1336
  const throttleTimers = {}
1318
1337
  function throttle(identifier, millis) {
1319
1338
  if (!throttleTimers[identifier] || Date.now() > throttleTimers[identifier]) {
@@ -2534,6 +2553,7 @@ exports.Promises = {
2534
2553
  exports.Dates = {
2535
2554
  getAgo,
2536
2555
  getAgoStructured,
2556
+ countCycles,
2537
2557
  isoDate,
2538
2558
  throttle,
2539
2559
  timeSince,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cafe-utility",
3
- "version": "10.8.0",
3
+ "version": "10.9.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "exports": {