From 1a3510f39a044758fe61b414319f32e29dd18c58 Mon Sep 17 00:00:00 2001 From: Rico Sandyca Novenza Date: Tue, 28 Jul 2020 02:57:38 +0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#46124=20Update=20@?= =?UTF-8?q?types/dot-object=20to=20v2.1.3=20by=20@ricosandyca?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update dot-object-tests.ts * Update index.d.ts * Update dot-object-tests.ts * Update index.d.ts * Add files via upload --- types/dot-object/dot-object-tests.ts | 24 ++++++++++++++ types/dot-object/index.d.ts | 48 +++++++++++++++++++++++++--- 2 files changed, 67 insertions(+), 5 deletions(-) diff --git a/types/dot-object/dot-object-tests.ts b/types/dot-object/dot-object-tests.ts index c87c6f5a80..f0e1fd810d 100644 --- a/types/dot-object/dot-object-tests.ts +++ b/types/dot-object/dot-object-tests.ts @@ -67,9 +67,15 @@ val = dot.pick('some.nested.value', newObj, true); // shorthand val = dot.remove('some.nested.value', newObj); +val = dot.remove(['some.nested.value'], newObj) // or use the alias `del` val = dot.del('some.nested.value', newObj); +val = dot.del(['some.nested.value'], newObj); + +// delete and get value of the deleted attribute +val = dot.delete('some.nested.value', newObj) +val = dot.delete(['some.nested.value'], newObj) // convert object to dot object var result = {}; @@ -77,3 +83,21 @@ dot.dot({ test: 'something' }, result); result = dot.dot({ test: 'something' }); var dotWithArrow = new dot('=>'); + +var dotWithAnotherSeparator = new dot('->', true, false) + +var objWithArray = { + author: 'John Doe', + books: ['A', 'B', 'C'], + about: { + hometown: 'Sampang, Jawa Timur', + skils: ['HTML', 'CSS', 'JS', 'TS'] + } +} + +var defaultResult = dot.dot(objWithArray) +// set keepArray property +dot.keepArray = true +var keepArrayResult = dot.dot(objWithArray) +console.log('Default:', defaultResult) +console.log('KeepArray:', keepArrayResult) diff --git a/types/dot-object/index.d.ts b/types/dot-object/index.d.ts index 1fc50795e0..e82b402c67 100644 --- a/types/dot-object/index.d.ts +++ b/types/dot-object/index.d.ts @@ -1,13 +1,19 @@ // Type definitions for Dot-Object 2.1 // Project: https://github.com/rhalff/dot-object // Definitions by: Niko Kovačič +// Rico Sandyca // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 declare namespace DotObject { interface DotConstructor extends Dot { - new (separator: string): Dot; + new ( + separator: string, + override?: boolean, + useArray?: boolean, + useBrackets?: boolean + ): Dot; } interface ModifierFunctionWrapper { @@ -58,11 +64,43 @@ declare namespace DotObject { * * Remove value from an object using dot notation. * - * @param {String} path + * @param {String | Array} path * @param {Object} obj * @return {Mixed} The removed value */ - del(path: string, obj: any): any; + del(path: string | string[], obj: any): any; + /** + * + * Delete value from an object using dot notation. + * + * @param {String | Array} path + * @param {Object} obj + * @return {any} The removed value + */ + delete(path: string | string[], obj: any): any; + /** + * + * Keep array + * + * example: + * + * var obj = { + * "id": "my-id", + * "other": [1, 2, 3] + * "some": { + * "array": ["A", "B"] + * } + * } + * + * if the keepArray property is true: + * + * { + * "id": "my-id", + * "other": [1, 2, 3], + * "some.array": ["A", "B"] + * } + */ + keepArray: boolean; /** * * Move a property from one place to the other. @@ -115,11 +153,11 @@ declare namespace DotObject { * * Remove value from an object using dot notation. * - * @param {String} path + * @param {String | Array} path * @param {Object} obj * @return {Mixed} The removed value */ - remove(path: string, obj: any): any; + remove(path: string | string[], obj: any): any; /** * * Replace/create with a string