@wener/utils 1.1.1 → 1.1.3
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/dist/cjs/index.js +2 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/server.js +2 -0
- package/dist/cjs/server.js.map +1 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/server.js +2 -0
- package/dist/esm/server.js.map +1 -0
- package/dist/system/index.js +2 -0
- package/dist/system/index.js.map +1 -0
- package/dist/system/server.js +2 -0
- package/dist/system/server.js.map +1 -0
- package/index.ts +1 -0
- package/lib/{esm/arrays → arrays}/MaybeArray.js +3 -6
- package/lib/arrays/MaybeArray.js.map +1 -0
- package/lib/{esm/asyncs → asyncs}/AsyncInterval.js +4 -5
- package/lib/asyncs/AsyncInterval.js.map +1 -0
- package/lib/asyncs/LazyPromise.js +27 -0
- package/lib/asyncs/LazyPromise.js.map +1 -0
- package/lib/{esm/asyncs → asyncs}/isPromise.js +3 -3
- package/lib/asyncs/isPromise.js.map +1 -0
- package/lib/{esm/asyncs → asyncs}/sleep.js +3 -3
- package/lib/asyncs/sleep.js.map +1 -0
- package/lib/{esm/asyncs → asyncs}/timeout.js +13 -11
- package/lib/asyncs/timeout.js.map +1 -0
- package/lib/{esm/browsers → browsers}/copy.js +7 -8
- package/lib/browsers/copy.js.map +1 -0
- package/lib/{esm/browsers → browsers}/download.js +3 -3
- package/lib/browsers/download.js.map +1 -0
- package/lib/{esm/browsers → browsers}/getFileFromDataTransfer.js +8 -6
- package/lib/browsers/getFileFromDataTransfer.js.map +1 -0
- package/lib/{esm/browsers → browsers}/loaders.js +24 -9
- package/lib/browsers/loaders.js.map +1 -0
- package/lib/crypto/hashing.js +24 -0
- package/lib/crypto/hashing.js.map +1 -0
- package/lib/crypto/hex.js +6 -0
- package/lib/crypto/hex.js.map +1 -0
- package/lib/crypto/randomUUID.js +13 -0
- package/lib/crypto/randomUUID.js.map +1 -0
- package/lib/{esm/formats → formats}/formatBytes.js +3 -3
- package/lib/formats/formatBytes.js.map +1 -0
- package/lib/index.js +27 -0
- package/lib/index.js.map +1 -0
- package/lib/{esm/io → io}/isBuffer.js +3 -3
- package/lib/io/isBuffer.js.map +1 -0
- package/lib/{esm/isomorphics → isomorphics}/getGlobalThis.js +3 -3
- package/lib/isomorphics/getGlobalThis.js.map +1 -0
- package/lib/{esm/maths → maths}/random.js +3 -3
- package/lib/maths/random.js.map +1 -0
- package/lib/modules/parseModuleId.js +31 -0
- package/lib/modules/parseModuleId.js.map +1 -0
- package/lib/server/polyfill.js +8 -0
- package/lib/server/polyfill.js.map +1 -0
- package/lib/server.js +2 -0
- package/lib/server.js.map +1 -0
- package/lib/{esm/shim → shim}/urljoin.js +3 -3
- package/lib/shim/urljoin.js.map +1 -0
- package/lib/{esm/strings → strings}/camelCase.js +3 -4
- package/lib/strings/camelCase.js.map +1 -0
- package/lib/{esm/strings → strings}/templates.js +3 -3
- package/lib/strings/templates.js.map +1 -0
- package/lib/validations/dequal.js +95 -0
- package/lib/validations/dequal.js.map +1 -0
- package/lib/{esm/validations → validations}/isClass.js +3 -3
- package/lib/validations/isClass.js.map +1 -0
- package/lib/{esm/validations → validations}/isDefined.js +3 -3
- package/lib/validations/isDefined.js.map +1 -0
- package/lib/{esm/validations → validations}/isEmptyObject.js +3 -3
- package/lib/validations/isEmptyObject.js.map +1 -0
- package/lib/validations/shallow.js +21 -0
- package/lib/validations/shallow.js.map +1 -0
- package/package.json +62 -47
- package/server.ts +1 -0
- package/src/arrays/MaybeArray.ts +8 -4
- package/src/browsers/copy.ts +7 -3
- package/src/browsers/download.ts +7 -0
- package/src/browsers/loaders.ts +32 -7
- package/src/crypto/hashing.test.ts +26 -0
- package/src/crypto/hashing.ts +27 -0
- package/src/crypto/hex.ts +5 -0
- package/src/crypto/randomUUID.ts +13 -0
- package/src/index.ts +46 -7
- package/src/modules/parseModuleId.test.ts +68 -0
- package/src/modules/parseModuleId.ts +46 -0
- package/src/server/polyfill.ts +5 -0
- package/src/server.ts +1 -0
- package/src/shim/urljoin.test.ts +3 -2
- package/src/types.d.ts +7 -0
- package/src/validations/dequal.test.ts +17 -0
- package/src/validations/dequal.ts +105 -0
- package/src/validations/shallow.ts +26 -0
- package/tsconfig.json +5 -9
- package/jest.config.js +0 -18
- package/jest.setup.js +0 -1
- package/lib/cjs/arrays/MaybeArray.js +0 -52
- package/lib/cjs/arrays/index.js +0 -17
- package/lib/cjs/asyncs/AsyncInterval.js +0 -35
- package/lib/cjs/asyncs/LazyPromise.js +0 -43
- package/lib/cjs/asyncs/MaybePromise.js +0 -15
- package/lib/cjs/asyncs/generatorOfStream.js +0 -35
- package/lib/cjs/asyncs/index.js +0 -23
- package/lib/cjs/asyncs/isPromise.js +0 -25
- package/lib/cjs/asyncs/promiseOfCallback.js +0 -37
- package/lib/cjs/asyncs/sleep.js +0 -23
- package/lib/cjs/asyncs/timeout.js +0 -44
- package/lib/cjs/browsers/copy.js +0 -71
- package/lib/cjs/browsers/download.js +0 -47
- package/lib/cjs/browsers/getFileFromDataTransfer.js +0 -48
- package/lib/cjs/browsers/index.js +0 -20
- package/lib/cjs/browsers/loaders.js +0 -54
- package/lib/cjs/formats/formatBytes.js +0 -36
- package/lib/cjs/index.js +0 -35
- package/lib/cjs/io/index.js +0 -17
- package/lib/cjs/io/isBuffer.js +0 -25
- package/lib/cjs/isomorphics/getGlobalThis.js +0 -33
- package/lib/cjs/maths/index.js +0 -17
- package/lib/cjs/maths/random.js +0 -36
- package/lib/cjs/shim/urljoin.js +0 -70
- package/lib/cjs/shim/urljoin.test.js +0 -4
- package/lib/cjs/strings/camelCase.js +0 -76
- package/lib/cjs/strings/index.js +0 -18
- package/lib/cjs/strings/templates.js +0 -27
- package/lib/cjs/validations/asserts.js +0 -29
- package/lib/cjs/validations/index.js +0 -20
- package/lib/cjs/validations/isClass.js +0 -25
- package/lib/cjs/validations/isDefined.js +0 -25
- package/lib/cjs/validations/isEmptyObject.js +0 -28
- package/lib/esm/arrays/MaybeArray.d.ts +0 -5
- package/lib/esm/arrays/index.d.ts +0 -1
- package/lib/esm/arrays/index.js +0 -1
- package/lib/esm/asyncs/AsyncInterval.d.ts +0 -3
- package/lib/esm/asyncs/LazyPromise.d.ts +0 -5
- package/lib/esm/asyncs/LazyPromise.js +0 -24
- package/lib/esm/asyncs/MaybePromise.d.ts +0 -1
- package/lib/esm/asyncs/MaybePromise.js +0 -0
- package/lib/esm/asyncs/generatorOfStream.d.ts +0 -1
- package/lib/esm/asyncs/generatorOfStream.js +0 -16
- package/lib/esm/asyncs/index.d.ts +0 -7
- package/lib/esm/asyncs/index.js +0 -7
- package/lib/esm/asyncs/isPromise.d.ts +0 -1
- package/lib/esm/asyncs/promiseOfCallback.d.ts +0 -1
- package/lib/esm/asyncs/promiseOfCallback.js +0 -18
- package/lib/esm/asyncs/sleep.d.ts +0 -1
- package/lib/esm/asyncs/timeout.d.ts +0 -4
- package/lib/esm/browsers/copy.d.ts +0 -1
- package/lib/esm/browsers/download.d.ts +0 -4
- package/lib/esm/browsers/getFileFromDataTransfer.d.ts +0 -4
- package/lib/esm/browsers/index.d.ts +0 -4
- package/lib/esm/browsers/index.js +0 -4
- package/lib/esm/browsers/loaders.d.ts +0 -6
- package/lib/esm/formats/formatBytes.d.ts +0 -11
- package/lib/esm/index.d.ts +0 -10
- package/lib/esm/index.js +0 -15
- package/lib/esm/io/index.d.ts +0 -1
- package/lib/esm/io/index.js +0 -1
- package/lib/esm/io/isBuffer.d.ts +0 -2
- package/lib/esm/isomorphics/getGlobalThis.d.ts +0 -9
- package/lib/esm/maths/index.d.ts +0 -1
- package/lib/esm/maths/index.js +0 -1
- package/lib/esm/maths/random.d.ts +0 -3
- package/lib/esm/shim/urljoin.d.ts +0 -2
- package/lib/esm/shim/urljoin.test.d.ts +0 -1
- package/lib/esm/shim/urljoin.test.js +0 -4
- package/lib/esm/strings/camelCase.d.ts +0 -4
- package/lib/esm/strings/index.d.ts +0 -2
- package/lib/esm/strings/index.js +0 -2
- package/lib/esm/strings/templates.d.ts +0 -7
- package/lib/esm/validations/asserts.d.ts +0 -2
- package/lib/esm/validations/asserts.js +0 -10
- package/lib/esm/validations/index.d.ts +0 -4
- package/lib/esm/validations/index.js +0 -4
- package/lib/esm/validations/isClass.d.ts +0 -1
- package/lib/esm/validations/isDefined.d.ts +0 -1
- package/lib/esm/validations/isEmptyObject.d.ts +0 -1
- package/src/arrays/index.ts +0 -1
- package/src/asyncs/index.ts +0 -8
- package/src/browsers/index.ts +0 -4
- package/src/io/index.ts +0 -1
- package/src/maths/index.ts +0 -1
- package/src/strings/index.ts +0 -2
- package/src/validations/index.ts +0 -4
- package/tsconfig.jest.json +0 -7
package/package.json
CHANGED
|
@@ -1,70 +1,85 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@wener/utils",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.3",
|
|
4
|
+
"type": "module",
|
|
4
5
|
"description": "Utils for daily use",
|
|
5
|
-
"keywords": [
|
|
6
|
-
"utils",
|
|
7
|
-
"core library",
|
|
8
|
-
"helper"
|
|
9
|
-
],
|
|
10
6
|
"repository": {
|
|
11
7
|
"type": "git",
|
|
12
8
|
"url": "git+https://github.com/wener/wode.git"
|
|
13
9
|
},
|
|
14
|
-
"
|
|
10
|
+
"homepage": "https://github.com/wenerme/wode#readme",
|
|
15
11
|
"author": "wener",
|
|
16
|
-
"
|
|
17
|
-
"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
"
|
|
22
|
-
"jsdelivr": "dist/utils.js",
|
|
23
|
-
"jsnext:main": "dist/utils.esm.js",
|
|
12
|
+
"license": "MIT",
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": "16",
|
|
15
|
+
"npm": "8"
|
|
16
|
+
},
|
|
17
|
+
"main": "dist/cjs/index.js",
|
|
24
18
|
"exports": {
|
|
25
19
|
".": {
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
|
|
20
|
+
"types": "./src/index.ts",
|
|
21
|
+
"import": "./lib/index.js",
|
|
22
|
+
"system": {
|
|
23
|
+
"default": "./dist/system/index.js"
|
|
24
|
+
},
|
|
25
|
+
"require": "./dist/cjs/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./types": {
|
|
28
|
+
"types": "./src/types.d.ts"
|
|
29
|
+
},
|
|
30
|
+
"./server": {
|
|
31
|
+
"types": "./src/server.ts",
|
|
32
|
+
"import": "./lib/server.js",
|
|
33
|
+
"system": {
|
|
34
|
+
"default": "./dist/system/server.js"
|
|
35
|
+
},
|
|
36
|
+
"require": "./dist/cjs/server.js"
|
|
37
|
+
},
|
|
38
|
+
"./src/*": "./src/",
|
|
39
|
+
"./package.json": "./package.json"
|
|
29
40
|
},
|
|
30
|
-
"
|
|
41
|
+
"types": "src/index.ts",
|
|
31
42
|
"files": [
|
|
32
|
-
"index.js",
|
|
33
|
-
"index.d.ts",
|
|
34
|
-
"lib",
|
|
35
43
|
"dist",
|
|
44
|
+
"index.ts",
|
|
45
|
+
"lib",
|
|
46
|
+
"server.ts",
|
|
36
47
|
"src",
|
|
37
|
-
"README.md",
|
|
38
|
-
"rollup.config.ts",
|
|
39
|
-
"jest.config.js",
|
|
40
|
-
"jest.setup.js",
|
|
41
|
-
"tsconfig.rollup.json",
|
|
42
|
-
"tsconfig.jest.json",
|
|
43
48
|
"tsconfig.json"
|
|
44
49
|
],
|
|
45
|
-
"
|
|
46
|
-
"
|
|
47
|
-
"
|
|
48
|
-
"
|
|
49
|
-
|
|
50
|
-
},
|
|
50
|
+
"keywords": [
|
|
51
|
+
"utils",
|
|
52
|
+
"core library",
|
|
53
|
+
"helper"
|
|
54
|
+
],
|
|
51
55
|
"devDependencies": {
|
|
52
|
-
"@types/jest": "^28",
|
|
53
56
|
"@types/lodash": "^4",
|
|
54
|
-
"
|
|
55
|
-
"
|
|
56
|
-
"esbuild": "^0.14",
|
|
57
|
-
"jest": "^28",
|
|
58
|
-
"lodash": "^4",
|
|
59
|
-
"ts-jest": "^28",
|
|
60
|
-
"tslib": "^2",
|
|
61
|
-
"typescript": "^4"
|
|
57
|
+
"ava": "^4.3.3",
|
|
58
|
+
"lodash": "^4"
|
|
62
59
|
},
|
|
63
60
|
"publishConfig": {
|
|
64
61
|
"access": "public"
|
|
65
62
|
},
|
|
66
|
-
"
|
|
67
|
-
|
|
68
|
-
"
|
|
63
|
+
"sideEffects": false,
|
|
64
|
+
"ava": {
|
|
65
|
+
"extensions": {
|
|
66
|
+
"ts": "module"
|
|
67
|
+
},
|
|
68
|
+
"nodeArguments": [
|
|
69
|
+
"--loader=tsx"
|
|
70
|
+
]
|
|
71
|
+
},
|
|
72
|
+
"import": "lib/index.js",
|
|
73
|
+
"rollup": {
|
|
74
|
+
"input": [
|
|
75
|
+
"./src/index.ts",
|
|
76
|
+
"./src/server.ts"
|
|
77
|
+
]
|
|
78
|
+
},
|
|
79
|
+
"scripts": {
|
|
80
|
+
"build": "make prepublish",
|
|
81
|
+
"clean": "make clean",
|
|
82
|
+
"dev": "make dev",
|
|
83
|
+
"test": "make test"
|
|
69
84
|
}
|
|
70
|
-
}
|
|
85
|
+
}
|
package/server.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './src/server';
|
package/src/arrays/MaybeArray.ts
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Array or element of an array
|
|
3
|
+
*/
|
|
1
4
|
export type MaybeArray<T> = T | T[];
|
|
2
5
|
|
|
6
|
+
/**
|
|
7
|
+
* convert to single value object
|
|
8
|
+
*/
|
|
3
9
|
export function objectOfMaybeArray<T = any>(
|
|
4
10
|
o: Record<string, MaybeArray<T>>,
|
|
5
11
|
keys: string[] | null = null,
|
|
6
|
-
picker = firstOfMaybeArray
|
|
12
|
+
picker = firstOfMaybeArray,
|
|
7
13
|
): Record<string, T> {
|
|
8
14
|
if (keys === null) {
|
|
9
|
-
return Object.fromEntries(
|
|
10
|
-
Object.entries(o).map(([k, v]) => [k, picker(v)])
|
|
11
|
-
);
|
|
15
|
+
return Object.fromEntries(Object.entries(o).map(([k, v]) => [k, picker(v)]));
|
|
12
16
|
}
|
|
13
17
|
return Object.fromEntries(keys.map((v) => [v, picker(v)])) as any;
|
|
14
18
|
}
|
package/src/browsers/copy.ts
CHANGED
|
@@ -45,13 +45,17 @@ function initCopy() {
|
|
|
45
45
|
};
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
/**
|
|
49
|
+
* Write text to clipboard
|
|
50
|
+
* @param content content
|
|
51
|
+
*/
|
|
52
|
+
export function copy(content: string) {
|
|
49
53
|
if (window.navigator?.clipboard?.writeText) {
|
|
50
|
-
window.navigator.clipboard.writeText(
|
|
54
|
+
window.navigator.clipboard.writeText(content);
|
|
51
55
|
return;
|
|
52
56
|
}
|
|
53
57
|
if (!_copy) {
|
|
54
58
|
initCopy();
|
|
55
59
|
}
|
|
56
|
-
_copy(
|
|
60
|
+
_copy(content);
|
|
57
61
|
}
|
package/src/browsers/download.ts
CHANGED
|
@@ -1,3 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Trigger browser download
|
|
3
|
+
* @param filename download as filename
|
|
4
|
+
* @param data data or url to download
|
|
5
|
+
* @param type content type
|
|
6
|
+
* @param raw if true, data is treated as raw data, not url
|
|
7
|
+
*/
|
|
1
8
|
export function download(filename: string, data: any, { type = 'application/octet-stream', raw = false } = {}) {
|
|
2
9
|
const a = document.createElement('a');
|
|
3
10
|
let closer: () => void = () => null;
|
package/src/browsers/loaders.ts
CHANGED
|
@@ -4,7 +4,7 @@ function load(
|
|
|
4
4
|
reject: (v: any) => void,
|
|
5
5
|
options: { attributes: Record<string, string> } | undefined,
|
|
6
6
|
) {
|
|
7
|
-
el.onload = resolve;
|
|
7
|
+
el.onload = () => resolve(el);
|
|
8
8
|
el.onerror = (e) => {
|
|
9
9
|
el.remove();
|
|
10
10
|
reject(e);
|
|
@@ -15,10 +15,34 @@ function load(
|
|
|
15
15
|
document.head.appendChild(el);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
export function loadScripts(
|
|
18
|
+
export function loadScripts(
|
|
19
|
+
src: string[],
|
|
20
|
+
options?: { attributes: Record<string, string> },
|
|
21
|
+
): Promise<HTMLScriptElement[]>;
|
|
22
|
+
export function loadScripts(src: string, options?: { attributes: Record<string, string> }): Promise<HTMLScriptElement>;
|
|
23
|
+
|
|
24
|
+
export function loadScripts(
|
|
25
|
+
src: string | string[],
|
|
26
|
+
options?: { attributes: Record<string, string> },
|
|
27
|
+
): Promise<HTMLScriptElement | HTMLScriptElement[]> {
|
|
28
|
+
if (Array.isArray(src)) {
|
|
29
|
+
return new Promise(async (resolve, reject) => {
|
|
30
|
+
const all = [];
|
|
31
|
+
try {
|
|
32
|
+
for (let s of src) {
|
|
33
|
+
all.push(await loadScripts(s));
|
|
34
|
+
}
|
|
35
|
+
} catch (e) {
|
|
36
|
+
reject(e);
|
|
37
|
+
return;
|
|
38
|
+
}
|
|
39
|
+
resolve(all);
|
|
40
|
+
});
|
|
41
|
+
}
|
|
19
42
|
// todo quote ?
|
|
20
|
-
|
|
21
|
-
|
|
43
|
+
let $ele = document.querySelector(`script[src="${src}"]`) as HTMLScriptElement;
|
|
44
|
+
if ($ele) {
|
|
45
|
+
return Promise.resolve($ele);
|
|
22
46
|
}
|
|
23
47
|
return new Promise((resolve, reject) => {
|
|
24
48
|
const el = document.createElement('script');
|
|
@@ -27,9 +51,10 @@ export function loadScripts(src: string, options?: { attributes: Record<string,
|
|
|
27
51
|
});
|
|
28
52
|
}
|
|
29
53
|
|
|
30
|
-
export function loadStyles(href: string, options?: { attributes: Record<string, string> }) {
|
|
31
|
-
|
|
32
|
-
|
|
54
|
+
export function loadStyles(href: string, options?: { attributes: Record<string, string> }): Promise<HTMLLinkElement> {
|
|
55
|
+
let $ele = document.querySelector(`link[href="${href}"]`) as HTMLLinkElement;
|
|
56
|
+
if ($ele) {
|
|
57
|
+
return Promise.resolve($ele);
|
|
33
58
|
}
|
|
34
59
|
|
|
35
60
|
return new Promise((resolve, reject) => {
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import test from 'ava';
|
|
2
|
+
import { sha1, sha256, sha384, sha512 } from './hashing';
|
|
3
|
+
import { hex } from './hex';
|
|
4
|
+
import { randomUUID } from './randomUUID';
|
|
5
|
+
|
|
6
|
+
test.before(async (t) => {
|
|
7
|
+
if (!('crypto' in globalThis)) {
|
|
8
|
+
(globalThis as any).crypto = (await import('node:crypto')).webcrypto;
|
|
9
|
+
t.log(`load node:crypto`);
|
|
10
|
+
}
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
test('sha', async (t) => {
|
|
14
|
+
t.is(hex(await sha1('')), 'da39a3ee5e6b4b0d3255bfef95601890afd80709');
|
|
15
|
+
t.is(hex(await sha1('abc')), 'a9993e364706816aba3e25717850c26c9cd0d89d');
|
|
16
|
+
t.is(hex(await sha256('')), 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855');
|
|
17
|
+
t.is(
|
|
18
|
+
hex(await sha384('')),
|
|
19
|
+
'38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b',
|
|
20
|
+
);
|
|
21
|
+
t.is(
|
|
22
|
+
hex(await sha512('')),
|
|
23
|
+
'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e',
|
|
24
|
+
);
|
|
25
|
+
t.regex(randomUUID(), /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
|
|
26
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export async function sha1(s: string | BufferSource) {
|
|
2
|
+
return digestOf('SHA-1', s);
|
|
3
|
+
}
|
|
4
|
+
|
|
5
|
+
export async function sha256(s: string | BufferSource) {
|
|
6
|
+
return digestOf('SHA-256', s);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export async function sha384(s: string | BufferSource) {
|
|
10
|
+
return digestOf('SHA-384', s);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export async function sha512(s: string | BufferSource) {
|
|
14
|
+
return digestOf('SHA-512', s);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function digestOf(a: string, s: string | BufferSource) {
|
|
18
|
+
return crypto.subtle.digest(a, bufferOf(s));
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function bufferOf(s: string | BufferSource) {
|
|
22
|
+
// ArrayBuffer, TypedArray, DataView
|
|
23
|
+
if (typeof s === 'string') {
|
|
24
|
+
return new TextEncoder().encode(s);
|
|
25
|
+
}
|
|
26
|
+
return s;
|
|
27
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* generate random UUIDv4
|
|
3
|
+
*/
|
|
4
|
+
export function randomUUID() {
|
|
5
|
+
if ('randomUUID' in crypto) {
|
|
6
|
+
return crypto.randomUUID();
|
|
7
|
+
}
|
|
8
|
+
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
|
|
9
|
+
const r = (Math.random() * 16) | 0;
|
|
10
|
+
const v = c === 'x' ? r : (r & 0x3) | 0x8;
|
|
11
|
+
return v.toString(16);
|
|
12
|
+
});
|
|
13
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,11 +1,50 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
// arrays
|
|
2
|
+
export {
|
|
3
|
+
firstOfMaybeArray,
|
|
4
|
+
lastOfMaybeArray,
|
|
5
|
+
arrayOfMaybeArray,
|
|
6
|
+
objectOfMaybeArray,
|
|
7
|
+
type MaybeArray,
|
|
8
|
+
} from './arrays/MaybeArray';
|
|
9
|
+
|
|
10
|
+
// async
|
|
11
|
+
export { createLazyPromise, type LazyPromise } from './asyncs/LazyPromise';
|
|
12
|
+
export { setAsyncInterval, clearAsyncInterval } from './asyncs/AsyncInterval';
|
|
13
|
+
export { type MaybePromise } from './asyncs/MaybePromise';
|
|
14
|
+
|
|
15
|
+
export { sleep } from './asyncs/sleep';
|
|
16
|
+
export { timeout, TimeoutError } from './asyncs/timeout';
|
|
17
|
+
export { isPromise } from './asyncs/isPromise';
|
|
18
|
+
// export * from './async/promiseOfCallback';
|
|
19
|
+
|
|
20
|
+
// validations
|
|
21
|
+
export { isClass } from './validations/isClass';
|
|
22
|
+
export { isDefined } from './validations/isDefined';
|
|
23
|
+
export { isEmptyObject } from './validations/isEmptyObject';
|
|
24
|
+
export { shallow } from './validations/shallow';
|
|
25
|
+
export { dequal } from './validations/dequal';
|
|
26
|
+
|
|
27
|
+
// modules
|
|
28
|
+
export { parseModuleId, type ParsedModuleId } from './modules/parseModuleId';
|
|
29
|
+
|
|
30
|
+
// strings
|
|
31
|
+
export { pascalCase, camelCase } from './strings/camelCase';
|
|
32
|
+
export { templateString } from './strings/templates';
|
|
33
|
+
|
|
34
|
+
export { createRandom } from './maths/random';
|
|
35
|
+
export { isBuffer } from './io/isBuffer';
|
|
36
|
+
|
|
37
|
+
export { copy } from './browsers/copy';
|
|
38
|
+
export { download } from './browsers/download';
|
|
39
|
+
export { loadScripts, loadStyles } from './browsers/loaders';
|
|
40
|
+
export { getFileFromDataTransfer } from './browsers/getFileFromDataTransfer';
|
|
8
41
|
|
|
9
42
|
export { getGlobalThis } from './isomorphics/getGlobalThis';
|
|
43
|
+
|
|
10
44
|
export { formatBytes } from './formats/formatBytes';
|
|
11
45
|
export { urljoin } from './shim/urljoin';
|
|
46
|
+
|
|
47
|
+
// crypto
|
|
48
|
+
export { randomUUID } from './crypto/randomUUID';
|
|
49
|
+
export { sha1, sha256, sha384, sha512 } from './crypto/hashing';
|
|
50
|
+
export { hex } from './crypto/hex';
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import test from 'ava';
|
|
2
|
+
import { parseModuleId } from './parseModuleId';
|
|
3
|
+
|
|
4
|
+
test('parseModuleId', (t) => {
|
|
5
|
+
const tests = {
|
|
6
|
+
'@wener/reaction': {
|
|
7
|
+
id: `@wener/reaction@latest`,
|
|
8
|
+
name: '@wener/reaction',
|
|
9
|
+
range: 'latest',
|
|
10
|
+
scoped: true,
|
|
11
|
+
org: 'wener',
|
|
12
|
+
pkg: 'reaction',
|
|
13
|
+
},
|
|
14
|
+
reaction: {
|
|
15
|
+
id: `reaction@latest`,
|
|
16
|
+
name: 'reaction',
|
|
17
|
+
range: 'latest',
|
|
18
|
+
pkg: 'reaction',
|
|
19
|
+
scoped: false,
|
|
20
|
+
},
|
|
21
|
+
'reaction@1': {
|
|
22
|
+
id: `reaction@1`,
|
|
23
|
+
name: 'reaction',
|
|
24
|
+
range: '1',
|
|
25
|
+
pkg: 'reaction',
|
|
26
|
+
scoped: false,
|
|
27
|
+
},
|
|
28
|
+
'reaction@1.1.1': {
|
|
29
|
+
id: `reaction@1.1.1`,
|
|
30
|
+
name: 'reaction',
|
|
31
|
+
version: '1.1.1',
|
|
32
|
+
range: '1.1.1',
|
|
33
|
+
pkg: 'reaction',
|
|
34
|
+
scoped: false,
|
|
35
|
+
},
|
|
36
|
+
'reaction@1.1.1-alpha': {
|
|
37
|
+
id: `reaction@1.1.1-alpha`,
|
|
38
|
+
name: 'reaction',
|
|
39
|
+
version: '1.1.1-alpha',
|
|
40
|
+
range: '1.1.1-alpha',
|
|
41
|
+
pkg: 'reaction',
|
|
42
|
+
|
|
43
|
+
scoped: false,
|
|
44
|
+
},
|
|
45
|
+
'reaction@1.1.1/index.js': {
|
|
46
|
+
id: `reaction@1.1.1`,
|
|
47
|
+
name: 'reaction',
|
|
48
|
+
version: '1.1.1',
|
|
49
|
+
range: '1.1.1',
|
|
50
|
+
scoped: false,
|
|
51
|
+
pkg: 'reaction',
|
|
52
|
+
|
|
53
|
+
path: '/index.js',
|
|
54
|
+
},
|
|
55
|
+
'reaction@1.1.1/': {
|
|
56
|
+
id: `reaction@1.1.1`,
|
|
57
|
+
name: 'reaction',
|
|
58
|
+
version: '1.1.1',
|
|
59
|
+
range: '1.1.1',
|
|
60
|
+
scoped: false,
|
|
61
|
+
pkg: 'reaction',
|
|
62
|
+
path: '/',
|
|
63
|
+
},
|
|
64
|
+
};
|
|
65
|
+
for (const [k, v] of Object.entries(tests)) {
|
|
66
|
+
t.deepEqual(parseModuleId(k), v);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// https://regex101.com/r/eMcXQ9/1
|
|
2
|
+
const regModuleId =
|
|
3
|
+
/^(?<n>(?:@(?<org>[a-z0-9-~][a-z0-9-._~]*)\/)?(?<pkg>[a-z0-9-~][a-z0-9-._~]*))(?:@(?<v>[-a-z0-9><=_.^~]+))?(?<p>\/[^\r\n]*)?$/;
|
|
4
|
+
|
|
5
|
+
export type ParsedModuleId = {
|
|
6
|
+
id: string; // name@version
|
|
7
|
+
name: string; // @org/pkg, pkg
|
|
8
|
+
version?: string; // 1.1.1
|
|
9
|
+
range: string; // version, tag, range
|
|
10
|
+
pkg: string;
|
|
11
|
+
path?: string;
|
|
12
|
+
} & (
|
|
13
|
+
| { scoped: false }
|
|
14
|
+
| {
|
|
15
|
+
scoped: true;
|
|
16
|
+
org?: string;
|
|
17
|
+
}
|
|
18
|
+
);
|
|
19
|
+
|
|
20
|
+
export function parseModuleId(s: string): ParsedModuleId | undefined {
|
|
21
|
+
const groups = s.match(regModuleId)?.groups;
|
|
22
|
+
if (!groups) {
|
|
23
|
+
return undefined;
|
|
24
|
+
}
|
|
25
|
+
const { n: name, v: version, p: path, org, pkg } = groups;
|
|
26
|
+
let scoped = Boolean(org);
|
|
27
|
+
const v = /^\d+\.\d+\.\d+/.test(version) ? version : undefined;
|
|
28
|
+
let range = version || 'latest';
|
|
29
|
+
const o = {
|
|
30
|
+
id: `${name}@${range}`,
|
|
31
|
+
name,
|
|
32
|
+
range: range,
|
|
33
|
+
scoped,
|
|
34
|
+
pkg,
|
|
35
|
+
} as ParsedModuleId;
|
|
36
|
+
if (v) {
|
|
37
|
+
o.version = v;
|
|
38
|
+
}
|
|
39
|
+
if (path) {
|
|
40
|
+
o.path = path;
|
|
41
|
+
}
|
|
42
|
+
if (o.scoped) {
|
|
43
|
+
o.org = org;
|
|
44
|
+
}
|
|
45
|
+
return o;
|
|
46
|
+
}
|
package/src/server.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { polyfill } from './server/polyfill';
|
package/src/shim/urljoin.test.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
import test from 'ava';
|
|
1
2
|
import { urljoin } from './urljoin';
|
|
2
3
|
|
|
3
|
-
test('join ext', () => {
|
|
4
|
-
|
|
4
|
+
test('join ext', (t) => {
|
|
5
|
+
t.is(urljoin('http://wener.me/sub/', '/a/', '/hello.js'), 'http://wener.me/sub/a/hello.js');
|
|
5
6
|
});
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
const has = Object.prototype.hasOwnProperty;
|
|
2
|
+
const hasElementType = typeof Element !== 'undefined';
|
|
3
|
+
|
|
4
|
+
function find(iter: any, tar: any, key?: any) {
|
|
5
|
+
for (key of iter.keys()) {
|
|
6
|
+
if (dequal(key, tar)) return key;
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* deep equal - support react, dom, Date, RegExp, Array, ArrayBuffer, Set, Map, Object
|
|
12
|
+
* @see {@link https://github.com/lukeed/dequal/blob/master/src/lite.js dequal/src/lite.js}
|
|
13
|
+
* @see {@link https://github.com/FormidableLabs/react-fast-compare/blob/master/index.js react-fast-compare/index.js}
|
|
14
|
+
*/
|
|
15
|
+
export function dequal(foo: any, bar: any) {
|
|
16
|
+
let ctor, len, tmp;
|
|
17
|
+
if (foo === bar) return true;
|
|
18
|
+
|
|
19
|
+
if (foo && bar && (ctor = foo.constructor) === bar.constructor) {
|
|
20
|
+
if (ctor === Date) return foo.getTime() === bar.getTime();
|
|
21
|
+
if (ctor === RegExp) return foo.toString() === bar.toString();
|
|
22
|
+
|
|
23
|
+
if (ctor === Array) {
|
|
24
|
+
if ((len = foo.length) === bar.length) {
|
|
25
|
+
while (len-- && dequal(foo[len], bar[len]));
|
|
26
|
+
}
|
|
27
|
+
return len === -1;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
if (ctor === Set) {
|
|
31
|
+
if (foo.size !== bar.size) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
for (len of foo) {
|
|
35
|
+
tmp = len;
|
|
36
|
+
if (tmp && typeof tmp === 'object') {
|
|
37
|
+
tmp = find(bar, tmp);
|
|
38
|
+
if (!tmp) return false;
|
|
39
|
+
}
|
|
40
|
+
if (!bar.has(tmp)) return false;
|
|
41
|
+
}
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
if (ctor === Map) {
|
|
46
|
+
if (foo.size !== bar.size) {
|
|
47
|
+
return false;
|
|
48
|
+
}
|
|
49
|
+
for (len of foo) {
|
|
50
|
+
tmp = len[0];
|
|
51
|
+
if (tmp && typeof tmp === 'object') {
|
|
52
|
+
tmp = find(bar, tmp);
|
|
53
|
+
if (!tmp) return false;
|
|
54
|
+
}
|
|
55
|
+
if (!dequal(len[1], bar.get(tmp))) {
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
return true;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (ctor === ArrayBuffer) {
|
|
63
|
+
foo = new Uint8Array(foo);
|
|
64
|
+
bar = new Uint8Array(bar);
|
|
65
|
+
} else if (ctor === DataView) {
|
|
66
|
+
if ((len = foo.byteLength) === bar.byteLength) {
|
|
67
|
+
while (len-- && foo.getInt8(len) === bar.getInt8(len));
|
|
68
|
+
}
|
|
69
|
+
return len === -1;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (ArrayBuffer.isView(foo)) {
|
|
73
|
+
if ((len = foo.byteLength) === bar.byteLength) {
|
|
74
|
+
while (len-- && (foo as any)[len] === bar[len]);
|
|
75
|
+
}
|
|
76
|
+
return len === -1;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// react & dom
|
|
80
|
+
if (hasElementType && foo instanceof Element) return false;
|
|
81
|
+
|
|
82
|
+
if (!ctor || typeof foo === 'object') {
|
|
83
|
+
len = 0;
|
|
84
|
+
for (ctor in foo) {
|
|
85
|
+
if ((ctor === '_owner' || ctor === '__v' || ctor === '__o') && foo.$$typeof) {
|
|
86
|
+
// React-specific: avoid traversing React elements' _owner
|
|
87
|
+
// Preact-specific: avoid traversing Preact elements' __v and __o
|
|
88
|
+
// __v = $_original / $_vnode
|
|
89
|
+
// __o = $_owner
|
|
90
|
+
// These properties contain circular references and are not needed when
|
|
91
|
+
// comparing the actual elements (and not their owners)
|
|
92
|
+
// .$$typeof and ._store on just reasonable markers of elements
|
|
93
|
+
|
|
94
|
+
continue;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (has.call(foo, ctor) && ++len && !has.call(bar, ctor)) return false;
|
|
98
|
+
if (!(ctor in bar) || !dequal(foo[ctor], bar[ctor])) return false;
|
|
99
|
+
}
|
|
100
|
+
return Object.keys(bar).length === len;
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return foo !== foo && bar !== bar;
|
|
105
|
+
}
|