@cto.af/utils 1.3.2 → 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 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 NodeJS.ErrnoException;
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
- CI?: boolean;
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
- promise: Promise<T>;
29
- resolve(value: T | PromiseLike<T>): void;
30
- reject(reason?: any): void;
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,26 +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;
58
- type Selector<T> = Partial<T> | (keyof T)[] | Set<keyof T>;
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;
59
101
  /**
60
102
  * Select some properties from an object into multiple other objects.
61
103
  * All unselected fields will be contained in a final object for the
62
104
  * "leftovers", meaning there will always be at least one element in the
63
105
  * 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.
106
+ * that object will be copied to the first element of the result array
107
+ * and subtracted from the rest of the results.
65
108
  *
66
109
  * @template T Composed options object.
67
110
  * @template U May be a Required<Partial<T>> type.
68
111
  * @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,
112
+ * @param defaultsU Defaults object or field names.
113
+ * @returns One object for each of args,
73
114
  * plus an extra one for everything that was left over.
74
115
  */
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 };
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
- 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};
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.2",
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.15.0",
21
+ "packageManager": "pnpm@10.20.0",
22
22
  "engines": {
23
23
  "node": ">=20"
24
24
  }