@oxog/utils 1.0.0
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/CHANGELOG.md +44 -0
- package/LICENSE +21 -0
- package/README.md +297 -0
- package/dist/index.cjs +645 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +780 -0
- package/dist/index.d.ts +780 -0
- package/dist/index.js +611 -0
- package/dist/index.js.map +1 -0
- package/dist/plugins/async.cjs +188 -0
- package/dist/plugins/async.cjs.map +1 -0
- package/dist/plugins/async.d.cts +169 -0
- package/dist/plugins/async.d.ts +169 -0
- package/dist/plugins/async.js +182 -0
- package/dist/plugins/async.js.map +1 -0
- package/dist/plugins/deep.cjs +157 -0
- package/dist/plugins/deep.cjs.map +1 -0
- package/dist/plugins/deep.d.cts +157 -0
- package/dist/plugins/deep.d.ts +157 -0
- package/dist/plugins/deep.js +152 -0
- package/dist/plugins/deep.js.map +1 -0
- package/dist/plugins/guard.cjs +47 -0
- package/dist/plugins/guard.cjs.map +1 -0
- package/dist/plugins/guard.d.cts +213 -0
- package/dist/plugins/guard.d.ts +213 -0
- package/dist/plugins/guard.js +38 -0
- package/dist/plugins/guard.js.map +1 -0
- package/dist/plugins/transform.cjs +49 -0
- package/dist/plugins/transform.cjs.map +1 -0
- package/dist/plugins/transform.d.cts +231 -0
- package/dist/plugins/transform.d.ts +231 -0
- package/dist/plugins/transform.js +42 -0
- package/dist/plugins/transform.js.map +1 -0
- package/dist/types-XmRC5wYO.d.cts +97 -0
- package/dist/types-XmRC5wYO.d.ts +97 -0
- package/package.json +115 -0
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transform object keys with a function.
|
|
3
|
+
*
|
|
4
|
+
* Creates a new object with keys transformed by the provided function.
|
|
5
|
+
*
|
|
6
|
+
* @param obj - Source object
|
|
7
|
+
* @param fn - Function to transform keys
|
|
8
|
+
* @returns New object with transformed keys
|
|
9
|
+
*
|
|
10
|
+
* @example Basic usage
|
|
11
|
+
* ```typescript
|
|
12
|
+
* mapKeys({ a: 1, b: 2 }, (key) => key.toUpperCase());
|
|
13
|
+
* // { A: 1, B: 2 }
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @example Adding prefix
|
|
17
|
+
* ```typescript
|
|
18
|
+
* mapKeys({ name: 'Alice', age: 30 }, (key) => `user_${key}`);
|
|
19
|
+
* // { user_name: 'Alice', user_age: 30 }
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @example Snake case conversion
|
|
23
|
+
* ```typescript
|
|
24
|
+
* mapKeys({ firstName: 'Alice', lastName: 'Smith' }, (key) => snakeCase(key));
|
|
25
|
+
* // { first_name: 'Alice', last_name: 'Smith' }
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @see {@link mapValues} for transforming values
|
|
29
|
+
*/
|
|
30
|
+
declare function mapKeys<T extends object, K extends PropertyKey>(obj: T, fn: (key: keyof T, value: T[keyof T]) => K): Record<K, T[keyof T]>;
|
|
31
|
+
/**
|
|
32
|
+
* Transform object values with a function.
|
|
33
|
+
*
|
|
34
|
+
* Creates a new object with values transformed by the provided function.
|
|
35
|
+
*
|
|
36
|
+
* @param obj - Source object
|
|
37
|
+
* @param fn - Function to transform values
|
|
38
|
+
* @returns New object with transformed values
|
|
39
|
+
*
|
|
40
|
+
* @example Basic usage
|
|
41
|
+
* ```typescript
|
|
42
|
+
* mapValues({ a: 1, b: 2 }, (val) => val * 2);
|
|
43
|
+
* // { a: 2, b: 4 }
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @example String transformation
|
|
47
|
+
* ```typescript
|
|
48
|
+
* mapValues({ name: 'alice', city: 'ny' }, (val) => val.toUpperCase());
|
|
49
|
+
* // { name: 'ALICE', city: 'NY' }
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @example Complex transformation
|
|
53
|
+
* ```typescript
|
|
54
|
+
* mapValues({ a: 1, b: 2 }, (val, key) => `${key}:${val}`);
|
|
55
|
+
* // { a: 'a:1', b: 'b:2' }
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* @see {@link mapKeys} for transforming keys
|
|
59
|
+
*/
|
|
60
|
+
declare function mapValues<T extends object, R>(obj: T, fn: (value: T[keyof T], key: keyof T) => R): Record<keyof T, R>;
|
|
61
|
+
/**
|
|
62
|
+
* Swap keys and values of an object.
|
|
63
|
+
*
|
|
64
|
+
* Creates a new object where keys become values and values become keys.
|
|
65
|
+
* If values are not unique, later values will overwrite earlier ones.
|
|
66
|
+
*
|
|
67
|
+
* @param obj - Source object
|
|
68
|
+
* @returns Inverted object
|
|
69
|
+
*
|
|
70
|
+
* @example Basic usage
|
|
71
|
+
* ```typescript
|
|
72
|
+
* invert({ a: '1', b: '2', c: '3' });
|
|
73
|
+
* // { '1': 'a', '2': 'b', '3': 'c' }
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* @example Number values
|
|
77
|
+
* ```typescript
|
|
78
|
+
* invert({ a: 1, b: 2, c: 3 });
|
|
79
|
+
* // { 1: 'a', 2: 'b', 3: 'c' }
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* @example Duplicate values
|
|
83
|
+
* ```typescript
|
|
84
|
+
* invert({ a: 'x', b: 'x', c: 'y' });
|
|
85
|
+
* // { 'x': 'b', 'y': 'c' } (last 'x' wins)
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* @example Non-string keys become strings
|
|
89
|
+
* ```typescript
|
|
90
|
+
* invert({ 1: 'a', 2: 'b' });
|
|
91
|
+
* // { 'a': '1', 'b': '2' }
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
declare function invert<T extends object>(obj: T): Record<string, keyof T>;
|
|
95
|
+
/**
|
|
96
|
+
* Flip function argument order.
|
|
97
|
+
*
|
|
98
|
+
* Creates a new function with reversed argument order.
|
|
99
|
+
*
|
|
100
|
+
* @param fn - Function to flip
|
|
101
|
+
* @returns New function with flipped arguments
|
|
102
|
+
*
|
|
103
|
+
* @example Basic usage
|
|
104
|
+
* ```typescript
|
|
105
|
+
* const divide = (a: number, b: number) => a / b;
|
|
106
|
+
* const flippedDivide = flip(divide);
|
|
107
|
+
* divide(10, 2); // 5
|
|
108
|
+
* flippedDivide(10, 2); // 0.2 (same as divide(2, 10))
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @example With map
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const subtract = (a: number, b: number) => a - b;
|
|
114
|
+
* const subtractFrom10 = flip(subtract)(10);
|
|
115
|
+
* [1, 2, 3, 4, 5].map(subtractFrom10);
|
|
116
|
+
* // [9, 8, 7, 6, 5]
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* @example With currying
|
|
120
|
+
* ```typescript
|
|
121
|
+
* const append = (prefix: string, suffix: string) => prefix + suffix;
|
|
122
|
+
* const appendToHello = flip(append)('hello');
|
|
123
|
+
* appendToHello(' world'); // ' worldhello'
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
declare function flip<T extends (...args: any[]) => any>(fn: T): T;
|
|
127
|
+
/**
|
|
128
|
+
* Compose functions right-to-left.
|
|
129
|
+
*
|
|
130
|
+
* Creates a function that applies functions from right to left.
|
|
131
|
+
* Result of each function is passed as argument to the next.
|
|
132
|
+
*
|
|
133
|
+
* @param fns - Functions to compose
|
|
134
|
+
* @returns Composed function
|
|
135
|
+
*
|
|
136
|
+
* @example Basic usage
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const add = (x: number) => x + 1;
|
|
139
|
+
* const multiply = (x: number) => x * 2;
|
|
140
|
+
* const toString = (x: number) => x.toString();
|
|
141
|
+
*
|
|
142
|
+
* const process = compose(toString, multiply, add);
|
|
143
|
+
* process(5);
|
|
144
|
+
* // '12' (5 + 1 = 6, 6 * 2 = 12, '12')
|
|
145
|
+
* ```
|
|
146
|
+
*
|
|
147
|
+
* @example Complex transformation
|
|
148
|
+
* ```typescript
|
|
149
|
+
* const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
150
|
+
* const exclaim = (s: string) => s + '!';
|
|
151
|
+
* const greet = compose(exclaim, capitalize);
|
|
152
|
+
*
|
|
153
|
+
* greet('hello');
|
|
154
|
+
* // 'Hello!'
|
|
155
|
+
* ```
|
|
156
|
+
*
|
|
157
|
+
* @example Pipeline explanation
|
|
158
|
+
* ```typescript
|
|
159
|
+
* const f1 = (x) => x + 1;
|
|
160
|
+
* const f2 = (x) => x * 2;
|
|
161
|
+
* const f3 = (x) => x.toString();
|
|
162
|
+
*
|
|
163
|
+
* compose(f3, f2, f1)(5);
|
|
164
|
+
* // Equivalent to: f3(f2(f1(5)))
|
|
165
|
+
* // Equivalent to: f3(f2(6))
|
|
166
|
+
* // Equivalent to: f3(12)
|
|
167
|
+
* // Result: '12'
|
|
168
|
+
* ```
|
|
169
|
+
*
|
|
170
|
+
* @see {@link pipe} for left-to-right composition
|
|
171
|
+
*/
|
|
172
|
+
declare function compose<T>(...fns: Array<(arg: any) => any>): (arg: T) => any;
|
|
173
|
+
/**
|
|
174
|
+
* Compose functions left-to-right.
|
|
175
|
+
*
|
|
176
|
+
* Creates a function that applies functions from left to right.
|
|
177
|
+
* Result of each function is passed as argument to the next.
|
|
178
|
+
*
|
|
179
|
+
* @param fns - Functions to compose
|
|
180
|
+
* @returns Composed function
|
|
181
|
+
*
|
|
182
|
+
* @example Basic usage
|
|
183
|
+
* ```typescript
|
|
184
|
+
* const add = (x: number) => x + 1;
|
|
185
|
+
* const multiply = (x: number) => x * 2;
|
|
186
|
+
* const toString = (x: number) => x.toString();
|
|
187
|
+
*
|
|
188
|
+
* const pipeline = pipe(add, multiply, toString);
|
|
189
|
+
* pipeline(5);
|
|
190
|
+
* // '12' (5 + 1 = 6, 6 * 2 = 12, '12')
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* @example Data transformation
|
|
194
|
+
* ```typescript
|
|
195
|
+
* const toNumber = (s: string) => parseInt(s);
|
|
196
|
+
* const double = (x: number) => x * 2;
|
|
197
|
+
* const toString = (x: number) => x.toString();
|
|
198
|
+
*
|
|
199
|
+
* const transform = pipe(toNumber, double, toString);
|
|
200
|
+
* transform('5');
|
|
201
|
+
* // '10'
|
|
202
|
+
* ```
|
|
203
|
+
*
|
|
204
|
+
* @example API data processing
|
|
205
|
+
* ```typescript
|
|
206
|
+
* const fetchData = () => ({ items: [{ price: 10 }, { price: 20 }] });
|
|
207
|
+
* const getItems = (data) => data.items;
|
|
208
|
+
* const sumPrices = (items) => items.reduce((sum, item) => sum + item.price, 0);
|
|
209
|
+
*
|
|
210
|
+
* const getTotal = pipe(fetchData, getItems, sumPrices);
|
|
211
|
+
* await getTotal(); // 30
|
|
212
|
+
* ```
|
|
213
|
+
*
|
|
214
|
+
* @example Pipeline explanation
|
|
215
|
+
* ```typescript
|
|
216
|
+
* const f1 = (x) => x + 1;
|
|
217
|
+
* const f2 = (x) => x * 2;
|
|
218
|
+
* const f3 = (x) => x.toString();
|
|
219
|
+
*
|
|
220
|
+
* pipe(f1, f2, f3)(5);
|
|
221
|
+
* // Equivalent to: f3(f2(f1(5)))
|
|
222
|
+
* // Equivalent to: f3(f2(6))
|
|
223
|
+
* // Equivalent to: f3(12)
|
|
224
|
+
* // Result: '12'
|
|
225
|
+
* ```
|
|
226
|
+
*
|
|
227
|
+
* @see {@link compose} for right-to-left composition
|
|
228
|
+
*/
|
|
229
|
+
declare function pipe<T>(...fns: Array<(arg: any) => any>): (arg: T) => any;
|
|
230
|
+
|
|
231
|
+
export { compose, flip, invert, mapKeys, mapValues, pipe };
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transform object keys with a function.
|
|
3
|
+
*
|
|
4
|
+
* Creates a new object with keys transformed by the provided function.
|
|
5
|
+
*
|
|
6
|
+
* @param obj - Source object
|
|
7
|
+
* @param fn - Function to transform keys
|
|
8
|
+
* @returns New object with transformed keys
|
|
9
|
+
*
|
|
10
|
+
* @example Basic usage
|
|
11
|
+
* ```typescript
|
|
12
|
+
* mapKeys({ a: 1, b: 2 }, (key) => key.toUpperCase());
|
|
13
|
+
* // { A: 1, B: 2 }
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @example Adding prefix
|
|
17
|
+
* ```typescript
|
|
18
|
+
* mapKeys({ name: 'Alice', age: 30 }, (key) => `user_${key}`);
|
|
19
|
+
* // { user_name: 'Alice', user_age: 30 }
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @example Snake case conversion
|
|
23
|
+
* ```typescript
|
|
24
|
+
* mapKeys({ firstName: 'Alice', lastName: 'Smith' }, (key) => snakeCase(key));
|
|
25
|
+
* // { first_name: 'Alice', last_name: 'Smith' }
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* @see {@link mapValues} for transforming values
|
|
29
|
+
*/
|
|
30
|
+
declare function mapKeys<T extends object, K extends PropertyKey>(obj: T, fn: (key: keyof T, value: T[keyof T]) => K): Record<K, T[keyof T]>;
|
|
31
|
+
/**
|
|
32
|
+
* Transform object values with a function.
|
|
33
|
+
*
|
|
34
|
+
* Creates a new object with values transformed by the provided function.
|
|
35
|
+
*
|
|
36
|
+
* @param obj - Source object
|
|
37
|
+
* @param fn - Function to transform values
|
|
38
|
+
* @returns New object with transformed values
|
|
39
|
+
*
|
|
40
|
+
* @example Basic usage
|
|
41
|
+
* ```typescript
|
|
42
|
+
* mapValues({ a: 1, b: 2 }, (val) => val * 2);
|
|
43
|
+
* // { a: 2, b: 4 }
|
|
44
|
+
* ```
|
|
45
|
+
*
|
|
46
|
+
* @example String transformation
|
|
47
|
+
* ```typescript
|
|
48
|
+
* mapValues({ name: 'alice', city: 'ny' }, (val) => val.toUpperCase());
|
|
49
|
+
* // { name: 'ALICE', city: 'NY' }
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @example Complex transformation
|
|
53
|
+
* ```typescript
|
|
54
|
+
* mapValues({ a: 1, b: 2 }, (val, key) => `${key}:${val}`);
|
|
55
|
+
* // { a: 'a:1', b: 'b:2' }
|
|
56
|
+
* ```
|
|
57
|
+
*
|
|
58
|
+
* @see {@link mapKeys} for transforming keys
|
|
59
|
+
*/
|
|
60
|
+
declare function mapValues<T extends object, R>(obj: T, fn: (value: T[keyof T], key: keyof T) => R): Record<keyof T, R>;
|
|
61
|
+
/**
|
|
62
|
+
* Swap keys and values of an object.
|
|
63
|
+
*
|
|
64
|
+
* Creates a new object where keys become values and values become keys.
|
|
65
|
+
* If values are not unique, later values will overwrite earlier ones.
|
|
66
|
+
*
|
|
67
|
+
* @param obj - Source object
|
|
68
|
+
* @returns Inverted object
|
|
69
|
+
*
|
|
70
|
+
* @example Basic usage
|
|
71
|
+
* ```typescript
|
|
72
|
+
* invert({ a: '1', b: '2', c: '3' });
|
|
73
|
+
* // { '1': 'a', '2': 'b', '3': 'c' }
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* @example Number values
|
|
77
|
+
* ```typescript
|
|
78
|
+
* invert({ a: 1, b: 2, c: 3 });
|
|
79
|
+
* // { 1: 'a', 2: 'b', 3: 'c' }
|
|
80
|
+
* ```
|
|
81
|
+
*
|
|
82
|
+
* @example Duplicate values
|
|
83
|
+
* ```typescript
|
|
84
|
+
* invert({ a: 'x', b: 'x', c: 'y' });
|
|
85
|
+
* // { 'x': 'b', 'y': 'c' } (last 'x' wins)
|
|
86
|
+
* ```
|
|
87
|
+
*
|
|
88
|
+
* @example Non-string keys become strings
|
|
89
|
+
* ```typescript
|
|
90
|
+
* invert({ 1: 'a', 2: 'b' });
|
|
91
|
+
* // { 'a': '1', 'b': '2' }
|
|
92
|
+
* ```
|
|
93
|
+
*/
|
|
94
|
+
declare function invert<T extends object>(obj: T): Record<string, keyof T>;
|
|
95
|
+
/**
|
|
96
|
+
* Flip function argument order.
|
|
97
|
+
*
|
|
98
|
+
* Creates a new function with reversed argument order.
|
|
99
|
+
*
|
|
100
|
+
* @param fn - Function to flip
|
|
101
|
+
* @returns New function with flipped arguments
|
|
102
|
+
*
|
|
103
|
+
* @example Basic usage
|
|
104
|
+
* ```typescript
|
|
105
|
+
* const divide = (a: number, b: number) => a / b;
|
|
106
|
+
* const flippedDivide = flip(divide);
|
|
107
|
+
* divide(10, 2); // 5
|
|
108
|
+
* flippedDivide(10, 2); // 0.2 (same as divide(2, 10))
|
|
109
|
+
* ```
|
|
110
|
+
*
|
|
111
|
+
* @example With map
|
|
112
|
+
* ```typescript
|
|
113
|
+
* const subtract = (a: number, b: number) => a - b;
|
|
114
|
+
* const subtractFrom10 = flip(subtract)(10);
|
|
115
|
+
* [1, 2, 3, 4, 5].map(subtractFrom10);
|
|
116
|
+
* // [9, 8, 7, 6, 5]
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* @example With currying
|
|
120
|
+
* ```typescript
|
|
121
|
+
* const append = (prefix: string, suffix: string) => prefix + suffix;
|
|
122
|
+
* const appendToHello = flip(append)('hello');
|
|
123
|
+
* appendToHello(' world'); // ' worldhello'
|
|
124
|
+
* ```
|
|
125
|
+
*/
|
|
126
|
+
declare function flip<T extends (...args: any[]) => any>(fn: T): T;
|
|
127
|
+
/**
|
|
128
|
+
* Compose functions right-to-left.
|
|
129
|
+
*
|
|
130
|
+
* Creates a function that applies functions from right to left.
|
|
131
|
+
* Result of each function is passed as argument to the next.
|
|
132
|
+
*
|
|
133
|
+
* @param fns - Functions to compose
|
|
134
|
+
* @returns Composed function
|
|
135
|
+
*
|
|
136
|
+
* @example Basic usage
|
|
137
|
+
* ```typescript
|
|
138
|
+
* const add = (x: number) => x + 1;
|
|
139
|
+
* const multiply = (x: number) => x * 2;
|
|
140
|
+
* const toString = (x: number) => x.toString();
|
|
141
|
+
*
|
|
142
|
+
* const process = compose(toString, multiply, add);
|
|
143
|
+
* process(5);
|
|
144
|
+
* // '12' (5 + 1 = 6, 6 * 2 = 12, '12')
|
|
145
|
+
* ```
|
|
146
|
+
*
|
|
147
|
+
* @example Complex transformation
|
|
148
|
+
* ```typescript
|
|
149
|
+
* const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);
|
|
150
|
+
* const exclaim = (s: string) => s + '!';
|
|
151
|
+
* const greet = compose(exclaim, capitalize);
|
|
152
|
+
*
|
|
153
|
+
* greet('hello');
|
|
154
|
+
* // 'Hello!'
|
|
155
|
+
* ```
|
|
156
|
+
*
|
|
157
|
+
* @example Pipeline explanation
|
|
158
|
+
* ```typescript
|
|
159
|
+
* const f1 = (x) => x + 1;
|
|
160
|
+
* const f2 = (x) => x * 2;
|
|
161
|
+
* const f3 = (x) => x.toString();
|
|
162
|
+
*
|
|
163
|
+
* compose(f3, f2, f1)(5);
|
|
164
|
+
* // Equivalent to: f3(f2(f1(5)))
|
|
165
|
+
* // Equivalent to: f3(f2(6))
|
|
166
|
+
* // Equivalent to: f3(12)
|
|
167
|
+
* // Result: '12'
|
|
168
|
+
* ```
|
|
169
|
+
*
|
|
170
|
+
* @see {@link pipe} for left-to-right composition
|
|
171
|
+
*/
|
|
172
|
+
declare function compose<T>(...fns: Array<(arg: any) => any>): (arg: T) => any;
|
|
173
|
+
/**
|
|
174
|
+
* Compose functions left-to-right.
|
|
175
|
+
*
|
|
176
|
+
* Creates a function that applies functions from left to right.
|
|
177
|
+
* Result of each function is passed as argument to the next.
|
|
178
|
+
*
|
|
179
|
+
* @param fns - Functions to compose
|
|
180
|
+
* @returns Composed function
|
|
181
|
+
*
|
|
182
|
+
* @example Basic usage
|
|
183
|
+
* ```typescript
|
|
184
|
+
* const add = (x: number) => x + 1;
|
|
185
|
+
* const multiply = (x: number) => x * 2;
|
|
186
|
+
* const toString = (x: number) => x.toString();
|
|
187
|
+
*
|
|
188
|
+
* const pipeline = pipe(add, multiply, toString);
|
|
189
|
+
* pipeline(5);
|
|
190
|
+
* // '12' (5 + 1 = 6, 6 * 2 = 12, '12')
|
|
191
|
+
* ```
|
|
192
|
+
*
|
|
193
|
+
* @example Data transformation
|
|
194
|
+
* ```typescript
|
|
195
|
+
* const toNumber = (s: string) => parseInt(s);
|
|
196
|
+
* const double = (x: number) => x * 2;
|
|
197
|
+
* const toString = (x: number) => x.toString();
|
|
198
|
+
*
|
|
199
|
+
* const transform = pipe(toNumber, double, toString);
|
|
200
|
+
* transform('5');
|
|
201
|
+
* // '10'
|
|
202
|
+
* ```
|
|
203
|
+
*
|
|
204
|
+
* @example API data processing
|
|
205
|
+
* ```typescript
|
|
206
|
+
* const fetchData = () => ({ items: [{ price: 10 }, { price: 20 }] });
|
|
207
|
+
* const getItems = (data) => data.items;
|
|
208
|
+
* const sumPrices = (items) => items.reduce((sum, item) => sum + item.price, 0);
|
|
209
|
+
*
|
|
210
|
+
* const getTotal = pipe(fetchData, getItems, sumPrices);
|
|
211
|
+
* await getTotal(); // 30
|
|
212
|
+
* ```
|
|
213
|
+
*
|
|
214
|
+
* @example Pipeline explanation
|
|
215
|
+
* ```typescript
|
|
216
|
+
* const f1 = (x) => x + 1;
|
|
217
|
+
* const f2 = (x) => x * 2;
|
|
218
|
+
* const f3 = (x) => x.toString();
|
|
219
|
+
*
|
|
220
|
+
* pipe(f1, f2, f3)(5);
|
|
221
|
+
* // Equivalent to: f3(f2(f1(5)))
|
|
222
|
+
* // Equivalent to: f3(f2(6))
|
|
223
|
+
* // Equivalent to: f3(12)
|
|
224
|
+
* // Result: '12'
|
|
225
|
+
* ```
|
|
226
|
+
*
|
|
227
|
+
* @see {@link compose} for right-to-left composition
|
|
228
|
+
*/
|
|
229
|
+
declare function pipe<T>(...fns: Array<(arg: any) => any>): (arg: T) => any;
|
|
230
|
+
|
|
231
|
+
export { compose, flip, invert, mapKeys, mapValues, pipe };
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
// src/plugins/optional/transform.ts
|
|
2
|
+
function mapKeys(obj, fn) {
|
|
3
|
+
const result = {};
|
|
4
|
+
for (const key of Object.keys(obj)) {
|
|
5
|
+
const newKey = fn(key, obj[key]);
|
|
6
|
+
result[newKey] = obj[key];
|
|
7
|
+
}
|
|
8
|
+
return result;
|
|
9
|
+
}
|
|
10
|
+
function mapValues(obj, fn) {
|
|
11
|
+
const result = {};
|
|
12
|
+
for (const key of Object.keys(obj)) {
|
|
13
|
+
result[key] = fn(obj[key], key);
|
|
14
|
+
}
|
|
15
|
+
return result;
|
|
16
|
+
}
|
|
17
|
+
function invert(obj) {
|
|
18
|
+
const result = {};
|
|
19
|
+
for (const key of Object.keys(obj)) {
|
|
20
|
+
result[String(obj[key])] = key;
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
function flip(fn) {
|
|
25
|
+
return ((...args) => {
|
|
26
|
+
return fn(...args.reverse());
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
function compose(...fns) {
|
|
30
|
+
return (arg) => {
|
|
31
|
+
return fns.reduceRight((acc, fn) => fn(acc), arg);
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
function pipe(...fns) {
|
|
35
|
+
return (arg) => {
|
|
36
|
+
return fns.reduce((acc, fn) => fn(acc), arg);
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export { compose, flip, invert, mapKeys, mapValues, pipe };
|
|
41
|
+
//# sourceMappingURL=transform.js.map
|
|
42
|
+
//# sourceMappingURL=transform.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/plugins/optional/transform.ts"],"names":[],"mappings":";AA6BO,SAAS,OAAA,CACd,KACA,EAAA,EACuB;AACvB,EAAA,MAAM,SAAS,EAAC;AAEhB,EAAA,KAAA,MAAW,GAAA,IAAO,MAAA,CAAO,IAAA,CAAK,GAAG,CAAA,EAAqB;AACpD,IAAA,MAAM,MAAA,GAAS,EAAA,CAAG,GAAA,EAAK,GAAA,CAAI,GAAG,CAAC,CAAA;AAC/B,IAAA,MAAA,CAAO,MAAM,CAAA,GAAI,GAAA,CAAI,GAAG,CAAA;AAAA,EAC1B;AAEA,EAAA,OAAO,MAAA;AACT;AA+BO,SAAS,SAAA,CACd,KACA,EAAA,EACoB;AACpB,EAAA,MAAM,SAAS,EAAC;AAEhB,EAAA,KAAA,MAAW,GAAA,IAAO,MAAA,CAAO,IAAA,CAAK,GAAG,CAAA,EAAqB;AACpD,IAAA,MAAA,CAAO,GAAG,CAAA,GAAI,EAAA,CAAG,GAAA,CAAI,GAAG,GAAG,GAAG,CAAA;AAAA,EAChC;AAEA,EAAA,OAAO,MAAA;AACT;AAmCO,SAAS,OAAyB,GAAA,EAAiC;AACxE,EAAA,MAAM,SAAS,EAAC;AAEhB,EAAA,KAAA,MAAW,GAAA,IAAO,MAAA,CAAO,IAAA,CAAK,GAAG,CAAA,EAAqB;AACpD,IAAA,MAAA,CAAO,MAAA,CAAO,GAAA,CAAI,GAAG,CAAC,CAAC,CAAA,GAAI,GAAA;AAAA,EAC7B;AAEA,EAAA,OAAO,MAAA;AACT;AAiCO,SAAS,KAAwC,EAAA,EAAU;AAChE,EAAA,QAAQ,IAAI,IAAA,KAAwB;AAClC,IAAA,OAAO,EAAA,CAAG,GAAG,IAAA,CAAK,OAAA,EAAS,CAAA;AAAA,EAC7B,CAAA;AACF;AA+CO,SAAS,WAAc,GAAA,EAAgD;AAC5E,EAAA,OAAO,CAAC,GAAA,KAAW;AACjB,IAAA,OAAO,GAAA,CAAI,YAAY,CAAC,GAAA,EAAK,OAAO,EAAA,CAAG,GAAG,GAAG,GAAG,CAAA;AAAA,EAClD,CAAA;AACF;AA0DO,SAAS,QAAW,GAAA,EAAgD;AACzE,EAAA,OAAO,CAAC,GAAA,KAAW;AACjB,IAAA,OAAO,GAAA,CAAI,OAAO,CAAC,GAAA,EAAK,OAAO,EAAA,CAAG,GAAG,GAAG,GAAG,CAAA;AAAA,EAC7C,CAAA;AACF","file":"transform.js","sourcesContent":["/**\n * Transform object keys with a function.\n *\n * Creates a new object with keys transformed by the provided function.\n *\n * @param obj - Source object\n * @param fn - Function to transform keys\n * @returns New object with transformed keys\n *\n * @example Basic usage\n * ```typescript\n * mapKeys({ a: 1, b: 2 }, (key) => key.toUpperCase());\n * // { A: 1, B: 2 }\n * ```\n *\n * @example Adding prefix\n * ```typescript\n * mapKeys({ name: 'Alice', age: 30 }, (key) => `user_${key}`);\n * // { user_name: 'Alice', user_age: 30 }\n * ```\n *\n * @example Snake case conversion\n * ```typescript\n * mapKeys({ firstName: 'Alice', lastName: 'Smith' }, (key) => snakeCase(key));\n * // { first_name: 'Alice', last_name: 'Smith' }\n * ```\n *\n * @see {@link mapValues} for transforming values\n */\nexport function mapKeys<T extends object, K extends PropertyKey>(\n obj: T,\n fn: (key: keyof T, value: T[keyof T]) => K\n): Record<K, T[keyof T]> {\n const result = {} as Record<K, T[keyof T]>;\n\n for (const key of Object.keys(obj) as Array<keyof T>) {\n const newKey = fn(key, obj[key]);\n result[newKey] = obj[key];\n }\n\n return result;\n}\n\n/**\n * Transform object values with a function.\n *\n * Creates a new object with values transformed by the provided function.\n *\n * @param obj - Source object\n * @param fn - Function to transform values\n * @returns New object with transformed values\n *\n * @example Basic usage\n * ```typescript\n * mapValues({ a: 1, b: 2 }, (val) => val * 2);\n * // { a: 2, b: 4 }\n * ```\n *\n * @example String transformation\n * ```typescript\n * mapValues({ name: 'alice', city: 'ny' }, (val) => val.toUpperCase());\n * // { name: 'ALICE', city: 'NY' }\n * ```\n *\n * @example Complex transformation\n * ```typescript\n * mapValues({ a: 1, b: 2 }, (val, key) => `${key}:${val}`);\n * // { a: 'a:1', b: 'b:2' }\n * ```\n *\n * @see {@link mapKeys} for transforming keys\n */\nexport function mapValues<T extends object, R>(\n obj: T,\n fn: (value: T[keyof T], key: keyof T) => R\n): Record<keyof T, R> {\n const result = {} as Record<keyof T, R>;\n\n for (const key of Object.keys(obj) as Array<keyof T>) {\n result[key] = fn(obj[key], key);\n }\n\n return result;\n}\n\n/**\n * Swap keys and values of an object.\n *\n * Creates a new object where keys become values and values become keys.\n * If values are not unique, later values will overwrite earlier ones.\n *\n * @param obj - Source object\n * @returns Inverted object\n *\n * @example Basic usage\n * ```typescript\n * invert({ a: '1', b: '2', c: '3' });\n * // { '1': 'a', '2': 'b', '3': 'c' }\n * ```\n *\n * @example Number values\n * ```typescript\n * invert({ a: 1, b: 2, c: 3 });\n * // { 1: 'a', 2: 'b', 3: 'c' }\n * ```\n *\n * @example Duplicate values\n * ```typescript\n * invert({ a: 'x', b: 'x', c: 'y' });\n * // { 'x': 'b', 'y': 'c' } (last 'x' wins)\n * ```\n *\n * @example Non-string keys become strings\n * ```typescript\n * invert({ 1: 'a', 2: 'b' });\n * // { 'a': '1', 'b': '2' }\n * ```\n */\nexport function invert<T extends object>(obj: T): Record<string, keyof T> {\n const result = {} as Record<string, keyof T>;\n\n for (const key of Object.keys(obj) as Array<keyof T>) {\n result[String(obj[key])] = key;\n }\n\n return result;\n}\n\n/**\n * Flip function argument order.\n *\n * Creates a new function with reversed argument order.\n *\n * @param fn - Function to flip\n * @returns New function with flipped arguments\n *\n * @example Basic usage\n * ```typescript\n * const divide = (a: number, b: number) => a / b;\n * const flippedDivide = flip(divide);\n * divide(10, 2); // 5\n * flippedDivide(10, 2); // 0.2 (same as divide(2, 10))\n * ```\n *\n * @example With map\n * ```typescript\n * const subtract = (a: number, b: number) => a - b;\n * const subtractFrom10 = flip(subtract)(10);\n * [1, 2, 3, 4, 5].map(subtractFrom10);\n * // [9, 8, 7, 6, 5]\n * ```\n *\n * @example With currying\n * ```typescript\n * const append = (prefix: string, suffix: string) => prefix + suffix;\n * const appendToHello = flip(append)('hello');\n * appendToHello(' world'); // ' worldhello'\n * ```\n */\nexport function flip<T extends (...args: any[]) => any>(fn: T): T {\n return ((...args: Parameters<T>) => {\n return fn(...args.reverse());\n }) as T;\n}\n\n/**\n * Compose functions right-to-left.\n *\n * Creates a function that applies functions from right to left.\n * Result of each function is passed as argument to the next.\n *\n * @param fns - Functions to compose\n * @returns Composed function\n *\n * @example Basic usage\n * ```typescript\n * const add = (x: number) => x + 1;\n * const multiply = (x: number) => x * 2;\n * const toString = (x: number) => x.toString();\n *\n * const process = compose(toString, multiply, add);\n * process(5);\n * // '12' (5 + 1 = 6, 6 * 2 = 12, '12')\n * ```\n *\n * @example Complex transformation\n * ```typescript\n * const capitalize = (s: string) => s.charAt(0).toUpperCase() + s.slice(1);\n * const exclaim = (s: string) => s + '!';\n * const greet = compose(exclaim, capitalize);\n *\n * greet('hello');\n * // 'Hello!'\n * ```\n *\n * @example Pipeline explanation\n * ```typescript\n * const f1 = (x) => x + 1;\n * const f2 = (x) => x * 2;\n * const f3 = (x) => x.toString();\n *\n * compose(f3, f2, f1)(5);\n * // Equivalent to: f3(f2(f1(5)))\n * // Equivalent to: f3(f2(6))\n * // Equivalent to: f3(12)\n * // Result: '12'\n * ```\n *\n * @see {@link pipe} for left-to-right composition\n */\nexport function compose<T>(...fns: Array<(arg: any) => any>): (arg: T) => any {\n return (arg: T) => {\n return fns.reduceRight((acc, fn) => fn(acc), arg);\n };\n}\n\n/**\n * Compose functions left-to-right.\n *\n * Creates a function that applies functions from left to right.\n * Result of each function is passed as argument to the next.\n *\n * @param fns - Functions to compose\n * @returns Composed function\n *\n * @example Basic usage\n * ```typescript\n * const add = (x: number) => x + 1;\n * const multiply = (x: number) => x * 2;\n * const toString = (x: number) => x.toString();\n *\n * const pipeline = pipe(add, multiply, toString);\n * pipeline(5);\n * // '12' (5 + 1 = 6, 6 * 2 = 12, '12')\n * ```\n *\n * @example Data transformation\n * ```typescript\n * const toNumber = (s: string) => parseInt(s);\n * const double = (x: number) => x * 2;\n * const toString = (x: number) => x.toString();\n *\n * const transform = pipe(toNumber, double, toString);\n * transform('5');\n * // '10'\n * ```\n *\n * @example API data processing\n * ```typescript\n * const fetchData = () => ({ items: [{ price: 10 }, { price: 20 }] });\n * const getItems = (data) => data.items;\n * const sumPrices = (items) => items.reduce((sum, item) => sum + item.price, 0);\n *\n * const getTotal = pipe(fetchData, getItems, sumPrices);\n * await getTotal(); // 30\n * ```\n *\n * @example Pipeline explanation\n * ```typescript\n * const f1 = (x) => x + 1;\n * const f2 = (x) => x * 2;\n * const f3 = (x) => x.toString();\n *\n * pipe(f1, f2, f3)(5);\n * // Equivalent to: f3(f2(f1(5)))\n * // Equivalent to: f3(f2(6))\n * // Equivalent to: f3(12)\n * // Result: '12'\n * ```\n *\n * @see {@link compose} for right-to-left composition\n */\nexport function pipe<T>(...fns: Array<(arg: any) => any>): (arg: T) => any {\n return (arg: T) => {\n return fns.reduce((acc, fn) => fn(acc), arg);\n };\n}\n"]}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Plugin interface for extending kernel functionality.
|
|
3
|
+
*
|
|
4
|
+
* @typeParam TContext - Shared context type between plugins
|
|
5
|
+
*/
|
|
6
|
+
interface Plugin<TContext = unknown> {
|
|
7
|
+
/** Unique plugin identifier (kebab-case) */
|
|
8
|
+
name: string;
|
|
9
|
+
/** Semantic version (e.g., "1.0.0") */
|
|
10
|
+
version: string;
|
|
11
|
+
/** Other plugins this plugin depends on */
|
|
12
|
+
dependencies?: string[];
|
|
13
|
+
/**
|
|
14
|
+
* Called when plugin is registered.
|
|
15
|
+
* @param kernel - The kernel instance
|
|
16
|
+
*/
|
|
17
|
+
install: (kernel: any) => void;
|
|
18
|
+
/**
|
|
19
|
+
* Called after all plugins are installed.
|
|
20
|
+
* @param context - Shared context object
|
|
21
|
+
*/
|
|
22
|
+
onInit?: (context: TContext) => void | Promise<void>;
|
|
23
|
+
/**
|
|
24
|
+
* Called when plugin is unregistered.
|
|
25
|
+
*/
|
|
26
|
+
onDestroy?: () => void | Promise<void>;
|
|
27
|
+
/**
|
|
28
|
+
* Called on error in this plugin.
|
|
29
|
+
* @param error - The error that occurred
|
|
30
|
+
*/
|
|
31
|
+
onError?: (error: Error) => void;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Options for debounce() function.
|
|
35
|
+
*/
|
|
36
|
+
interface DebounceOptions {
|
|
37
|
+
/**
|
|
38
|
+
* Invoke on leading edge of timeout.
|
|
39
|
+
* @default false
|
|
40
|
+
*/
|
|
41
|
+
leading?: boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Invoke on trailing edge of timeout.
|
|
44
|
+
* @default true
|
|
45
|
+
*/
|
|
46
|
+
trailing?: boolean;
|
|
47
|
+
/**
|
|
48
|
+
* Maximum time to wait.
|
|
49
|
+
* @default undefined
|
|
50
|
+
*/
|
|
51
|
+
maxWait?: number;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Options for retry() function.
|
|
55
|
+
*/
|
|
56
|
+
interface RetryOptions<T = any> {
|
|
57
|
+
/**
|
|
58
|
+
* Number of retry attempts.
|
|
59
|
+
* @default 3
|
|
60
|
+
*/
|
|
61
|
+
attempts?: number;
|
|
62
|
+
/**
|
|
63
|
+
* Delay between retries in ms.
|
|
64
|
+
* @default 1000
|
|
65
|
+
*/
|
|
66
|
+
delay?: number;
|
|
67
|
+
/**
|
|
68
|
+
* Backoff strategy.
|
|
69
|
+
* @default 'exponential'
|
|
70
|
+
*/
|
|
71
|
+
backoff?: 'linear' | 'exponential' | 'none';
|
|
72
|
+
/**
|
|
73
|
+
* Condition to retry.
|
|
74
|
+
* @default () => true
|
|
75
|
+
*/
|
|
76
|
+
retryIf?: (error: Error) => boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Function to retry.
|
|
79
|
+
*/
|
|
80
|
+
fn: () => Promise<T>;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Result object for diff() function.
|
|
84
|
+
*/
|
|
85
|
+
interface DiffResult {
|
|
86
|
+
/** Values that changed */
|
|
87
|
+
changed: Record<string, {
|
|
88
|
+
from: any;
|
|
89
|
+
to: any;
|
|
90
|
+
}>;
|
|
91
|
+
/** Values that were added */
|
|
92
|
+
added: Record<string, any>;
|
|
93
|
+
/** Values that were removed */
|
|
94
|
+
removed: Record<string, any>;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export type { DebounceOptions as D, Plugin as P, RetryOptions as R, DiffResult as a };
|