@naturalcycles/js-lib 15.59.0 → 15.60.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/array/array.util.d.ts +14 -0
- package/dist/array/array.util.js +24 -0
- package/dist/types.d.ts +2 -2
- package/package.json +1 -1
- package/src/array/array.util.ts +27 -0
- package/src/types.ts +4 -2
|
@@ -158,6 +158,20 @@ export declare function _intersectsWith<T>(a1: T[], a2: T[] | Set<T>): boolean;
|
|
|
158
158
|
* Passing second array as Set is more performant (it'll skip turning the array into Set in-place).
|
|
159
159
|
*/
|
|
160
160
|
export declare function _difference<T>(a1: T[], a2: T[] | Set<T>): T[];
|
|
161
|
+
/**
|
|
162
|
+
* Does NOT mutate the array, returns a filtered array instead.
|
|
163
|
+
*/
|
|
164
|
+
export declare function _arrayRemove<T>(a: T[], itemToRemove: T): T[];
|
|
165
|
+
/**
|
|
166
|
+
* "Toggles" an item to be present or absent in the array,
|
|
167
|
+
* based on the predicate. Respects uniqueness.
|
|
168
|
+
*
|
|
169
|
+
* If predicate==false - item gets removed from the array.
|
|
170
|
+
* If predicate==true - item gets pushed to the array (unless it was already present).
|
|
171
|
+
*
|
|
172
|
+
* Pushing the item DOES MUTATE the array, same if you would do array.push manually.
|
|
173
|
+
*/
|
|
174
|
+
export declare function _arrayPushOrRemove<T>(a: T[], item: T, predicate: boolean): T[];
|
|
161
175
|
/**
|
|
162
176
|
* Returns the sum of items, or 0 for empty array.
|
|
163
177
|
*/
|
package/dist/array/array.util.js
CHANGED
|
@@ -282,6 +282,30 @@ export function _difference(a1, a2) {
|
|
|
282
282
|
const a2set = a2 instanceof Set ? a2 : new Set(a2);
|
|
283
283
|
return a1.filter(v => !a2set.has(v));
|
|
284
284
|
}
|
|
285
|
+
/**
|
|
286
|
+
* Does NOT mutate the array, returns a filtered array instead.
|
|
287
|
+
*/
|
|
288
|
+
export function _arrayRemove(a, itemToRemove) {
|
|
289
|
+
return a.filter(r => r !== itemToRemove);
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* "Toggles" an item to be present or absent in the array,
|
|
293
|
+
* based on the predicate. Respects uniqueness.
|
|
294
|
+
*
|
|
295
|
+
* If predicate==false - item gets removed from the array.
|
|
296
|
+
* If predicate==true - item gets pushed to the array (unless it was already present).
|
|
297
|
+
*
|
|
298
|
+
* Pushing the item DOES MUTATE the array, same if you would do array.push manually.
|
|
299
|
+
*/
|
|
300
|
+
export function _arrayPushOrRemove(a, item, predicate) {
|
|
301
|
+
if (predicate) {
|
|
302
|
+
if (!a.includes(item)) {
|
|
303
|
+
a.push(item);
|
|
304
|
+
}
|
|
305
|
+
return a;
|
|
306
|
+
}
|
|
307
|
+
return a.filter(r => r !== item);
|
|
308
|
+
}
|
|
285
309
|
/**
|
|
286
310
|
* Returns the sum of items, or 0 for empty array.
|
|
287
311
|
*/
|
package/dist/types.d.ts
CHANGED
|
@@ -291,14 +291,14 @@ export declare const _stringMapEntries: <T>(map: StringMap<T>) => [k: string, v:
|
|
|
291
291
|
* Alias of `Object.keys`, but returns keys typed as `keyof T`, not as just `string`.
|
|
292
292
|
* This is how TypeScript should work, actually.
|
|
293
293
|
*/
|
|
294
|
-
export declare const _objectKeys: <K extends PropertyKey>(obj: Record<K, any
|
|
294
|
+
export declare const _objectKeys: <K extends PropertyKey>(obj: Partial<Record<K, any>>) => K[];
|
|
295
295
|
/**
|
|
296
296
|
* Alias of `Object.entries`, but returns better-typed output.
|
|
297
297
|
*
|
|
298
298
|
* So e.g you can use _objectEntries(obj).map([k, v] => {})
|
|
299
299
|
* and `k` will be `keyof obj` instead of generic `string`.
|
|
300
300
|
*/
|
|
301
|
-
export declare const _objectEntries: <K extends PropertyKey, V>(obj: Record<K, V
|
|
301
|
+
export declare const _objectEntries: <K extends PropertyKey, V>(obj: Partial<Record<K, V>>) => [k: K, v: V][];
|
|
302
302
|
export type NullishValue = null | undefined;
|
|
303
303
|
export type FalsyValue = false | '' | 0 | null | undefined;
|
|
304
304
|
/**
|
package/package.json
CHANGED
package/src/array/array.util.ts
CHANGED
|
@@ -326,6 +326,33 @@ export function _difference<T>(a1: T[], a2: T[] | Set<T>): T[] {
|
|
|
326
326
|
return a1.filter(v => !a2set.has(v))
|
|
327
327
|
}
|
|
328
328
|
|
|
329
|
+
/**
|
|
330
|
+
* Does NOT mutate the array, returns a filtered array instead.
|
|
331
|
+
*/
|
|
332
|
+
export function _arrayRemove<T>(a: T[], itemToRemove: T): T[] {
|
|
333
|
+
return a.filter(r => r !== itemToRemove)
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* "Toggles" an item to be present or absent in the array,
|
|
338
|
+
* based on the predicate. Respects uniqueness.
|
|
339
|
+
*
|
|
340
|
+
* If predicate==false - item gets removed from the array.
|
|
341
|
+
* If predicate==true - item gets pushed to the array (unless it was already present).
|
|
342
|
+
*
|
|
343
|
+
* Pushing the item DOES MUTATE the array, same if you would do array.push manually.
|
|
344
|
+
*/
|
|
345
|
+
export function _arrayPushOrRemove<T>(a: T[], item: T, predicate: boolean): T[] {
|
|
346
|
+
if (predicate) {
|
|
347
|
+
if (!a.includes(item)) {
|
|
348
|
+
a.push(item)
|
|
349
|
+
}
|
|
350
|
+
return a
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
return a.filter(r => r !== item)
|
|
354
|
+
}
|
|
355
|
+
|
|
329
356
|
/**
|
|
330
357
|
* Returns the sum of items, or 0 for empty array.
|
|
331
358
|
*/
|
package/src/types.ts
CHANGED
|
@@ -364,7 +364,9 @@ export const _stringMapEntries = Object.entries as <T>(map: StringMap<T>) => [k:
|
|
|
364
364
|
* Alias of `Object.keys`, but returns keys typed as `keyof T`, not as just `string`.
|
|
365
365
|
* This is how TypeScript should work, actually.
|
|
366
366
|
*/
|
|
367
|
-
export const _objectKeys = Object.keys as <K extends PropertyKey>(
|
|
367
|
+
export const _objectKeys = Object.keys as <K extends PropertyKey>(
|
|
368
|
+
obj: Partial<Record<K, any>>,
|
|
369
|
+
) => K[]
|
|
368
370
|
|
|
369
371
|
/**
|
|
370
372
|
* Alias of `Object.entries`, but returns better-typed output.
|
|
@@ -373,7 +375,7 @@ export const _objectKeys = Object.keys as <K extends PropertyKey>(obj: Record<K,
|
|
|
373
375
|
* and `k` will be `keyof obj` instead of generic `string`.
|
|
374
376
|
*/
|
|
375
377
|
export const _objectEntries = Object.entries as <K extends PropertyKey, V>(
|
|
376
|
-
obj: Record<K, V
|
|
378
|
+
obj: Partial<Record<K, V>>,
|
|
377
379
|
) => [k: K, v: V][]
|
|
378
380
|
|
|
379
381
|
export type NullishValue = null | undefined
|