@terrygonguet/utils 0.0.13 → 0.1.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/LICENSE CHANGED
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2023 Terry Gonguet
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Terry Gonguet
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,2 +1,2 @@
1
- # utils
2
- Okay fine I'll make my own
1
+ # utils
2
+ Okay fine I'll make my own
@@ -0,0 +1,15 @@
1
+ declare function pause(ms: number): Promise<void>;
2
+ interface RetryOptions {
3
+ count?: number;
4
+ delay?: number | ((retryCount: number, error: any) => number);
5
+ }
6
+ declare function retry(options: RetryOptions): <T>(provider: () => Promise<T>) => Promise<T>;
7
+ declare function retry<T>(provider: () => Promise<T>, options?: RetryOptions): Promise<T>;
8
+ interface AsyncMapOptions {
9
+ concurrent?: number;
10
+ }
11
+ declare function asyncMap<T, U>(f: AsyncMapFn<T, U>, options?: AsyncMapOptions): (data: T[]) => Promise<U[]>;
12
+ declare function asyncMap<T, U>(data: T[], f: AsyncMapFn<T, U>, options?: AsyncMapOptions): Promise<U[]>;
13
+ type AsyncMapFn<T, U> = (el: T, i: number, data: T[]) => Promise<U>;
14
+
15
+ export { asyncMap, pause, retry };
package/dist/async.js CHANGED
@@ -1,65 +1,77 @@
1
- import { findIndex as g } from "./functional.js";
2
- function h(n) {
3
- return new Promise((t) => setTimeout(t, n));
1
+ // src/async.ts
2
+ function pause(ms) {
3
+ return new Promise((resolve) => setTimeout(resolve, ms));
4
4
  }
5
- function x(n, t) {
6
- return typeof n == "function" ? p(n, t) : (r) => p(r, n);
5
+ function retry(providerOrOptions, options) {
6
+ if (typeof providerOrOptions == "function")
7
+ return _retry(providerOrOptions, options);
8
+ else
9
+ return (provider) => _retry(provider, providerOrOptions);
7
10
  }
8
- async function p(n, { count: t = 1 / 0, delay: r } = {}) {
9
- let o = 0, s;
10
- do
11
+ async function _retry(provider, { count = Infinity, delay } = {}) {
12
+ let retryCount = 0;
13
+ let lastError;
14
+ do {
11
15
  try {
12
- return await n();
13
- } catch (c) {
14
- switch (s = c, o++, typeof r) {
16
+ return await provider();
17
+ } catch (error) {
18
+ lastError = error;
19
+ retryCount++;
20
+ switch (typeof delay) {
15
21
  case "number":
16
- await h(r);
22
+ await pause(delay);
17
23
  break;
18
24
  case "function":
19
- await h(r(o, c));
25
+ await pause(delay(retryCount, error));
20
26
  break;
21
27
  }
22
28
  }
23
- while (o <= t);
24
- throw s;
29
+ } while (retryCount <= count);
30
+ throw lastError;
25
31
  }
26
- function I(n, t, r) {
27
- if (typeof n == "function" && typeof t != "function") {
28
- const o = n, s = t;
29
- return (c) => w(c, o, s);
30
- } else {
31
- if (Array.isArray(n) && typeof t == "function")
32
- return w(n, t, r);
33
- throw new Error("Invalid arguments passed to asyncMap");
34
- }
32
+ function asyncMap(dataOrF, ForOptions, options) {
33
+ if (typeof dataOrF == "function" && typeof ForOptions != "function") {
34
+ const f = dataOrF;
35
+ const options2 = ForOptions;
36
+ return (data) => asyncMap_(data, f, options2);
37
+ } else if (Array.isArray(dataOrF) && typeof ForOptions == "function") {
38
+ const data = dataOrF;
39
+ const f = ForOptions;
40
+ return asyncMap_(data, f, options);
41
+ } else throw new Error("Invalid arguments passed to asyncMap");
35
42
  }
36
- async function w(n, t, { concurrent: r = 5 } = {}) {
37
- return new Promise((o, s) => {
38
- const c = [], u = [];
39
- let i = 0;
40
- function y(e) {
41
- if (c.length == n.length && u.length == 0)
42
- return m();
43
- if (e >= n.length)
44
- return;
45
- const f = n[e];
46
- u.push([
47
- e,
48
- t(f, e, n).then((a) => {
49
- c.push([e, a]), g(u, ([l]) => e == l).map((l) => u.splice(l, 1)), y(i);
50
- }, s)
51
- ]), i++;
43
+ async function asyncMap_(data, f, { concurrent = 5 } = {}) {
44
+ return new Promise((resolve, reject) => {
45
+ const resolved = [];
46
+ const inFlight = [];
47
+ let next = 0;
48
+ function queue(i) {
49
+ if (resolved.length == data.length && inFlight.length == 0)
50
+ return finish();
51
+ if (i >= data.length) return;
52
+ const el = data[i];
53
+ inFlight.push([
54
+ i,
55
+ f(el, i, data).then((result) => {
56
+ resolved.push([i, result]);
57
+ const j = inFlight.findIndex(([j2]) => i == j2);
58
+ inFlight.splice(j, 1);
59
+ queue(next);
60
+ }, reject)
61
+ ]);
62
+ next++;
52
63
  }
53
- function m() {
54
- const e = c.sort(([f], [a]) => f - a);
55
- o(e.map(([, f]) => f));
64
+ function finish() {
65
+ const sorted = resolved.sort(([a], [b]) => a - b);
66
+ resolve(sorted.map(([, value]) => value));
67
+ }
68
+ for (let i = 0; i < concurrent; i++) {
69
+ queue(next);
56
70
  }
57
- for (let e = 0; e < r; e++)
58
- y(i);
59
71
  });
60
72
  }
61
73
  export {
62
- I as asyncMap,
63
- h as pause,
64
- x as retry
74
+ asyncMap,
75
+ pause,
76
+ retry
65
77
  };
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Behaviour is undefined when max < min
3
+ */
4
+ declare function clamp(value: number, min: number, max: number): number;
5
+ type JSONReviver = (key: string, value: any) => any;
6
+ /**
7
+ * This function does no runtime type checking,
8
+ * make sure that the parsed value is valid
9
+ */
10
+ declare function safeParse<T>(str: string, defaultValue: T, reviver?: JSONReviver): T;
11
+ declare function composeJSONRevivers(...revivers: JSONReviver[]): JSONReviver;
12
+ declare function createNoopProxy<T>(): T;
13
+ declare function noop(): void;
14
+ declare function exhaustive(_: never): never;
15
+ declare function hash(message: string): Promise<ArrayBuffer>;
16
+ declare function range(start: number, end: number, step?: number): Generator<number, void, unknown>;
17
+
18
+ export { clamp, composeJSONRevivers, createNoopProxy, exhaustive, hash, noop, range, safeParse };
package/dist/index.js CHANGED
@@ -1,55 +1,62 @@
1
- import { constant as r } from "./functional.js";
2
- function s(n, t, e) {
3
- return Math.min(Math.max(n, t), e);
1
+ // src/index.ts
2
+ function clamp(value, min, max) {
3
+ return Math.min(Math.max(value, min), max);
4
4
  }
5
- function a(n, t, e) {
5
+ function safeParse(str, defaultValue, reviver) {
6
6
  try {
7
- return JSON.parse(n, e);
8
- } catch {
9
- return t;
7
+ return JSON.parse(str, reviver);
8
+ } catch (_) {
9
+ return defaultValue;
10
10
  }
11
11
  }
12
- function i(...n) {
13
- return function(t, e) {
14
- for (const o of n)
15
- e = o(t, e);
16
- return e;
12
+ function composeJSONRevivers(...revivers) {
13
+ return function(key, value) {
14
+ for (const reviver of revivers) {
15
+ value = reviver(key, value);
16
+ }
17
+ return value;
17
18
  };
18
19
  }
19
- function u() {
20
- const n = () => o, t = r(!1), e = r(!0), o = new Proxy(() => {
20
+ function createNoopProxy() {
21
+ const noop2 = () => proxy;
22
+ const no = () => false;
23
+ const yes = () => true;
24
+ const proxy = new Proxy(() => {
21
25
  }, {
22
- get: n,
23
- set: n,
24
- apply: n,
25
- construct: n,
26
- deleteProperty: e,
27
- has: e,
28
- preventExtensions: t,
29
- defineProperty: t
26
+ get: noop2,
27
+ set: noop2,
28
+ apply: noop2,
29
+ construct: noop2,
30
+ deleteProperty: yes,
31
+ has: yes,
32
+ preventExtensions: no,
33
+ defineProperty: no
30
34
  });
31
- return o;
35
+ return proxy;
32
36
  }
33
- function f() {
37
+ function noop() {
34
38
  }
35
- function p(n) {
39
+ function exhaustive(_) {
36
40
  throw new Error("This should never be called");
37
41
  }
38
- async function h(n) {
39
- const e = new TextEncoder().encode(n);
40
- return await crypto.subtle.digest("SHA-1", e);
42
+ async function hash(message) {
43
+ const encoder = new TextEncoder();
44
+ const data = encoder.encode(message);
45
+ const hash2 = await crypto.subtle.digest("SHA-1", data);
46
+ return hash2;
41
47
  }
42
- function* d(n, t, e = 1) {
43
- for (let o = n; o < t; o += e)
44
- yield o;
48
+ function* range(start, end, step = 1) {
49
+ for (let i = start; i < end; i += step) {
50
+ yield i;
51
+ }
45
52
  }
46
53
  export {
47
- s as clamp,
48
- i as composeJSONRevivers,
49
- u as createNoopProxy,
50
- p as exhaustive,
51
- h as hash,
52
- f as noop,
53
- d as range,
54
- a as safeParse
54
+ clamp,
55
+ composeJSONRevivers,
56
+ createNoopProxy,
57
+ exhaustive,
58
+ hash,
59
+ noop,
60
+ range,
61
+ safeParse
55
62
  };
package/package.json CHANGED
@@ -1,63 +1,45 @@
1
- {
2
- "name": "@terrygonguet/utils",
3
- "version": "0.0.13",
4
- "type": "module",
5
- "module": "./dist/index.js",
6
- "author": {
7
- "email": "terry@gonguet.com",
8
- "name": "Terry Gonguet",
9
- "url": "https://terry.gonguet.com/"
10
- },
11
- "scripts": {
12
- "dev": "vite",
13
- "build": "tsc && vite build",
14
- "test": "vitest test --ui --watch --update",
15
- "coverage": "vitest run --coverage"
16
- },
17
- "devDependencies": {
18
- "@vitest/coverage-v8": "^0.32.0",
19
- "@vitest/ui": "^0.32.0",
20
- "prettier": "^2.8.8",
21
- "typescript": "^5.0.2",
22
- "vite": "^4.3.9",
23
- "vitest": "^0.32.0"
24
- },
25
- "files": [
26
- "dist/**/*.js",
27
- "src/**/!(*.test).ts",
28
- "types/**/!(*.test).d.ts"
29
- ],
30
- "exports": {
31
- ".": {
32
- "import": "./dist/index.js"
33
- },
34
- "./async": {
35
- "import": "./dist/async.js"
36
- },
37
- "./functional": {
38
- "import": "./dist/functional.js"
39
- }
40
- },
41
- "typesVersions": {
42
- "*": {
43
- "async": [
44
- "./types/async.d.ts"
45
- ],
46
- "functional": [
47
- "./types/functional/index.d.ts"
48
- ]
49
- }
50
- },
51
- "types": "./types/index.d.ts",
52
- "dependencies": {
53
- "just-compose": "^2.3.0",
54
- "just-pipe": "^1.0.0"
55
- },
56
- "repository": {
57
- "type": "git",
58
- "url": "https://github.com/terrygonguet/utils.git"
59
- },
60
- "bugs": {
61
- "url": "https://github.com/terrygonguet/utils/issues"
62
- }
63
- }
1
+ {
2
+ "name": "@terrygonguet/utils",
3
+ "version": "0.1.0",
4
+ "type": "module",
5
+ "scripts": {
6
+ "dev": "vite",
7
+ "build": "tsup",
8
+ "test": "vitest test --update",
9
+ "coverage": "vitest run --coverage"
10
+ },
11
+ "files": [
12
+ "dist"
13
+ ],
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "import": "./dist/index.js"
18
+ },
19
+ "./async": {
20
+ "types": "./dist/async.d.ts",
21
+ "import": "./dist/async.js"
22
+ }
23
+ },
24
+ "devDependencies": {
25
+ "@vitest/coverage-v8": "^0.32.0",
26
+ "@vitest/ui": "^0.32.0",
27
+ "prettier": "^2.8.8",
28
+ "tsup": "^8.1.0",
29
+ "typescript": "^5.0.2",
30
+ "vite": "^4.3.9",
31
+ "vitest": "^0.32.0"
32
+ },
33
+ "author": {
34
+ "email": "terry@gonguet.com",
35
+ "name": "Terry Gonguet",
36
+ "url": "https://terry.gonguet.com/"
37
+ },
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "https://github.com/terrygonguet/utils.git"
41
+ },
42
+ "bugs": {
43
+ "url": "https://github.com/terrygonguet/utils/issues"
44
+ }
45
+ }