@webreflection/utils 0.2.10 → 0.2.12
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 +2 -0
- package/package.json +13 -1
- package/src/README.md +43 -0
- package/src/all.js +41 -0
- package/src/ascii.js +14 -0
- package/types/all.d.ts +7 -0
- package/types/ascii.d.ts +2 -0
package/README.md
CHANGED
|
@@ -7,6 +7,8 @@
|
|
|
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
|
|
11
|
+
* **[ascii](https://github.com/WebReflection/utils/tree/main/src#ascii)** - basic string to buffer conversion (without validation)
|
|
10
12
|
* **[bound-once](https://github.com/WebReflection/utils/tree/main/src#bound-once)** - to retrieve unique bound methods per realm
|
|
11
13
|
* **[bound](https://github.com/WebReflection/utils/tree/main/src#bound)** - to retrieve one-off bound methods
|
|
12
14
|
* **[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,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@webreflection/utils",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.12",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"types": {
|
|
6
|
+
"./all": "./types/all.d.ts",
|
|
7
|
+
"./ascii": "./types/ascii.d.ts",
|
|
6
8
|
"./bound-once": "./types/bound-once.d.ts",
|
|
7
9
|
"./bound": "./types/bound.d.ts",
|
|
8
10
|
"./shared-array-buffer": "./types/shared-array-buffer.d.ts",
|
|
@@ -10,6 +12,14 @@
|
|
|
10
12
|
"./with-resolvers": "./types/with-resolvers.d.ts"
|
|
11
13
|
},
|
|
12
14
|
"exports": {
|
|
15
|
+
"./all": {
|
|
16
|
+
"types": "./types/all.d.ts",
|
|
17
|
+
"import": "./src/all.js"
|
|
18
|
+
},
|
|
19
|
+
"./ascii": {
|
|
20
|
+
"types": "./types/ascii.d.ts",
|
|
21
|
+
"import": "./src/ascii.js"
|
|
22
|
+
},
|
|
13
23
|
"./bound-once": {
|
|
14
24
|
"types": "./types/bound-once.d.ts",
|
|
15
25
|
"import": "./src/bound-once.js"
|
|
@@ -33,6 +43,8 @@
|
|
|
33
43
|
"./package.json": "./package.json"
|
|
34
44
|
},
|
|
35
45
|
"tests": [
|
|
46
|
+
"all",
|
|
47
|
+
"ascii",
|
|
36
48
|
"bound-once",
|
|
37
49
|
"bound",
|
|
38
50
|
"shared-array-buffer",
|
package/src/README.md
CHANGED
|
@@ -6,6 +6,49 @@ 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/utils/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
|
+
|
|
31
|
+
## ascii
|
|
32
|
+
|
|
33
|
+
An extremely small string to `Uint8Array` converter for known ASCII-compatible
|
|
34
|
+
content. It does not validate or encode Unicode code points; it simply stores
|
|
35
|
+
each string unit as its `0-255` char code.
|
|
36
|
+
|
|
37
|
+
This is meant for niche cases where the input is already constrained, such as
|
|
38
|
+
ISO date strings, plain-English global names or method names, and other small
|
|
39
|
+
ad-hoc values.
|
|
40
|
+
|
|
41
|
+
```js
|
|
42
|
+
import { encode, decode } from '@webreflection/utils/ascii';
|
|
43
|
+
|
|
44
|
+
console.log(decode(encode('ASCII')));
|
|
45
|
+
// ASCII
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Please note that decoding also fails for inputs bigger than about 64K bytes, or
|
|
49
|
+
whatever argument limit your runtime has for `String.fromCharCode`.
|
|
50
|
+
|
|
51
|
+
|
|
9
52
|
## bound-once
|
|
10
53
|
|
|
11
54
|
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/src/ascii.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
const { fromCharCode } = String;
|
|
2
|
+
const toCharCode = Uint8Array.from.bind(Uint8Array);
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* @param {string} str
|
|
6
|
+
* @returns
|
|
7
|
+
*/
|
|
8
|
+
export const encode = str => toCharCode(str, c => c.charCodeAt(0));
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* @param {Uint8Array} view
|
|
12
|
+
* @returns {string}
|
|
13
|
+
*/
|
|
14
|
+
export const decode = view => fromCharCode(...view);
|
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
|
+
};
|
package/types/ascii.d.ts
ADDED