@satoshibits/functional 1.0.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/README.md +242 -0
- package/dist/array-utils.d.mts +317 -0
- package/dist/array-utils.d.mts.map +1 -0
- package/dist/array-utils.mjs +370 -0
- package/dist/array-utils.mjs.map +1 -0
- package/dist/composition.d.mts +603 -0
- package/dist/composition.d.mts.map +1 -0
- package/dist/composition.mjs +516 -0
- package/dist/composition.mjs.map +1 -0
- package/dist/object-utils.d.mts +267 -0
- package/dist/object-utils.d.mts.map +1 -0
- package/dist/object-utils.mjs +258 -0
- package/dist/object-utils.mjs.map +1 -0
- package/dist/option.d.mts +622 -0
- package/dist/option.d.mts.map +1 -0
- package/dist/option.mjs +637 -0
- package/dist/option.mjs.map +1 -0
- package/dist/performance.d.mts +265 -0
- package/dist/performance.d.mts.map +1 -0
- package/dist/performance.mjs +453 -0
- package/dist/performance.mjs.map +1 -0
- package/dist/pipeline.d.mts +431 -0
- package/dist/pipeline.d.mts.map +1 -0
- package/dist/pipeline.mjs +460 -0
- package/dist/pipeline.mjs.map +1 -0
- package/dist/predicates.d.mts +722 -0
- package/dist/predicates.d.mts.map +1 -0
- package/dist/predicates.mjs +802 -0
- package/dist/predicates.mjs.map +1 -0
- package/dist/reader-result.d.mts +422 -0
- package/dist/reader-result.d.mts.map +1 -0
- package/dist/reader-result.mjs +758 -0
- package/dist/reader-result.mjs.map +1 -0
- package/dist/result.d.mts +684 -0
- package/dist/result.d.mts.map +1 -0
- package/dist/result.mjs +814 -0
- package/dist/result.mjs.map +1 -0
- package/dist/types.d.mts +439 -0
- package/dist/types.d.mts.map +1 -0
- package/dist/types.mjs +191 -0
- package/dist/types.mjs.map +1 -0
- package/dist/validation.d.mts +622 -0
- package/dist/validation.d.mts.map +1 -0
- package/dist/validation.mjs +852 -0
- package/dist/validation.mjs.map +1 -0
- package/package.json +46 -0
|
@@ -0,0 +1,267 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module object-utils
|
|
3
|
+
* @description Functional utilities for working with objects in a type-safe, immutable manner.
|
|
4
|
+
* These functions are designed to be composed and follow functional programming principles.
|
|
5
|
+
* All operations return new objects, preserving immutability of the original data.
|
|
6
|
+
*
|
|
7
|
+
* @example
|
|
8
|
+
* ```typescript
|
|
9
|
+
* import { pick, omit, mapValues, merge } from './object-utils.mts';
|
|
10
|
+
*
|
|
11
|
+
* // pick specific properties
|
|
12
|
+
* const user = { id: 1, name: 'Alice', email: 'alice@example.com', password: 'secret' };
|
|
13
|
+
* const publicData = pick(['id', 'name', 'email'])(user);
|
|
14
|
+
* // => { id: 1, name: 'Alice', email: 'alice@example.com' }
|
|
15
|
+
*
|
|
16
|
+
* // omit sensitive fields
|
|
17
|
+
* const safeUser = omit(['password'])(user);
|
|
18
|
+
* // => { id: 1, name: 'Alice', email: 'alice@example.com' }
|
|
19
|
+
*
|
|
20
|
+
* // transform values
|
|
21
|
+
* const scores = { math: 85, science: 92, history: 78 };
|
|
22
|
+
* const percentages = mapValues((score: number) => `${score}%`)(scores);
|
|
23
|
+
* // => { math: '85%', science: '92%', history: '78%' }
|
|
24
|
+
*
|
|
25
|
+
* // deep merge objects
|
|
26
|
+
* const defaults = { server: { port: 3000, host: 'localhost' } };
|
|
27
|
+
* const userConfig = { server: { port: 8080 } };
|
|
28
|
+
* const config = merge(defaults, userConfig);
|
|
29
|
+
* // => { server: { port: 8080, host: 'localhost' } }
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @category Utilities
|
|
33
|
+
* @since 2025-07-03
|
|
34
|
+
*/
|
|
35
|
+
/**
|
|
36
|
+
* Map over object values while preserving keys.
|
|
37
|
+
* @description Transforms each value in an object while maintaining the same key structure.
|
|
38
|
+
* Creates a new object with the same keys but transformed values.
|
|
39
|
+
* The transformation function receives only the value, not the key.
|
|
40
|
+
*
|
|
41
|
+
* @template T - The type of values in the input object
|
|
42
|
+
* @template U - The type of values in the output object
|
|
43
|
+
* @param {function(T): U} fn - Transformation function to apply to each value
|
|
44
|
+
* @returns {function(Record<string, T>): Record<string, U>} A function that transforms object values
|
|
45
|
+
*
|
|
46
|
+
* @category Transformation
|
|
47
|
+
* @example
|
|
48
|
+
* const doubled = mapValues((n: number) => n * 2)({ a: 1, b: 2, c: 3 });
|
|
49
|
+
* // => { a: 2, b: 4, c: 6 }
|
|
50
|
+
*
|
|
51
|
+
* @example
|
|
52
|
+
* // Convert all strings to uppercase
|
|
53
|
+
* const config = { host: 'localhost', env: 'dev', mode: 'debug' };
|
|
54
|
+
* const upperConfig = mapValues((s: string) => s.toUpperCase())(config);
|
|
55
|
+
* // => { host: 'LOCALHOST', env: 'DEV', mode: 'DEBUG' }
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* // Calculate percentages
|
|
59
|
+
* const scores = { math: 85, science: 92, history: 78 };
|
|
60
|
+
* const percentages = mapValues((score: number) => `${score}%`)(scores);
|
|
61
|
+
* // => { math: '85%', science: '92%', history: '78%' }
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* // Process nested data
|
|
65
|
+
* const users = {
|
|
66
|
+
* user1: { name: 'Alice', age: 30 },
|
|
67
|
+
* user2: { name: 'Bob', age: 25 }
|
|
68
|
+
* };
|
|
69
|
+
* const userSummaries = mapValues((user: { name: string; age: number }) =>
|
|
70
|
+
* `${user.name} (${user.age} years old)`
|
|
71
|
+
* )(users);
|
|
72
|
+
* // => { user1: 'Alice (30 years old)', user2: 'Bob (25 years old)' }
|
|
73
|
+
*
|
|
74
|
+
* @see pick - Select specific keys
|
|
75
|
+
* @see omit - Remove specific keys
|
|
76
|
+
* @since 2025-07-03
|
|
77
|
+
*/
|
|
78
|
+
export declare const mapValues: <T, U>(fn: (value: T) => U) => (obj: Record<string, T>) => Record<string, U>;
|
|
79
|
+
/**
|
|
80
|
+
* Pick specific keys from an object.
|
|
81
|
+
* @description Creates a new object containing only the specified properties from the source object.
|
|
82
|
+
* Type-safe selection that ensures only existing keys can be picked.
|
|
83
|
+
* Missing keys are silently ignored.
|
|
84
|
+
*
|
|
85
|
+
* @template T - The type of the source object
|
|
86
|
+
* @template K - The keys to pick from the object
|
|
87
|
+
* @param {K[]} keys - Array of keys to select from the object
|
|
88
|
+
* @returns {function(T): Pick<T, K>} A function that picks the specified keys from an object
|
|
89
|
+
*
|
|
90
|
+
* @category Selection
|
|
91
|
+
* @example
|
|
92
|
+
* const user = { id: 1, name: 'Alice', email: 'alice@example.com', password: 'secret' };
|
|
93
|
+
* const publicData = pick(['id', 'name', 'email'])(user);
|
|
94
|
+
* // => { id: 1, name: 'Alice', email: 'alice@example.com' }
|
|
95
|
+
*
|
|
96
|
+
* @example
|
|
97
|
+
* // Extract configuration subset
|
|
98
|
+
* const config = {
|
|
99
|
+
* apiUrl: 'https://api.example.com',
|
|
100
|
+
* apiKey: 'secret-key',
|
|
101
|
+
* timeout: 5000,
|
|
102
|
+
* debug: true,
|
|
103
|
+
* version: '1.0.0'
|
|
104
|
+
* };
|
|
105
|
+
* const clientConfig = pick(['apiUrl', 'timeout'])(config);
|
|
106
|
+
* // => { apiUrl: 'https://api.example.com', timeout: 5000 }
|
|
107
|
+
*
|
|
108
|
+
* @example
|
|
109
|
+
* // Create DTO from entity
|
|
110
|
+
* const entity = {
|
|
111
|
+
* id: '123',
|
|
112
|
+
* name: 'Product',
|
|
113
|
+
* internalCode: 'PRD-123',
|
|
114
|
+
* _createdAt: new Date(),
|
|
115
|
+
* _updatedAt: new Date()
|
|
116
|
+
* };
|
|
117
|
+
* const dto = pick(['id', 'name'])(entity);
|
|
118
|
+
* // => { id: '123', name: 'Product' }
|
|
119
|
+
*
|
|
120
|
+
* @example
|
|
121
|
+
* // Type-safe key selection
|
|
122
|
+
* interface User {
|
|
123
|
+
* id: number;
|
|
124
|
+
* name: string;
|
|
125
|
+
* email: string;
|
|
126
|
+
* privateData: unknown;
|
|
127
|
+
* }
|
|
128
|
+
* const pickPublicFields = pick<User, 'id' | 'name' | 'email'>(['id', 'name', 'email']);
|
|
129
|
+
* // Result type is Pick<User, 'id' | 'name' | 'email'>
|
|
130
|
+
*
|
|
131
|
+
* @see omit - Remove specific keys instead
|
|
132
|
+
* @see mapValues - Transform values
|
|
133
|
+
* @since 2025-07-03
|
|
134
|
+
*/
|
|
135
|
+
export declare const pick: <T extends object, K extends keyof T>(keys: K[]) => (obj: T) => Pick<T, K>;
|
|
136
|
+
/**
|
|
137
|
+
* Omit specific keys from an object.
|
|
138
|
+
* @description Creates a new object containing all properties except the specified ones.
|
|
139
|
+
* Type-safe exclusion that ensures only existing keys can be omitted.
|
|
140
|
+
* Creates a shallow copy of the object without the excluded properties.
|
|
141
|
+
*
|
|
142
|
+
* @template T - The type of the source object
|
|
143
|
+
* @template K - The keys to omit from the object
|
|
144
|
+
* @param {K[]} keys - Array of keys to exclude from the object
|
|
145
|
+
* @returns {function(T): Omit<T, K>} A function that omits the specified keys from an object
|
|
146
|
+
*
|
|
147
|
+
* @category Selection
|
|
148
|
+
* @example
|
|
149
|
+
* const user = { id: 1, name: 'Alice', email: 'alice@example.com', password: 'secret' };
|
|
150
|
+
* const safeUser = omit(['password'])(user);
|
|
151
|
+
* // => { id: 1, name: 'Alice', email: 'alice@example.com' }
|
|
152
|
+
*
|
|
153
|
+
* @example
|
|
154
|
+
* // Remove internal fields
|
|
155
|
+
* const data = {
|
|
156
|
+
* value: 42,
|
|
157
|
+
* label: 'Answer',
|
|
158
|
+
* _internal: true,
|
|
159
|
+
* _timestamp: Date.now()
|
|
160
|
+
* };
|
|
161
|
+
* const publicData = omit(['_internal', '_timestamp'])(data);
|
|
162
|
+
* // => { value: 42, label: 'Answer' }
|
|
163
|
+
*
|
|
164
|
+
* @example
|
|
165
|
+
* // Create update payload without readonly fields
|
|
166
|
+
* const entity = {
|
|
167
|
+
* id: '123',
|
|
168
|
+
* name: 'Updated Name',
|
|
169
|
+
* createdAt: '2024-01-01',
|
|
170
|
+
* updatedAt: '2024-01-15'
|
|
171
|
+
* };
|
|
172
|
+
* const updatePayload = omit(['id', 'createdAt'])(entity);
|
|
173
|
+
* // => { name: 'Updated Name', updatedAt: '2024-01-15' }
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* // Type-safe key exclusion
|
|
177
|
+
* interface Config {
|
|
178
|
+
* apiUrl: string;
|
|
179
|
+
* apiKey: string;
|
|
180
|
+
* timeout: number;
|
|
181
|
+
* debug: boolean;
|
|
182
|
+
* }
|
|
183
|
+
* const omitSecrets = omit<Config, 'apiKey'>(['apiKey']);
|
|
184
|
+
* // Result type is Omit<Config, 'apiKey'>
|
|
185
|
+
*
|
|
186
|
+
* @see pick - Select specific keys instead
|
|
187
|
+
* @see mapValues - Transform values
|
|
188
|
+
* @since 2025-07-03
|
|
189
|
+
*/
|
|
190
|
+
export declare const omit: <T, K extends keyof T>(keys: K[]) => (obj: T) => Omit<T, K>;
|
|
191
|
+
/**
|
|
192
|
+
* Deep merge two objects.
|
|
193
|
+
* @description Recursively merges source into target, with source values taking precedence.
|
|
194
|
+
* Objects are merged deeply, but arrays, dates, and other non-plain objects are replaced entirely.
|
|
195
|
+
* Null values in source will overwrite target values. Undefined values are ignored.
|
|
196
|
+
* Creates new objects during merge to ensure immutability.
|
|
197
|
+
*
|
|
198
|
+
* @template T - The type of the target object (must extend Record<string, unknown>)
|
|
199
|
+
* @param {T} target - The base object to merge into
|
|
200
|
+
* @param {DeepPartial<T>} source - The object with updates to apply
|
|
201
|
+
* @returns {T} A new object with merged properties
|
|
202
|
+
*
|
|
203
|
+
* @category Transformation
|
|
204
|
+
* @example
|
|
205
|
+
* const base = { a: 1, b: { x: 2, y: 3 }, c: 4 };
|
|
206
|
+
* const updates = { b: { y: 5, z: 6 }, d: 7 };
|
|
207
|
+
* const merged = merge(base, updates);
|
|
208
|
+
* // => { a: 1, b: { x: 2, y: 5, z: 6 }, c: 4, d: 7 }
|
|
209
|
+
*
|
|
210
|
+
* @example
|
|
211
|
+
* // Merge configuration with defaults
|
|
212
|
+
* const defaults = {
|
|
213
|
+
* server: { port: 3000, host: 'localhost' },
|
|
214
|
+
* database: { pool: { min: 2, max: 10 } },
|
|
215
|
+
* logging: { level: 'info' }
|
|
216
|
+
* };
|
|
217
|
+
* const userConfig = {
|
|
218
|
+
* server: { port: 8080 },
|
|
219
|
+
* database: { pool: { max: 20 } }
|
|
220
|
+
* };
|
|
221
|
+
* const finalConfig = merge(defaults, userConfig);
|
|
222
|
+
* // => {
|
|
223
|
+
* // server: { port: 8080, host: 'localhost' },
|
|
224
|
+
* // database: { pool: { min: 2, max: 20 } },
|
|
225
|
+
* // logging: { level: 'info' }
|
|
226
|
+
* // }
|
|
227
|
+
*
|
|
228
|
+
* @example
|
|
229
|
+
* // Update nested state
|
|
230
|
+
* const state = {
|
|
231
|
+
* user: { id: 1, preferences: { theme: 'light', lang: 'en' } },
|
|
232
|
+
* app: { version: '1.0.0' }
|
|
233
|
+
* };
|
|
234
|
+
* const updates = {
|
|
235
|
+
* user: { preferences: { theme: 'dark' } }
|
|
236
|
+
* };
|
|
237
|
+
* const newState = merge(state, updates);
|
|
238
|
+
* // => {
|
|
239
|
+
* // user: { id: 1, preferences: { theme: 'dark', lang: 'en' } },
|
|
240
|
+
* // app: { version: '1.0.0' }
|
|
241
|
+
* // }
|
|
242
|
+
*
|
|
243
|
+
* @example
|
|
244
|
+
* // Handling null and undefined
|
|
245
|
+
* const data = { a: { b: 2 }, c: 3 };
|
|
246
|
+
* const updates = { a: null, c: undefined, d: 4 };
|
|
247
|
+
* const result = merge(data, updates);
|
|
248
|
+
* // => { a: null, c: 3, d: 4 }
|
|
249
|
+
* // null overwrites, undefined is ignored
|
|
250
|
+
*
|
|
251
|
+
* @see pick - Select specific keys
|
|
252
|
+
* @see omit - Remove specific keys
|
|
253
|
+
* @since 2025-07-03
|
|
254
|
+
*/
|
|
255
|
+
/**
|
|
256
|
+
* DeepPartial type - makes all properties and nested properties optional.
|
|
257
|
+
* @description Utility type that recursively makes all properties of an object optional.
|
|
258
|
+
* Properly handles null values as valid partial values.
|
|
259
|
+
*
|
|
260
|
+
* @template T - The type to make deeply partial
|
|
261
|
+
*/
|
|
262
|
+
type DeepPartial<T> = {
|
|
263
|
+
[P in keyof T]?: T[P] extends object ? DeepPartial<T[P]> | null : T[P] | null;
|
|
264
|
+
};
|
|
265
|
+
export declare const merge: <T extends Record<string, unknown>>(target: T, source: DeepPartial<T>) => T;
|
|
266
|
+
export {};
|
|
267
|
+
//# sourceMappingURL=object-utils.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object-utils.d.mts","sourceRoot":"","sources":["../src/object-utils.mts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiCG;AAEH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,eAAO,MAAM,SAAS,GACnB,CAAC,EAAE,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,WACpB,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,KAAG,MAAM,CAAC,MAAM,EAAE,CAAC,CAMzC,CAAC;AAEJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,eAAO,MAAM,IAAI,GACd,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,CAAC,QAAQ,CAAC,EAAE,WACzC,CAAC,KAAG,IAAI,CAAC,CAAC,EAAE,CAAC,CAQlB,CAAC;AAEJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqDG;AACH,eAAO,MAAM,IAAI,GACd,CAAC,EAAE,CAAC,SAAS,MAAM,CAAC,QAAQ,CAAC,EAAE,WAC1B,CAAC,KAAG,IAAI,CAAC,CAAC,EAAE,CAAC,CAMlB,CAAC;AAEJ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA+DG;AACH;;;;;;GAMG;AACH,KAAK,WAAW,CAAC,CAAC,IAAI;KACnB,CAAC,IAAI,MAAM,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,SAAS,MAAM,GAAG,WAAW,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,IAAI;CAC9E,CAAC;AAEF,eAAO,MAAM,KAAK,GAAI,CAAC,SAAS,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,UAAU,CAAC,UAAU,WAAW,CAAC,CAAC,CAAC,KAAG,CAwB5F,CAAC"}
|
|
@@ -0,0 +1,258 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @module object-utils
|
|
4
|
+
* @description Functional utilities for working with objects in a type-safe, immutable manner.
|
|
5
|
+
* These functions are designed to be composed and follow functional programming principles.
|
|
6
|
+
* All operations return new objects, preserving immutability of the original data.
|
|
7
|
+
*
|
|
8
|
+
* @example
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { pick, omit, mapValues, merge } from './object-utils.mts';
|
|
11
|
+
*
|
|
12
|
+
* // pick specific properties
|
|
13
|
+
* const user = { id: 1, name: 'Alice', email: 'alice@example.com', password: 'secret' };
|
|
14
|
+
* const publicData = pick(['id', 'name', 'email'])(user);
|
|
15
|
+
* // => { id: 1, name: 'Alice', email: 'alice@example.com' }
|
|
16
|
+
*
|
|
17
|
+
* // omit sensitive fields
|
|
18
|
+
* const safeUser = omit(['password'])(user);
|
|
19
|
+
* // => { id: 1, name: 'Alice', email: 'alice@example.com' }
|
|
20
|
+
*
|
|
21
|
+
* // transform values
|
|
22
|
+
* const scores = { math: 85, science: 92, history: 78 };
|
|
23
|
+
* const percentages = mapValues((score: number) => `${score}%`)(scores);
|
|
24
|
+
* // => { math: '85%', science: '92%', history: '78%' }
|
|
25
|
+
*
|
|
26
|
+
* // deep merge objects
|
|
27
|
+
* const defaults = { server: { port: 3000, host: 'localhost' } };
|
|
28
|
+
* const userConfig = { server: { port: 8080 } };
|
|
29
|
+
* const config = merge(defaults, userConfig);
|
|
30
|
+
* // => { server: { port: 8080, host: 'localhost' } }
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* @category Utilities
|
|
34
|
+
* @since 2025-07-03
|
|
35
|
+
*/
|
|
36
|
+
var __assign = (this && this.__assign) || function () {
|
|
37
|
+
__assign = Object.assign || function(t) {
|
|
38
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
39
|
+
s = arguments[i];
|
|
40
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
41
|
+
t[p] = s[p];
|
|
42
|
+
}
|
|
43
|
+
return t;
|
|
44
|
+
};
|
|
45
|
+
return __assign.apply(this, arguments);
|
|
46
|
+
};
|
|
47
|
+
/**
|
|
48
|
+
* Map over object values while preserving keys.
|
|
49
|
+
* @description Transforms each value in an object while maintaining the same key structure.
|
|
50
|
+
* Creates a new object with the same keys but transformed values.
|
|
51
|
+
* The transformation function receives only the value, not the key.
|
|
52
|
+
*
|
|
53
|
+
* @template T - The type of values in the input object
|
|
54
|
+
* @template U - The type of values in the output object
|
|
55
|
+
* @param {function(T): U} fn - Transformation function to apply to each value
|
|
56
|
+
* @returns {function(Record<string, T>): Record<string, U>} A function that transforms object values
|
|
57
|
+
*
|
|
58
|
+
* @category Transformation
|
|
59
|
+
* @example
|
|
60
|
+
* const doubled = mapValues((n: number) => n * 2)({ a: 1, b: 2, c: 3 });
|
|
61
|
+
* // => { a: 2, b: 4, c: 6 }
|
|
62
|
+
*
|
|
63
|
+
* @example
|
|
64
|
+
* // Convert all strings to uppercase
|
|
65
|
+
* const config = { host: 'localhost', env: 'dev', mode: 'debug' };
|
|
66
|
+
* const upperConfig = mapValues((s: string) => s.toUpperCase())(config);
|
|
67
|
+
* // => { host: 'LOCALHOST', env: 'DEV', mode: 'DEBUG' }
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* // Calculate percentages
|
|
71
|
+
* const scores = { math: 85, science: 92, history: 78 };
|
|
72
|
+
* const percentages = mapValues((score: number) => `${score}%`)(scores);
|
|
73
|
+
* // => { math: '85%', science: '92%', history: '78%' }
|
|
74
|
+
*
|
|
75
|
+
* @example
|
|
76
|
+
* // Process nested data
|
|
77
|
+
* const users = {
|
|
78
|
+
* user1: { name: 'Alice', age: 30 },
|
|
79
|
+
* user2: { name: 'Bob', age: 25 }
|
|
80
|
+
* };
|
|
81
|
+
* const userSummaries = mapValues((user: { name: string; age: number }) =>
|
|
82
|
+
* `${user.name} (${user.age} years old)`
|
|
83
|
+
* )(users);
|
|
84
|
+
* // => { user1: 'Alice (30 years old)', user2: 'Bob (25 years old)' }
|
|
85
|
+
*
|
|
86
|
+
* @see pick - Select specific keys
|
|
87
|
+
* @see omit - Remove specific keys
|
|
88
|
+
* @since 2025-07-03
|
|
89
|
+
*/
|
|
90
|
+
export var mapValues = function (fn) {
|
|
91
|
+
return function (obj) {
|
|
92
|
+
var result = {};
|
|
93
|
+
for (var _i = 0, _a = Object.entries(obj); _i < _a.length; _i++) {
|
|
94
|
+
var _b = _a[_i], key = _b[0], value = _b[1];
|
|
95
|
+
result[key] = fn(value);
|
|
96
|
+
}
|
|
97
|
+
return result;
|
|
98
|
+
};
|
|
99
|
+
};
|
|
100
|
+
/**
|
|
101
|
+
* Pick specific keys from an object.
|
|
102
|
+
* @description Creates a new object containing only the specified properties from the source object.
|
|
103
|
+
* Type-safe selection that ensures only existing keys can be picked.
|
|
104
|
+
* Missing keys are silently ignored.
|
|
105
|
+
*
|
|
106
|
+
* @template T - The type of the source object
|
|
107
|
+
* @template K - The keys to pick from the object
|
|
108
|
+
* @param {K[]} keys - Array of keys to select from the object
|
|
109
|
+
* @returns {function(T): Pick<T, K>} A function that picks the specified keys from an object
|
|
110
|
+
*
|
|
111
|
+
* @category Selection
|
|
112
|
+
* @example
|
|
113
|
+
* const user = { id: 1, name: 'Alice', email: 'alice@example.com', password: 'secret' };
|
|
114
|
+
* const publicData = pick(['id', 'name', 'email'])(user);
|
|
115
|
+
* // => { id: 1, name: 'Alice', email: 'alice@example.com' }
|
|
116
|
+
*
|
|
117
|
+
* @example
|
|
118
|
+
* // Extract configuration subset
|
|
119
|
+
* const config = {
|
|
120
|
+
* apiUrl: 'https://api.example.com',
|
|
121
|
+
* apiKey: 'secret-key',
|
|
122
|
+
* timeout: 5000,
|
|
123
|
+
* debug: true,
|
|
124
|
+
* version: '1.0.0'
|
|
125
|
+
* };
|
|
126
|
+
* const clientConfig = pick(['apiUrl', 'timeout'])(config);
|
|
127
|
+
* // => { apiUrl: 'https://api.example.com', timeout: 5000 }
|
|
128
|
+
*
|
|
129
|
+
* @example
|
|
130
|
+
* // Create DTO from entity
|
|
131
|
+
* const entity = {
|
|
132
|
+
* id: '123',
|
|
133
|
+
* name: 'Product',
|
|
134
|
+
* internalCode: 'PRD-123',
|
|
135
|
+
* _createdAt: new Date(),
|
|
136
|
+
* _updatedAt: new Date()
|
|
137
|
+
* };
|
|
138
|
+
* const dto = pick(['id', 'name'])(entity);
|
|
139
|
+
* // => { id: '123', name: 'Product' }
|
|
140
|
+
*
|
|
141
|
+
* @example
|
|
142
|
+
* // Type-safe key selection
|
|
143
|
+
* interface User {
|
|
144
|
+
* id: number;
|
|
145
|
+
* name: string;
|
|
146
|
+
* email: string;
|
|
147
|
+
* privateData: unknown;
|
|
148
|
+
* }
|
|
149
|
+
* const pickPublicFields = pick<User, 'id' | 'name' | 'email'>(['id', 'name', 'email']);
|
|
150
|
+
* // Result type is Pick<User, 'id' | 'name' | 'email'>
|
|
151
|
+
*
|
|
152
|
+
* @see omit - Remove specific keys instead
|
|
153
|
+
* @see mapValues - Transform values
|
|
154
|
+
* @since 2025-07-03
|
|
155
|
+
*/
|
|
156
|
+
export var pick = function (keys) {
|
|
157
|
+
return function (obj) {
|
|
158
|
+
var result = {};
|
|
159
|
+
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
|
|
160
|
+
var key = keys_1[_i];
|
|
161
|
+
if (key in obj) {
|
|
162
|
+
result[key] = obj[key];
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return result;
|
|
166
|
+
};
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* Omit specific keys from an object.
|
|
170
|
+
* @description Creates a new object containing all properties except the specified ones.
|
|
171
|
+
* Type-safe exclusion that ensures only existing keys can be omitted.
|
|
172
|
+
* Creates a shallow copy of the object without the excluded properties.
|
|
173
|
+
*
|
|
174
|
+
* @template T - The type of the source object
|
|
175
|
+
* @template K - The keys to omit from the object
|
|
176
|
+
* @param {K[]} keys - Array of keys to exclude from the object
|
|
177
|
+
* @returns {function(T): Omit<T, K>} A function that omits the specified keys from an object
|
|
178
|
+
*
|
|
179
|
+
* @category Selection
|
|
180
|
+
* @example
|
|
181
|
+
* const user = { id: 1, name: 'Alice', email: 'alice@example.com', password: 'secret' };
|
|
182
|
+
* const safeUser = omit(['password'])(user);
|
|
183
|
+
* // => { id: 1, name: 'Alice', email: 'alice@example.com' }
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* // Remove internal fields
|
|
187
|
+
* const data = {
|
|
188
|
+
* value: 42,
|
|
189
|
+
* label: 'Answer',
|
|
190
|
+
* _internal: true,
|
|
191
|
+
* _timestamp: Date.now()
|
|
192
|
+
* };
|
|
193
|
+
* const publicData = omit(['_internal', '_timestamp'])(data);
|
|
194
|
+
* // => { value: 42, label: 'Answer' }
|
|
195
|
+
*
|
|
196
|
+
* @example
|
|
197
|
+
* // Create update payload without readonly fields
|
|
198
|
+
* const entity = {
|
|
199
|
+
* id: '123',
|
|
200
|
+
* name: 'Updated Name',
|
|
201
|
+
* createdAt: '2024-01-01',
|
|
202
|
+
* updatedAt: '2024-01-15'
|
|
203
|
+
* };
|
|
204
|
+
* const updatePayload = omit(['id', 'createdAt'])(entity);
|
|
205
|
+
* // => { name: 'Updated Name', updatedAt: '2024-01-15' }
|
|
206
|
+
*
|
|
207
|
+
* @example
|
|
208
|
+
* // Type-safe key exclusion
|
|
209
|
+
* interface Config {
|
|
210
|
+
* apiUrl: string;
|
|
211
|
+
* apiKey: string;
|
|
212
|
+
* timeout: number;
|
|
213
|
+
* debug: boolean;
|
|
214
|
+
* }
|
|
215
|
+
* const omitSecrets = omit<Config, 'apiKey'>(['apiKey']);
|
|
216
|
+
* // Result type is Omit<Config, 'apiKey'>
|
|
217
|
+
*
|
|
218
|
+
* @see pick - Select specific keys instead
|
|
219
|
+
* @see mapValues - Transform values
|
|
220
|
+
* @since 2025-07-03
|
|
221
|
+
*/
|
|
222
|
+
export var omit = function (keys) {
|
|
223
|
+
return function (obj) {
|
|
224
|
+
var result = __assign({}, obj);
|
|
225
|
+
for (var _i = 0, keys_2 = keys; _i < keys_2.length; _i++) {
|
|
226
|
+
var key = keys_2[_i];
|
|
227
|
+
delete result[key];
|
|
228
|
+
}
|
|
229
|
+
return result;
|
|
230
|
+
};
|
|
231
|
+
};
|
|
232
|
+
export var merge = function (target, source) {
|
|
233
|
+
var result = __assign({}, target);
|
|
234
|
+
for (var _i = 0, _a = Object.entries(source); _i < _a.length; _i++) {
|
|
235
|
+
var _b = _a[_i], key = _b[0], value = _b[1];
|
|
236
|
+
if (value !== undefined) {
|
|
237
|
+
if (typeof value === "object" &&
|
|
238
|
+
!Array.isArray(value) &&
|
|
239
|
+
value !== null &&
|
|
240
|
+
!(value instanceof Date) &&
|
|
241
|
+
!(value instanceof RegExp) &&
|
|
242
|
+
Object.prototype.toString.call(value) === '[object Object]') {
|
|
243
|
+
var targetValue = result[key];
|
|
244
|
+
if (targetValue && typeof targetValue === "object" && !Array.isArray(targetValue) && !(targetValue instanceof Date) && !(targetValue instanceof RegExp) && Object.prototype.toString.call(targetValue) === '[object Object]') {
|
|
245
|
+
result[key] = merge(targetValue, value);
|
|
246
|
+
}
|
|
247
|
+
else {
|
|
248
|
+
result[key] = value;
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
else {
|
|
252
|
+
result[key] = value;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return result;
|
|
257
|
+
};
|
|
258
|
+
//# sourceMappingURL=object-utils.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"object-utils.mjs","sourceRoot":"","sources":["../src/object-utils.mts"],"names":[],"mappings":"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"}
|