@cacheable/utils
Advanced tools
+68
-70
@@ -47,2 +47,69 @@ "use strict"; | ||
| // src/shorthand-time.ts | ||
| var shorthandToMilliseconds = (shorthand) => { | ||
| let milliseconds; | ||
| if (shorthand === void 0) { | ||
| return void 0; | ||
| } | ||
| if (typeof shorthand === "number") { | ||
| milliseconds = shorthand; | ||
| } else if (typeof shorthand === "string") { | ||
| shorthand = shorthand.trim(); | ||
| if (Number.isNaN(Number(shorthand))) { | ||
| const match = /^([\d.]+)\s*(ms|s|m|h|hr|d)$/i.exec(shorthand); | ||
| if (!match) { | ||
| throw new Error( | ||
| `Unsupported time format: "${shorthand}". Use 'ms', 's', 'm', 'h', 'hr', or 'd'.` | ||
| ); | ||
| } | ||
| const [, value, unit] = match; | ||
| const numericValue = Number.parseFloat(value); | ||
| const unitLower = unit.toLowerCase(); | ||
| switch (unitLower) { | ||
| case "ms": { | ||
| milliseconds = numericValue; | ||
| break; | ||
| } | ||
| case "s": { | ||
| milliseconds = numericValue * 1e3; | ||
| break; | ||
| } | ||
| case "m": { | ||
| milliseconds = numericValue * 1e3 * 60; | ||
| break; | ||
| } | ||
| case "h": { | ||
| milliseconds = numericValue * 1e3 * 60 * 60; | ||
| break; | ||
| } | ||
| case "hr": { | ||
| milliseconds = numericValue * 1e3 * 60 * 60; | ||
| break; | ||
| } | ||
| case "d": { | ||
| milliseconds = numericValue * 1e3 * 60 * 60 * 24; | ||
| break; | ||
| } | ||
| /* c8 ignore next 3 */ | ||
| default: { | ||
| milliseconds = Number(shorthand); | ||
| } | ||
| } | ||
| } else { | ||
| milliseconds = Number(shorthand); | ||
| } | ||
| } else { | ||
| throw new TypeError("Time must be a string or a number."); | ||
| } | ||
| return milliseconds; | ||
| }; | ||
| var shorthandToTime = (shorthand, fromDate) => { | ||
| fromDate ??= /* @__PURE__ */ new Date(); | ||
| const milliseconds = shorthandToMilliseconds(shorthand); | ||
| if (milliseconds === void 0) { | ||
| return fromDate.getTime(); | ||
| } | ||
| return fromDate.getTime() + milliseconds; | ||
| }; | ||
| // src/coalesce-async.ts | ||
@@ -145,67 +212,2 @@ var callbacks = /* @__PURE__ */ new Map(); | ||
| // src/shorthand-time.ts | ||
| var shorthandToMilliseconds = (shorthand) => { | ||
| let milliseconds; | ||
| if (shorthand === void 0) { | ||
| return void 0; | ||
| } | ||
| if (typeof shorthand === "number") { | ||
| milliseconds = shorthand; | ||
| } else if (typeof shorthand === "string") { | ||
| shorthand = shorthand.trim(); | ||
| if (Number.isNaN(Number(shorthand))) { | ||
| const match = /^([\d.]+)\s*(ms|s|m|h|hr|d)$/i.exec(shorthand); | ||
| if (!match) { | ||
| throw new Error(`Unsupported time format: "${shorthand}". Use 'ms', 's', 'm', 'h', 'hr', or 'd'.`); | ||
| } | ||
| const [, value, unit] = match; | ||
| const numericValue = Number.parseFloat(value); | ||
| const unitLower = unit.toLowerCase(); | ||
| switch (unitLower) { | ||
| case "ms": { | ||
| milliseconds = numericValue; | ||
| break; | ||
| } | ||
| case "s": { | ||
| milliseconds = numericValue * 1e3; | ||
| break; | ||
| } | ||
| case "m": { | ||
| milliseconds = numericValue * 1e3 * 60; | ||
| break; | ||
| } | ||
| case "h": { | ||
| milliseconds = numericValue * 1e3 * 60 * 60; | ||
| break; | ||
| } | ||
| case "hr": { | ||
| milliseconds = numericValue * 1e3 * 60 * 60; | ||
| break; | ||
| } | ||
| case "d": { | ||
| milliseconds = numericValue * 1e3 * 60 * 60 * 24; | ||
| break; | ||
| } | ||
| /* c8 ignore next 3 */ | ||
| default: { | ||
| milliseconds = Number(shorthand); | ||
| } | ||
| } | ||
| } else { | ||
| milliseconds = Number(shorthand); | ||
| } | ||
| } else { | ||
| throw new TypeError("Time must be a string or a number."); | ||
| } | ||
| return milliseconds; | ||
| }; | ||
| var shorthandToTime = (shorthand, fromDate) => { | ||
| fromDate ??= /* @__PURE__ */ new Date(); | ||
| const milliseconds = shorthandToMilliseconds(shorthand); | ||
| if (milliseconds === void 0) { | ||
| return fromDate.getTime(); | ||
| } | ||
| return fromDate.getTime() + milliseconds; | ||
| }; | ||
| // src/sleep.ts | ||
@@ -342,3 +344,2 @@ var sleep = async (ms) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
| } | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| incrementVSize(value) { | ||
@@ -350,3 +351,2 @@ if (!this._enabled) { | ||
| } | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| decreaseVSize(value) { | ||
@@ -358,3 +358,2 @@ if (!this._enabled) { | ||
| } | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| incrementKSize(key) { | ||
@@ -366,3 +365,2 @@ if (!this._enabled) { | ||
| } | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| decreaseKSize(key) { | ||
@@ -458,3 +456,3 @@ if (!this._enabled) { | ||
| } | ||
| if (expires > expiresFromTtl) { | ||
| if (expires && expires > expiresFromTtl) { | ||
| return ttl; | ||
@@ -461,0 +459,0 @@ } |
+23
-23
| /** | ||
| * Converts a shorthand time string or number into milliseconds. | ||
| * The shorthand can be a string like '1s', '2m', '3h', '4d', or a number representing milliseconds. | ||
| * If the input is undefined, it returns undefined. | ||
| * If the input is a string that does not match the expected format, it throws an error. | ||
| * @param shorthand - A shorthand time string or number representing milliseconds. | ||
| * @returns The equivalent time in milliseconds or undefined. | ||
| */ | ||
| declare const shorthandToMilliseconds: (shorthand?: string | number) => number | undefined; | ||
| /** | ||
| * Converts a shorthand time string or number into a timestamp. | ||
| * If the shorthand is undefined, it returns the current date's timestamp. | ||
| * If the shorthand is a valid time format, it adds that duration to the current date's timestamp. | ||
| * @param shorthand - A shorthand time string or number representing milliseconds. | ||
| * @param fromDate - An optional Date object to calculate from. Defaults to the current date if not provided. | ||
| * @returns The timestamp in milliseconds since epoch. | ||
| */ | ||
| declare const shorthandToTime: (shorthand?: string | number, fromDate?: Date) => number; | ||
| /** | ||
| * CacheableItem | ||
@@ -41,8 +60,8 @@ * @typedef {Object} CacheableItem | ||
| /** | ||
| * Any identifier to group requests together. | ||
| */ | ||
| * Any identifier to group requests together. | ||
| */ | ||
| key: string, | ||
| /** | ||
| * The function to run. | ||
| */ | ||
| * The function to run. | ||
| */ | ||
| fnc: () => T | PromiseLike<T>): Promise<T>; | ||
@@ -65,21 +84,2 @@ | ||
| /** | ||
| * Converts a shorthand time string or number into milliseconds. | ||
| * The shorthand can be a string like '1s', '2m', '3h', '4d', or a number representing milliseconds. | ||
| * If the input is undefined, it returns undefined. | ||
| * If the input is a string that does not match the expected format, it throws an error. | ||
| * @param shorthand - A shorthand time string or number representing milliseconds. | ||
| * @returns The equivalent time in milliseconds or undefined. | ||
| */ | ||
| declare const shorthandToMilliseconds: (shorthand?: string | number) => number | undefined; | ||
| /** | ||
| * Converts a shorthand time string or number into a timestamp. | ||
| * If the shorthand is undefined, it returns the current date's timestamp. | ||
| * If the shorthand is a valid time format, it adds that duration to the current date's timestamp. | ||
| * @param shorthand - A shorthand time string or number representing milliseconds. | ||
| * @param fromDate - An optional Date object to calculate from. Defaults to the current date if not provided. | ||
| * @returns The timestamp in milliseconds since epoch. | ||
| */ | ||
| declare const shorthandToTime: (shorthand?: string | number, fromDate?: Date) => number; | ||
| declare const sleep: (ms: number) => Promise<unknown>; | ||
@@ -86,0 +86,0 @@ |
+23
-23
| /** | ||
| * Converts a shorthand time string or number into milliseconds. | ||
| * The shorthand can be a string like '1s', '2m', '3h', '4d', or a number representing milliseconds. | ||
| * If the input is undefined, it returns undefined. | ||
| * If the input is a string that does not match the expected format, it throws an error. | ||
| * @param shorthand - A shorthand time string or number representing milliseconds. | ||
| * @returns The equivalent time in milliseconds or undefined. | ||
| */ | ||
| declare const shorthandToMilliseconds: (shorthand?: string | number) => number | undefined; | ||
| /** | ||
| * Converts a shorthand time string or number into a timestamp. | ||
| * If the shorthand is undefined, it returns the current date's timestamp. | ||
| * If the shorthand is a valid time format, it adds that duration to the current date's timestamp. | ||
| * @param shorthand - A shorthand time string or number representing milliseconds. | ||
| * @param fromDate - An optional Date object to calculate from. Defaults to the current date if not provided. | ||
| * @returns The timestamp in milliseconds since epoch. | ||
| */ | ||
| declare const shorthandToTime: (shorthand?: string | number, fromDate?: Date) => number; | ||
| /** | ||
| * CacheableItem | ||
@@ -41,8 +60,8 @@ * @typedef {Object} CacheableItem | ||
| /** | ||
| * Any identifier to group requests together. | ||
| */ | ||
| * Any identifier to group requests together. | ||
| */ | ||
| key: string, | ||
| /** | ||
| * The function to run. | ||
| */ | ||
| * The function to run. | ||
| */ | ||
| fnc: () => T | PromiseLike<T>): Promise<T>; | ||
@@ -65,21 +84,2 @@ | ||
| /** | ||
| * Converts a shorthand time string or number into milliseconds. | ||
| * The shorthand can be a string like '1s', '2m', '3h', '4d', or a number representing milliseconds. | ||
| * If the input is undefined, it returns undefined. | ||
| * If the input is a string that does not match the expected format, it throws an error. | ||
| * @param shorthand - A shorthand time string or number representing milliseconds. | ||
| * @returns The equivalent time in milliseconds or undefined. | ||
| */ | ||
| declare const shorthandToMilliseconds: (shorthand?: string | number) => number | undefined; | ||
| /** | ||
| * Converts a shorthand time string or number into a timestamp. | ||
| * If the shorthand is undefined, it returns the current date's timestamp. | ||
| * If the shorthand is a valid time format, it adds that duration to the current date's timestamp. | ||
| * @param shorthand - A shorthand time string or number representing milliseconds. | ||
| * @param fromDate - An optional Date object to calculate from. Defaults to the current date if not provided. | ||
| * @returns The timestamp in milliseconds since epoch. | ||
| */ | ||
| declare const shorthandToTime: (shorthand?: string | number, fromDate?: Date) => number; | ||
| declare const sleep: (ms: number) => Promise<unknown>; | ||
@@ -86,0 +86,0 @@ |
+68
-70
@@ -0,1 +1,68 @@ | ||
| // src/shorthand-time.ts | ||
| var shorthandToMilliseconds = (shorthand) => { | ||
| let milliseconds; | ||
| if (shorthand === void 0) { | ||
| return void 0; | ||
| } | ||
| if (typeof shorthand === "number") { | ||
| milliseconds = shorthand; | ||
| } else if (typeof shorthand === "string") { | ||
| shorthand = shorthand.trim(); | ||
| if (Number.isNaN(Number(shorthand))) { | ||
| const match = /^([\d.]+)\s*(ms|s|m|h|hr|d)$/i.exec(shorthand); | ||
| if (!match) { | ||
| throw new Error( | ||
| `Unsupported time format: "${shorthand}". Use 'ms', 's', 'm', 'h', 'hr', or 'd'.` | ||
| ); | ||
| } | ||
| const [, value, unit] = match; | ||
| const numericValue = Number.parseFloat(value); | ||
| const unitLower = unit.toLowerCase(); | ||
| switch (unitLower) { | ||
| case "ms": { | ||
| milliseconds = numericValue; | ||
| break; | ||
| } | ||
| case "s": { | ||
| milliseconds = numericValue * 1e3; | ||
| break; | ||
| } | ||
| case "m": { | ||
| milliseconds = numericValue * 1e3 * 60; | ||
| break; | ||
| } | ||
| case "h": { | ||
| milliseconds = numericValue * 1e3 * 60 * 60; | ||
| break; | ||
| } | ||
| case "hr": { | ||
| milliseconds = numericValue * 1e3 * 60 * 60; | ||
| break; | ||
| } | ||
| case "d": { | ||
| milliseconds = numericValue * 1e3 * 60 * 60 * 24; | ||
| break; | ||
| } | ||
| /* c8 ignore next 3 */ | ||
| default: { | ||
| milliseconds = Number(shorthand); | ||
| } | ||
| } | ||
| } else { | ||
| milliseconds = Number(shorthand); | ||
| } | ||
| } else { | ||
| throw new TypeError("Time must be a string or a number."); | ||
| } | ||
| return milliseconds; | ||
| }; | ||
| var shorthandToTime = (shorthand, fromDate) => { | ||
| fromDate ??= /* @__PURE__ */ new Date(); | ||
| const milliseconds = shorthandToMilliseconds(shorthand); | ||
| if (milliseconds === void 0) { | ||
| return fromDate.getTime(); | ||
| } | ||
| return fromDate.getTime() + milliseconds; | ||
| }; | ||
| // src/coalesce-async.ts | ||
@@ -98,67 +165,2 @@ var callbacks = /* @__PURE__ */ new Map(); | ||
| // src/shorthand-time.ts | ||
| var shorthandToMilliseconds = (shorthand) => { | ||
| let milliseconds; | ||
| if (shorthand === void 0) { | ||
| return void 0; | ||
| } | ||
| if (typeof shorthand === "number") { | ||
| milliseconds = shorthand; | ||
| } else if (typeof shorthand === "string") { | ||
| shorthand = shorthand.trim(); | ||
| if (Number.isNaN(Number(shorthand))) { | ||
| const match = /^([\d.]+)\s*(ms|s|m|h|hr|d)$/i.exec(shorthand); | ||
| if (!match) { | ||
| throw new Error(`Unsupported time format: "${shorthand}". Use 'ms', 's', 'm', 'h', 'hr', or 'd'.`); | ||
| } | ||
| const [, value, unit] = match; | ||
| const numericValue = Number.parseFloat(value); | ||
| const unitLower = unit.toLowerCase(); | ||
| switch (unitLower) { | ||
| case "ms": { | ||
| milliseconds = numericValue; | ||
| break; | ||
| } | ||
| case "s": { | ||
| milliseconds = numericValue * 1e3; | ||
| break; | ||
| } | ||
| case "m": { | ||
| milliseconds = numericValue * 1e3 * 60; | ||
| break; | ||
| } | ||
| case "h": { | ||
| milliseconds = numericValue * 1e3 * 60 * 60; | ||
| break; | ||
| } | ||
| case "hr": { | ||
| milliseconds = numericValue * 1e3 * 60 * 60; | ||
| break; | ||
| } | ||
| case "d": { | ||
| milliseconds = numericValue * 1e3 * 60 * 60 * 24; | ||
| break; | ||
| } | ||
| /* c8 ignore next 3 */ | ||
| default: { | ||
| milliseconds = Number(shorthand); | ||
| } | ||
| } | ||
| } else { | ||
| milliseconds = Number(shorthand); | ||
| } | ||
| } else { | ||
| throw new TypeError("Time must be a string or a number."); | ||
| } | ||
| return milliseconds; | ||
| }; | ||
| var shorthandToTime = (shorthand, fromDate) => { | ||
| fromDate ??= /* @__PURE__ */ new Date(); | ||
| const milliseconds = shorthandToMilliseconds(shorthand); | ||
| if (milliseconds === void 0) { | ||
| return fromDate.getTime(); | ||
| } | ||
| return fromDate.getTime() + milliseconds; | ||
| }; | ||
| // src/sleep.ts | ||
@@ -295,3 +297,2 @@ var sleep = async (ms) => new Promise((resolve) => setTimeout(resolve, ms)); | ||
| } | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| incrementVSize(value) { | ||
@@ -303,3 +304,2 @@ if (!this._enabled) { | ||
| } | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| decreaseVSize(value) { | ||
@@ -311,3 +311,2 @@ if (!this._enabled) { | ||
| } | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| incrementKSize(key) { | ||
@@ -319,3 +318,2 @@ if (!this._enabled) { | ||
| } | ||
| // eslint-disable-next-line @typescript-eslint/naming-convention | ||
| decreaseKSize(key) { | ||
@@ -411,3 +409,3 @@ if (!this._enabled) { | ||
| } | ||
| if (expires > expiresFromTtl) { | ||
| if (expires && expires > expiresFromTtl) { | ||
| return ttl; | ||
@@ -414,0 +412,0 @@ } |
+11
-11
| { | ||
| "name": "@cacheable/utils", | ||
| "version": "1.1.0", | ||
| "version": "1.1.1", | ||
| "description": "Cacheable Utilities for Caching Libraries", | ||
@@ -24,7 +24,7 @@ "type": "module", | ||
| "devDependencies": { | ||
| "@faker-js/faker": "^9.9.0", | ||
| "@keyv/redis": "^5.0.0", | ||
| "@keyv/valkey": "^1.0.7", | ||
| "@types/eslint": "^9.6.1", | ||
| "@types/node": "^24.1.0", | ||
| "@biomejs/biome": "^2.2.2", | ||
| "@faker-js/faker": "^10.0.0", | ||
| "@keyv/redis": "^5.1.1", | ||
| "@keyv/valkey": "^1.0.8", | ||
| "@types/node": "^24.3.0", | ||
| "@vitest/coverage-v8": "^3.2.4", | ||
@@ -34,5 +34,4 @@ "lru-cache": "^11.1.0", | ||
| "tsup": "^8.5.0", | ||
| "typescript": "^5.8.3", | ||
| "vitest": "^3.2.4", | ||
| "xo": "^1.2.1" | ||
| "typescript": "^5.9.2", | ||
| "vitest": "^3.2.4" | ||
| }, | ||
@@ -54,6 +53,7 @@ "keywords": [ | ||
| "prepublish": "pnpm build", | ||
| "test": "xo --fix && vitest run --coverage", | ||
| "test:ci": "xo && vitest run --coverage", | ||
| "lint": "biome check --write --error-on-warnings", | ||
| "test": "pnpm lint && vitest run --coverage", | ||
| "test:ci": "biome check --error-on-warnings && vitest run --coverage", | ||
| "clean": "rimraf ./dist ./coverage ./node_modules" | ||
| } | ||
| } |
11
-8.33%44823
-0.88%1065
-0.37%