@serwist/utils 9.4.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/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.
@@ -0,0 +1,6 @@
1
+ import { nonNullable } from "./nonNullable.js";
2
+ import { parallel } from "./parallel.js";
3
+ import { toUnix } from "./toUnix.js";
4
+ export { nonNullable, parallel, toUnix };
5
+ export type * from "./types.js";
6
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAC/C,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AACzC,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAErC,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC;AAEzC,mBAAmB,YAAY,CAAC"}
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 };
@@ -0,0 +1,2 @@
1
+ export declare const nonNullable: <T>(value: T) => value is NonNullable<T>;
2
+ //# sourceMappingURL=nonNullable.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"nonNullable.d.ts","sourceRoot":"","sources":["../src/nonNullable.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,WAAW,GAAI,CAAC,EAAE,OAAO,CAAC,KAAG,KAAK,IAAI,WAAW,CAAC,CAAC,CAA0C,CAAC"}
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Executes many async functions in parallel. Returns the
3
+ * results from all functions as an array. Does not handle
4
+ * any error.
5
+ */
6
+ export declare const parallel: <T, K>(limit: number, array: readonly T[], func: (item: T) => Promise<K>) => Promise<K[]>;
7
+ //# sourceMappingURL=parallel.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parallel.d.ts","sourceRoot":"","sources":["../src/parallel.ts"],"names":[],"mappings":"AAQA;;;;GAIG;AACH,eAAO,MAAM,QAAQ,GAAU,CAAC,EAAE,CAAC,EAAE,OAAO,MAAM,EAAE,OAAO,SAAS,CAAC,EAAE,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC,KAAK,OAAO,CAAC,CAAC,CAAC,KAAG,OAAO,CAAC,CAAC,EAAE,CA4BnH,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare const toUnix: (p: string) => string;
2
+ //# sourceMappingURL=toUnix.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"toUnix.d.ts","sourceRoot":"","sources":["../src/toUnix.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,MAAM,GAAI,GAAG,MAAM,WAE/B,CAAC"}
@@ -0,0 +1,55 @@
1
+ /**
2
+ * Make 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
+ * Make certain fields in a object type optional
21
+ *
22
+ * @example
23
+ * interface A {
24
+ * a: string;
25
+ * b: string;
26
+ * c: string;
27
+ * }
28
+ * type B = Optional<A, "b" | "c">;
29
+ * const b: B = { a: "hehe" }; //valid
30
+ * const b: B = {}; //invalid
31
+ */
32
+ export type Optional<T, U extends keyof T> = Omit<T, U> & Partial<Pick<T, U>>;
33
+ /**
34
+ * Makes an object type's hover overlay more readable
35
+ *
36
+ * @example
37
+ *
38
+ * interface A {
39
+ * b: string;
40
+ * c: boolean;
41
+ * }
42
+ *
43
+ * interface B {
44
+ * c: number;
45
+ * }
46
+ *
47
+ * type D = A | B; // Displayed as is written
48
+ *
49
+ * type C = Prettify<A | B>; // { b: string; c: boolean; } | { c: number; }
50
+ */
51
+ export type Prettify<T> = {
52
+ [K in keyof T]: T[K];
53
+ } & {};
54
+ export type MaybePromise<T> = T | Promise<T>;
55
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,OAAO,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,IAAI,CAAC,GAAG,QAAQ,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAErE;;;;;;;;;;;;GAYG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,IAAI,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC;AAE9E;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAM,MAAM,QAAQ,CAAC,CAAC,IAAI;KACvB,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CACrB,GAAG,EAAE,CAAC;AAEP,MAAM,MAAM,YAAY,CAAC,CAAC,IAAI,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC"}
package/package.json ADDED
@@ -0,0 +1,42 @@
1
+ {
2
+ "name": "@serwist/utils",
3
+ "version": "9.4.3",
4
+ "type": "module",
5
+ "description": "This module contains utilities used by Serwist packages.",
6
+ "files": [
7
+ "src",
8
+ "dist"
9
+ ],
10
+ "keywords": [
11
+ "serwist",
12
+ "serwistjs",
13
+ "service worker",
14
+ "sw"
15
+ ],
16
+ "author": "Serwist <ducanh2912.rusty@gmail.com> (https://serwist.pages.dev/)",
17
+ "license": "MIT",
18
+ "repository": "https://github.com/serwist/serwist",
19
+ "bugs": "https://github.com/serwist/serwist/issues",
20
+ "homepage": "https://serwist.pages.dev",
21
+ "main": "./dist/index.js",
22
+ "types": "./dist/index.d.ts",
23
+ "exports": {
24
+ ".": {
25
+ "types": "./dist/index.d.ts",
26
+ "default": "./dist/index.js"
27
+ },
28
+ "./package.json": "./package.json"
29
+ },
30
+ "devDependencies": {
31
+ "@types/common-tags": "1.8.4",
32
+ "rollup": "4.54.0",
33
+ "typescript": "5.9.3",
34
+ "@serwist/configs": "9.4.3"
35
+ },
36
+ "scripts": {
37
+ "build": "rimraf dist && NODE_ENV=production rollup --config rollup.config.js",
38
+ "dev": "rollup --config rollup.config.js --watch",
39
+ "lint": "biome lint ./src",
40
+ "typecheck": "tsc"
41
+ }
42
+ }
package/src/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ import { nonNullable } from "./nonNullable.js";
2
+ import { parallel } from "./parallel.js";
3
+ import { toUnix } from "./toUnix.js";
4
+
5
+ export { nonNullable, parallel, toUnix };
6
+
7
+ export type * from "./types.js";
@@ -0,0 +1 @@
1
+ export const nonNullable = <T>(value: T): value is NonNullable<T> => value !== null && value !== undefined;
@@ -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
@@ -0,0 +1,3 @@
1
+ export const toUnix = (p: string) => {
2
+ return p.replace(/\\/g, "/").replace(/(?<!^)\/+/g, "/");
3
+ };
package/src/types.ts ADDED
@@ -0,0 +1,57 @@
1
+ /**
2
+ * Make 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
+ * Make 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
+ } & {};
56
+
57
+ export type MaybePromise<T> = T | Promise<T>;