Merge branch 'object-path' of github.com:pocesar/DefinitelyTyped into pocesar-object-path

This commit is contained in:
vvakame
2014-07-30 23:15:27 +09:00
3 changed files with 309 additions and 2 deletions

View File

@@ -252,7 +252,7 @@ All definitions files include a header with the author and editors, so at some p
* [Node.js](http://nodejs.org/) (from TypeScript samples)
* [node_redis](https://github.com/mranney/node_redis) (by [Boris Yankov](https://github.com/borisyankov))
* [node-ffi](https://github.com/rbranson/node-ffi) (by [Paul Loyd](https://github.com/loyd))
* [node-form] (https://github.com/rsamec/form) (by [Roman Samec] (https://github.com/rsamec))
* [node-form](https://github.com/rsamec/form) (by [Roman Samec](https://github.com/rsamec))
* [node-git](https://github.com/christkv/node-git) (by [vvakame](https://github.com/vvakame))
* [nodeunit](https://github.com/caolan/nodeunit) (by [Jeff Goddard](https://github.com/jedigo))
* [node_zeromq](https://github.com/JustinTulloss/zeromq.node) (by [Dave McKeown](https://github.com/davemckeown))
@@ -261,6 +261,7 @@ All definitions files include a header with the author and editors, so at some p
* [notify.js](https://github.com/alexgibson/notify.js) (by [soundTricker](https://github.com/soundTricker))
* [NProgress](https://github.com/rstacruz/nprogress) (by [Judah Gabriel Himango](https://github.com/judahgabriel))
* [Numeral.js](https://github.com/adamwdraper/Numeral-js) (by [Vincent Bortone](https://github.com/vbortone/))
* [object-path](https://github.com/mariocasciaro/object-path) (by [Paulo Cesar](https://github.com/pocesar/))
* [ocLazyLoad](https://github.com/ocombe/ocLazyLoad) (by [Roland Zwaga](https://github.com/rolandzwaga/))
* [OpenLayers](https://github.com/openlayers/openlayers) (by [Ilya Bolkhovsky](https://github.com/bolhovsky/))
* [Optimist](https://github.com/substack/node-optimist) (by [Carlos Ballesteros Velasco](https://github.com/soywiz))
@@ -294,7 +295,7 @@ All definitions files include a header with the author and editors, so at some p
* [Raphael](http://raphaeljs.com/) (by [CheCoxshall](https://github.com/CheCoxshall))
* [Restangular](https://github.com/mgonto/restangular/) (by [Boris Yankov](https://github.com/borisyankov))
* [require.js](http://requirejs.org/) (by [Josh Baldwin](https://github.com/jbaldwin/))
* [rtree.js] (https://github.com/leaflet-extras/RTree) (by [Omede Firouz](https://github.com/oefirouz))
* [rtree.js](https://github.com/leaflet-extras/RTree) (by [Omede Firouz](https://github.com/oefirouz))
* [Sammy.js](http://sammyjs.org/) (by [Boris Yankov](https://github.com/borisyankov))
* [Select2](http://ivaynberg.github.com/select2/) (by [Boris Yankov](https://github.com/borisyankov))
* [Selenium WebDriverJS](https://code.google.com/p/selenium/) (by [Bill Armstrong](https://github.com/BillArmstrong))

View File

@@ -0,0 +1,68 @@
/// <reference path="object-path.d.ts" />
var
object = {
one: 1,
two: {
three: 3,
four: ['4']
}
},
array: any[] = [],
Null:any = null;
objectPath.del<string[]>(array) === ['12'];
objectPath.del<typeof object>(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, <Array<number[]>>[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<typeof object, number>(object, ['1','2'], 2) === 3;
objectPath.ensureExists<typeof object, any[][]>(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<typeof object, string[]>(object, 0, ['1','2']) === ['1','2'];
objectPath.get<typeof object, number>(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<typeof object, string[]>(object, '1.2', true, false) === ['string'];
objectPath.set<typeof object, typeof object>(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);

238
object-path/object-path.d.ts vendored Normal file
View File

@@ -0,0 +1,238 @@
// Type definitions for objectPath v0.6.0
// Project: https://github.com/mariocasciaro/object-path
// Definitions by: Paulo Cesar <https://github.com/pocesar/>
// 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<T extends {}>(object: T, path: IStringArray): T;
/**
* @see objectPath.del
*/
del<T extends {}>(object: T, path: INumberArray): T;
/**
* @see objectPath.del
*/
del<T extends {}>(object: T, path: number): T;
/**
* @see objectPath.del
*/
del<T extends {}>(object: T, path: string): T;
/**
* @see objectPath.del
*/
del<T extends {}>(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<T extends {}, TResult>(object: T, path: string, defaultValue?: TResult): TResult;
/**
* @see objectPath.get
*/
get<T extends {}, TResult>(object: T, path: IStringArray, defaultValue?: TResult): TResult;
/**
* @see objectPath.get
*/
get<T extends {}, TResult>(object: T, path: number, defaultValue?: TResult): TResult;
/**
* @see objectPath.get
*/
get<T extends {}, TResult>(object: T, path: INumberArray, defaultValue?: TResult): TResult;
/**
* @see objectPath.get
*/
get<T extends {}>(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<T extends {}, TExisting>(object: T, path: string, value: any, doNotReplace?:boolean): TExisting;
/**
* @see objectPath.set
*/
set<T extends {}, TExisting>(object: T, path: number, value: any, doNotReplace?:boolean): TExisting;
/**
* @see objectPath.set
*/
set<T extends {}, TExisting>(object: T, path: IStringArray, value: any, doNotReplace?:boolean): TExisting;
/**
* @see objectPath.set
*/
set<T extends {}, TExisting>(object: T, path: INumberArray, value: any, doNotReplace?:boolean): TExisting;
/**
* @see objectPath.set
*/
set<T extends {}>(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<T extends {}>(object: T, path: INumberArray, ...args:any[]):void;
/**
* @see objectPath.push
*/
push<T extends {}>(object: T, path: IStringArray, ...args:any[]):void;
/**
* @see objectPath.push
*/
push<T extends {}>(object: T, path: number, ...args:any[]):void;
/**
* @see objectPath.push
*/
push<T extends {}>(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<T extends {}, TResult>(object: T, paths: IStringArray, defaultValue?: any):TResult;
/**
* @see objectPath.coalesce
*/
coalesce<T extends {}, TResult>(object: T, paths: INumberArray, defaultValue?: any):TResult;
/**
* @see objectPath.coalesce
*/
coalesce<T extends {}, TResult>(object: T, paths: IStringArray[], defaultValue?: any):TResult;
/**
* @see objectPath.coalesce
*/
coalesce<T extends {}, TResult>(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<T extends {}, TResult>(object: T, path: string):TResult;
/**
* @see objectPath.empty
*/
empty<T extends {}, TResult>(object: T, path: INumberArray):TResult;
/**
* @see objectPath.empty
*/
empty<T extends {}, TResult>(object: T, path: IStringArray):TResult;
/**
* @see objectPath.empty
*/
empty<T extends {}, TResult>(object: T, path: number):TResult;
/**
* @see objectPath.empty
*/
empty<T extends {}>(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<T extends {}, TResult>(object: T, path: string, value: any):TResult;
/**
* @see objectPath.ensureExists
*/
ensureExists<T extends {}, TResult>(object: T, path: number, value: any):TResult;
/**
* @see objectPath.ensureExists
*/
ensureExists<T extends {}, TResult>(object: T, path: INumberArray, value: any):TResult;
/**
* @see objectPath.ensureExists
*/
ensureExists<T extends {}, TResult>(object: T, path: IStringArray, value: any):TResult;
/**
* @see objectPath.ensureExists
*/
ensureExists<T extends {}>(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<T extends {}>(object: T, path: string, value: any, at?: number):void;
/**
* @see objectPath.insert
*/
insert<T extends {}>(object: T, path: INumberArray, value: any, at?: number):void;
/**
* @see objectPath.insert
*/
insert<T extends {}>(object: T, path: IStringArray, value: any, at?: number):void;
/**
* @see objectPath.insert
*/
insert<T extends {}>(object: T, path: number, value: any, at?: number):void;
}
}
declare module 'objectPath' {
export = objectPath;
}