From 858c0021ff62e0223e248ad2994e3a34327bead5 Mon Sep 17 00:00:00 2001 From: nkovacic Date: Wed, 29 Jun 2016 12:37:17 +0200 Subject: [PATCH 1/2] Added Dot-object typings --- dot-object/dot-object-tests.ts | 64 +++++++++++++ dot-object/dot-object.d.ts | 162 +++++++++++++++++++++++++++++++++ 2 files changed, 226 insertions(+) create mode 100644 dot-object/dot-object-tests.ts create mode 100644 dot-object/dot-object.d.ts diff --git a/dot-object/dot-object-tests.ts b/dot-object/dot-object-tests.ts new file mode 100644 index 0000000000..c6098defd2 --- /dev/null +++ b/dot-object/dot-object-tests.ts @@ -0,0 +1,64 @@ +/// + + +var obj = { + 'first_name': 'John', + 'last_name': 'Doe' +}; + +dot.move('first_name', 'contact.firstname', obj); +dot.move('last_name', 'contact.lastname', obj); + +var src = { + name: 'John', + stuff: { + phone: { + brand: 'iphone', + version: 6 + } + } +}; + +var tgt = {name: 'Brandon'}; + +dot.copy('stuff.phone', 'wanna.haves.phone', src, tgt); + +dot.transfer('stuff.phone', 'wanna.haves.phone', src, tgt); + +var row = { + 'id': 2, + 'contact.name.first': 'John', + 'contact.name.last': 'Doe', + 'contact.email': 'example@gmail.com', + 'contact.info.about.me': 'classified', + 'devices[0]': 'mobile', + 'devices[1]': 'laptop', + 'some.other.things.0': 'this', + 'some.other.things.1': 'that' +}; + +dot.object(row); + +dot.str('this.is.my.string', 'value', tgt); + +var newObj = { + some: { + nested: { + value: 'Hi there!' + } + } +}; + +var val = dot.pick('some.nested.value', newObj); +console.log(val); + +// Pick & Remove the value +val = dot.pick('some.nested.value', newObj, true); + +// shorthand +val = dot.remove('some.nested.value', newObj); + +// or use the alias `del` +val = dot.del('some.nested.value', newObj); + +var dot = new dot('=>'); \ No newline at end of file diff --git a/dot-object/dot-object.d.ts b/dot-object/dot-object.d.ts new file mode 100644 index 0000000000..f87fc0aa53 --- /dev/null +++ b/dot-object/dot-object.d.ts @@ -0,0 +1,162 @@ +// Type definitions for Dot-Object v1.4.1 +// Project: https://github.com/rhalff/dot-object +// Definitions by: Niko Kovačič +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + + +declare namespace DotObject { + interface Dot { + new(separator: string): Dot; + /** + * + * Copy a property from one object to another object. + * + * If the source path does not exist (undefined) + * the property on the other object will not be set. + * + * @param {String} source + * @param {String} target + * @param {Object} obj1 + * @param {Object} obj2 + * @param {Function|Array} mods + * @param {Boolean} merge + */ + copy(source: string, target: string, obj1: any, obj2: any, mods?: Function | Array, merge?: boolean): void; + /** + * + * Convert object to dotted-key/value pair + * + * Usage: + * + * var tgt = dot.dot(obj) + * + * or + * + * var tgt = {} + * dot.dot(obj, tgt) + * + * @param {Object} obj source object + * @param {Object} tgt target object + */ + dot(obj: any, tgt: any): void + /** + * + * Remove value from an object using dot notation. + * + * @param {String} path + * @param {Object} obj + * @return {Mixed} The removed value + */ + del(path: string, obj: any): any; + /** + * + * Move a property from one place to the other. + * + * If the source path does not exist (undefined) + * the target property will not be set. + * + * @param {String} source + * @param {String} target + * @param {Object} obj + * @param {Function|Array} mods + * @param {Boolean} merge + */ + move(source: string, target: string, obj: any, mods?: Function | Array, merge?: boolean): void; + /** + * + * Converts an object with dotted-key/value pairs to it's expanded version + * + * Optionally transformed by a set of modifiers. + * + * Usage: + * + * var row = { + * 'nr': 200, + * 'doc.name': ' My Document ' + * } + * + * var mods = { + * 'doc.name': [_s.trim, _s.underscored] + * } + * + * dot.object(row, mods) + * + * @param {Object} obj + * @param {Object} mods + */ + object(obj: any, mods?: Function | Array): void; + /** + * + * Pick a value from an object using dot notation. + * + * Optionally remove the value + * + * @param {String} path + * @param {Object} obj + * @param {Boolean} remove + */ + pick(path: string, obj: any, remove?: boolean): void; + /** + * + * Remove value from an object using dot notation. + * + * @param {String} path + * @param {Object} obj + * @return {Mixed} The removed value + */ + remove(path: string, obj: any): any; + /** + * @param {String} path dotted path + * @param {String} v value to be set + * @param {Object} obj object to be modified + * @param {Function|Array} mods optional modifier + */ + str(path: string, v: any, obj: Object, mods?: Function | Array): void; + /** + * + * Transfer a property from one object to another object. + * + * If the source path does not exist (undefined) + * the property on the other object will not be set. + * + * @param {String} source + * @param {String} target + * @param {Object} obj1 + * @param {Object} obj2 + * @param {Function|Array} mods + * @param {Boolean} merge + */ + transfer(source: string, target: string, obj1: any, obj2: any, mods?: Function | Array, merge?: boolean): void; + /** + * + * Transform an object + * + * Usage: + * + * var obj = { + * "id": 1, + * "some": { + * "thing": "else" + * } + * } + * + * var transform = { + * "id": "nr", + * "some.thing": "name" + * } + * + * var tgt = dot.transform(transform, obj) + * + * @param {Object} recipe Transform recipe + * @param {Object} obj Object to be transformed + * @param {Array} mods modifiers for the target + */ + transform(recipe: any, obj: any, mods?: Function | Array): void; + } +} + +declare var dot: DotObject.Dot; + +declare module 'dot-object' { + export = dot; +} \ No newline at end of file From 706a6d27ed18500149c10a4cb507029bf62e2f43 Mon Sep 17 00:00:00 2001 From: nkovacic Date: Thu, 30 Jun 2016 10:55:56 +0200 Subject: [PATCH 2/2] Fixed constructor possible recursion, more precise function definition for modifiers and more tests for them. --- dot-object/dot-object-tests.ts | 11 +++++++---- dot-object/dot-object.d.ts | 23 +++++++++++++++-------- 2 files changed, 22 insertions(+), 12 deletions(-) diff --git a/dot-object/dot-object-tests.ts b/dot-object/dot-object-tests.ts index c6098defd2..ca7aa3f676 100644 --- a/dot-object/dot-object-tests.ts +++ b/dot-object/dot-object-tests.ts @@ -1,6 +1,5 @@ /// - var obj = { 'first_name': 'John', 'last_name': 'Doe' @@ -21,7 +20,9 @@ var src = { var tgt = {name: 'Brandon'}; -dot.copy('stuff.phone', 'wanna.haves.phone', src, tgt); +dot.copy('stuff.phone', 'wanna.haves.phone', src, tgt, [(arg: any) => { + return arg; +}]); dot.transfer('stuff.phone', 'wanna.haves.phone', src, tgt); @@ -37,7 +38,9 @@ var row = { 'some.other.things.1': 'that' }; -dot.object(row); +dot.object(row, (arg: any) => { + return arg; +}); dot.str('this.is.my.string', 'value', tgt); @@ -61,4 +64,4 @@ val = dot.remove('some.nested.value', newObj); // or use the alias `del` val = dot.del('some.nested.value', newObj); -var dot = new dot('=>'); \ No newline at end of file +var dotWithArrow = new dot('=>'); \ No newline at end of file diff --git a/dot-object/dot-object.d.ts b/dot-object/dot-object.d.ts index f87fc0aa53..953291c631 100644 --- a/dot-object/dot-object.d.ts +++ b/dot-object/dot-object.d.ts @@ -5,8 +5,15 @@ declare namespace DotObject { - interface Dot { + interface DotConstructor extends Dot { new(separator: string): Dot; + } + + interface ModifierFunctionWrapper { + (arg: any): any; + } + + interface Dot { /** * * Copy a property from one object to another object. @@ -21,7 +28,7 @@ declare namespace DotObject { * @param {Function|Array} mods * @param {Boolean} merge */ - copy(source: string, target: string, obj1: any, obj2: any, mods?: Function | Array, merge?: boolean): void; + copy(source: string, target: string, obj1: any, obj2: any, mods?: ModifierFunctionWrapper | Array, merge?: boolean): void; /** * * Convert object to dotted-key/value pair @@ -61,7 +68,7 @@ declare namespace DotObject { * @param {Function|Array} mods * @param {Boolean} merge */ - move(source: string, target: string, obj: any, mods?: Function | Array, merge?: boolean): void; + move(source: string, target: string, obj: any, mods?: ModifierFunctionWrapper | Array, merge?: boolean): void; /** * * Converts an object with dotted-key/value pairs to it's expanded version @@ -84,7 +91,7 @@ declare namespace DotObject { * @param {Object} obj * @param {Object} mods */ - object(obj: any, mods?: Function | Array): void; + object(obj: any, mods?: ModifierFunctionWrapper | Array): void; /** * * Pick a value from an object using dot notation. @@ -111,7 +118,7 @@ declare namespace DotObject { * @param {Object} obj object to be modified * @param {Function|Array} mods optional modifier */ - str(path: string, v: any, obj: Object, mods?: Function | Array): void; + str(path: string, v: any, obj: Object, mods?: ModifierFunctionWrapper | Array): void; /** * * Transfer a property from one object to another object. @@ -126,7 +133,7 @@ declare namespace DotObject { * @param {Function|Array} mods * @param {Boolean} merge */ - transfer(source: string, target: string, obj1: any, obj2: any, mods?: Function | Array, merge?: boolean): void; + transfer(source: string, target: string, obj1: any, obj2: any, mods?: ModifierFunctionWrapper | Array, merge?: boolean): void; /** * * Transform an object @@ -151,11 +158,11 @@ declare namespace DotObject { * @param {Object} obj Object to be transformed * @param {Array} mods modifiers for the target */ - transform(recipe: any, obj: any, mods?: Function | Array): void; + transform(recipe: any, obj: any, mods?: ModifierFunctionWrapper | Array): void; } } -declare var dot: DotObject.Dot; +declare var dot: DotObject.DotConstructor; declare module 'dot-object' { export = dot;