ansuko 1.2.13 → 1.3.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/dist/index.d.ts +49 -1
- package/dist/index.js +79 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -134,6 +134,52 @@ declare const castArray: <T>(value: T | T[] | null | undefined) => T[];
|
|
|
134
134
|
* @category Object Utilities
|
|
135
135
|
*/
|
|
136
136
|
declare const changes: <T extends Record<string, any>, E extends Record<string, any>>(sourceValue: T, currentValue: E, keys: string[], options?: ChangesOptions, finallyCallback?: ChangesAfterFinallyCallback<Record<string, any>>, notEmptyCallback?: ChangesAfterCallback<Record<string, any>>) => Record<string, any>;
|
|
137
|
+
/**
|
|
138
|
+
* Executes a function and returns undefined if an error occurs.
|
|
139
|
+
* For functions returning a Promise, returns undefined if the Promise is rejected.
|
|
140
|
+
*
|
|
141
|
+
* @template T - The return type of the function
|
|
142
|
+
* @param fn - The function to execute
|
|
143
|
+
* @returns The result of the function execution, or undefined on error
|
|
144
|
+
*
|
|
145
|
+
* @example
|
|
146
|
+
* // Synchronous function
|
|
147
|
+
* swallow(() => data.remove() )
|
|
148
|
+
* // => undefined (error ignored)
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* // Asynchronous function
|
|
152
|
+
* const data = await swallow(async () => await fetchData());
|
|
153
|
+
* // => data or undefined
|
|
154
|
+
*/
|
|
155
|
+
declare const swallow: <T>(fn: () => T) => T extends Promise<infer U> ? Promise<U | undefined> : T | undefined;
|
|
156
|
+
/**
|
|
157
|
+
* Maps over an array, treating errors as undefined.
|
|
158
|
+
* When compact is true, filters out undefined results (errors).
|
|
159
|
+
*
|
|
160
|
+
* @template T - The array element type
|
|
161
|
+
* @template U - The function return type
|
|
162
|
+
* @param array - The array to process
|
|
163
|
+
* @param fn - The function to apply to each element
|
|
164
|
+
* @param compact - If true, filters out undefined results (errors) from the output
|
|
165
|
+
* @returns Array of results, or Promise of results for async functions
|
|
166
|
+
*
|
|
167
|
+
* @example
|
|
168
|
+
* // Keep errors as undefined
|
|
169
|
+
* const results = swallowMap(items, item => processItem(item));
|
|
170
|
+
* // => [result1, undefined, result3, ...]
|
|
171
|
+
*
|
|
172
|
+
* @example
|
|
173
|
+
* // Filter out errors (compact)
|
|
174
|
+
* const results = swallowMap(items, item => processItem(item), true);
|
|
175
|
+
* // => [result1, result3, ...]
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* // Async processing
|
|
179
|
+
* const data = await swallowMap(urls, async url => await fetch(url), true);
|
|
180
|
+
* // => array of successful responses only
|
|
181
|
+
*/
|
|
182
|
+
declare const swallowMap: <T, U>(array: T[] | undefined | null, fn: (item: T, index: number) => U, compact?: boolean) => U extends Promise<infer V> ? Promise<V[]> : U[];
|
|
137
183
|
/**
|
|
138
184
|
* Returns nesting depth of arrays. Non-array: 0; empty array: 1. Uses minimum depth for mixed nesting.
|
|
139
185
|
* @param ary - Array
|
|
@@ -175,6 +221,8 @@ export interface AnsukoType extends Omit<_.LoDashStatic, "castArray" | "isEmpty"
|
|
|
175
221
|
jsonStringify: typeof jsonStringify;
|
|
176
222
|
castArray: typeof castArray;
|
|
177
223
|
changes: typeof changes;
|
|
224
|
+
swallow: typeof swallow;
|
|
225
|
+
swallowMap: typeof swallowMap;
|
|
178
226
|
size: typeof _.size;
|
|
179
227
|
isNil: typeof _.isNil;
|
|
180
228
|
debounce: typeof _.debounce;
|
|
@@ -195,4 +243,4 @@ export interface AnsukoType extends Omit<_.LoDashStatic, "castArray" | "isEmpty"
|
|
|
195
243
|
}
|
|
196
244
|
declare const _default: AnsukoType;
|
|
197
245
|
export default _default;
|
|
198
|
-
export { isEmpty, toNumber, boolIf, isValidStr, valueOr, equalsOr, waited, parseJSON, jsonStringify, castArray, changes, arrayDepth, };
|
|
246
|
+
export { isEmpty, toNumber, boolIf, isValidStr, valueOr, equalsOr, waited, parseJSON, jsonStringify, castArray, changes, swallow, swallowMap, arrayDepth, };
|
package/dist/index.js
CHANGED
|
@@ -447,6 +447,82 @@ const changes = (sourceValue, currentValue, keys, options, finallyCallback, notE
|
|
|
447
447
|
}
|
|
448
448
|
return diff;
|
|
449
449
|
};
|
|
450
|
+
/**
|
|
451
|
+
* Executes a function and returns undefined if an error occurs.
|
|
452
|
+
* For functions returning a Promise, returns undefined if the Promise is rejected.
|
|
453
|
+
*
|
|
454
|
+
* @template T - The return type of the function
|
|
455
|
+
* @param fn - The function to execute
|
|
456
|
+
* @returns The result of the function execution, or undefined on error
|
|
457
|
+
*
|
|
458
|
+
* @example
|
|
459
|
+
* // Synchronous function
|
|
460
|
+
* swallow(() => data.remove() )
|
|
461
|
+
* // => undefined (error ignored)
|
|
462
|
+
*
|
|
463
|
+
* @example
|
|
464
|
+
* // Asynchronous function
|
|
465
|
+
* const data = await swallow(async () => await fetchData());
|
|
466
|
+
* // => data or undefined
|
|
467
|
+
*/
|
|
468
|
+
const swallow = (fn) => {
|
|
469
|
+
try {
|
|
470
|
+
const result = fn();
|
|
471
|
+
if (result instanceof Promise) {
|
|
472
|
+
return result.catch(() => undefined);
|
|
473
|
+
}
|
|
474
|
+
return result;
|
|
475
|
+
}
|
|
476
|
+
catch {
|
|
477
|
+
return undefined;
|
|
478
|
+
}
|
|
479
|
+
};
|
|
480
|
+
/**
|
|
481
|
+
* Maps over an array, treating errors as undefined.
|
|
482
|
+
* When compact is true, filters out undefined results (errors).
|
|
483
|
+
*
|
|
484
|
+
* @template T - The array element type
|
|
485
|
+
* @template U - The function return type
|
|
486
|
+
* @param array - The array to process
|
|
487
|
+
* @param fn - The function to apply to each element
|
|
488
|
+
* @param compact - If true, filters out undefined results (errors) from the output
|
|
489
|
+
* @returns Array of results, or Promise of results for async functions
|
|
490
|
+
*
|
|
491
|
+
* @example
|
|
492
|
+
* // Keep errors as undefined
|
|
493
|
+
* const results = swallowMap(items, item => processItem(item));
|
|
494
|
+
* // => [result1, undefined, result3, ...]
|
|
495
|
+
*
|
|
496
|
+
* @example
|
|
497
|
+
* // Filter out errors (compact)
|
|
498
|
+
* const results = swallowMap(items, item => processItem(item), true);
|
|
499
|
+
* // => [result1, result3, ...]
|
|
500
|
+
*
|
|
501
|
+
* @example
|
|
502
|
+
* // Async processing
|
|
503
|
+
* const data = await swallowMap(urls, async url => await fetch(url), true);
|
|
504
|
+
* // => array of successful responses only
|
|
505
|
+
*/
|
|
506
|
+
const swallowMap = (array, fn, compact) => {
|
|
507
|
+
if (!array)
|
|
508
|
+
return [];
|
|
509
|
+
const results = array.map((item, index) => {
|
|
510
|
+
try {
|
|
511
|
+
const result = fn(item, index);
|
|
512
|
+
if (result instanceof Promise) {
|
|
513
|
+
return result.catch(() => undefined);
|
|
514
|
+
}
|
|
515
|
+
return result;
|
|
516
|
+
}
|
|
517
|
+
catch {
|
|
518
|
+
return undefined;
|
|
519
|
+
}
|
|
520
|
+
});
|
|
521
|
+
if (results.some(r => r instanceof Promise)) {
|
|
522
|
+
return Promise.all(results).then(resolved => compact ? resolved.filter(Boolean) : resolved);
|
|
523
|
+
}
|
|
524
|
+
return (compact ? results.filter(Boolean) : results);
|
|
525
|
+
};
|
|
450
526
|
/**
|
|
451
527
|
* Returns nesting depth of arrays. Non-array: 0; empty array: 1. Uses minimum depth for mixed nesting.
|
|
452
528
|
* @param ary - Array
|
|
@@ -502,7 +578,9 @@ export default {
|
|
|
502
578
|
jsonStringify,
|
|
503
579
|
castArray,
|
|
504
580
|
changes,
|
|
581
|
+
swallow,
|
|
582
|
+
swallowMap,
|
|
505
583
|
arrayDepth,
|
|
506
584
|
};
|
|
507
585
|
// 個別エクスポートはそのまま
|
|
508
|
-
export { isEmpty, toNumber, boolIf, isValidStr, valueOr, equalsOr, waited, parseJSON, jsonStringify, castArray, changes, arrayDepth, };
|
|
586
|
+
export { isEmpty, toNumber, boolIf, isValidStr, valueOr, equalsOr, waited, parseJSON, jsonStringify, castArray, changes, swallow, swallowMap, arrayDepth, };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ansuko",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "A modern JavaScript/TypeScript utility library that extends lodash with practical, intuitive behaviors. Fixes lodash quirks, adds Promise support, Japanese text processing, and GeoJSON utilities.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"lodash",
|