@webreflection/utils 0.2.11 → 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 +1 -0
- package/package.json +7 -1
- package/src/README.md +22 -1
- package/src/ascii.js +14 -0
- package/types/ascii.d.ts +2 -0
package/README.md
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
A [collection](./src/) of utility functions:
|
|
9
9
|
|
|
10
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)
|
|
11
12
|
* **[bound-once](https://github.com/WebReflection/utils/tree/main/src#bound-once)** - to retrieve unique bound methods per realm
|
|
12
13
|
* **[bound](https://github.com/WebReflection/utils/tree/main/src#bound)** - to retrieve one-off bound methods
|
|
13
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,9 +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
6
|
"./all": "./types/all.d.ts",
|
|
7
|
+
"./ascii": "./types/ascii.d.ts",
|
|
7
8
|
"./bound-once": "./types/bound-once.d.ts",
|
|
8
9
|
"./bound": "./types/bound.d.ts",
|
|
9
10
|
"./shared-array-buffer": "./types/shared-array-buffer.d.ts",
|
|
@@ -15,6 +16,10 @@
|
|
|
15
16
|
"types": "./types/all.d.ts",
|
|
16
17
|
"import": "./src/all.js"
|
|
17
18
|
},
|
|
19
|
+
"./ascii": {
|
|
20
|
+
"types": "./types/ascii.d.ts",
|
|
21
|
+
"import": "./src/ascii.js"
|
|
22
|
+
},
|
|
18
23
|
"./bound-once": {
|
|
19
24
|
"types": "./types/bound-once.d.ts",
|
|
20
25
|
"import": "./src/bound-once.js"
|
|
@@ -39,6 +44,7 @@
|
|
|
39
44
|
},
|
|
40
45
|
"tests": [
|
|
41
46
|
"all",
|
|
47
|
+
"ascii",
|
|
42
48
|
"bound-once",
|
|
43
49
|
"bound",
|
|
44
50
|
"shared-array-buffer",
|
package/src/README.md
CHANGED
|
@@ -13,7 +13,7 @@ single object literal, it resolves each value and returns an object with the
|
|
|
13
13
|
same keys.
|
|
14
14
|
|
|
15
15
|
```js
|
|
16
|
-
import all from '@webreflection/all';
|
|
16
|
+
import all from '@webreflection/utils/all';
|
|
17
17
|
|
|
18
18
|
const user = await all({
|
|
19
19
|
name: fetchName(),
|
|
@@ -28,6 +28,27 @@ positional array juggling required by `Promise.all`. For arrays, or for two or
|
|
|
28
28
|
more arguments, it behaves like `Promise.all` and resolves to an array.
|
|
29
29
|
|
|
30
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
|
+
|
|
31
52
|
## bound-once
|
|
32
53
|
|
|
33
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/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/ascii.d.ts
ADDED