@cto.af/utils 1.3.2 → 1.3.6
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.mts +120 -0
- package/lib/index.mjs +1 -0
- package/package.json +3 -3
- package/lib/index.d.ts +0 -77
- package/lib/index.js +0 -1
package/lib/index.d.mts
ADDED
|
@@ -0,0 +1,120 @@
|
|
|
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
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Is the object an ErrnoException?
|
|
23
|
+
*
|
|
24
|
+
* @param e Object to check.
|
|
25
|
+
* @returns Type assertion.
|
|
26
|
+
*/
|
|
27
|
+
declare function isErrno(e: unknown): e is ErrnoException;
|
|
28
|
+
/**
|
|
29
|
+
* Does the error contain a specific code?
|
|
30
|
+
*
|
|
31
|
+
* @param e Exception to check.
|
|
32
|
+
* @param code Code such as 'ENOENT' or -2.
|
|
33
|
+
* @returns True if code matches.
|
|
34
|
+
* @throws {TypeError} On invalid code type.
|
|
35
|
+
*/
|
|
36
|
+
declare function errCode(e: unknown, code: string | number): boolean;
|
|
37
|
+
interface CiOptions {
|
|
38
|
+
CI?: boolean;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Is the current environment CI-like.
|
|
42
|
+
*
|
|
43
|
+
* @param opts Override the environment determination, for testing.
|
|
44
|
+
* @returns True if in CI.
|
|
45
|
+
*/
|
|
46
|
+
declare function isCI(opts?: CiOptions): boolean;
|
|
47
|
+
interface PromiseWithResolvers<T> {
|
|
48
|
+
promise: Promise<T>;
|
|
49
|
+
resolve(value: T | PromiseLike<T>): void;
|
|
50
|
+
reject(reason?: any): void;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Polyfill for Promise.withResolvers. Once node 22 is the minimum version,
|
|
54
|
+
* this should be removed.
|
|
55
|
+
*
|
|
56
|
+
* @template T Return type for resolve.
|
|
57
|
+
* @returns An object containing a new Promise object and two functions to
|
|
58
|
+
* resolve or reject it, corresponding to the two parameters passed to the
|
|
59
|
+
* executor of the Promise() constructor.
|
|
60
|
+
*/
|
|
61
|
+
declare function promiseWithResolvers<T>(): PromiseWithResolvers<T>;
|
|
62
|
+
/**
|
|
63
|
+
* Get the names of the keys of an options type, from the defaults.
|
|
64
|
+
*
|
|
65
|
+
* @template T Options type.
|
|
66
|
+
* @param defaults Default values for the options.
|
|
67
|
+
* @returns List of keys of the given object.
|
|
68
|
+
*/
|
|
69
|
+
declare function nameSet<T extends object>(defaults: T): Set<keyof T>;
|
|
70
|
+
/**
|
|
71
|
+
* Assert that none of the given sets of strings share a value.
|
|
72
|
+
* Useful for validating inputs to select.
|
|
73
|
+
*
|
|
74
|
+
* @param sets Sets to check.
|
|
75
|
+
* @throws {AssertionError} If sets are the wrong type.
|
|
76
|
+
*/
|
|
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;
|
|
101
|
+
/**
|
|
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.
|
|
108
|
+
*
|
|
109
|
+
* @template T Composed options object.
|
|
110
|
+
* @template U May be a Required<Partial<T>> type.
|
|
111
|
+
* @param obj The source object.
|
|
112
|
+
* @param defaultsU Defaults object or field names.
|
|
113
|
+
* @returns One object for each of args,
|
|
114
|
+
* plus an extra one for everything that was left over.
|
|
115
|
+
*/
|
|
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.mjs
ADDED
|
@@ -0,0 +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,8 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cto.af/utils",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.6",
|
|
4
4
|
"decription": "",
|
|
5
|
-
"main": "lib/index.
|
|
5
|
+
"main": "lib/index.mjs",
|
|
6
6
|
"files": [
|
|
7
7
|
"lib/*"
|
|
8
8
|
],
|
|
@@ -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.22.0",
|
|
22
22
|
"engines": {
|
|
23
23
|
"node": ">=20"
|
|
24
24
|
}
|
package/lib/index.d.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Is the object an ErrnoException?
|
|
3
|
-
*
|
|
4
|
-
* @param e Object to check.
|
|
5
|
-
* @returns Type assertion.
|
|
6
|
-
*/
|
|
7
|
-
declare function isErrno(e: unknown): e is NodeJS.ErrnoException;
|
|
8
|
-
/**
|
|
9
|
-
* Does the error contain a specific code?
|
|
10
|
-
*
|
|
11
|
-
* @param e Exception to check.
|
|
12
|
-
* @param code Code such as 'ENOENT' or -2.
|
|
13
|
-
* @returns True if code matches.
|
|
14
|
-
* @throws {TypeError} On invalid code type.
|
|
15
|
-
*/
|
|
16
|
-
declare function errCode(e: unknown, code: string | number): boolean;
|
|
17
|
-
interface CiOptions {
|
|
18
|
-
CI?: boolean;
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Is the current environment CI-like.
|
|
22
|
-
*
|
|
23
|
-
* @param opts Override the environment determination, for testing.
|
|
24
|
-
* @returns True if in CI.
|
|
25
|
-
*/
|
|
26
|
-
declare function isCI(opts?: CiOptions): boolean;
|
|
27
|
-
interface PromiseWithResolvers<T> {
|
|
28
|
-
promise: Promise<T>;
|
|
29
|
-
resolve(value: T | PromiseLike<T>): void;
|
|
30
|
-
reject(reason?: any): void;
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Polyfill for Promise.withResolvers. Once node 22 is the minimum version,
|
|
34
|
-
* this should be removed.
|
|
35
|
-
*
|
|
36
|
-
* @template T Return type for resolve.
|
|
37
|
-
* @returns An object containing a new Promise object and two functions to
|
|
38
|
-
* resolve or reject it, corresponding to the two parameters passed to the
|
|
39
|
-
* executor of the Promise() constructor.
|
|
40
|
-
*/
|
|
41
|
-
declare function promiseWithResolvers<T>(): PromiseWithResolvers<T>;
|
|
42
|
-
/**
|
|
43
|
-
* Get the names of the keys of an options type, from the defaults.
|
|
44
|
-
*
|
|
45
|
-
* @template T
|
|
46
|
-
* @param defaults Default values for the options.
|
|
47
|
-
* @returns List of keys of the given object.
|
|
48
|
-
*/
|
|
49
|
-
declare function nameSet<T extends object>(defaults: T): Set<keyof T>;
|
|
50
|
-
/**
|
|
51
|
-
* Assert that none of the given sets of strings share a value.
|
|
52
|
-
* Useful for validating inputs to select.
|
|
53
|
-
*
|
|
54
|
-
* @param sets Sets to check.
|
|
55
|
-
* @throws If sets are the wrong type.
|
|
56
|
-
*/
|
|
57
|
-
declare function assertDisjoint(...sets: (Set<string> | string[] | object)[]): void;
|
|
58
|
-
type Selector<T> = Partial<T> | (keyof T)[] | Set<keyof T>;
|
|
59
|
-
/**
|
|
60
|
-
* Select some properties from an object into multiple other objects.
|
|
61
|
-
* All unselected fields will be contained in a final object for the
|
|
62
|
-
* "leftovers", meaning there will always be at least one element in the
|
|
63
|
-
* result array. If the first selector is a set of defaults, the type from
|
|
64
|
-
* that object will be copied to the first element of the result array.
|
|
65
|
-
*
|
|
66
|
-
* @template T Composed options object.
|
|
67
|
-
* @template U May be a Required<Partial<T>> type.
|
|
68
|
-
* @param obj The source object.
|
|
69
|
-
* @param defaults Defaults object or field names.
|
|
70
|
-
* @param args Arrays of strings to select into the result
|
|
71
|
-
* objects.
|
|
72
|
-
* @returns {Partial<Record<keyof T, any>>[]} One object for each of args,
|
|
73
|
-
* plus an extra one for everything that was left over.
|
|
74
|
-
*/
|
|
75
|
-
declare function select<T extends object, U extends Selector<T>>(obj: T, defaults?: U, ...args: Selector<T>[]): [U extends Partial<T> ? U : Partial<T>, ...Partial<T>[]];
|
|
76
|
-
|
|
77
|
-
export { type CiOptions, type PromiseWithResolvers, type Selector, assertDisjoint, errCode, isCI, isErrno, nameSet, promiseWithResolvers, select };
|
package/lib/index.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import u from"assert/strict";function p(e){return e instanceof Error&&Object.prototype.hasOwnProperty.call(e,"code")}function v(e,t){if(!p(e))return!1;switch(typeof t){case"string":return e.code===t;case"number":return e.errno===t}throw new TypeError(`Invalid code: ${JSON.stringify(t)}`)}function P(e){if(e&&"CI"in e)return!!e.CI;let{env:t}=process;return!!(t.CI||t.CONTINUOUS_INTEGRATION||t.BUILD_NUMBER||t.RUN_ID)}var l=()=>{};function b(){let e=l,t=l;return{promise:new Promise((n,s)=>{e=n,t=s}),resolve:e,reject:t}}function y(e){return new Set(Object.keys(e))}function k(...e){let t=new Set;for(let o of e){let n=null;if(Array.isArray(o))n=o;else if(o instanceof Set)n=o;else if(typeof o=="object"&&o)n=Object.keys(o);else throw new Error("Invalid set");for(let s of n)u(!t.has(s)),t.add(s)}}function m(e,t,...o){t!==void 0&&o.unshift(t);let n=[],s=o.map(r=>{let i=Object.create(null);if(r instanceof Set)return n.push(r),i;if(Array.isArray(r))return n.push(new Set(r)),i;if(!r||typeof r!="object")throw new Error(`Invalid set: ${r}`);return n.push(y(r)),Object.assign(i,r)}),f=Object.create(null);if(s.push(f),!e)return s;for(let[r,i]of Object.entries(e)){let a=!1;n.forEach((c,T)=>{!a&&c.has(r)&&(a=!0,s[T][r]=i)}),a||(f[r]=i)}return s}export{k as assertDisjoint,v as errCode,P as isCI,p as isErrno,y as nameSet,b as promiseWithResolvers,m as select};
|