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.22.0",
"version": "5.23.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
11 changes: 11 additions & 0 deletions src/helpers/Json.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,17 @@ export class Json {
return lodash.get(object, key, defaultValue)
}

/**
* Set the object values based on a key.
*/
public static set<T = any>(object: T, key: string, value: any): T {
if (key === '' && object) {
return object
}

return lodash.set<T>(object as unknown as any, key, value)
}

/**
* Sort an object or an array of objects by it keys names.
*/
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/helpers/JsonTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,32 @@ export default class JsonTest {
assert.deepEqual(falsyDefaultValue, false)
}

@Test()
public async shouldBeAbleToSetNestedPropertiesInsideAnObject({ assert }: Context) {
const object = {}

const newObject = Json.set(object, 'hello.world.value.hello', 'Hello World!')

assert.deepEqual(object, {
hello: {
world: {
value: {
hello: 'Hello World!'
}
}
}
})
assert.deepEqual(newObject, {
hello: {
world: {
value: {
hello: 'Hello World!'
}
}
}
})
}

@Test()
public async shouldBeAbleToSortObjects({ assert }: Context) {
const object = {
Expand Down