@serwist/utils 10.0.0-preview.1
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/LICENSE +21 -0
- package/dist/index.js +33 -0
- package/package.json +40 -0
- package/src/index.ts +7 -0
- package/src/nonNullable.ts +1 -0
- package/src/parallel.ts +42 -0
- package/src/toUnix.ts +3 -0
- package/src/types.ts +55 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018-2023 Google LLC, 2019-2023 ShadowWalker w@weiw.io https://weiw.io, 2020-2023 Anthony Fu <https://github.com/antfu>, 2023-PRESENT Serwist
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
const nonNullable = (value)=>value !== null && value !== undefined;
|
|
2
|
+
|
|
3
|
+
const parallel = async (limit, array, func)=>{
|
|
4
|
+
const work = array.map((item, index)=>({
|
|
5
|
+
index,
|
|
6
|
+
item
|
|
7
|
+
}));
|
|
8
|
+
const processor = async (res)=>{
|
|
9
|
+
const results = [];
|
|
10
|
+
while(true){
|
|
11
|
+
const next = work.pop();
|
|
12
|
+
if (!next) {
|
|
13
|
+
return res(results);
|
|
14
|
+
}
|
|
15
|
+
const result = await func(next.item);
|
|
16
|
+
results.push({
|
|
17
|
+
result: result,
|
|
18
|
+
index: next.index
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
const queues = Array.from({
|
|
23
|
+
length: limit
|
|
24
|
+
}, ()=>new Promise(processor));
|
|
25
|
+
const results = (await Promise.all(queues)).flat().sort((a, b)=>a.index < b.index ? -1 : 1).map((res)=>res.result);
|
|
26
|
+
return results;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
const toUnix = (p)=>{
|
|
30
|
+
return p.replace(/\\/g, "/").replace(/(?<!^)\/+/g, "/");
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export { nonNullable, parallel, toUnix };
|
package/package.json
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@serwist/utils",
|
|
3
|
+
"version": "10.0.0-preview.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "This module contains internal utilities used by Serwist packages.",
|
|
6
|
+
"files": [
|
|
7
|
+
"src"
|
|
8
|
+
],
|
|
9
|
+
"keywords": [
|
|
10
|
+
"serwist",
|
|
11
|
+
"serwistjs",
|
|
12
|
+
"service worker",
|
|
13
|
+
"sw"
|
|
14
|
+
],
|
|
15
|
+
"author": "Serwist <ducanh2912.rusty@gmail.com> (https://serwist.pages.dev/)",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"repository": "https://github.com/serwist/serwist",
|
|
18
|
+
"bugs": "https://github.com/serwist/serwist/issues",
|
|
19
|
+
"homepage": "https://serwist.pages.dev",
|
|
20
|
+
"main": "./dist/index.js",
|
|
21
|
+
"types": "./dist/index.d.ts",
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"types": "./dist/index.d.ts",
|
|
25
|
+
"default": "./dist/index.js"
|
|
26
|
+
},
|
|
27
|
+
"./package.json": "./package.json"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"rollup": "4.34.8",
|
|
31
|
+
"typescript": "5.7.3",
|
|
32
|
+
"@serwist/configs": "10.0.0-preview.1"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"build": "rimraf dist && NODE_ENV=production rollup --config rollup.config.js",
|
|
36
|
+
"dev": "rollup --config rollup.config.js --watch",
|
|
37
|
+
"lint": "biome lint ./src",
|
|
38
|
+
"typecheck": "tsc"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const nonNullable = <T>(value: T): value is NonNullable<T> => value !== null && value !== undefined;
|
package/src/parallel.ts
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// Source code: https://github.com/rayepps/radash/blob/03dd3152f560414e933cedcd3bda3c6db3e8306b/src/async.ts#L112-L147
|
|
2
|
+
// License: MIT
|
|
3
|
+
// Author: rayepps
|
|
4
|
+
interface ItemResult<K> {
|
|
5
|
+
index: number;
|
|
6
|
+
result: K;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Executes many async functions in parallel. Returns the
|
|
11
|
+
* results from all functions as an array. Does not handle
|
|
12
|
+
* any error.
|
|
13
|
+
*/
|
|
14
|
+
export const parallel = async <T, K>(limit: number, array: readonly T[], func: (item: T) => Promise<K>): Promise<K[]> => {
|
|
15
|
+
const work = array.map((item, index) => ({
|
|
16
|
+
index,
|
|
17
|
+
item,
|
|
18
|
+
}));
|
|
19
|
+
// Process array items
|
|
20
|
+
const processor = async (res: (value: ItemResult<K>[]) => void) => {
|
|
21
|
+
const results: ItemResult<K>[] = [];
|
|
22
|
+
while (true) {
|
|
23
|
+
const next = work.pop();
|
|
24
|
+
if (!next) {
|
|
25
|
+
return res(results);
|
|
26
|
+
}
|
|
27
|
+
const result = await func(next.item);
|
|
28
|
+
results.push({
|
|
29
|
+
result: result,
|
|
30
|
+
index: next.index,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
// Create queues
|
|
35
|
+
const queues = Array.from({ length: limit }, () => new Promise(processor));
|
|
36
|
+
// Wait for all queues to complete
|
|
37
|
+
const results = (await Promise.all(queues))
|
|
38
|
+
.flat()
|
|
39
|
+
.sort((a, b) => (a.index < b.index ? -1 : 1))
|
|
40
|
+
.map((res) => res.result);
|
|
41
|
+
return results;
|
|
42
|
+
};
|
package/src/toUnix.ts
ADDED
package/src/types.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Makes certain fields in a object type required
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* interface A {
|
|
6
|
+
* a?: string;
|
|
7
|
+
* b?: string;
|
|
8
|
+
* c?: string;
|
|
9
|
+
* }
|
|
10
|
+
* type B = RequiredFields<A, "b" | "c">;
|
|
11
|
+
* const b: B = {
|
|
12
|
+
* b: "hehe",
|
|
13
|
+
* c: "hehe",
|
|
14
|
+
* }; //valid
|
|
15
|
+
* const b: B = { a: "hehe" }; //invalid
|
|
16
|
+
* const c: B = { a: "hehe", b: "hehe" }; //invalid
|
|
17
|
+
*/
|
|
18
|
+
export type Require<T, U extends keyof T> = T & Required<Pick<T, U>>;
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Makes certain fields in a object type optional
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* interface A {
|
|
25
|
+
* a: string;
|
|
26
|
+
* b: string;
|
|
27
|
+
* c: string;
|
|
28
|
+
* }
|
|
29
|
+
* type B = Optional<A, "b" | "c">;
|
|
30
|
+
* const b: B = { a: "hehe" }; //valid
|
|
31
|
+
* const b: B = {}; //invalid
|
|
32
|
+
*/
|
|
33
|
+
export type Optional<T, U extends keyof T> = Omit<T, U> & Partial<Pick<T, U>>;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Makes an object type's hover overlay more readable
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
*
|
|
40
|
+
* interface A {
|
|
41
|
+
* b: string;
|
|
42
|
+
* c: boolean;
|
|
43
|
+
* }
|
|
44
|
+
*
|
|
45
|
+
* interface B {
|
|
46
|
+
* c: number;
|
|
47
|
+
* }
|
|
48
|
+
*
|
|
49
|
+
* type D = A | B; // Displayed as is written
|
|
50
|
+
*
|
|
51
|
+
* type C = Prettify<A | B>; // { b: string; c: boolean; } | { c: number; }
|
|
52
|
+
*/
|
|
53
|
+
export type Prettify<T> = {
|
|
54
|
+
[K in keyof T]: T[K];
|
|
55
|
+
} & {};
|