From d5da919d44a23a618aee1e1f67e12dcdb8362db2 Mon Sep 17 00:00:00 2001 From: Paulo Cesar Date: Thu, 2 Apr 2015 19:25:39 -0300 Subject: [PATCH] fixes for new version --- object-path/object-path-tests.ts | 162 ++++--- object-path/object-path.d.ts | 706 ++++++++++++++++++++----------- 2 files changed, 563 insertions(+), 305 deletions(-) diff --git a/object-path/object-path-tests.ts b/object-path/object-path-tests.ts index 5984c78a8a..d29cec1410 100644 --- a/object-path/object-path-tests.ts +++ b/object-path/object-path-tests.ts @@ -1,68 +1,94 @@ -/// - -var - object = { - one: 1, - two: { - three: 3, - four: ['4'] - } - }, - array: any[] = [], - Null:any = null; - -objectPath.del(array) === ['12']; -objectPath.del(object) === object; -objectPath.del(object) === object; - -objectPath.del() === void 0; -objectPath.del(object, ['1','2','3']); -objectPath.del(object, [1,2,3]); -objectPath.del(object, 1); -objectPath.del(object, 'one').one === 1; - -objectPath.coalesce(object, ['1','2']) === void 0; -objectPath.coalesce(object, ['1',['2','1']]) === void 0; -objectPath.coalesce(object, ['1',['2','1']], 1) === 1; -objectPath.coalesce(object, [1,1], 1) === 1; -objectPath.coalesce(object, >[1,[1,1]], 1) === 1; - -objectPath.ensureExists(object, '1.2', 2); -objectPath.ensureExists(object, 1, 2); -objectPath.ensureExists(object, [1,2], 2); -objectPath.ensureExists(object, ['1','2'], 2); -objectPath.ensureExists(object, ['1','2'], 2) === 3; -objectPath.ensureExists(object, ['1','2'], 2) === [[]]; - -objectPath.push(object, 1, 1,2,3,4); -objectPath.push(object, 1, 1,'2', 3, false); -objectPath.push(object, 'one.four', 1,'2', 3, false); -objectPath.push(object, ['one','two'], [1,'2', 3, false]); - -objectPath.get(array) === array; -objectPath.get(Null) === Null; -objectPath.get() === void 0; -objectPath.get(object, 'one') === 1; -objectPath.get(object, ['two','three']) === 3; -objectPath.get(object, ['three'], 3) === 3; -objectPath.get(object, 'three', 3) === 3; -objectPath.get(object, 0, 3) === 3; -objectPath.get(object, 0, '3') === '3'; -objectPath.get(object, 0, ['1','2']) === ['1','2']; -objectPath.get(object, 0) === 10; - -objectPath.set(object, '1.2', true); -objectPath.set(object, ['1','2'], true); -objectPath.set(object, [1, 2], true); -objectPath.set(object, '1.2', true, true); -objectPath.set(object, '1.2', true, false); -objectPath.set(object, '1.2', true, false) === ['string']; -objectPath.set(object, '1.2', true, false) === object; - -objectPath.insert(object, '1.2', 1); -objectPath.insert(object, ['1','2'], 1); -objectPath.insert(object, 1, 1); -objectPath.insert(object, [1,2], 1); -objectPath.insert(object, '1.2', 1, 2); -objectPath.insert(object, ['1.2'], 1, 6); - +/// + +import ObjectPath = require('object-path'); + +var + object = { + one: 1, + two: { + three: 3, + four: ['4'] + } + }, + array: any[] = [], + Null:any = null; + +var obj = ObjectPath(object); + +obj.del(array); +obj.coalesce([1,2]); +obj.ensureExists('1.2', 1); +obj.push(1, 'value'); +obj.get(array); +obj.set(array, 'value'); +obj.insert(1, 10); + +objectPath.del(array) === ['12']; +objectPath.del(object) === object; +objectPath.del(object) === object; +obj.del() === object; + +objectPath.has(object, ['1','2','3']) === true; +objectPath.has(object, ['1.2.3']) === false; +objectPath.has(object, [1,2,3]) === true; +objectPath.has(object, 1) === false; +objectPath.has() === false; + +objectPath.del() === void 0; +objectPath.del(object, ['1','2','3']); +objectPath.del(object, [1,2,3]); +objectPath.del(object, 1); +objectPath.del(object, 'one').one === 1; +obj.del('one').one === 1; + +objectPath.coalesce(object, ['1','2']) === void 0; +objectPath.coalesce(object, ['1',['2','1']]) === void 0; +objectPath.coalesce(object, ['1',['2','1']], 1) === 1; +objectPath.coalesce(object, [1,1], 1) === 1; +objectPath.coalesce(object, >[1,[1,1]], 1) === 1; +obj.coalesce(>[1,[1,1]], 1) === 1; + +objectPath.ensureExists(object, '1.2', 2); +objectPath.ensureExists(object, 1, 2); +objectPath.ensureExists(object, [1,2], 2); +objectPath.ensureExists(object, ['1','2'], 2); +objectPath.ensureExists(object, ['1','2'], 2) === 3; +objectPath.ensureExists(object, ['1','2'], 2) === [[]]; +obj.ensureExists(['1','2'], 2) === [[]]; + +objectPath.push(object, 1, 1,2,3,4); +objectPath.push(object, 1, 1,'2', 3, false); +objectPath.push(object, 'one.four', 1,'2', 3, false); +objectPath.push(object, ['one','two'], [1,'2', 3, false]); +obj.push(['one','two'], [1,'2', 3, false]); + +objectPath.get(array) === array; +objectPath.get(Null) === Null; +objectPath.get() === void 0; +objectPath.get(object, 'one') === 1; +objectPath.get(object, ['two','three']) === 3; +objectPath.get(object, ['three'], 3) === 3; +objectPath.get(object, 'three', 3) === 3; +objectPath.get(object, 0, 3) === 3; +objectPath.get(object, 0, '3') === '3'; +objectPath.get(object, 0, ['1','2']) === ['1','2']; +objectPath.get(object, 0) === 10; +obj.get(0) === 10; + +objectPath.set(object, '1.2', true); +objectPath.set(object, ['1','2'], true); +objectPath.set(object, [1, 2], true); +objectPath.set(object, '1.2', true, true); +objectPath.set(object, '1.2', true, false); +objectPath.set(object, '1.2', true, false) === ['string']; +objectPath.set(object, '1.2', true, false) === object; +obj.set('1.2', true, false) === object; + +objectPath.insert(object, '1.2', 1); +objectPath.insert(object, ['1','2'], 1); +objectPath.insert(object, 1, 1); +objectPath.insert(object, [1,2], 1); +objectPath.insert(object, '1.2', 1, 2); +objectPath.insert(object, ['1.2'], 1, 6); +obj.insert(['1.2'], 1, 6); + diff --git a/object-path/object-path.d.ts b/object-path/object-path.d.ts index fe38f87028..711a305dc4 100644 --- a/object-path/object-path.d.ts +++ b/object-path/object-path.d.ts @@ -1,238 +1,470 @@ -// Type definitions for objectPath v0.6.0 -// Project: https://github.com/mariocasciaro/object-path -// Definitions by: Paulo Cesar -// Definitions: https://github.com/borisyankov/DefinitelyTyped - -declare var objectPath: objectPath.IObjectPathStatic; - -declare module objectPath { - - interface IStringArray { - [index: number]: string; - } - - interface INumberArray { - [index: number]: number; - } - - interface IObjectPathStatic { - /*======== Del =========*/ - - /** - * Deletes a member from object or array - * @param {object} object - * @param {string[]|string} path - * @return object - */ - del(object: T, path: IStringArray): T; - /** - * @see objectPath.del - */ - del(object: T, path: INumberArray): T; - /** - * @see objectPath.del - */ - del(object: T, path: number): T; - /** - * @see objectPath.del - */ - del(object: T, path: string): T; - /** - * @see objectPath.del - */ - del(object: T): T; - /** - * @see objectPath.del - */ - del():void; - - /*======== Get =========*/ - /** - * Get a path from an object - * @param {object} object - * @param {string|string[]|number|number[]} path - * @param {*} [defaultValue=undefined] - */ - get(object: T, path: string, defaultValue?: TResult): TResult; - /** - * @see objectPath.get - */ - get(object: T, path: IStringArray, defaultValue?: TResult): TResult; - /** - * @see objectPath.get - */ - get(object: T, path: number, defaultValue?: TResult): TResult; - /** - * @see objectPath.get - */ - get(object: T, path: INumberArray, defaultValue?: TResult): TResult; - /** - * @see objectPath.get - */ - get(object: T): T; - /** - * @see objectPath.get - */ - get():void; - - /*======== Set =========*/ - /** - * Set a path to a value - * @param {object} object - * @param {string|string[]|number|number[]} path - * @param {*} value - * @param {boolean} [doNotReplace=false] - * @return Any existing value on the path if any - */ - set(object: T, path: string, value: any, doNotReplace?:boolean): TExisting; - /** - * @see objectPath.set - */ - set(object: T, path: number, value: any, doNotReplace?:boolean): TExisting; - /** - * @see objectPath.set - */ - set(object: T, path: IStringArray, value: any, doNotReplace?:boolean): TExisting; - /** - * @see objectPath.set - */ - set(object: T, path: INumberArray, value: any, doNotReplace?:boolean): TExisting; - /** - * @see objectPath.set - */ - set(object: T): T; - /** - * @see objectPath.set - */ - set():void; - - /*======== Push =========*/ - /** - * Create (if path isn't an array) and push the value to it. Can push unlimited number of values - * @param {object} object - */ - push(object: T, path: INumberArray, ...args:any[]):void; - /** - * @see objectPath.push - */ - push(object: T, path: IStringArray, ...args:any[]):void; - /** - * @see objectPath.push - */ - push(object: T, path: number, ...args:any[]):void; - /** - * @see objectPath.push - */ - push(object: T, path: string, ...args:any[]):void; - /** - * @see objectPath.push - */ - push():void; - - /*======== Coalesce =========*/ - /** - * Get the first non undefined property - * @param {object} object - * @param {string[]|string[][]|number[]|number[][]} paths - * @param {*} defaultValue - * @return {*} - */ - coalesce(object: T, paths: IStringArray, defaultValue?: any):TResult; - /** - * @see objectPath.coalesce - */ - coalesce(object: T, paths: INumberArray, defaultValue?: any):TResult; - /** - * @see objectPath.coalesce - */ - coalesce(object: T, paths: IStringArray[], defaultValue?: any):TResult; - /** - * @see objectPath.coalesce - */ - coalesce(object: T, paths: INumberArray[], defaultValue?: any):TResult; - - /*======== Empty =========*/ - /** - * Empty a path. Arrays are set to length 0, objects have all elements deleted, strings - * are set to empty, numbers to 0, everything else is set to null - * @param {object} object - * @param {string|string[]|number[]} path - */ - empty(object: T, path: string):TResult; - /** - * @see objectPath.empty - */ - empty(object: T, path: INumberArray):TResult; - /** - * @see objectPath.empty - */ - empty(object: T, path: IStringArray):TResult; - /** - * @see objectPath.empty - */ - empty(object: T, path: number):TResult; - /** - * @see objectPath.empty - */ - empty(object: T):T; - /** - * @see objectPath.empty - */ - empty():void; - - /*======== EnsureExists =========*/ - /** - * Set a value if it doesn't exist, do nothing if it does - * @param {object} object - * @param {string|string[]|number|number[]} path - */ - ensureExists(object: T, path: string, value: any):TResult; - /** - * @see objectPath.ensureExists - */ - ensureExists(object: T, path: number, value: any):TResult; - /** - * @see objectPath.ensureExists - */ - ensureExists(object: T, path: INumberArray, value: any):TResult; - /** - * @see objectPath.ensureExists - */ - ensureExists(object: T, path: IStringArray, value: any):TResult; - /** - * @see objectPath.ensureExists - */ - ensureExists(object: T): T; - /** - * @see objectPath.ensureExists - */ - ensureExists():void; - - /*======== Insert =========*/ - /** - * Insert an item in an array path - * @param {object} object - * @param {string|string[]|number|number[]} path - * @param {*} value - * @param {number} [at=0] - */ - insert(object: T, path: string, value: any, at?: number):void; - /** - * @see objectPath.insert - */ - insert(object: T, path: INumberArray, value: any, at?: number):void; - /** - * @see objectPath.insert - */ - insert(object: T, path: IStringArray, value: any, at?: number):void; - /** - * @see objectPath.insert - */ - insert(object: T, path: number, value: any, at?: number):void; - } - -} - -declare module 'objectPath' { - export = objectPath; +// Type definitions for objectPath v0.9.x +// Project: https://github.com/mariocasciaro/object-path +// Definitions by: Paulo Cesar +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +declare var objectPath: objectPath.IObjectPathStatic; + +declare module objectPath { + + interface IStringArray { + [index: number]: string; + } + + interface INumberArray { + [index: number]: number; + } + + interface IObjectPathStatic { + /** + * Binds an object + */ + (object: T): IObjectPathBound; + + /*======== Del =========*/ + + /** + * Deletes a member from object or array + * @param {object} object + * @param {string[]|string} path + * @return object + */ + del(object: T, path: IStringArray): T; + /** + * @see objectPath.del + */ + del(object: T, path: INumberArray): T; + /** + * @see objectPath.del + */ + del(object: T, path: number): T; + /** + * @see objectPath.del + */ + del(object: T, path: string): T; + /** + * @see objectPath.del + */ + del(object: T): T; + /** + * @see objectPath.del + */ + del():void; + + /*======== Has =========*/ + /** + * Tests path existence + * @param {object} object + * @param {string[]|string} path + * @return object + */ + has(object: T, path: IStringArray): boolean; + /** + * @see objectPath.has + */ + has(object: T, path: INumberArray): boolean; + /** + * @see objectPath.has + */ + has(object: T, path: string): boolean; + /** + * @see objectPath.has + */ + has(object: T, path: number): boolean; + /** + * @see objectPath.has + */ + has(object: T): boolean; + /** + * @see objectPath.has + */ + has(): boolean; + + /*======== Get =========*/ + /** + * Get a path from an object + * @param {object} object + * @param {string|string[]|number|number[]} path + * @param {*} [defaultValue=undefined] + */ + get(object: T, path: string, defaultValue?: TResult): TResult; + /** + * @see objectPath.get + */ + get(object: T, path: IStringArray, defaultValue?: TResult): TResult; + /** + * @see objectPath.get + */ + get(object: T, path: number, defaultValue?: TResult): TResult; + /** + * @see objectPath.get + */ + get(object: T, path: INumberArray, defaultValue?: TResult): TResult; + /** + * @see objectPath.get + */ + get(object: T): T; + /** + * @see objectPath.get + */ + get():void; + + /*======== Set =========*/ + /** + * Set a path to a value + * @param {object} object + * @param {string|string[]|number|number[]} path + * @param {*} value + * @param {boolean} [doNotReplace=false] + * @return Any existing value on the path if any + */ + set(object: T, path: string, value: any, doNotReplace?:boolean): TExisting; + /** + * @see objectPath.set + */ + set(object: T, path: number, value: any, doNotReplace?:boolean): TExisting; + /** + * @see objectPath.set + */ + set(object: T, path: IStringArray, value: any, doNotReplace?:boolean): TExisting; + /** + * @see objectPath.set + */ + set(object: T, path: INumberArray, value: any, doNotReplace?:boolean): TExisting; + /** + * @see objectPath.set + */ + set(object: T): T; + /** + * @see objectPath.set + */ + set():void; + + /*======== Push =========*/ + /** + * Create (if path isn't an array) and push the value to it. Can push unlimited number of values + * @param {object} object + */ + push(object: T, path: INumberArray, ...args:any[]):void; + /** + * @see objectPath.push + */ + push(object: T, path: IStringArray, ...args:any[]):void; + /** + * @see objectPath.push + */ + push(object: T, path: number, ...args:any[]):void; + /** + * @see objectPath.push + */ + push(object: T, path: string, ...args:any[]):void; + /** + * @see objectPath.push + */ + push():void; + + /*======== Coalesce =========*/ + /** + * Get the first non undefined property + * @param {object} object + * @param {string[]|string[][]|number[]|number[][]} paths + * @param {*} defaultValue + * @return {*} + */ + coalesce(object: T, paths: IStringArray, defaultValue?: any):TResult; + /** + * @see objectPath.coalesce + */ + coalesce(object: T, paths: INumberArray, defaultValue?: any):TResult; + /** + * @see objectPath.coalesce + */ + coalesce(object: T, paths: IStringArray[], defaultValue?: any):TResult; + /** + * @see objectPath.coalesce + */ + coalesce(object: T, paths: INumberArray[], defaultValue?: any):TResult; + + /*======== Empty =========*/ + /** + * Empty a path. Arrays are set to length 0, objects have all elements deleted, strings + * are set to empty, numbers to 0, everything else is set to null + * @param {object} object + * @param {string|string[]|number[]} path + */ + empty(object: T, path: string):TResult; + /** + * @see objectPath.empty + */ + empty(object: T, path: INumberArray):TResult; + /** + * @see objectPath.empty + */ + empty(object: T, path: IStringArray):TResult; + /** + * @see objectPath.empty + */ + empty(object: T, path: number):TResult; + /** + * @see objectPath.empty + */ + empty(object: T):T; + /** + * @see objectPath.empty + */ + empty():void; + + /*======== EnsureExists =========*/ + /** + * Set a value if it doesn't exist, do nothing if it does + * @param {object} object + * @param {string|string[]|number|number[]} path + */ + ensureExists(object: T, path: string, value: any):TResult; + /** + * @see objectPath.ensureExists + */ + ensureExists(object: T, path: number, value: any):TResult; + /** + * @see objectPath.ensureExists + */ + ensureExists(object: T, path: INumberArray, value: any):TResult; + /** + * @see objectPath.ensureExists + */ + ensureExists(object: T, path: IStringArray, value: any):TResult; + /** + * @see objectPath.ensureExists + */ + ensureExists(object: T): T; + /** + * @see objectPath.ensureExists + */ + ensureExists():void; + + /*======== Insert =========*/ + /** + * Insert an item in an array path + * @param {object} object + * @param {string|string[]|number|number[]} path + * @param {*} value + * @param {number} [at=0] + */ + insert(object: T, path: string, value: any, at?: number):void; + /** + * @see objectPath.insert + */ + insert(object: T, path: INumberArray, value: any, at?: number):void; + /** + * @see objectPath.insert + */ + insert(object: T, path: IStringArray, value: any, at?: number):void; + /** + * @see objectPath.insert + */ + insert(object: T, path: number, value: any, at?: number):void; + } + + interface IObjectPathBound { + /*======== Del =========*/ + + /** + * @see objectPath.ensureExists + */ + del(path: IStringArray): T; + /** + * @see objectPath.del + */ + del(path: INumberArray): T; + /** + * @see objectPath.del + */ + del(path: number): T; + /** + * @see objectPath.del + */ + del(path: string): T; + /** + * @see objectPath.del + */ + del(): T; + + /*======== Has =========*/ + /** + * @see objectPath.ensureExists + */ + has(path: IStringArray): boolean; + /** + * @see objectPath.has + */ + has(path: INumberArray): boolean; + /** + * @see objectPath.has + */ + has(path: string): boolean; + /** + * @see objectPath.has + */ + has(path: number): boolean; + /** + * @see objectPath.has + */ + has(): boolean; + + /*======== Get =========*/ + /** + * @see objectPath.ensureExists + */ + get(path: string, defaultValue?: TResult): TResult; + /** + * @see objectPath.get + */ + get(path: IStringArray, defaultValue?: TResult): TResult; + /** + * @see objectPath.get + */ + get(path: number, defaultValue?: TResult): TResult; + /** + * @see objectPath.get + */ + get(path: INumberArray, defaultValue?: TResult): TResult; + /** + * @see objectPath.get + */ + get(): T; + + /*======== Set =========*/ + /** + * @see objectPath.ensureExists + */ + set(path: string, value: any, doNotReplace?:boolean): TExisting; + /** + * @see objectPath.set + */ + set(path: number, value: any, doNotReplace?:boolean): TExisting; + /** + * @see objectPath.set + */ + set(path: IStringArray, value: any, doNotReplace?:boolean): TExisting; + /** + * @see objectPath.set + */ + set(path: INumberArray, value: any, doNotReplace?:boolean): TExisting; + /** + * @see objectPath.set + */ + set(): T; + + /*======== Push =========*/ + /** + * @see objectPath.ensureExists + */ + push(path: INumberArray, ...args:any[]):void; + /** + * @see objectPath.push + */ + push(path: IStringArray, ...args:any[]):void; + /** + * @see objectPath.push + */ + push(path: number, ...args:any[]):void; + /** + * @see objectPath.push + */ + push(path: string, ...args:any[]):void; + /** + * @see objectPath.push + */ + push():void; + + /*======== Coalesce =========*/ + /** + * @see objectPath.ensureExists + */ + coalesce(paths: IStringArray, defaultValue?: any):TResult; + /** + * @see objectPath.coalesce + */ + coalesce(paths: INumberArray, defaultValue?: any):TResult; + /** + * @see objectPath.coalesce + */ + coalesce(paths: IStringArray[], defaultValue?: any):TResult; + /** + * @see objectPath.coalesce + */ + coalesce(paths: INumberArray[], defaultValue?: any):TResult; + + /*======== Empty =========*/ + /** + * @see objectPath.ensureExists + */ + empty(path: string):TResult; + /** + * @see objectPath.empty + */ + empty(path: INumberArray):TResult; + /** + * @see objectPath.empty + */ + empty(path: IStringArray):TResult; + /** + * @see objectPath.empty + */ + empty(path: number):TResult; + /** + * @see objectPath.empty + */ + empty():T; + + /*======== EnsureExists =========*/ + /** + * @see objectPath.ensureExists + */ + ensureExists(path: string, value: any):TResult; + /** + * @see objectPath.ensureExists + */ + ensureExists(path: number, value: any):TResult; + /** + * @see objectPath.ensureExists + */ + ensureExists(path: INumberArray, value: any):TResult; + /** + * @see objectPath.ensureExists + */ + ensureExists(path: IStringArray, value: any):TResult; + /** + * @see objectPath.ensureExists + */ + ensureExists(): T; + + /*======== Insert =========*/ + /** + * @see objectPath.insert + */ + insert(path: string, value: any, at?: number):void; + /** + * @see objectPath.insert + */ + insert(path: INumberArray, value: any, at?: number):void; + /** + * @see objectPath.insert + */ + insert(path: IStringArray, value: any, at?: number):void; + /** + * @see objectPath.insert + */ + insert(path: number, value: any, at?: number):void; + } +} + +// browser version +declare module 'objectPath' { + export = objectPath; +} + +// node version +declare module 'object-path' { + export = objectPath; } \ No newline at end of file