@rwaterman/utils 1.1.1 → 1.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.
package/README.md CHANGED
@@ -29,6 +29,7 @@ Node 24+, TypeScript 7 (native compiler), Vitest, oxlint.
29
29
 
30
30
  ```sh
31
31
  npm ci
32
+ pre-commit install
32
33
  npm test
33
34
  npm run lint
34
35
  npm run build
@@ -1,6 +1,15 @@
1
+ function defaultKey(...args) {
2
+ return JSON.stringify(args, (_, value) => {
3
+ if (value === undefined)
4
+ return '\u0000undefined';
5
+ if (typeof value === 'bigint')
6
+ return `${value}n`;
7
+ return value;
8
+ });
9
+ }
1
10
  export function memoize(fn, options = {}) {
2
11
  const cache = new Map();
3
- const keyOf = options.key ?? ((...args) => JSON.stringify(args));
12
+ const keyOf = options.key ?? defaultKey;
4
13
  return (...args) => {
5
14
  const key = keyOf(...args);
6
15
  const hit = cache.get(key);
@@ -6,7 +6,7 @@ export async function retry(params) {
6
6
  for (let attempt = 0; attempt < numTimes; ++attempt) {
7
7
  if (attempt > 0 && delayMs > 0)
8
8
  await delay(Math.min(delayMs * factor ** (attempt - 1), maxDelayMs));
9
- const { err, res } = await to(params.yourAsyncFn());
9
+ const { err, res } = await to((async () => params.yourAsyncFn())());
10
10
  if (err === null)
11
11
  return { res, errors };
12
12
  errors.push(err);
@@ -4,7 +4,10 @@ export class TimeoutError extends Error {
4
4
  this.name = 'TimeoutError';
5
5
  }
6
6
  }
7
+ const MAX_TIMER_MS = 2 ** 31 - 1;
7
8
  export async function timeout(promise, ms) {
9
+ if (ms > MAX_TIMER_MS)
10
+ return promise;
8
11
  let timer;
9
12
  const expiry = new Promise((_, reject) => {
10
13
  timer = setTimeout(() => reject(new TimeoutError(ms)), ms);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rwaterman/utils",
3
- "version": "1.1.1",
3
+ "version": "1.3.0",
4
4
  "description": "Typed, small, dependency-free, lightweight async helper/utility functions we use all the time",
5
5
  "type": "module",
6
6
  "exports": {
@@ -47,6 +47,6 @@
47
47
  "oxlint-tsgolint": "^7.0.2001",
48
48
  "prettier": "^3.9.6",
49
49
  "typescript": "^7.0.2",
50
- "vitest": "^4.1.11"
50
+ "vitest": "^5.0.5"
51
51
  }
52
52
  }