@cto.af/utils 1.3.1 → 1.3.2

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
@@ -55,16 +55,23 @@ declare function nameSet<T extends object>(defaults: T): Set<keyof T>;
55
55
  * @throws If sets are the wrong type.
56
56
  */
57
57
  declare function assertDisjoint(...sets: (Set<string> | string[] | object)[]): void;
58
+ type Selector<T> = Partial<T> | (keyof T)[] | Set<keyof T>;
58
59
  /**
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.
60
65
  *
61
66
  * @template T Composed options object.
67
+ * @template U May be a Required<Partial<T>> type.
62
68
  * @param obj The source object.
69
+ * @param defaults Defaults object or field names.
63
70
  * @param args Arrays of strings to select into the result
64
71
  * objects.
65
72
  * @returns {Partial<Record<keyof T, any>>[]} One object for each of args,
66
73
  * plus an extra one for everything that was left over.
67
74
  */
68
- declare function select<T extends object>(obj: T, ...args: (Partial<T> | (keyof T)[] | Set<keyof T>)[]): Partial<T>[];
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>[]];
69
76
 
70
- export { type CiOptions, type PromiseWithResolvers, assertDisjoint, errCode, isCI, isErrno, nameSet, promiseWithResolvers, select };
77
+ export { type CiOptions, type PromiseWithResolvers, type Selector, assertDisjoint, errCode, isCI, isErrno, nameSet, promiseWithResolvers, select };
package/lib/index.js CHANGED
@@ -1 +1 @@
1
- import u from"assert/strict";function T(e){return e instanceof Error&&Object.prototype.hasOwnProperty.call(e,"code")}function v(e,r){if(!T(e))return!1;switch(typeof r){case"string":return e.code===r;case"number":return e.errno===r}throw new TypeError(`Invalid code: ${JSON.stringify(r)}`)}function b(e){if(e&&"CI"in e)return!!e.CI;let{env:r}=process;return!!(r.CI||r.CONTINUOUS_INTEGRATION||r.BUILD_NUMBER||r.RUN_ID)}var a=()=>{};function d(){let e=a,r=a;return{promise:new Promise((n,s)=>{e=n,r=s}),resolve:e,reject:r}}function p(e){return new Set(Object.keys(e))}function k(...e){let r=new Set;for(let t of e){let n=null;if(Array.isArray(t))n=t;else if(t instanceof Set)n=t;else if(typeof t=="object"&&t)n=Object.keys(t);else throw new Error("Invalid set");for(let s of n)u(!r.has(s)),r.add(s)}}function m(e,...r){let t=[],n=r.map(o=>{let i=Object.create(null);if(Array.isArray(o))return t.push(new Set(o)),i;if(o instanceof Set)return t.push(o),i;if(!o||typeof o!="object")throw new Error("Invalid set");return t.push(p(o)),Object.assign(i,o)}),s=Object.create(null);if(n.push(s),!e)return n;for(let[o,i]of Object.entries(e)){let f=!1;t.forEach((c,l)=>{!f&&c.has(o)&&(f=!0,n[l][o]=i)}),f||(s[o]=i)}return n}export{k as assertDisjoint,v as errCode,b as isCI,T as isErrno,p as nameSet,d as promiseWithResolvers,m as select};
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};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cto.af/utils",
3
- "version": "1.3.1",
3
+ "version": "1.3.2",
4
4
  "decription": "",
5
5
  "main": "lib/index.js",
6
6
  "files": [