Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/common",
"version": "5.18.0",
"version": "5.19.0",
"description": "The Athenna common helpers to use in any Node.js ESM project.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down
21 changes: 21 additions & 0 deletions src/helpers/Json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,27 @@ export class Json {
return lodash.get(object, key, defaultValue)
}

/**
* Sort an object or an array of objects by it keys names.
*/
public static sort<T = any>(object: T) {
if (Is.Array(object)) {
return object.map(Json.sort)
}

if (!object || !Is.Object(object)) {
return object
}

return Object.keys(object)
.sort()
.reduce((sortedObject, key) => {
sortedObject[key] = Json.sort(object[key])

return sortedObject
}, {})
}

/**
* Validate if an object or array is equal to another.
*
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/helpers/JsonTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,26 @@ export default class JsonTest {
assert.deepEqual(falsyDefaultValue, false)
}

@Test()
public async shouldBeAbleToSortObjects({ assert }: Context) {
const object = {
c: 'c',
b: 'b',
a: 'a'
}

const sortedObject = Json.sort(object)
const sortedArrayOfObjects = Json.sort([object, object])
const sortedObjectRecursive = Json.sort({ d: object, ...object })

assert.deepEqual(Object.keys(object), ['c', 'b', 'a'])
assert.deepEqual(Object.keys(sortedObject), ['a', 'b', 'c'])
assert.deepEqual(Object.keys(sortedArrayOfObjects[0]), ['a', 'b', 'c'])
assert.deepEqual(Object.keys(sortedArrayOfObjects[1]), ['a', 'b', 'c'])
assert.deepEqual(Object.keys(sortedObjectRecursive), ['a', 'b', 'c', 'd'])
assert.deepEqual(Object.keys(sortedObjectRecursive.d), ['a', 'b', 'c'])
}

@Test()
public async shouldBeAbleToBuildObjectsUsingTheObjectBuilder({ assert }: Context) {
const me = Json.builder()
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/helpers/PathTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,10 +219,10 @@ export default class PathTest {
assert.isTrue(Path.database().endsWith(`build${sep}src${sep}database`))
assert.isTrue(Path.seeders().endsWith(`build${sep}src${sep}database${sep}seeders`))
assert.isTrue(Path.migrations().endsWith(`build${sep}src${sep}database${sep}migrations`))
assert.isTrue(Path.resources().endsWith(`build${sep}src${sep}resources`))
assert.isTrue(Path.apiResources().endsWith(`build${sep}src${sep}resources${sep}resources`))
assert.isTrue(Path.views().endsWith(`build${sep}src${sep}resources${sep}views`))
assert.isTrue(Path.locales().endsWith(`build${sep}src${sep}resources${sep}locales`))
assert.isTrue(Path.resources().endsWith(`build${sep}resources`))
assert.isTrue(Path.views().endsWith(`build${sep}resources${sep}views`))
assert.isTrue(Path.locales().endsWith(`build${sep}resources${sep}locales`))
assert.isTrue(Path.apiResources().endsWith(`build${sep}src${sep}resources`))
assert.isTrue(Path.nodeModules().endsWith(`build${sep}node_modules`))
assert.isTrue(Path.nodeModulesBin().endsWith(`build${sep}node_modules${sep}.bin`))
assert.isTrue(Path.providers().endsWith(`build${sep}src${sep}providers`))
Expand Down
Loading