@bedrock-ui/utils
Advanced tools
Comparing version 0.0.10 to 0.1.0
@@ -8,3 +8,3 @@ "use strict"; | ||
if (map.has(element[property])) { | ||
console.warn(`duplicate key for keyBy: "${element[property]}" already exists in the map`); | ||
console.warn(`keyBy - duplicate key: "${element[property]}" already exists in the map`); | ||
} | ||
@@ -11,0 +11,0 @@ map.set(element[property], element); |
import type { TypeofArray } from '../../types/array'; | ||
type Order = 'asc' | 'desc'; | ||
export declare function orderBy<T extends TypeofArray<T>[], P extends keyof TypeofArray<T>>(array: T, property: P, order?: Order): T[number][]; | ||
export declare function orderBy<T extends TypeofArray<T>[], P extends keyof TypeofArray<T>>(array: T, properties: P[], orders: Order[]): T[number][]; | ||
export {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.orderBy = void 0; | ||
function sortFn(a, b, order) { | ||
function sortFn(a, b) { | ||
if (typeof a === 'string' && typeof b === 'string') { | ||
@@ -16,9 +16,21 @@ return a.localeCompare(b, 'en', { sensitivity: 'base' }); | ||
} | ||
function orderBy(array, property, order = 'asc') { | ||
const sortedArray = [...array].sort((a, b) => sortFn(a[property], b[property], order)); | ||
if (order === 'desc') { | ||
return sortedArray.reverse(); | ||
function orderBy(array, properties, orders) { | ||
if (properties.length !== orders.length) { | ||
console.warn(`orderBy - property and order arrays are not the same length - properties: ${properties}, orders: ${orders}`); | ||
} | ||
return sortedArray; | ||
return [...array].sort((a, b) => properties | ||
.map((property, index) => { | ||
const order = orders[index]; | ||
if (order === 'desc') { | ||
return sortFn(b[property], a[property]); | ||
} | ||
return sortFn(a[property], b[property]); | ||
}) | ||
.reduce((accumulator, current) => { | ||
if (accumulator === 0) { | ||
return current; | ||
} | ||
return accumulator; | ||
}, 0)); | ||
} | ||
exports.orderBy = orderBy; |
@@ -5,3 +5,3 @@ export function keyBy(array, property) { | ||
if (map.has(element[property])) { | ||
console.warn(`duplicate key for keyBy: "${element[property]}" already exists in the map`); | ||
console.warn(`keyBy - duplicate key: "${element[property]}" already exists in the map`); | ||
} | ||
@@ -8,0 +8,0 @@ map.set(element[property], element); |
import type { TypeofArray } from '../../types/array'; | ||
type Order = 'asc' | 'desc'; | ||
export declare function orderBy<T extends TypeofArray<T>[], P extends keyof TypeofArray<T>>(array: T, property: P, order?: Order): T[number][]; | ||
export declare function orderBy<T extends TypeofArray<T>[], P extends keyof TypeofArray<T>>(array: T, properties: P[], orders: Order[]): T[number][]; | ||
export {}; |
@@ -1,2 +0,2 @@ | ||
function sortFn(a, b, order) { | ||
function sortFn(a, b) { | ||
if (typeof a === 'string' && typeof b === 'string') { | ||
@@ -13,8 +13,20 @@ return a.localeCompare(b, 'en', { sensitivity: 'base' }); | ||
} | ||
export function orderBy(array, property, order = 'asc') { | ||
const sortedArray = [...array].sort((a, b) => sortFn(a[property], b[property], order)); | ||
if (order === 'desc') { | ||
return sortedArray.reverse(); | ||
export function orderBy(array, properties, orders) { | ||
if (properties.length !== orders.length) { | ||
console.warn(`orderBy - property and order arrays are not the same length - properties: ${properties}, orders: ${orders}`); | ||
} | ||
return sortedArray; | ||
return [...array].sort((a, b) => properties | ||
.map((property, index) => { | ||
const order = orders[index]; | ||
if (order === 'desc') { | ||
return sortFn(b[property], a[property]); | ||
} | ||
return sortFn(a[property], b[property]); | ||
}) | ||
.reduce((accumulator, current) => { | ||
if (accumulator === 0) { | ||
return current; | ||
} | ||
return accumulator; | ||
}, 0)); | ||
} |
{ | ||
"name": "@bedrock-ui/utils", | ||
"version": "0.0.10", | ||
"version": "0.1.0", | ||
"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
9969
215