@cto.af/utils 1.3.1 → 1.3.5
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/lib/index.d.ts +63 -13
- package/lib/index.js +1 -1
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
|
@@ -1,10 +1,30 @@
|
|
|
1
|
+
//#region src/index.d.ts
|
|
2
|
+
declare class AssertionError extends Error {
|
|
3
|
+
constructor(actual: unknown, expected: unknown, message?: string);
|
|
4
|
+
toString(): string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Very simplified assert, which will work in the browser.
|
|
8
|
+
*
|
|
9
|
+
* @param value Any value that should be truthy.
|
|
10
|
+
* @param message Optional message.
|
|
11
|
+
* @throws {AssertionError} If value is falsy.
|
|
12
|
+
*/
|
|
13
|
+
declare function assert(value: unknown, message?: string): asserts value;
|
|
14
|
+
type Pretty<T> = { [K in keyof T]: T[K] } & {};
|
|
15
|
+
interface ErrnoException extends Error {
|
|
16
|
+
errno?: number | undefined;
|
|
17
|
+
code?: string | undefined;
|
|
18
|
+
path?: string | undefined;
|
|
19
|
+
syscall?: string | undefined;
|
|
20
|
+
}
|
|
1
21
|
/**
|
|
2
22
|
* Is the object an ErrnoException?
|
|
3
23
|
*
|
|
4
24
|
* @param e Object to check.
|
|
5
25
|
* @returns Type assertion.
|
|
6
26
|
*/
|
|
7
|
-
declare function isErrno(e: unknown): e is
|
|
27
|
+
declare function isErrno(e: unknown): e is ErrnoException;
|
|
8
28
|
/**
|
|
9
29
|
* Does the error contain a specific code?
|
|
10
30
|
*
|
|
@@ -15,7 +35,7 @@ declare function isErrno(e: unknown): e is NodeJS.ErrnoException;
|
|
|
15
35
|
*/
|
|
16
36
|
declare function errCode(e: unknown, code: string | number): boolean;
|
|
17
37
|
interface CiOptions {
|
|
18
|
-
|
|
38
|
+
CI?: boolean;
|
|
19
39
|
}
|
|
20
40
|
/**
|
|
21
41
|
* Is the current environment CI-like.
|
|
@@ -25,9 +45,9 @@ interface CiOptions {
|
|
|
25
45
|
*/
|
|
26
46
|
declare function isCI(opts?: CiOptions): boolean;
|
|
27
47
|
interface PromiseWithResolvers<T> {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
48
|
+
promise: Promise<T>;
|
|
49
|
+
resolve(value: T | PromiseLike<T>): void;
|
|
50
|
+
reject(reason?: any): void;
|
|
31
51
|
}
|
|
32
52
|
/**
|
|
33
53
|
* Polyfill for Promise.withResolvers. Once node 22 is the minimum version,
|
|
@@ -42,7 +62,7 @@ declare function promiseWithResolvers<T>(): PromiseWithResolvers<T>;
|
|
|
42
62
|
/**
|
|
43
63
|
* Get the names of the keys of an options type, from the defaults.
|
|
44
64
|
*
|
|
45
|
-
* @template T
|
|
65
|
+
* @template T Options type.
|
|
46
66
|
* @param defaults Default values for the options.
|
|
47
67
|
* @returns List of keys of the given object.
|
|
48
68
|
*/
|
|
@@ -52,19 +72,49 @@ declare function nameSet<T extends object>(defaults: T): Set<keyof T>;
|
|
|
52
72
|
* Useful for validating inputs to select.
|
|
53
73
|
*
|
|
54
74
|
* @param sets Sets to check.
|
|
55
|
-
* @throws If sets are the wrong type.
|
|
75
|
+
* @throws {AssertionError} If sets are the wrong type.
|
|
56
76
|
*/
|
|
57
77
|
declare function assertDisjoint(...sets: (Set<string> | string[] | object)[]): void;
|
|
78
|
+
/**
|
|
79
|
+
* Select with a defaults object, a list of keys, or a set of keys. The
|
|
80
|
+
* set of keys might perform better, but we can't extract types from it.
|
|
81
|
+
*
|
|
82
|
+
* @template T Options object, where most properties are optional.
|
|
83
|
+
*/
|
|
84
|
+
type Selector<T extends object> = Partial<T> | (keyof T)[] | Set<keyof T>;
|
|
85
|
+
/**
|
|
86
|
+
* A defaults object extracts a required subset of T.
|
|
87
|
+
* A list of keys extracts a partial subset of T with those possible keys.
|
|
88
|
+
* A Set extracts a full partial of T.
|
|
89
|
+
*
|
|
90
|
+
* @template T Options object, where most properties are optional.
|
|
91
|
+
* @template U Selector.
|
|
92
|
+
*/
|
|
93
|
+
type Selected<T extends object, U extends Selector<T>> = U extends Partial<T> ? U : U extends (keyof T)[] ? Pretty<Partial<Pick<T, U[number]>>> : Partial<T>;
|
|
94
|
+
/**
|
|
95
|
+
* The keys that were selected.
|
|
96
|
+
*
|
|
97
|
+
* @template T Options object, where most properties are optional.
|
|
98
|
+
* @template U Selector.
|
|
99
|
+
*/
|
|
100
|
+
type SelectedKeys<T extends object, U extends Selector<T>> = U extends Partial<T> ? keyof U : U extends (keyof T)[] ? U[number] : never;
|
|
58
101
|
/**
|
|
59
102
|
* Select some properties from an object into multiple other objects.
|
|
103
|
+
* All unselected fields will be contained in a final object for the
|
|
104
|
+
* "leftovers", meaning there will always be at least one element in the
|
|
105
|
+
* result array. If the first selector is a set of defaults, the type from
|
|
106
|
+
* that object will be copied to the first element of the result array
|
|
107
|
+
* and subtracted from the rest of the results.
|
|
60
108
|
*
|
|
61
109
|
* @template T Composed options object.
|
|
110
|
+
* @template U May be a Required<Partial<T>> type.
|
|
62
111
|
* @param obj The source object.
|
|
63
|
-
* @param
|
|
64
|
-
*
|
|
65
|
-
* @returns {Partial<Record<keyof T, any>>[]} One object for each of args,
|
|
112
|
+
* @param defaultsU Defaults object or field names.
|
|
113
|
+
* @returns One object for each of args,
|
|
66
114
|
* plus an extra one for everything that was left over.
|
|
67
115
|
*/
|
|
68
|
-
declare function select<T extends object
|
|
69
|
-
|
|
70
|
-
|
|
116
|
+
declare function select<T extends object, U extends Selector<T>>(obj: T, defaultsU: U): [Selected<T, U>, Pretty<Omit<T, SelectedKeys<T, U>>>];
|
|
117
|
+
declare function select<T extends object, U extends Selector<T>, V extends Selector<T>>(obj: T, defaultsU: U, defaultsV: V): [Selected<T, U>, Selected<T, V>, Pretty<Omit<T, SelectedKeys<T, U> | SelectedKeys<T, V>>>];
|
|
118
|
+
declare function select<T extends object, U extends Selector<T>, V extends Selector<T>, W extends Selector<T>>(obj: T, defaultsU: U, defaultsV: V, defaultsW: W): [Selected<T, U>, Selected<T, V>, Selected<T, W>, Pretty<Omit<T, SelectedKeys<T, U> | SelectedKeys<T, V> | SelectedKeys<T, W>>>];
|
|
119
|
+
//#endregion
|
|
120
|
+
export { AssertionError, CiOptions, ErrnoException, Pretty, PromiseWithResolvers, Selected, SelectedKeys, Selector, assert, assertDisjoint, errCode, isCI, isErrno, nameSet, promiseWithResolvers, select };
|
package/lib/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
var e=class extends Error{constructor(e,t,n){let r=!1;n||(n=`The expression evaluated to a falsy value:\n\n assert(${e})\n`,r=!0),super(n),Object.assign(this,{code:`ERR_ASSERTION`,actual:e,expected:t,operator:`==`,generatedMessage:r})}toString(){return`AssertionError [ERR_ASSERTION]: ${this.message}`}};function t(t,n){if(!t)throw new e(t,!0,n)}function n(e){return e instanceof Error&&Object.prototype.hasOwnProperty.call(e,`code`)}function r(e,t){if(!n(e))return!1;switch(typeof t){case`string`:return e.code===t;case`number`:return e.errno===t}throw TypeError(`Invalid code: ${JSON.stringify(t)}`)}function i(e){if(e&&`CI`in e)return!!e.CI;if(typeof process>`u`)return!1;let{env:t}=process;return!!(t.CI||t.CONTINUOUS_INTEGRATION||t.BUILD_NUMBER||t.RUN_ID)}const a=()=>void 0;function o(){let e=a,t=a;return{promise:new Promise((n,r)=>{e=n,t=r}),resolve:e,reject:t}}function s(e){return new Set(Object.keys(e))}function c(...e){let n=new Set;for(let r of e){let e=null;if(Array.isArray(r))e=r;else if(r instanceof Set)e=r;else if(typeof r==`object`&&r)e=Object.keys(r);else throw Error(`Invalid set`);for(let r of e)t(!n.has(r),r),n.add(r)}}function l(e,...t){let n=[],r=t.map(e=>{let t=Object.create(null);if(e instanceof Set)return n.push(e),t;if(Array.isArray(e))return n.push(new Set(e)),t;if(!e||typeof e!=`object`)throw Error(`Invalid set: ${e}`);return n.push(s(e)),Object.assign(t,e)}),i=Object.create(null);if(r.push(i),!e)return r;for(let[t,a]of Object.entries(e)){let e=!1;n.forEach((n,i)=>{!e&&n.has(t)&&(e=!0,r[i][t]=a)}),e||(i[t]=a)}return r}export{e as AssertionError,t as assert,c as assertDisjoint,r as errCode,i as isCI,n as isErrno,s as nameSet,o as promiseWithResolvers,l as select};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cto.af/utils",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.5",
|
|
4
4
|
"decription": "",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"files": [
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
"bugs": {
|
|
19
19
|
"url": "https://github.com/cto-af/utils/issues"
|
|
20
20
|
},
|
|
21
|
-
"packageManager": "pnpm@10.
|
|
21
|
+
"packageManager": "pnpm@10.20.0",
|
|
22
22
|
"engines": {
|
|
23
23
|
"node": ">=20"
|
|
24
24
|
}
|