@webreflection/utils 0.2.0 → 0.2.2

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
@@ -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,8 +1,18 @@
1
1
  {
2
2
  "name": "@webreflection/utils",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "type": "module",
5
+ "types": {
6
+ "./bound-once": "./types/bound-once.d.ts",
7
+ "./bound": "./types/bound.d.ts",
8
+ "./shared-array-buffer": "./types/shared-array-buffer.d.ts",
9
+ "./with-resolvers": "./types/with-resolvers.d.ts"
10
+ },
5
11
  "exports": {
12
+ "./bound-once": {
13
+ "types": "./types/bound-once.d.ts",
14
+ "import": "./src/bound-once.js"
15
+ },
6
16
  "./bound": {
7
17
  "types": "./types/bound.d.ts",
8
18
  "import": "./src/bound.js"
@@ -18,13 +28,14 @@
18
28
  "./package.json": "./package.json"
19
29
  },
20
30
  "tests": [
31
+ "bound-once",
21
32
  "bound",
22
33
  "shared-array-buffer",
23
34
  "with-resolvers"
24
35
  ],
25
36
  "scripts": {
26
37
  "build": "npm run build:types && npm run test",
27
- "build:types": "tsc --allowJs --checkJs --declaration --emitDeclarationOnly --outDir types --target es2022 --lib es2024 --module nodenext --moduleResolution nodenext src/*.js",
38
+ "build:types": "tsc --allowJs --checkJs --declaration --emitDeclarationOnly --stripInternal --outDir types --target es2022 --lib es2024 --module nodenext --moduleResolution nodenext src/*.js",
28
39
  "coverage": "mkdir -p ./coverage; c8 report --reporter=text-lcov > ./coverage/lcov.info",
29
40
  "test": "c8 node -e 'let q=Promise.resolve();for(const t of require(`./package.json`).tests)q=q.then(()=>import(`./test/${t}.js`));'"
30
41
  },
@@ -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
+ };
@@ -0,0 +1,2 @@
1
+ declare function _default<T extends object>(target: T): import("./bound.js").Bound<T>;
2
+ export default _default;