@stacksjs/objects 0.74.31 → 0.74.33

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/dist/index.d.ts CHANGED
@@ -26,7 +26,7 @@ import type { DeepMerge } from '@stacksjs/types';
26
26
  * // { b: 2 }
27
27
  * ```
28
28
  */
29
- export declare function objectMap<K extends string, V, NK = K, NV = V>(obj: Record<K, V>, fn: (key: K, value: V) => [NK, NV] | undefined): Record<K, V>;
29
+ export declare function objectMap<K extends string, V, NK extends PropertyKey = K, NV = V>(obj: Record<K, V>, fn: (key: K, value: V) => [NK, NV] | undefined): Record<NK, NV>;
30
30
  /**
31
31
  * Type guard for any key, `k`.
32
32
  * Marks `k` as a key of `T` if `k` is in `obj`.
@@ -72,3 +72,4 @@ export declare function clearUndefined<T extends object>(obj: T): T;
72
72
  * @category Object
73
73
  */
74
74
  export declare function hasOwnProperty<T>(obj: T, v: PropertyKey): boolean;
75
+ export * from './transform';
package/dist/index.js CHANGED
@@ -1,2 +1,2 @@
1
1
  // @bun
2
- import{notNullish as s}from"@stacksjs/utils";import{isObject as f}from"@stacksjs/validation";function p(e,n){return Object.fromEntries(Object.entries(e).map(([t,o])=>n(t,o)).filter(s))}function a(e,n){return n in e}function u(e){return Object.keys(e)}function b(e){return Object.entries(e)}function c(e,...n){if(!n.length)return e;let[t,...o]=n;if(t===void 0)return e;if(i(e)&&i(t))u(t).forEach((r)=>{if(i(t[r])){if(!e[r])e[r]={};c(e[r],t[r])}else e[r]=t[r]});return c(e,...o)}function i(e){return f(e)&&!Array.isArray(e)}function l(e,n,t=!1){return n.reduce((o,r)=>{if(r in e){if(!t||e[r]!==void 0)o[r]=e[r]}return o},{})}function y(e){return Object.keys(e).forEach((n)=>e[n]===void 0&&delete e[n]),e}function x(e,n){return e==null?!1:Object.prototype.hasOwnProperty.call(e,n)}export{y as clearUndefined,c as deepMerge,x as hasOwnProperty,a as isKeyOf,b as objectEntries,u as objectKeys,p as objectMap,l as objectPick};
2
+ import{notNullish as O}from"@stacksjs/utils";import{isObject as p}from"@stacksjs/validation";function k(e,n){let t=new Set(n),r={};for(let[o,s]of Object.entries(e))if(!t.has(o))r[o]=s;return r}function a(e,n){let t={};for(let[r,o]of Object.entries(e))if(n(o,r))t[r]=o;return t}function l(e,n){return a(e,(t,r)=>!n(t,r))}function x(e,n){let t={};for(let[r,o]of Object.entries(e))t[r]=n(o,r);return t}function b(e,n){let t={};for(let[r,o]of Object.entries(e))t[n(r,o)]=o;return t}function j(e){let n={};for(let[t,r]of Object.entries(e))n[String(r)]=t;return n}function K(e){for(let n in e)if(Object.hasOwn(e,n))return!1;return!0}function T(e,n,t){let r=e;for(let o of n){if(r===null||r===void 0)return t;r=r[o]}return r===void 0?t:r}function y(e,n,t){if(n.length===0)return t;let[r,...o]=n,s=Array.isArray(e)?[...e]:{...e};if(o.length===0)return s[r]=t,s;let i=s[r],u=i===null||typeof i!=="object"?typeof o[0]==="number"?[]:{}:i;return s[r]=y(u,o,t),s}function m(e,n){return Object.fromEntries(Object.entries(e).map(([t,r])=>n(t,r)).filter(O))}function w(e,n){return n in e}function d(e){return Object.keys(e)}function R(e){return Object.entries(e)}function f(e,...n){if(!n.length)return e;let[t,...r]=n;if(t===void 0)return e;if(c(e)&&c(t))d(t).forEach((o)=>{if(c(t[o])){if(!e[o])e[o]={};f(e[o],t[o])}else e[o]=t[o]});return f(e,...r)}function c(e){return p(e)&&!Array.isArray(e)}function S(e,n,t=!1){return n.reduce((r,o)=>{if(o in e){if(!t||e[o]!==void 0)r[o]=e[o]}return r},{})}function V(e){return Object.keys(e).forEach((n)=>e[n]===void 0&&delete e[n]),e}function v(e,n){return e==null?!1:Object.prototype.hasOwnProperty.call(e,n)}export{V as clearUndefined,f as deepMerge,T as getPath,v as hasOwnProperty,j as invert,K as isEmptyObject,w as isKeyOf,b as mapKeys,x as mapValues,R as objectEntries,d as objectKeys,m as objectMap,S as objectPick,k as omit,l as omitBy,a as pickBy,y as setPath};
@@ -0,0 +1,147 @@
1
+ /**
2
+ * Reshaping an object: selecting parts of it, rewriting its keys or values,
3
+ * reading and writing through a path.
4
+ *
5
+ * The counterpart to `arrays/src/transform.ts`, and the same answer to the same
6
+ * request (stacksjs/stacks#412): the operations an application kept writing
7
+ * inline, implemented in the package that already owns this surface rather than
8
+ * imported from a second utility library.
9
+ *
10
+ * Everything here is immutable and shallow unless its own documentation says
11
+ * otherwise. Only an object's own enumerable string keys are visited -
12
+ * inherited and symbol keys are left alone, which matches `Object.entries` and
13
+ * is what stops a plain-object helper from wandering into a prototype.
14
+ */
15
+ /**
16
+ * A copy without the given keys.
17
+ *
18
+ * The inverse of `objectPick`. Keys that are not present are ignored rather
19
+ * than an error, so a caller can omit a superset without checking first.
20
+ *
21
+ * @category Object
22
+ * @example
23
+ * ```ts
24
+ * omit({ a: 1, b: 2, c: 3 }, ['b']) // { a: 1, c: 3 }
25
+ * ```
26
+ */
27
+ export declare function omit<O extends object, K extends keyof O>(obj: O, keys: readonly K[]): Omit<O, K>;
28
+ /**
29
+ * A copy keeping only the entries the predicate accepts.
30
+ *
31
+ * @category Object
32
+ * @example
33
+ * ```ts
34
+ * pickBy({ a: 1, b: 2 }, value => value > 1) // { b: 2 }
35
+ * ```
36
+ */
37
+ export declare function pickBy<O extends object>(obj: O, predicate: (value: O[keyof O], key: keyof O & string) => boolean): Partial<O>;
38
+ /**
39
+ * A copy dropping the entries the predicate accepts.
40
+ *
41
+ * The complement of {@link pickBy} - the same predicate partitions an object
42
+ * between the two.
43
+ *
44
+ * @category Object
45
+ * @example
46
+ * ```ts
47
+ * omitBy({ a: 1, b: undefined }, value => value === undefined) // { a: 1 }
48
+ * ```
49
+ */
50
+ export declare function omitBy<O extends object>(obj: O, predicate: (value: O[keyof O], key: keyof O & string) => boolean): Partial<O>;
51
+ /**
52
+ * A copy with every value replaced by what `transform` returns.
53
+ *
54
+ * Keys are untouched, which is what distinguishes this from `objectMap` and
55
+ * makes the result's type exact: `Record<keyof O, R>` rather than something
56
+ * that depends on what the callback did with the key.
57
+ *
58
+ * @category Object
59
+ * @example
60
+ * ```ts
61
+ * mapValues({ a: 1, b: 2 }, value => value * 10) // { a: 10, b: 20 }
62
+ * ```
63
+ */
64
+ export declare function mapValues<O extends object, R>(obj: O, transform: (value: O[keyof O], key: keyof O & string) => R): { [K in keyof O]: R };
65
+ /**
66
+ * A copy with every key replaced by what `transform` returns.
67
+ *
68
+ * Two keys can collide once rewritten; the last one visited wins, matching what
69
+ * an assignment loop would do.
70
+ *
71
+ * @category Object
72
+ * @example
73
+ * ```ts
74
+ * mapKeys({ a: 1, b: 2 }, key => key.toUpperCase()) // { A: 1, B: 2 }
75
+ * ```
76
+ */
77
+ export declare function mapKeys<O extends object, K extends PropertyKey>(obj: O, transform: (key: keyof O & string, value: O[keyof O]) => K): Record<K, O[keyof O]>;
78
+ /**
79
+ * Swap keys and values.
80
+ *
81
+ * Every value becomes a key, so values are coerced to strings by the assignment
82
+ * - `{ a: 1 }` inverts to `{ '1': 'a' }`. Duplicate values collapse, last one
83
+ * winning, which is why this is only meaningful for an object whose values are
84
+ * unique.
85
+ *
86
+ * @category Object
87
+ * @example
88
+ * ```ts
89
+ * invert({ a: 'x', b: 'y' }) // { x: 'a', y: 'b' }
90
+ * ```
91
+ */
92
+ export declare function invert<O extends Record<string, PropertyKey>>(obj: O): Record<string, keyof O & string>;
93
+ /**
94
+ * Whether an object has no own enumerable string keys.
95
+ *
96
+ * Named for what it checks rather than the broader "is this empty" this could
97
+ * have been: a single helper that also answers for strings, arrays, Maps and
98
+ * Sets reads well at the call site and badly everywhere else, because the
99
+ * reader cannot tell which question is being asked.
100
+ *
101
+ * @category Object
102
+ * @example
103
+ * ```ts
104
+ * isEmptyObject({}) // true
105
+ * isEmptyObject({ a: 1 }) // false
106
+ * ```
107
+ */
108
+ export declare function isEmptyObject(obj: object): boolean;
109
+ /**
110
+ * Read through a path, answering `fallback` when any step is missing.
111
+ *
112
+ * The path is an array of steps rather than a dotted string, deliberately: a
113
+ * dotted string cannot express a key that contains a dot, and every
114
+ * implementation that parses one eventually has to grow an escape syntax.
115
+ *
116
+ * A `null` or `undefined` anywhere along the way ends the walk and yields the
117
+ * fallback, so this never throws on a partially-populated object - which is the
118
+ * entire reason to use it instead of `a?.b?.c`.
119
+ *
120
+ * @category Object
121
+ * @example
122
+ * ```ts
123
+ * getPath({ a: { b: [10, 20] } }, ['a', 'b', 1], 0) // 20
124
+ * getPath({ a: {} }, ['a', 'b', 'c'], 'default') // 'default'
125
+ * ```
126
+ */
127
+ export declare function getPath<T>(source: unknown, path: readonly PathStep[], fallback: T): T;
128
+ export declare function getPath(source: unknown, path: readonly PathStep[]): unknown;
129
+ /**
130
+ * A copy with the value at `path` replaced, creating missing steps on the way.
131
+ *
132
+ * Only the objects along the path are copied - siblings are shared with the
133
+ * original, which is what makes this cheap enough to use on every update. A
134
+ * numeric step creates an array where a step is missing, so
135
+ * `setPath({}, ['items', 0], x)` produces `{ items: [x] }` rather than
136
+ * `{ items: { 0: x } }`.
137
+ *
138
+ * @category Object
139
+ * @example
140
+ * ```ts
141
+ * setPath({ a: { b: 1 } }, ['a', 'b'], 2) // { a: { b: 2 } }
142
+ * setPath({}, ['a', 'b'], 1) // { a: { b: 1 } }
143
+ * ```
144
+ */
145
+ export declare function setPath<O extends object>(source: O, path: readonly PathStep[], value: unknown): O;
146
+ /** One step of a path: an object key or an array index. */
147
+ export type PathStep = string | number;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@stacksjs/objects",
3
3
  "type": "module",
4
- "version": "0.74.31",
4
+ "version": "0.74.33",
5
5
  "description": "The Stacks objects.",
6
6
  "author": "Chris Breuer",
7
7
  "contributors": [
@@ -56,12 +56,12 @@
56
56
  "prepublishOnly": "bun run build"
57
57
  },
58
58
  "dependencies": {
59
- "@stacksjs/utils": "0.74.31",
60
- "@stacksjs/validation": "0.74.31"
59
+ "@stacksjs/utils": "0.74.33",
60
+ "@stacksjs/validation": "0.74.33"
61
61
  },
62
62
  "devDependencies": {
63
- "@stacksjs/collections": "0.74.31",
64
- "@stacksjs/types": "0.74.31",
63
+ "@stacksjs/collections": "0.74.33",
64
+ "@stacksjs/types": "0.74.33",
65
65
  "better-dx": "^0.2.24"
66
66
  },
67
67
  "sideEffects": false