An analog of sort-keys-recursive (https://www.npmjs.com/package/sort-keys-recursive) that sorts object keys alphabetically and arrays in objects.
Last active
May 17, 2022 14:15
-
-
Save IvanAdmaers/c42cfc26f55a110b416fa4e53ff7c414 to your computer and use it in GitHub Desktop.
Sort object | Sort the keys of an object recursively
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sortObject from './sortObject'; | |
describe('sortObject', () => { | |
it('should sort one-dimensional object', () => { | |
const input = { c: 3, b: '2', a: null }; | |
const output = { a: null, b: '2', c: 3 }; | |
expect(sortObject(input)).toEqual(output); | |
}); | |
it('should sort two-dimensional object', () => { | |
const input = { c: 3, a: { b2: 'b2', a2: 'a2' }, b: '2' }; | |
const output = { a: { a2: 'a2', b2: 'b2' }, b: '2', c: 3 }; | |
expect(sortObject(input)).toEqual(output); | |
}); | |
it('should sort three-dimensional object', () => { | |
const input = { | |
c: 3, | |
a: { b2: 'b2', a2: 'a2', c2: { b3: 'b3', a3: 'a3' } }, | |
b: null, | |
}; | |
const output = { | |
a: { a2: 'a2', b2: 'b2', c2: { a3: 'a3', b3: 'b3' } }, | |
b: null, | |
c: 3, | |
}; | |
expect(sortObject(input)).toEqual(output); | |
}); | |
it('should sort an array of strings in object', () => { | |
const input = { arr: ['a', 'c', 'b'] }; | |
const output = { arr: ['a', 'b', 'c'] }; | |
expect(sortObject(input)).toEqual(output); | |
}); | |
it('should sort an array of numbers in object', () => { | |
const input = { arr: [1, 3, 2] }; | |
const output = { arr: [1, 2, 3] }; | |
expect(sortObject(input)).toEqual(output); | |
}); | |
it('should sort an array of strings, numbers and objects in object', () => { | |
const input = { arr: [1, { b: 'b', a: 'a' }, 2, 'lol'] }; | |
const output = { arr: [1, 2, { a: 'a', b: 'b' }, 'lol'] }; | |
expect(sortObject(input)).toEqual(output); | |
}); | |
it('should sort correct when there are non-primitives with primitives', () => { | |
const input = { arr: [{ a: [{}, 'a'] }] }; | |
const wrongOutput = { arr: [{ a: ['a', {}] }] }; | |
expect(sortObject(input)).not.toEqual(wrongOutput); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This function sorts object keys alphabetical and arrays in object | |
* { b: 2, a: 1 } => { a: 1, b: 2 } | |
* { c: { e: 2, d: 1 } } => { c: { d: 1, e: 2 } } | |
* { f: ['h', 'g'] } => { f: ['g', 'h'] } | |
* { i: [{ k: 2, j: 1 }] } => { i: [{ j: 1, k: 2 }] } | |
*/ | |
const sortObject = <InputObjectType>( | |
inputObject: InputObjectType | |
): InputObjectType => { | |
const result = {}; | |
const keys: Array<string> = Object.keys(inputObject); | |
const sortedKeys: Array<string> = keys.sort(); | |
sortedKeys.forEach((key: string): void => { | |
const value = inputObject[key]; | |
if (typeof value === 'object' && !Array.isArray(value) && value !== null) { | |
result[key] = sortObject(value); | |
return; | |
} | |
if (Array.isArray(value)) { | |
const resultArray: Array< | |
string | number | undefined | null | object | boolean | symbol | bigint | |
> = []; | |
const sortedArray = value.sort(); | |
sortedArray.forEach((arrayItem) => { | |
if (typeof arrayItem !== 'object' && arrayItem !== null) { | |
resultArray.push(arrayItem); | |
return; | |
} | |
const sortedObject = sortObject(arrayItem); | |
resultArray.push(sortedObject); | |
}); | |
result[key] = resultArray; | |
return; | |
} | |
result[key] = value; | |
}); | |
return result as InputObjectType; | |
}; | |
export default sortObject; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment