mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-04-24 05:06:02 +08:00
Move all packages to a types directory
This commit is contained in:
249
types/object-path/index.d.ts
vendored
Normal file
249
types/object-path/index.d.ts
vendored
Normal file
@@ -0,0 +1,249 @@
|
||||
// Type definitions for objectPath v0.9.x
|
||||
// Project: https://github.com/mariocasciaro/object-path
|
||||
// Definitions by: Paulo Cesar <https://github.com/pocesar/>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
|
||||
declare var objectPath: ObjectPathGlobal.IObjectPathStatic;
|
||||
|
||||
declare namespace ObjectPathGlobal {
|
||||
|
||||
type IPath = Array<number|string>|number|string;
|
||||
type IMultiArray = Array<IPath>;
|
||||
|
||||
interface IObjectPathStatic {
|
||||
/**
|
||||
* Binds an object
|
||||
*/
|
||||
<T extends {}>(object: T): IObjectPathBound<T>;
|
||||
|
||||
/*======== Del =========*/
|
||||
|
||||
/**
|
||||
* Deletes a member from object or array
|
||||
* @param {object} object
|
||||
* @param {string[]|string} path
|
||||
* @return object
|
||||
*/
|
||||
del<T extends {}>(object: T, path: IPath): T;
|
||||
/**
|
||||
* @see objectPath.del
|
||||
*/
|
||||
del<T extends {}>(object: T):T;
|
||||
/**
|
||||
* @see objectPath.del
|
||||
*/
|
||||
del():void;
|
||||
|
||||
/*======== Has =========*/
|
||||
/**
|
||||
* Tests path existence
|
||||
* @param {object} object
|
||||
* @param {string[]|string} path
|
||||
* @return object
|
||||
*/
|
||||
has<T extends {}>(object: T, path: IPath): boolean;
|
||||
/**
|
||||
* @see objectPath.has
|
||||
*/
|
||||
has<T extends {}>(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<T extends {}, TResult>(object: T, path: IPath, 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: IPath, 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: IPath, ...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: IMultiArray, defaultValue?: TResult):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: IPath):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 {}, TExisting>(object: T, path: IPath, value: any):TExisting;
|
||||
/**
|
||||
* @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: IPath, value: any, at?: number):void;
|
||||
}
|
||||
|
||||
interface IObjectPathBound<T extends {}> {
|
||||
/*======== Del =========*/
|
||||
|
||||
/**
|
||||
* @see objectPath.ensureExists
|
||||
*/
|
||||
del(path: IPath): T;
|
||||
/**
|
||||
* @see objectPath.del
|
||||
*/
|
||||
del(): T;
|
||||
|
||||
/*======== Has =========*/
|
||||
/**
|
||||
* @see objectPath.ensureExists
|
||||
*/
|
||||
has(path: IPath): boolean;
|
||||
/**
|
||||
* @see objectPath.has
|
||||
*/
|
||||
has(): boolean;
|
||||
|
||||
/*======== Get =========*/
|
||||
/**
|
||||
* @see objectPath.ensureExists
|
||||
*/
|
||||
get<TResult>(path: IPath, defaultValue?: TResult): TResult;
|
||||
/**
|
||||
* @see objectPath.get
|
||||
*/
|
||||
get(): T;
|
||||
|
||||
/*======== Set =========*/
|
||||
/**
|
||||
* @see objectPath.ensureExists
|
||||
*/
|
||||
set<TExisting>(path: IPath, value: any, doNotReplace?:boolean): TExisting;
|
||||
/**
|
||||
* @see objectPath.set
|
||||
*/
|
||||
set(): T;
|
||||
|
||||
/*======== Push =========*/
|
||||
/**
|
||||
* @see objectPath.ensureExists
|
||||
*/
|
||||
push(path: IPath, ...args:any[]):void;
|
||||
/**
|
||||
* @see objectPath.push
|
||||
*/
|
||||
push():void;
|
||||
|
||||
/*======== Coalesce =========*/
|
||||
/**
|
||||
* @see objectPath.ensureExists
|
||||
*/
|
||||
coalesce<TResult>(paths: IMultiArray, defaultValue?: TResult):TResult;
|
||||
|
||||
/*======== Empty =========*/
|
||||
/**
|
||||
* @see objectPath.ensureExists
|
||||
*/
|
||||
empty(path: IPath):T;
|
||||
/**
|
||||
* @see objectPath.empty
|
||||
*/
|
||||
empty():T;
|
||||
|
||||
/*======== EnsureExists =========*/
|
||||
/**
|
||||
* @see objectPath.ensureExists
|
||||
*/
|
||||
ensureExists<TExisting>(path: IPath, value: any):TExisting;
|
||||
/**
|
||||
* @see objectPath.ensureExists
|
||||
*/
|
||||
ensureExists(): T;
|
||||
|
||||
/*======== Insert =========*/
|
||||
/**
|
||||
* @see objectPath.insert
|
||||
*/
|
||||
insert(path: IPath, value: any, at?: number):void;
|
||||
}
|
||||
}
|
||||
|
||||
// browser version
|
||||
declare module 'objectPath' {
|
||||
export = objectPath;
|
||||
}
|
||||
|
||||
// node version
|
||||
declare module 'object-path' {
|
||||
export = objectPath;
|
||||
}
|
||||
111
types/object-path/object-path-tests.ts
Normal file
111
types/object-path/object-path-tests.ts
Normal file
@@ -0,0 +1,111 @@
|
||||
|
||||
|
||||
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(object, array) === object;
|
||||
objectPath.del(object, [1,2]) === object;
|
||||
objectPath.del(object, [1,'2']) === object;
|
||||
objectPath.del(object, 1) === object;
|
||||
objectPath.del(object, '1') === 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,'2',3]) === true;
|
||||
objectPath.has(object, 1) === false;
|
||||
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,'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;
|
||||
objectPath.coalesce(object, [1,[1,1],'1',[1,'1']], 1) === 1;
|
||||
obj.coalesce([1,[1,1],'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);
|
||||
objectPath.ensureExists<typeof object, number>(object, ['1','2'], 2) === 3;
|
||||
objectPath.ensureExists<typeof object, any[][]>(object, ['1','2'], 2) === [[]];
|
||||
obj.ensureExists<any[][]>(['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.push(object, [1, 'two'], [1,'2', 3, false]);
|
||||
obj.push(['one','two'], [1,'2', 3, false]);
|
||||
obj.push(['one', 2], [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.get<typeof object, number>(object, 0) === 10;
|
||||
obj.get(0, 10) === 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);
|
||||
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;
|
||||
obj.set<typeof object>('1.2', true, false) === object;
|
||||
obj.set<typeof object>(['1','2'], true, false) === object;
|
||||
obj.set<typeof 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);
|
||||
objectPath.insert(object, ['1',2], 1, 6);
|
||||
obj.insert(['1.2'], 1, 6);
|
||||
obj.insert(1, 1, 6);
|
||||
obj.insert(['1',1], 1, 6);
|
||||
obj.insert('1', 1, 6);
|
||||
22
types/object-path/tsconfig.json
Normal file
22
types/object-path/tsconfig.json
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": false,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"object-path-tests.ts"
|
||||
]
|
||||
}
|
||||
Reference in New Issue
Block a user