@webreflection/utils 0.2.10 → 0.2.11

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
@@ -7,6 +7,7 @@
7
7
 
8
8
  A [collection](./src/) of utility functions:
9
9
 
10
+ * **[all](https://github.com/WebReflection/utils/tree/main/src#all)** - `Promise.all` via object destructuring
10
11
  * **[bound-once](https://github.com/WebReflection/utils/tree/main/src#bound-once)** - to retrieve unique bound methods per realm
11
12
  * **[bound](https://github.com/WebReflection/utils/tree/main/src#bound)** - to retrieve one-off bound methods
12
13
  * **[shared-array-buffer](https://github.com/WebReflection/utils/tree/main/src#shared-array-buffer)** - to simulate *SAB* when not available
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@webreflection/utils",
3
- "version": "0.2.10",
3
+ "version": "0.2.11",
4
4
  "type": "module",
5
5
  "types": {
6
+ "./all": "./types/all.d.ts",
6
7
  "./bound-once": "./types/bound-once.d.ts",
7
8
  "./bound": "./types/bound.d.ts",
8
9
  "./shared-array-buffer": "./types/shared-array-buffer.d.ts",
@@ -10,6 +11,10 @@
10
11
  "./with-resolvers": "./types/with-resolvers.d.ts"
11
12
  },
12
13
  "exports": {
14
+ "./all": {
15
+ "types": "./types/all.d.ts",
16
+ "import": "./src/all.js"
17
+ },
13
18
  "./bound-once": {
14
19
  "types": "./types/bound-once.d.ts",
15
20
  "import": "./src/bound-once.js"
@@ -33,6 +38,7 @@
33
38
  "./package.json": "./package.json"
34
39
  },
35
40
  "tests": [
41
+ "all",
36
42
  "bound-once",
37
43
  "bound",
38
44
  "shared-array-buffer",
package/src/README.md CHANGED
@@ -6,6 +6,28 @@ Each utility can be loaded from a *CDN* via either `https://esm.run/@webreflecti
6
6
  This document describes each utility separately.
7
7
 
8
8
 
9
+ ## all
10
+
11
+ A `Promise.all` companion with one extra convenience: when called with a
12
+ single object literal, it resolves each value and returns an object with the
13
+ same keys.
14
+
15
+ ```js
16
+ import all from '@webreflection/all';
17
+
18
+ const user = await all({
19
+ name: fetchName(),
20
+ age: fetchAge()
21
+ });
22
+
23
+ // { name: 'Ada', age: 36 }
24
+ ```
25
+
26
+ This preserves the shape and names of object-literal work, avoiding the
27
+ positional array juggling required by `Promise.all`. For arrays, or for two or
28
+ more arguments, it behaves like `Promise.all` and resolves to an array.
29
+
30
+
9
31
  ## bound-once
10
32
 
11
33
  This is equivalent to **bound**, except each bound method is created only once. It is useful when bound method identity must be preserved across multiple calls.
package/src/all.js ADDED
@@ -0,0 +1,41 @@
1
+ // @ts-check
2
+
3
+ import bound from './bound.js';
4
+
5
+ const { isArray } = Array;
6
+ const { entries, fromEntries } = Object;
7
+
8
+ const { all, resolve } = bound(Promise);
9
+
10
+ /**
11
+ * @typedef {{
12
+ * <T extends object>(obj: T): Promise<{ [K in keyof T]: Awaited<T[K]> }>;
13
+ * <T extends unknown[]>(values: T): Promise<{ [K in keyof T]: Awaited<T[K]> }>;
14
+ * <T extends [unknown, unknown, ...unknown[]]>(...values: T): Promise<{ [K in keyof T]: Awaited<T[K]> }>;
15
+ * }} All
16
+ */
17
+
18
+ /**
19
+ * @param {unknown} obj
20
+ * @param {...unknown} rest
21
+ * @returns {Promise<unknown>}
22
+ */
23
+ const values = (obj, ...rest) => {
24
+ // resolve all arguments passed along as array
25
+ if (rest.length)
26
+ return all([obj, ...rest]);
27
+
28
+ // resolve all entries in the array
29
+ if (isArray(obj))
30
+ return all(obj);
31
+
32
+ // resolve the object literal values re-mapping
33
+ // them as key-value pairs
34
+ for (const [k, v] of entries(/** @type {object} */ (obj)))
35
+ /** @type {Promise<[string, unknown]>[]} */(rest).push(resolve(v).then(v => [k, v]));
36
+
37
+ // return the object literal with the resolved values
38
+ return all(/** @type {Promise<[string, unknown]>[]} */(rest)).then(fromEntries);
39
+ };
40
+
41
+ export default /** @type {All} */(values);
package/types/all.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ declare const _default: All;
2
+ export default _default;
3
+ export type All = {
4
+ <T extends object>(obj: T): Promise<{ [K in keyof T]: Awaited<T[K]>; }>;
5
+ <T extends unknown[]>(values: T): Promise<{ [K in keyof T]: Awaited<T[K]>; }>;
6
+ <T extends [unknown, unknown, ...unknown[]]>(...values: T): Promise<{ [K in keyof T]: Awaited<T[K]>; }>;
7
+ };