@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,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if collection is empty.
|
|
3
|
+
*
|
|
4
|
+
* Returns true for empty arrays, objects, Maps, or Sets.
|
|
5
|
+
* Note: Unlike lodash, this returns false for strings and numbers!
|
|
6
|
+
*
|
|
7
|
+
* @param value - Value to check
|
|
8
|
+
* @returns True if collection is empty
|
|
9
|
+
*
|
|
10
|
+
* @example Arrays
|
|
11
|
+
* ```typescript
|
|
12
|
+
* isEmpty([]); // true
|
|
13
|
+
* isEmpty([1, 2, 3]); // false
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @example Objects
|
|
17
|
+
* ```typescript
|
|
18
|
+
* isEmpty({}); // true
|
|
19
|
+
* isEmpty({ a: 1 }); // false
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @example Map and Set
|
|
23
|
+
* ```typescript
|
|
24
|
+
* isEmpty(new Map()); // true
|
|
25
|
+
* isEmpty(new Set()); // true
|
|
26
|
+
* isEmpty(new Map([['a', 1]])); // false
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @example Non-collections (returns false)
|
|
30
|
+
* ```typescript
|
|
31
|
+
* isEmpty(''); // false - strings are not collections
|
|
32
|
+
* isEmpty(0); // false - numbers are not collections
|
|
33
|
+
* isEmpty(false); // false - booleans are not collections
|
|
34
|
+
* isEmpty(null); // false - null is not a collection
|
|
35
|
+
* isEmpty(undefined); // false - undefined is not a collection
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @see {@link isNil} for null/undefined check
|
|
39
|
+
*/
|
|
40
|
+
declare function isEmpty(value: unknown): value is [] | Record<string, never> | Map<any, never> | Set<never>;
|
|
41
|
+
/**
|
|
42
|
+
* Check if value is null or undefined.
|
|
43
|
+
*
|
|
44
|
+
* @param value - Value to check
|
|
45
|
+
* @returns True if value is null or undefined
|
|
46
|
+
*
|
|
47
|
+
* @example Basic usage
|
|
48
|
+
* ```typescript
|
|
49
|
+
* isNil(null); // true
|
|
50
|
+
* isNil(undefined); // true
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @example False cases
|
|
54
|
+
* ```typescript
|
|
55
|
+
* isNil(0); // false
|
|
56
|
+
* isNil(''); // false
|
|
57
|
+
* isNil(false); // false
|
|
58
|
+
* isNil([]); // false
|
|
59
|
+
* isNil({}); // false
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @see {@link isEmpty} for collection emptiness check
|
|
63
|
+
*/
|
|
64
|
+
declare function isNil(value: unknown): value is null | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* Check if value is a plain object.
|
|
67
|
+
*
|
|
68
|
+
* Returns true for plain objects created by object literal or Object.create(null).
|
|
69
|
+
* Returns false for class instances, arrays, Date, RegExp, etc.
|
|
70
|
+
*
|
|
71
|
+
* @param value - Value to check
|
|
72
|
+
* @returns True if value is a plain object
|
|
73
|
+
*
|
|
74
|
+
* @example Plain objects
|
|
75
|
+
* ```typescript
|
|
76
|
+
* isPlainObject({}); // true
|
|
77
|
+
* isPlainObject({ a: 1 }); // true
|
|
78
|
+
* isPlainObject(Object.create(null)); // true
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @example Non-plain objects
|
|
82
|
+
* ```typescript
|
|
83
|
+
* isPlainObject(new Date()); // false (class instance)
|
|
84
|
+
* isPlainObject([]); // false (array)
|
|
85
|
+
* isPlainObject(/test/); // false (RegExp)
|
|
86
|
+
* isPlainObject(new Map()); // false (Map)
|
|
87
|
+
* isPlainObject(new Set()); // false (Set)
|
|
88
|
+
* isPlainObject(() => {}); // false (function)
|
|
89
|
+
* isPlainObject(null); // false
|
|
90
|
+
* isPlainObject(undefined); // false
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* @example Class instances
|
|
94
|
+
* ```typescript
|
|
95
|
+
* class MyClass {}
|
|
96
|
+
* isPlainObject(new MyClass()); // false
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
|
100
|
+
/**
|
|
101
|
+
* Type guard for arrays.
|
|
102
|
+
*
|
|
103
|
+
* @param value - Value to check
|
|
104
|
+
* @returns True if value is an array
|
|
105
|
+
*
|
|
106
|
+
* @example Basic usage
|
|
107
|
+
* ```typescript
|
|
108
|
+
* const value: unknown = [1, 2, 3];
|
|
109
|
+
* if (isArray(value)) {
|
|
110
|
+
* value.map(x => x * 2); // TypeScript knows it's an array
|
|
111
|
+
* }
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
declare function isArray(value: unknown): value is any[];
|
|
115
|
+
/**
|
|
116
|
+
* Type guard for strings.
|
|
117
|
+
*
|
|
118
|
+
* @param value - Value to check
|
|
119
|
+
* @returns True if value is a string
|
|
120
|
+
*
|
|
121
|
+
* @example Basic usage
|
|
122
|
+
* ```typescript
|
|
123
|
+
* const value: unknown = 'hello';
|
|
124
|
+
* if (isString(value)) {
|
|
125
|
+
* value.toUpperCase(); // TypeScript knows it's a string
|
|
126
|
+
* }
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
declare function isString(value: unknown): value is string;
|
|
130
|
+
/**
|
|
131
|
+
* Type guard for numbers.
|
|
132
|
+
*
|
|
133
|
+
* Returns false for NaN (unlike typeof operator).
|
|
134
|
+
*
|
|
135
|
+
* @param value - Value to check
|
|
136
|
+
* @returns True if value is a valid number
|
|
137
|
+
*
|
|
138
|
+
* @example Basic usage
|
|
139
|
+
* ```typescript
|
|
140
|
+
* isNumber(42); // true
|
|
141
|
+
* isNumber(3.14); // true
|
|
142
|
+
* isNumber(-5); // true
|
|
143
|
+
* isNumber(Infinity); // true
|
|
144
|
+
* ```
|
|
145
|
+
*
|
|
146
|
+
* @example NaN handling
|
|
147
|
+
* ```typescript
|
|
148
|
+
* isNumber(NaN); // false (unlike typeof!)
|
|
149
|
+
* typeof NaN === 'number'; // true
|
|
150
|
+
* ```
|
|
151
|
+
*
|
|
152
|
+
* @example Non-numbers
|
|
153
|
+
* ```typescript
|
|
154
|
+
* isNumber('42'); // false
|
|
155
|
+
* isNumber(null); // false
|
|
156
|
+
* isNumber(undefined); // false
|
|
157
|
+
* isNumber({}); // false
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
declare function isNumber(value: unknown): value is number;
|
|
161
|
+
/**
|
|
162
|
+
* Type guard for functions.
|
|
163
|
+
*
|
|
164
|
+
* @param value - Value to check
|
|
165
|
+
* @returns True if value is a function
|
|
166
|
+
*
|
|
167
|
+
* @example Basic usage
|
|
168
|
+
* ```typescript
|
|
169
|
+
* isFunction(() => {}); // true
|
|
170
|
+
* isFunction(function() {}); // true
|
|
171
|
+
* isFunction(async () => {}); // true
|
|
172
|
+
* ```
|
|
173
|
+
*
|
|
174
|
+
* @example Non-functions
|
|
175
|
+
* ```typescript
|
|
176
|
+
* isFunction({}); // false
|
|
177
|
+
* isFunction(null); // false
|
|
178
|
+
* isFunction(undefined); // false
|
|
179
|
+
* isFunction('string'); // false
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
declare function isFunction(value: unknown): value is (...args: any[]) => any;
|
|
183
|
+
/**
|
|
184
|
+
* Type guard for valid Date objects.
|
|
185
|
+
*
|
|
186
|
+
* Returns true for Date objects with valid time values.
|
|
187
|
+
*
|
|
188
|
+
* @param value - Value to check
|
|
189
|
+
* @returns True if value is a valid Date
|
|
190
|
+
*
|
|
191
|
+
* @example Valid dates
|
|
192
|
+
* ```typescript
|
|
193
|
+
* isDate(new Date()); // true
|
|
194
|
+
* isDate(new Date('2024-01-01')); // true
|
|
195
|
+
* ```
|
|
196
|
+
*
|
|
197
|
+
* @example Invalid dates
|
|
198
|
+
* ```typescript
|
|
199
|
+
* isDate(new Date('invalid')); // false
|
|
200
|
+
* isDate(Date.now()); // false (number)
|
|
201
|
+
* ```
|
|
202
|
+
*
|
|
203
|
+
* @example Non-dates
|
|
204
|
+
* ```typescript
|
|
205
|
+
* isDate({}); // false
|
|
206
|
+
* isDate(null); // false
|
|
207
|
+
* isDate(undefined); // false
|
|
208
|
+
* isDate('2024-01-01'); // false (string)
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
declare function isDate(value: unknown): value is Date;
|
|
212
|
+
|
|
213
|
+
export { isArray, isDate, isEmpty, isFunction, isNil, isNumber, isPlainObject, isString };
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Check if collection is empty.
|
|
3
|
+
*
|
|
4
|
+
* Returns true for empty arrays, objects, Maps, or Sets.
|
|
5
|
+
* Note: Unlike lodash, this returns false for strings and numbers!
|
|
6
|
+
*
|
|
7
|
+
* @param value - Value to check
|
|
8
|
+
* @returns True if collection is empty
|
|
9
|
+
*
|
|
10
|
+
* @example Arrays
|
|
11
|
+
* ```typescript
|
|
12
|
+
* isEmpty([]); // true
|
|
13
|
+
* isEmpty([1, 2, 3]); // false
|
|
14
|
+
* ```
|
|
15
|
+
*
|
|
16
|
+
* @example Objects
|
|
17
|
+
* ```typescript
|
|
18
|
+
* isEmpty({}); // true
|
|
19
|
+
* isEmpty({ a: 1 }); // false
|
|
20
|
+
* ```
|
|
21
|
+
*
|
|
22
|
+
* @example Map and Set
|
|
23
|
+
* ```typescript
|
|
24
|
+
* isEmpty(new Map()); // true
|
|
25
|
+
* isEmpty(new Set()); // true
|
|
26
|
+
* isEmpty(new Map([['a', 1]])); // false
|
|
27
|
+
* ```
|
|
28
|
+
*
|
|
29
|
+
* @example Non-collections (returns false)
|
|
30
|
+
* ```typescript
|
|
31
|
+
* isEmpty(''); // false - strings are not collections
|
|
32
|
+
* isEmpty(0); // false - numbers are not collections
|
|
33
|
+
* isEmpty(false); // false - booleans are not collections
|
|
34
|
+
* isEmpty(null); // false - null is not a collection
|
|
35
|
+
* isEmpty(undefined); // false - undefined is not a collection
|
|
36
|
+
* ```
|
|
37
|
+
*
|
|
38
|
+
* @see {@link isNil} for null/undefined check
|
|
39
|
+
*/
|
|
40
|
+
declare function isEmpty(value: unknown): value is [] | Record<string, never> | Map<any, never> | Set<never>;
|
|
41
|
+
/**
|
|
42
|
+
* Check if value is null or undefined.
|
|
43
|
+
*
|
|
44
|
+
* @param value - Value to check
|
|
45
|
+
* @returns True if value is null or undefined
|
|
46
|
+
*
|
|
47
|
+
* @example Basic usage
|
|
48
|
+
* ```typescript
|
|
49
|
+
* isNil(null); // true
|
|
50
|
+
* isNil(undefined); // true
|
|
51
|
+
* ```
|
|
52
|
+
*
|
|
53
|
+
* @example False cases
|
|
54
|
+
* ```typescript
|
|
55
|
+
* isNil(0); // false
|
|
56
|
+
* isNil(''); // false
|
|
57
|
+
* isNil(false); // false
|
|
58
|
+
* isNil([]); // false
|
|
59
|
+
* isNil({}); // false
|
|
60
|
+
* ```
|
|
61
|
+
*
|
|
62
|
+
* @see {@link isEmpty} for collection emptiness check
|
|
63
|
+
*/
|
|
64
|
+
declare function isNil(value: unknown): value is null | undefined;
|
|
65
|
+
/**
|
|
66
|
+
* Check if value is a plain object.
|
|
67
|
+
*
|
|
68
|
+
* Returns true for plain objects created by object literal or Object.create(null).
|
|
69
|
+
* Returns false for class instances, arrays, Date, RegExp, etc.
|
|
70
|
+
*
|
|
71
|
+
* @param value - Value to check
|
|
72
|
+
* @returns True if value is a plain object
|
|
73
|
+
*
|
|
74
|
+
* @example Plain objects
|
|
75
|
+
* ```typescript
|
|
76
|
+
* isPlainObject({}); // true
|
|
77
|
+
* isPlainObject({ a: 1 }); // true
|
|
78
|
+
* isPlainObject(Object.create(null)); // true
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @example Non-plain objects
|
|
82
|
+
* ```typescript
|
|
83
|
+
* isPlainObject(new Date()); // false (class instance)
|
|
84
|
+
* isPlainObject([]); // false (array)
|
|
85
|
+
* isPlainObject(/test/); // false (RegExp)
|
|
86
|
+
* isPlainObject(new Map()); // false (Map)
|
|
87
|
+
* isPlainObject(new Set()); // false (Set)
|
|
88
|
+
* isPlainObject(() => {}); // false (function)
|
|
89
|
+
* isPlainObject(null); // false
|
|
90
|
+
* isPlainObject(undefined); // false
|
|
91
|
+
* ```
|
|
92
|
+
*
|
|
93
|
+
* @example Class instances
|
|
94
|
+
* ```typescript
|
|
95
|
+
* class MyClass {}
|
|
96
|
+
* isPlainObject(new MyClass()); // false
|
|
97
|
+
* ```
|
|
98
|
+
*/
|
|
99
|
+
declare function isPlainObject(value: unknown): value is Record<string, unknown>;
|
|
100
|
+
/**
|
|
101
|
+
* Type guard for arrays.
|
|
102
|
+
*
|
|
103
|
+
* @param value - Value to check
|
|
104
|
+
* @returns True if value is an array
|
|
105
|
+
*
|
|
106
|
+
* @example Basic usage
|
|
107
|
+
* ```typescript
|
|
108
|
+
* const value: unknown = [1, 2, 3];
|
|
109
|
+
* if (isArray(value)) {
|
|
110
|
+
* value.map(x => x * 2); // TypeScript knows it's an array
|
|
111
|
+
* }
|
|
112
|
+
* ```
|
|
113
|
+
*/
|
|
114
|
+
declare function isArray(value: unknown): value is any[];
|
|
115
|
+
/**
|
|
116
|
+
* Type guard for strings.
|
|
117
|
+
*
|
|
118
|
+
* @param value - Value to check
|
|
119
|
+
* @returns True if value is a string
|
|
120
|
+
*
|
|
121
|
+
* @example Basic usage
|
|
122
|
+
* ```typescript
|
|
123
|
+
* const value: unknown = 'hello';
|
|
124
|
+
* if (isString(value)) {
|
|
125
|
+
* value.toUpperCase(); // TypeScript knows it's a string
|
|
126
|
+
* }
|
|
127
|
+
* ```
|
|
128
|
+
*/
|
|
129
|
+
declare function isString(value: unknown): value is string;
|
|
130
|
+
/**
|
|
131
|
+
* Type guard for numbers.
|
|
132
|
+
*
|
|
133
|
+
* Returns false for NaN (unlike typeof operator).
|
|
134
|
+
*
|
|
135
|
+
* @param value - Value to check
|
|
136
|
+
* @returns True if value is a valid number
|
|
137
|
+
*
|
|
138
|
+
* @example Basic usage
|
|
139
|
+
* ```typescript
|
|
140
|
+
* isNumber(42); // true
|
|
141
|
+
* isNumber(3.14); // true
|
|
142
|
+
* isNumber(-5); // true
|
|
143
|
+
* isNumber(Infinity); // true
|
|
144
|
+
* ```
|
|
145
|
+
*
|
|
146
|
+
* @example NaN handling
|
|
147
|
+
* ```typescript
|
|
148
|
+
* isNumber(NaN); // false (unlike typeof!)
|
|
149
|
+
* typeof NaN === 'number'; // true
|
|
150
|
+
* ```
|
|
151
|
+
*
|
|
152
|
+
* @example Non-numbers
|
|
153
|
+
* ```typescript
|
|
154
|
+
* isNumber('42'); // false
|
|
155
|
+
* isNumber(null); // false
|
|
156
|
+
* isNumber(undefined); // false
|
|
157
|
+
* isNumber({}); // false
|
|
158
|
+
* ```
|
|
159
|
+
*/
|
|
160
|
+
declare function isNumber(value: unknown): value is number;
|
|
161
|
+
/**
|
|
162
|
+
* Type guard for functions.
|
|
163
|
+
*
|
|
164
|
+
* @param value - Value to check
|
|
165
|
+
* @returns True if value is a function
|
|
166
|
+
*
|
|
167
|
+
* @example Basic usage
|
|
168
|
+
* ```typescript
|
|
169
|
+
* isFunction(() => {}); // true
|
|
170
|
+
* isFunction(function() {}); // true
|
|
171
|
+
* isFunction(async () => {}); // true
|
|
172
|
+
* ```
|
|
173
|
+
*
|
|
174
|
+
* @example Non-functions
|
|
175
|
+
* ```typescript
|
|
176
|
+
* isFunction({}); // false
|
|
177
|
+
* isFunction(null); // false
|
|
178
|
+
* isFunction(undefined); // false
|
|
179
|
+
* isFunction('string'); // false
|
|
180
|
+
* ```
|
|
181
|
+
*/
|
|
182
|
+
declare function isFunction(value: unknown): value is (...args: any[]) => any;
|
|
183
|
+
/**
|
|
184
|
+
* Type guard for valid Date objects.
|
|
185
|
+
*
|
|
186
|
+
* Returns true for Date objects with valid time values.
|
|
187
|
+
*
|
|
188
|
+
* @param value - Value to check
|
|
189
|
+
* @returns True if value is a valid Date
|
|
190
|
+
*
|
|
191
|
+
* @example Valid dates
|
|
192
|
+
* ```typescript
|
|
193
|
+
* isDate(new Date()); // true
|
|
194
|
+
* isDate(new Date('2024-01-01')); // true
|
|
195
|
+
* ```
|
|
196
|
+
*
|
|
197
|
+
* @example Invalid dates
|
|
198
|
+
* ```typescript
|
|
199
|
+
* isDate(new Date('invalid')); // false
|
|
200
|
+
* isDate(Date.now()); // false (number)
|
|
201
|
+
* ```
|
|
202
|
+
*
|
|
203
|
+
* @example Non-dates
|
|
204
|
+
* ```typescript
|
|
205
|
+
* isDate({}); // false
|
|
206
|
+
* isDate(null); // false
|
|
207
|
+
* isDate(undefined); // false
|
|
208
|
+
* isDate('2024-01-01'); // false (string)
|
|
209
|
+
* ```
|
|
210
|
+
*/
|
|
211
|
+
declare function isDate(value: unknown): value is Date;
|
|
212
|
+
|
|
213
|
+
export { isArray, isDate, isEmpty, isFunction, isNil, isNumber, isPlainObject, isString };
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
// src/plugins/optional/guard.ts
|
|
2
|
+
function isEmpty(value) {
|
|
3
|
+
if (Array.isArray(value)) return value.length === 0;
|
|
4
|
+
if (value instanceof Map) return value.size === 0;
|
|
5
|
+
if (value instanceof Set) return value.size === 0;
|
|
6
|
+
if (value instanceof Date || value instanceof RegExp) return false;
|
|
7
|
+
if (typeof value === "object" && value !== null && !Array.isArray(value) && !(value instanceof Map) && !(value instanceof Set)) {
|
|
8
|
+
return Object.keys(value).length === 0;
|
|
9
|
+
}
|
|
10
|
+
return false;
|
|
11
|
+
}
|
|
12
|
+
function isNil(value) {
|
|
13
|
+
return value === null || value === void 0;
|
|
14
|
+
}
|
|
15
|
+
function isPlainObject(value) {
|
|
16
|
+
if (typeof value !== "object" || value === null) return false;
|
|
17
|
+
const proto = Object.getPrototypeOf(value);
|
|
18
|
+
return proto === null || proto === Object.prototype;
|
|
19
|
+
}
|
|
20
|
+
function isArray(value) {
|
|
21
|
+
return Array.isArray(value);
|
|
22
|
+
}
|
|
23
|
+
function isString(value) {
|
|
24
|
+
return typeof value === "string";
|
|
25
|
+
}
|
|
26
|
+
function isNumber(value) {
|
|
27
|
+
return typeof value === "number" && !Number.isNaN(value);
|
|
28
|
+
}
|
|
29
|
+
function isFunction(value) {
|
|
30
|
+
return typeof value === "function";
|
|
31
|
+
}
|
|
32
|
+
function isDate(value) {
|
|
33
|
+
return value instanceof Date && !Number.isNaN(value.getTime());
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export { isArray, isDate, isEmpty, isFunction, isNil, isNumber, isPlainObject, isString };
|
|
37
|
+
//# sourceMappingURL=guard.js.map
|
|
38
|
+
//# sourceMappingURL=guard.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/plugins/optional/guard.ts"],"names":[],"mappings":";AAuCO,SAAS,QACd,KAAA,EAKa;AACb,EAAA,IAAI,MAAM,OAAA,CAAQ,KAAK,CAAA,EAAG,OAAO,MAAM,MAAA,KAAW,CAAA;AAClD,EAAA,IAAI,KAAA,YAAiB,GAAA,EAAK,OAAO,KAAA,CAAM,IAAA,KAAS,CAAA;AAChD,EAAA,IAAI,KAAA,YAAiB,GAAA,EAAK,OAAO,KAAA,CAAM,IAAA,KAAS,CAAA;AAEhD,EAAA,IAAI,KAAA,YAAiB,IAAA,IAAQ,KAAA,YAAiB,MAAA,EAAQ,OAAO,KAAA;AAC7D,EAAA,IACE,OAAO,KAAA,KAAU,QAAA,IACjB,KAAA,KAAU,QACV,CAAC,KAAA,CAAM,OAAA,CAAQ,KAAK,KACpB,EAAE,KAAA,YAAiB,GAAA,CAAA,IACnB,EAAE,iBAAiB,GAAA,CAAA,EACnB;AACA,IAAA,OAAO,MAAA,CAAO,IAAA,CAAK,KAAK,CAAA,CAAE,MAAA,KAAW,CAAA;AAAA,EACvC;AACA,EAAA,OAAO,KAAA;AACT;AAyBO,SAAS,MAAM,KAAA,EAA2C;AAC/D,EAAA,OAAO,KAAA,KAAU,QAAQ,KAAA,KAAU,MAAA;AACrC;AAoCO,SAAS,cAAc,KAAA,EAAkD;AAC9E,EAAA,IAAI,OAAO,KAAA,KAAU,QAAA,IAAY,KAAA,KAAU,MAAM,OAAO,KAAA;AAExD,EAAA,MAAM,KAAA,GAAQ,MAAA,CAAO,cAAA,CAAe,KAAK,CAAA;AACzC,EAAA,OAAO,KAAA,KAAU,IAAA,IAAQ,KAAA,KAAU,MAAA,CAAO,SAAA;AAC5C;AAgBO,SAAS,QAAQ,KAAA,EAAgC;AACtD,EAAA,OAAO,KAAA,CAAM,QAAQ,KAAK,CAAA;AAC5B;AAgBO,SAAS,SAAS,KAAA,EAAiC;AACxD,EAAA,OAAO,OAAO,KAAA,KAAU,QAAA;AAC1B;AAgCO,SAAS,SAAS,KAAA,EAAiC;AACxD,EAAA,OAAO,OAAO,KAAA,KAAU,QAAA,IAAY,CAAC,MAAA,CAAO,MAAM,KAAK,CAAA;AACzD;AAuBO,SAAS,WAAW,KAAA,EAAkD;AAC3E,EAAA,OAAO,OAAO,KAAA,KAAU,UAAA;AAC1B;AA8BO,SAAS,OAAO,KAAA,EAA+B;AACpD,EAAA,OAAO,iBAAiB,IAAA,IAAQ,CAAC,OAAO,KAAA,CAAM,KAAA,CAAM,SAAS,CAAA;AAC/D","file":"guard.js","sourcesContent":["/**\n * Check if collection is empty.\n *\n * Returns true for empty arrays, objects, Maps, or Sets.\n * Note: Unlike lodash, this returns false for strings and numbers!\n *\n * @param value - Value to check\n * @returns True if collection is empty\n *\n * @example Arrays\n * ```typescript\n * isEmpty([]); // true\n * isEmpty([1, 2, 3]); // false\n * ```\n *\n * @example Objects\n * ```typescript\n * isEmpty({}); // true\n * isEmpty({ a: 1 }); // false\n * ```\n *\n * @example Map and Set\n * ```typescript\n * isEmpty(new Map()); // true\n * isEmpty(new Set()); // true\n * isEmpty(new Map([['a', 1]])); // false\n * ```\n *\n * @example Non-collections (returns false)\n * ```typescript\n * isEmpty(''); // false - strings are not collections\n * isEmpty(0); // false - numbers are not collections\n * isEmpty(false); // false - booleans are not collections\n * isEmpty(null); // false - null is not a collection\n * isEmpty(undefined); // false - undefined is not a collection\n * ```\n *\n * @see {@link isNil} for null/undefined check\n */\nexport function isEmpty(\n value: unknown\n): value is\n | []\n | Record<string, never>\n | Map<any, never>\n | Set<never> {\n if (Array.isArray(value)) return value.length === 0;\n if (value instanceof Map) return value.size === 0;\n if (value instanceof Set) return value.size === 0;\n // Exclude Date, RegExp, and other built-in objects from \"empty object\" check\n if (value instanceof Date || value instanceof RegExp) return false;\n if (\n typeof value === 'object' &&\n value !== null &&\n !Array.isArray(value) &&\n !(value instanceof Map) &&\n !(value instanceof Set)\n ) {\n return Object.keys(value).length === 0;\n }\n return false;\n}\n\n/**\n * Check if value is null or undefined.\n *\n * @param value - Value to check\n * @returns True if value is null or undefined\n *\n * @example Basic usage\n * ```typescript\n * isNil(null); // true\n * isNil(undefined); // true\n * ```\n *\n * @example False cases\n * ```typescript\n * isNil(0); // false\n * isNil(''); // false\n * isNil(false); // false\n * isNil([]); // false\n * isNil({}); // false\n * ```\n *\n * @see {@link isEmpty} for collection emptiness check\n */\nexport function isNil(value: unknown): value is null | undefined {\n return value === null || value === undefined;\n}\n\n/**\n * Check if value is a plain object.\n *\n * Returns true for plain objects created by object literal or Object.create(null).\n * Returns false for class instances, arrays, Date, RegExp, etc.\n *\n * @param value - Value to check\n * @returns True if value is a plain object\n *\n * @example Plain objects\n * ```typescript\n * isPlainObject({}); // true\n * isPlainObject({ a: 1 }); // true\n * isPlainObject(Object.create(null)); // true\n * ```\n *\n * @example Non-plain objects\n * ```typescript\n * isPlainObject(new Date()); // false (class instance)\n * isPlainObject([]); // false (array)\n * isPlainObject(/test/); // false (RegExp)\n * isPlainObject(new Map()); // false (Map)\n * isPlainObject(new Set()); // false (Set)\n * isPlainObject(() => {}); // false (function)\n * isPlainObject(null); // false\n * isPlainObject(undefined); // false\n * ```\n *\n * @example Class instances\n * ```typescript\n * class MyClass {}\n * isPlainObject(new MyClass()); // false\n * ```\n */\nexport function isPlainObject(value: unknown): value is Record<string, unknown> {\n if (typeof value !== 'object' || value === null) return false;\n\n const proto = Object.getPrototypeOf(value);\n return proto === null || proto === Object.prototype;\n}\n\n/**\n * Type guard for arrays.\n *\n * @param value - Value to check\n * @returns True if value is an array\n *\n * @example Basic usage\n * ```typescript\n * const value: unknown = [1, 2, 3];\n * if (isArray(value)) {\n * value.map(x => x * 2); // TypeScript knows it's an array\n * }\n * ```\n */\nexport function isArray(value: unknown): value is any[] {\n return Array.isArray(value);\n}\n\n/**\n * Type guard for strings.\n *\n * @param value - Value to check\n * @returns True if value is a string\n *\n * @example Basic usage\n * ```typescript\n * const value: unknown = 'hello';\n * if (isString(value)) {\n * value.toUpperCase(); // TypeScript knows it's a string\n * }\n * ```\n */\nexport function isString(value: unknown): value is string {\n return typeof value === 'string';\n}\n\n/**\n * Type guard for numbers.\n *\n * Returns false for NaN (unlike typeof operator).\n *\n * @param value - Value to check\n * @returns True if value is a valid number\n *\n * @example Basic usage\n * ```typescript\n * isNumber(42); // true\n * isNumber(3.14); // true\n * isNumber(-5); // true\n * isNumber(Infinity); // true\n * ```\n *\n * @example NaN handling\n * ```typescript\n * isNumber(NaN); // false (unlike typeof!)\n * typeof NaN === 'number'; // true\n * ```\n *\n * @example Non-numbers\n * ```typescript\n * isNumber('42'); // false\n * isNumber(null); // false\n * isNumber(undefined); // false\n * isNumber({}); // false\n * ```\n */\nexport function isNumber(value: unknown): value is number {\n return typeof value === 'number' && !Number.isNaN(value);\n}\n\n/**\n * Type guard for functions.\n *\n * @param value - Value to check\n * @returns True if value is a function\n *\n * @example Basic usage\n * ```typescript\n * isFunction(() => {}); // true\n * isFunction(function() {}); // true\n * isFunction(async () => {}); // true\n * ```\n *\n * @example Non-functions\n * ```typescript\n * isFunction({}); // false\n * isFunction(null); // false\n * isFunction(undefined); // false\n * isFunction('string'); // false\n * ```\n */\nexport function isFunction(value: unknown): value is (...args: any[]) => any {\n return typeof value === 'function';\n}\n\n/**\n * Type guard for valid Date objects.\n *\n * Returns true for Date objects with valid time values.\n *\n * @param value - Value to check\n * @returns True if value is a valid Date\n *\n * @example Valid dates\n * ```typescript\n * isDate(new Date()); // true\n * isDate(new Date('2024-01-01')); // true\n * ```\n *\n * @example Invalid dates\n * ```typescript\n * isDate(new Date('invalid')); // false\n * isDate(Date.now()); // false (number)\n * ```\n *\n * @example Non-dates\n * ```typescript\n * isDate({}); // false\n * isDate(null); // false\n * isDate(undefined); // false\n * isDate('2024-01-01'); // false (string)\n * ```\n */\nexport function isDate(value: unknown): value is Date {\n return value instanceof Date && !Number.isNaN(value.getTime());\n}\n"]}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/plugins/optional/transform.ts
|
|
4
|
+
function mapKeys(obj, fn) {
|
|
5
|
+
const result = {};
|
|
6
|
+
for (const key of Object.keys(obj)) {
|
|
7
|
+
const newKey = fn(key, obj[key]);
|
|
8
|
+
result[newKey] = obj[key];
|
|
9
|
+
}
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
12
|
+
function mapValues(obj, fn) {
|
|
13
|
+
const result = {};
|
|
14
|
+
for (const key of Object.keys(obj)) {
|
|
15
|
+
result[key] = fn(obj[key], key);
|
|
16
|
+
}
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
function invert(obj) {
|
|
20
|
+
const result = {};
|
|
21
|
+
for (const key of Object.keys(obj)) {
|
|
22
|
+
result[String(obj[key])] = key;
|
|
23
|
+
}
|
|
24
|
+
return result;
|
|
25
|
+
}
|
|
26
|
+
function flip(fn) {
|
|
27
|
+
return ((...args) => {
|
|
28
|
+
return fn(...args.reverse());
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
function compose(...fns) {
|
|
32
|
+
return (arg) => {
|
|
33
|
+
return fns.reduceRight((acc, fn) => fn(acc), arg);
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function pipe(...fns) {
|
|
37
|
+
return (arg) => {
|
|
38
|
+
return fns.reduce((acc, fn) => fn(acc), arg);
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
exports.compose = compose;
|
|
43
|
+
exports.flip = flip;
|
|
44
|
+
exports.invert = invert;
|
|
45
|
+
exports.mapKeys = mapKeys;
|
|
46
|
+
exports.mapValues = mapValues;
|
|
47
|
+
exports.pipe = pipe;
|
|
48
|
+
//# sourceMappingURL=transform.cjs.map
|
|
49
|
+
//# sourceMappingURL=transform.cjs.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.cjs","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"]}
|