@webreflection/utils 0.1.2 → 0.2.1

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 ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright © 2025-today, Andrea Giammarchi, @WebReflection
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
10
+ is furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included
13
+ in all 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
21
+ IN THE SOFTWARE.
package/README.md CHANGED
@@ -9,10 +9,29 @@ A [collection](./src/) of utility functions.
9
9
 
10
10
  ```js
11
11
  // example: a shim based on ArrayBuffer if native is `false`
12
- import { SharedArrayBuffer, native } from 'https://esm.run/@webreflection/utils/shared-array-buffer';
12
+ import { SharedArrayBuffer, native } from '@webreflection/utils/shared-array-buffer';
13
13
 
14
14
  // example: self bound Promise.withResolvers()
15
- import withResolvers from 'https://esm.run/@webreflection/utils/with-resolvers';
15
+ import withResolvers from '@webreflection/utils/with-resolvers';
16
16
 
17
17
  const { promise, resolve, reject } = withResolvers();
18
+
19
+ // example: any object bound method
20
+ import bound from '@webreflection/utils/bound';
21
+
22
+ const { all, resolve } = bound(Promise);
23
+ all([1, 2, 3]);
24
+ resolve(4);
25
+
26
+
27
+ // example: any object bound method is identical
28
+ // particularly useful for listeners identity
29
+ import boundOnce from '@webreflection/utils/bound-once';
30
+
31
+ const { all, resolve } = boundOnce(Promise);
32
+ all([1, 2, 3]);
33
+ resolve(4);
34
+
35
+ boundOnce(Promise).all === all;
36
+ boundOnce(Promise).resolve === resolve;
18
37
  ```
package/package.json CHANGED
@@ -1,20 +1,44 @@
1
1
  {
2
2
  "name": "@webreflection/utils",
3
- "version": "0.1.2",
3
+ "version": "0.2.1",
4
4
  "type": "module",
5
5
  "exports": {
6
- "./shared-array-buffer": "./src/shared-array-buffer.js",
7
- "./with-resolvers": "./src/with-resolvers.js",
6
+ "./bound": {
7
+ "types": "./types/bound.d.ts",
8
+ "import": "./src/bound.js"
9
+ },
10
+ "./bound-once": {
11
+ "types": "./types/bound-once.d.ts",
12
+ "import": "./src/bound-once.js"
13
+ },
14
+ "./shared-array-buffer": {
15
+ "types": "./types/shared-array-buffer.d.ts",
16
+ "import": "./src/shared-array-buffer.js"
17
+ },
18
+ "./with-resolvers": {
19
+ "types": "./types/with-resolvers.d.ts",
20
+ "import": "./src/with-resolvers.js"
21
+ },
8
22
  "./package.json": "./package.json"
9
23
  },
10
24
  "tests": [
25
+ "bound",
26
+ "bound-once",
11
27
  "shared-array-buffer",
12
28
  "with-resolvers"
13
29
  ],
14
30
  "scripts": {
31
+ "build": "npm run build:types && npm run test",
32
+ "build:types": "tsc --allowJs --checkJs --declaration --emitDeclarationOnly --outDir types --target es2022 --lib es2024 --module nodenext --moduleResolution nodenext src/*.js",
15
33
  "coverage": "mkdir -p ./coverage; c8 report --reporter=text-lcov > ./coverage/lcov.info",
16
34
  "test": "c8 node -e 'let q=Promise.resolve();for(const t of require(`./package.json`).tests)q=q.then(()=>import(`./test/${t}.js`));'"
17
35
  },
36
+ "files": [
37
+ "src",
38
+ "types",
39
+ "README.md",
40
+ "LICENSE"
41
+ ],
18
42
  "keywords": [
19
43
  "shared",
20
44
  "utility",
@@ -32,6 +56,7 @@
32
56
  },
33
57
  "homepage": "https://github.com/WebReflection/utils#readme",
34
58
  "devDependencies": {
35
- "c8": "^10.1.3"
59
+ "c8": "^11.0.0",
60
+ "typescript": "^6.0.3"
36
61
  }
37
62
  }
@@ -0,0 +1,29 @@
1
+ //@ts-check
2
+
3
+ /**
4
+ * @type {WeakMap<object, Map<string | symbol, (...args: any[]) => unknown>>}
5
+ */
6
+ const methods = new WeakMap;
7
+
8
+ /**
9
+ * @type {ProxyHandler<any>}
10
+ */
11
+ const handler = {
12
+ get(target, prop) {
13
+ const known = /** @type {Map<string | symbol, (...args: any[]) => unknown>} */ (methods.get(target));
14
+ let method = known.get(prop);
15
+ if (!method) known.set(prop, (method = target[prop].bind(target)));
16
+ return method;
17
+ }
18
+ };
19
+
20
+ /**
21
+ * @template {object} T
22
+ * @param {T} target
23
+ * @returns {import('./bound.js').Bound<T>}
24
+ */
25
+ export default target => {
26
+ let known = methods.get(target);
27
+ if (!known) methods.set(target, (known = new Map));
28
+ return new Proxy(target, handler);
29
+ };
package/src/bound.js ADDED
@@ -0,0 +1,25 @@
1
+ //@ts-check
2
+
3
+ /**
4
+ * @template F
5
+ * @typedef {unknown extends ThisParameterType<F> ? F : F extends (this: any, ...args: infer A) => infer R ? (...args: A) => R : never} BoundMethod
6
+ */
7
+
8
+ /**
9
+ * @template T
10
+ * @typedef {{[K in keyof T as T[K] extends Function ? K : never]: BoundMethod<T[K]>}} Bound
11
+ */
12
+
13
+ /**
14
+ * @type {ProxyHandler<any>}
15
+ */
16
+ const handler = {
17
+ get: (target, prop) => target[prop].bind(target),
18
+ };
19
+
20
+ /**
21
+ * @template {object} T
22
+ * @param {T} target
23
+ * @returns {Bound<T>}
24
+ */
25
+ export default target => new Proxy(target, handler);
@@ -2,21 +2,26 @@
2
2
 
