mirror of
https://github.com/dzeiocom/libs.git
synced 2025-06-07 08:39:56 +00:00
Updated objectSort to support an Array containing a specific order
Signed-off-by: Avior <florian.bouillon@delta-wings.net>
This commit is contained in:
parent
485e3b7b36
commit
76e2263812
@ -53,6 +53,10 @@ objectSize(object)
|
|||||||
// like Array.sort it sort and return an ordered object
|
// like Array.sort it sort and return an ordered object
|
||||||
objectSort(object, /*optionnal*/ (key1, key2) => key1 - key2)
|
objectSort(object, /*optionnal*/ (key1, key2) => key1 - key2)
|
||||||
|
|
||||||
|
// You can also sort by keys
|
||||||
|
// items not set in the array won't have their order changed and will be after the sorted ones
|
||||||
|
objectSort(object, ['key2', 'key1']) // => {key2: value, key1: value, key3: value}
|
||||||
|
|
||||||
// deeply clone an object
|
// deeply clone an object
|
||||||
cloneObject(object)
|
cloneObject(object)
|
||||||
|
|
||||||
|
@ -52,7 +52,7 @@ describe('Object Keys Tests', () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Object Sort Tests', () => {
|
describe('Object Size Tests', () => {
|
||||||
it('shoud return length of the object', () => {
|
it('shoud return length of the object', () => {
|
||||||
const obj = {
|
const obj = {
|
||||||
index0: true,
|
index0: true,
|
||||||
@ -82,6 +82,20 @@ describe('Object sort Tests', () => {
|
|||||||
b: 'first'
|
b: 'first'
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
it('should sort by the specified key', () => {
|
||||||
|
const obj = {
|
||||||
|
b: 'first',
|
||||||
|
a: 'second',
|
||||||
|
c: 'zero',
|
||||||
|
d: 'fourth'
|
||||||
|
}
|
||||||
|
expect(objectSort(obj, ['c', 'a'])).toEqual({
|
||||||
|
c: 'zero',
|
||||||
|
a: 'second',
|
||||||
|
b: 'first',
|
||||||
|
d: 'fourth'
|
||||||
|
})
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('Object Clone Tests', () => {
|
describe('Object Clone Tests', () => {
|
||||||
|
@ -62,12 +62,18 @@ export function objectSize(obj: Record<string, any>): number {
|
|||||||
* @param obj the object to sort
|
* @param obj the object to sort
|
||||||
* @param fn (Optionnal) the function to run to sort
|
* @param fn (Optionnal) the function to run to sort
|
||||||
*/
|
*/
|
||||||
export function objectSort<T = Record<string, any>>(
|
export function objectSort<T extends Record<string, any> = Record<string, any>>(
|
||||||
obj: Record<string, any>,
|
obj: T,
|
||||||
fn?: (a: string, b: string) => number
|
fn?: Array<keyof T> | ((a: keyof T, b: keyof T) => number)
|
||||||
): T {
|
): T {
|
||||||
const ordered: any = {}
|
const ordered: any = {}
|
||||||
for (const key of objectKeys(obj).sort(fn)) {
|
let sortedKeys: Array<keyof T> = []
|
||||||
|
if (Array.isArray(fn)) {
|
||||||
|
sortedKeys = fn.concat(objectKeys(obj).filter((k) => !fn.includes(k)))
|
||||||
|
} else {
|
||||||
|
sortedKeys = objectKeys(obj).sort(fn)
|
||||||
|
}
|
||||||
|
for (const key of sortedKeys) {
|
||||||
ordered[key] = obj[key]
|
ordered[key] = obj[key]
|
||||||
}
|
}
|
||||||
return ordered
|
return ordered
|
||||||
|
Loading…
x
Reference in New Issue
Block a user