@bedrock-ui/utils
Advanced tools
Comparing version 0.4.2 to 0.4.3
import type { TypeofArray } from '../../types/array'; | ||
export declare function countBy<T extends TypeofArray<T>[], P extends keyof TypeofArray<T>>(array: T, property: P): Map<TypeofArray<T>[P], number>; | ||
type FnParam<T extends TypeofArray<T>[]> = (element: TypeofArray<T>) => unknown; | ||
type ArrayParam<T> = keyof TypeofArray<T>; | ||
declare function countBy<T extends TypeofArray<T>[], P extends FnParam<T>>(array: T, property: P): Map<ReturnType<P>, number>; | ||
declare function countBy<T extends TypeofArray<T>[], P extends ArrayParam<T>>(array: T, property: P): Map<TypeofArray<T>[P], number>; | ||
export { countBy }; |
@@ -7,3 +7,10 @@ "use strict"; | ||
array.forEach((element) => { | ||
map.set(element[property], (map.get(element[property]) || 0) + 1); | ||
if (typeof property === 'function') { | ||
const value = property(element); | ||
map.set(value, (map.get(value) || 0) + 1); | ||
} | ||
else { | ||
const value = property; | ||
map.set(element[value], (map.get(element[value]) || 0) + 1); | ||
} | ||
}); | ||
@@ -10,0 +17,0 @@ return map; |
import type { TypeofArray } from '../../types/array.js'; | ||
export declare function countBy<T extends TypeofArray<T>[], P extends keyof TypeofArray<T>>(array: T, property: P): Map<TypeofArray<T>[P], number>; | ||
type FnParam<T extends TypeofArray<T>[]> = (element: TypeofArray<T>) => unknown; | ||
type ArrayParam<T> = keyof TypeofArray<T>; | ||
declare function countBy<T extends TypeofArray<T>[], P extends FnParam<T>>(array: T, property: P): Map<ReturnType<P>, number>; | ||
declare function countBy<T extends TypeofArray<T>[], P extends ArrayParam<T>>(array: T, property: P): Map<TypeofArray<T>[P], number>; | ||
export { countBy }; |
@@ -1,7 +0,15 @@ | ||
export function countBy(array, property) { | ||
function countBy(array, property) { | ||
const map = new Map(); | ||
array.forEach((element) => { | ||
map.set(element[property], (map.get(element[property]) || 0) + 1); | ||
if (typeof property === 'function') { | ||
const value = property(element); | ||
map.set(value, (map.get(value) || 0) + 1); | ||
} | ||
else { | ||
const value = property; | ||
map.set(element[value], (map.get(element[value]) || 0) + 1); | ||
} | ||
}); | ||
return map; | ||
} | ||
export { countBy }; |
{ | ||
"name": "@bedrock-ui/utils", | ||
"version": "0.4.2", | ||
"version": "0.4.3", | ||
"description": "Bedrock UI Utils", | ||
@@ -5,0 +5,0 @@ "author": "Matthew Wolfe", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17879
394