3
3
  /**
4
4
  * @template T
5
- * @typedef {{promise: Promise<T>, resolve: (value: T) => void, reject: (reason?: any) => void}} Resolvers
5
+ * @typedef {{promise: Promise<T>, resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void}} Resolvers
6
6
  */
7
7
 
8
8
  // fallback for Android WebView
9
- //@ts-ignore
10
- const withResolvers = Promise.withResolvers || function withResolvers() {
9
+ /**
10
+ * @template T
11
+ * @this {PromiseConstructor}
12
+ * @returns {Resolvers<T>}
13
+ */
14
+ function withResolvers() {
11
15
  var a, b, c = new this((resolve, reject) => {
12
16
  a = resolve;
13
17
  b = reject;
14
18
  });
15
- return {resolve: a, reject: b, promise: c};
16
- };
19
+ // @ts-ignore
20
+ return { resolve: a, reject: b, promise: c };
21
+ }
17
22
 
18
- /**
19
- * @template T
20
- * @type {() => Resolvers<T>}
21
- */
22
- export default withResolvers.bind(Promise);
23
+
24
+ export default /** @type {<T>() => Resolvers<T>} */((
25
+ /** @type {PromiseConstructor & {withResolvers?: <T>() => Resolvers<T>}} */ (Promise).withResolvers ||
26
+ withResolvers
27
+ ).bind(Promise));
@@ -0,0 +1,2 @@
1
+ declare function _default<T extends object>(target: T): import("./bound.js").Bound<T>;
2
+ export default _default;
@@ -0,0 +1,4 @@
1
+ declare function _default<T extends object>(target: T): Bound<T>;
2
+ export default _default;
3
+ export type BoundMethod<F> = unknown extends ThisParameterType<F> ? F : F extends (this: any, ...args: infer A) => infer R ? (...args: A) => R : never;
4
+ export type Bound<T_1> = { [K in keyof T_1 as T_1[K] extends Function ? K : never]: BoundMethod<T_1[K]>; };
@@ -0,0 +1,3 @@
1
+ declare let SAB: SharedArrayBufferConstructor;
2
+ export let native: boolean;
3
+ export { SAB as SharedArrayBuffer };
@@ -0,0 +1,7 @@
1
+ declare const _default: <T>() => Resolvers<T>;
2
+ export default _default;
3
+ export type Resolvers<T> = {
4
+ promise: Promise<T>;
5
+ resolve: (value: T | PromiseLike<T>) => void;
6
+ reject: (reason?: any) => void;
7
+ };
@@ -1,13 +0,0 @@
1
- # Keep GitHub Actions up to date with GitHub's Dependabot...
2
- # https://docs.github.com/en/code-security/dependabot/working-with-dependabot/keeping-your-actions-up-to-date-with-dependabot
3
- # https://docs.github.com/en/code-security/dependabot/dependabot-version-updates/configuration-options-for-the-dependabot.yml-file#package-ecosystem
4
- version: 2
5
- updates:
6
- - package-ecosystem: github-actions
7
- directory: /
8
- groups:
9
- github-actions:
10
- patterns:
11
- - "*" # Group all Actions updates into a single larger pull request
12
- schedule:
13
- interval: weekly
@@ -1,31 +0,0 @@
1
- # This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2
- # For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3
-
4
- name: build
5
-
6
- on: [push, pull_request]
7
-
8
- jobs:
9
- build:
10
-
11
- runs-on: ubuntu-latest
12
-
13
- strategy:
14
- matrix:
15
- node-version: [22]
16
-
17
- steps:
18
- - uses: actions/checkout@v5
19
- - name: Use Node.js ${{ matrix.node-version }}
20
- uses: actions/setup-node@v4
21
- with:
22
- node-version: ${{ matrix.node-version }}
23
- cache: 'npm'
24
- - run: npm ci
25
- - run: npm run build --if-present
26
- - run: npm run test
27
- - run: npm run coverage --if-present
28
- - name: Coveralls
29
- uses: coverallsapp/github-action@master
30
- with:
31
- github-token: ${{ secrets.GITHUB_TOKEN }}
@@ -1,22 +0,0 @@
1
- const { SharedArrayBuffer: SAB } = globalThis;
2
-
3
- globalThis.SharedArrayBuffer = null;
4
-
5
- const { SharedArrayBuffer, native } = await import("../src/shared-array-buffer.js");
6
-
7
- globalThis.SharedArrayBuffer = SAB;
8
-
9
- console.assert(!native);
10
-
11
- const gsab = new SharedArrayBuffer(4, { maxByteLength: 8 });
12
-
13
- console.assert(gsab.byteLength === 4);
14
- console.assert(gsab.growable);
15
-
16
- gsab.grow(8);
17
-
18
- console.assert(gsab.byteLength === 8);
19
-
20
- const fsab = new SharedArrayBuffer(6);
21
- console.assert(fsab.byteLength === 6);
22
- console.assert(!fsab.growable);
@@ -1,13 +0,0 @@
1
- Promise.withResolvers = undefined;
2
-
3
- const { default: withResolvers } = await import('../src/with-resolvers.js');
4
-
5
- const { promise, resolve, reject } = withResolvers();
6
-
7
- console.assert(typeof reject === 'function');
8
- console.assert(typeof resolve === 'function');
9
- console.assert(promise instanceof Promise);
10
-
11
- resolve('OK');
12
-
13
- console.log(await promise);