@zelgadis87/utils-core 5.0.4 → 5.1.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/tsconfig.tsbuildinfo +1 -1
- package/dist/utils/arrays.d.ts +3 -1
- package/esbuild/index.cjs +24 -4
- package/esbuild/index.cjs.map +3 -3
- package/esbuild/index.mjs +21 -3
- package/esbuild/index.mjs.map +3 -3
- package/package.json +1 -1
- package/src/utils/arrays.ts +24 -6
package/package.json
CHANGED
package/src/utils/arrays.ts
CHANGED
|
@@ -133,14 +133,32 @@ export function partition<T>( arr: T[], predicate: TPredicate<T> ): [ T[], T[] ]
|
|
|
133
133
|
* @param mapFn - The mapping function.
|
|
134
134
|
* @returns The first truthy value returned by the mapping function, or null if no truthy value is found.
|
|
135
135
|
*/
|
|
136
|
-
export function mapFirstTruthy<T, R>(arr: T[], mapFn: TTransformer<T, R>): R | null {
|
|
137
|
-
for (let i = 0; i < arr.length; i++) {
|
|
138
|
-
const result = mapFn(arr[i]);
|
|
139
|
-
if (result) return result;
|
|
136
|
+
export function mapFirstTruthy<T, R>( arr: T[], mapFn: TTransformer<T, R> ): R | null {
|
|
137
|
+
for ( let i = 0; i < arr.length; i++ ) {
|
|
138
|
+
const result = mapFn( arr[ i ] );
|
|
139
|
+
if ( result ) return result;
|
|
140
140
|
}
|
|
141
141
|
return null;
|
|
142
142
|
}
|
|
143
143
|
|
|
144
|
-
export function
|
|
145
|
-
arr.
|
|
144
|
+
export function listToDict<T, K extends string, V>( arr: T[], mapFn: TTransformer<T, [ K, V ]> ): Record<K, V> {
|
|
145
|
+
return arr.reduce( ( dict: Partial<Record<K, V>>, cur: T ) => {
|
|
146
|
+
const [ key, value ] = mapFn( cur );
|
|
147
|
+
return { ...dict, [ key ]: value };
|
|
148
|
+
}, {} satisfies Partial<Record<K, V>> );
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function countArrayBy<T, K extends string>( arr: T[], mapFn: TTransformer<T, K> ): Partial<Record<K, number>> {
|
|
152
|
+
return arr.reduce( ( dict: Partial<Record<K, number>>, cur: T ) => {
|
|
153
|
+
const value = mapFn( cur );
|
|
154
|
+
const count = ( dict[ value ] ?? 0 ) + 1;
|
|
155
|
+
return { ...dict, [ value ]: count };
|
|
156
|
+
}, {} satisfies Partial<Record<K, number>> );
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export function sumArrayBy<T>( arr: T[], mapFn: TTransformer<T, number> ): number {
|
|
160
|
+
return arr.reduce( ( sum: number, cur: T ) => {
|
|
161
|
+
const value = mapFn( cur );
|
|
162
|
+
return sum + value;
|
|
163
|
+
}, 0 );
|
|
146
164
|
}
|