Repository navigation
Expand file tree
/
Copy pathobjects.ts
More file actions
286 lines (267 loc) · 7.66 KB
/
Copy pathobjects.ts
File metadata and controls
286 lines (267 loc) · 7.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/**
* Safely access a nested property in an object using dot notation or bracket syntax.
*
* @param obj - The object to access
* @param path - The path to the property (e.g., "user.address.city" or "items[0].name")
* @returns The value at the path, or undefined if not found
*
* @example
* ```typescript
* const obj = { user: { name: 'John', addresses: [{ city: 'NYC' }] } }
*
* getObjPath(obj, 'user.name') // 'John'
* getObjPath(obj, 'user.addresses[0].city') // 'NYC'
* getObjPath(obj, 'user.missing') // undefined
* getObjPath<string>(obj, 'user.name') // typed as string | undefined
* ```
*/
export function getObjPath<T = any>(obj: unknown, path: string): T | undefined {
return path
.replace(/\[(\d+)\]/g, '.$1')
.split('.')
.reduce((acc, key) => (acc && typeof acc === 'object' ? (acc as any)[key] : undefined), obj) as T | undefined
}
/**
* Check if an object is empty (null, undefined, or has no keys).
*
* @param obj - The value to check
* @returns True if null, undefined, or an empty object
*
* @example
* ```typescript
* isObjEmpty(null) // true
* isObjEmpty(undefined) // true
* isObjEmpty({}) // true
* isObjEmpty({ a: 1 }) // false
* isObjEmpty([]) // true (empty array)
* ```
*/
export function isObjEmpty(obj: unknown): boolean {
return obj === null || obj === undefined || (typeof obj === 'object' && Object.keys(obj).length === 0)
}
/**
* Type-safe Object.keys that returns properly typed keys.
*
* @param obj - The object to get keys from
* @returns Array of keys typed as keyof Obj
*
* @example
* ```typescript
* const user = { name: 'John', age: 30 }
* const keys = getObjectKeys(user) // ('name' | 'age')[]
*
* keys.forEach(key => {
* console.log(user[key]) // No type error
* })
* ```
*/
export function getObjectKeys<Obj extends object>(obj: Obj): Array<keyof Obj> {
return Object.keys(obj) as Array<keyof Obj>
}
/**
* Type-safe Object.values that returns properly typed values.
*
* @param obj - The object to get values from
* @returns Array of values typed as Obj[keyof Obj]
*
* @example
* ```typescript
* const user = { name: 'John', age: 30 }
* const values = getObjectValues(user) // (string | number)[]
* ```
*/
export function getObjectValues<Obj extends object>(obj: Obj): Array<Obj[keyof Obj]> {
return Object.values(obj) as Array<Obj[keyof Obj]>
}
/**
* Type-safe Object.entries that preserves each returned string key and its matching value type.
*
* @param obj - The object to get entries from
* @returns Array of [key, value] tuples with matching key and value types
*
* @example
* ```typescript
* const user = { name: 'John', age: 30 }
* const entries = getObjectEntries(user) // ['name' | 'age', string | number][]
*
* entries.forEach(([key, value]) => {
* console.log(key, value) // Both properly typed
* })
* ```
*/
type ObjectEntry<Obj extends object> = {
[Key in keyof Obj]-?: Key extends string | number ? [`${Key}`, Obj[Key]] : never
}[keyof Obj]
export function getObjectEntries<Obj extends object>(obj: Obj): Array<ObjectEntry<Obj>> {
return Object.entries(obj) as Array<ObjectEntry<Obj>>
}
/**
* Extract only primitive (non-nested) properties from an object.
* Filters out nested objects, arrays, null, and functions.
*
* @param obj - The object to filter
* @returns A partial object containing only primitive values
*
* @example
* ```typescript
* const data = {
* id: 1,
* name: 'John',
* active: true,
* metadata: { created: '2024-01-01' },
* tags: ['a', 'b'],
* callback: () => {},
* }
*
* getRootLevelObject(data)
* // { id: 1, name: 'John', active: true }
* ```
*/
/**
* Creates a new object with specified keys removed.
*
* @param object - The source object
* @param keys - Keys to omit from the result
* @returns New object without the specified keys
*
* @example
* Single key:
* ```typescript
* const user = { id: 1, name: 'John', password: 'secret' }
* omit(user, 'password') // { id: 1, name: 'John' }
* ```
*
* @example
* Multiple keys:
* ```typescript
* const user = { id: 1, name: 'John', password: 'secret', token: 'abc' }
* omit(user, 'password', 'token') // { id: 1, name: 'John' }
* ```
*
* @example
* Sanitizing API responses:
* ```typescript
* const publicUser = omit(dbUser, 'passwordHash', 'email', 'privateNotes')
* ```
*/
/**
* Creates a new object with null and undefined values removed.
*
* @param obj - The source object
* @returns New object without nullish values
*
* @example
* ```typescript
* const data = { a: 1, b: null, c: 'hello', d: undefined, e: 0 }
* removeNullishValues(data) // { a: 1, c: 'hello', e: 0 }
* ```
*
* @example
* Cleaning up API payloads:
* ```typescript
* const payload = removeNullishValues({
* name: formData.name,
* email: formData.email || null,
* phone: formData.phone || undefined,
* })
* // Only includes fields with actual values
* ```
*
* @example
* Merging with defaults (nullish removed first):
* ```typescript
* const config = { ...defaults, ...removeNullishValues(userConfig) }
* ```
*/
/**
* Creates a new object containing only properties that pass the predicate.
*
* @param obj - The source object
* @param predicate - Function that returns true for properties to keep
* @returns New object with only matching properties
*
* @example
* Filter by value:
* ```typescript
* const scores = { alice: 85, bob: 42, carol: 91 }
* pickBy(scores, (v) => v > 50) // { alice: 85, carol: 91 }
* ```
*
* @example
* Filter by key:
* ```typescript
* const data = { _id: 1, name: 'John', _rev: '2' }
* pickBy(data, (_, k) => !String(k).startsWith('_')) // { name: 'John' }
* ```
*
* @example
* Remove falsy values:
* ```typescript
* const form = { name: 'John', email: '', age: 0, active: false }
* pickBy(form, Boolean) // { name: 'John' }
* ```
*/
export function pickBy<T extends object>(obj: T, predicate: (value: T[keyof T], key: keyof T) => boolean): Partial<T> {
return Object.entries(obj).reduce((acc, [key, value]) => {
const k = key as keyof T
if (predicate(value, k)) {
acc[k] = value
}
return acc
}, {} as Partial<T>)
}
export function removeNullishValues<T extends object>(obj: T): Partial<T> {
const newObj = {} as Partial<T>
for (const key in obj) {
if (obj[key] !== null && obj[key] !== undefined) {
newObj[key] = obj[key]
}
}
return newObj
}
export function omit<O extends object, K extends keyof O>(object: O, ...keys: K[]): Omit<O, K> {
const result = { ...object }
for (const key of keys) {
delete result[key]
}
return result
}
/**
* Creates a new object containing only the specified keys.
*
* @param object - The source object
* @param keys - Keys to include in the result
* @returns New object containing only the specified properties
*
* @example
* ```typescript
* const user = { id: 1, name: 'John', password: 'secret' }
* pick(user, 'id', 'name') // { id: 1, name: 'John' }
* ```
*
* @example
* Selecting fields for a request payload:
* ```typescript
* const payload = pick(formData, 'name', 'email')
* ```
*/
export function pick<O extends object, K extends keyof O>(object: O, ...keys: K[]): Pick<O, K> {
const result = {} as Pick<O, K>
for (const key of keys) {
result[key] = object[key]
}
return result
}
export function getRootLevelObject<Obj extends object>(obj: Obj): Partial<Obj> {
const result: Partial<Obj> = {}
for (const key in obj) {
if (!Object.prototype.hasOwnProperty.call(obj, key)) continue
const value = obj[key]
const isNestedObject = typeof value === 'object' || value === null || Array.isArray(value)
const isFunction = typeof value === 'function'
if (!isNestedObject && !isFunction) {
result[key] = value
}
}
return result
}