diff --git a/package-lock.json b/package-lock.json index f31c2c9..609021a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/common", - "version": "4.19.0", + "version": "4.20.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@athenna/common", - "version": "4.19.0", + "version": "4.20.0", "license": "MIT", "dependencies": { "@fastify/formbody": "^7.4.0", diff --git a/package.json b/package.json index 554e7d8..e149f4c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/common", - "version": "4.19.0", + "version": "4.20.0", "description": "The Athenna common helpers to use in any Node.js ESM project.", "license": "MIT", "author": "João Lenon ", diff --git a/src/helpers/Json.ts b/src/helpers/Json.ts index d3abd85..ffbf6d1 100644 --- a/src/helpers/Json.ts +++ b/src/helpers/Json.ts @@ -108,6 +108,20 @@ export class ObjectBuilder { return this.getValue(value) } + /** + * Omit data from object. + */ + public omit(keys: string[]) { + return Json.omit(this.object, keys) + } + + /** + * Omit data from object. + */ + public pick(keys: string[]) { + return Json.pick(this.object, keys) + } + /** * Return an array with the property names. */ @@ -229,7 +243,7 @@ export class ObjectBuilder { /** * Get the value referenced or not depending on - * "options.referecendValues". Also will auto set + * "options.referencedValues". Also will auto set * the "options.defaultValue" if any value is set. */ private getValue(value: any, defaultValue = this.options.defaultValue): any { @@ -255,10 +269,50 @@ export class Json { /** * Deep copy any object properties without reference. */ - public static copy(object: any): any { + public static copy(object: T): T { return lodash.cloneDeep(object) } + /** + * Omit data from an object. + * + * @example + * ```ts + * const obj = { + * name: 'Lenon', + * age: 22 + * } + * + * const omitted = Json.omit(obj, ['name']) // { age: 22 } + * ``` + */ + public static omit( + object: T, + keys: K[] + ): Omit { + return lodash.omit(object, keys) + } + + /** + * Pick data from an object. + * + * @example + * ```ts + * const obj = { + * name: 'Lenon', + * age: 22 + * } + * + * const picked = Json.pick(obj, ['name']) // { name: 'Lenon' } + * ``` + */ + public static pick( + object: T, + keys: K[] + ): Pick { + return lodash.pick(object, keys) + } + /** * Find all JSON inside string and return it. */ diff --git a/tests/unit/JsonTest.ts b/tests/unit/JsonTest.ts index 74dcf78..01881bc 100644 --- a/tests/unit/JsonTest.ts +++ b/tests/unit/JsonTest.ts @@ -348,4 +348,60 @@ export default class JsonTest { ['AGE', 22] ]) } + + @Test() + public async shouldBeAbleToOmitValuesFromObject({ assert }: Context) { + const object = { + name: 'Lenon', + age: 22 + } + + const omitted = Json.omit(object, ['age']) + + assert.deepEqual(omitted, { name: 'Lenon' }) + } + + @Test() + public async shouldBeAbleToPickValuesFromObject({ assert }: Context) { + const object = { + name: 'Lenon', + age: 22 + } + + const picked = Json.pick(object, ['age']) + + assert.deepEqual(picked, { age: 22 }) + } + + @Test() + public async shouldBeAbleToPickValuesFromBuilder({ assert }: Context) { + const builder = Json.builder().set({ name: 'Lenon', age: 22 }) + const picked = builder.pick(['name']) + + assert.deepEqual(picked, { name: 'Lenon' }) + } + + @Test() + public async shouldBeAbleToOmitValuesFromBuilder({ assert }: Context) { + const builder = Json.builder().set({ name: 'Lenon', age: 22 }) + const omitted = builder.omit(['name']) + + assert.deepEqual(omitted, { age: 22 }) + } + + @Test() + public async shouldBeAbleToPickAdvancedKeyValuesFromBuilder({ assert }: Context) { + const builder = Json.builder().set({ users: [{ name: 'Lenon', age: 22 }] }) + const picked = builder.pick(['users[0].name']) + + assert.deepEqual(picked, { users: [{ name: 'Lenon' }] }) + } + + @Test() + public async shouldBeAbleToOmitAdvancedKeyValuesFromBuilder({ assert }: Context) { + const builder = Json.builder().set({ users: [{ name: 'Lenon', age: 22 }] }) + const omitted = builder.omit(['users[0].name']) + + assert.deepEqual(omitted, { users: [{ age: 22 }] }) + } }