[object-path] Update types to v0.11 (#31746)

* [object-path] Update types to v0.11

* [object-path] Fixes after review
This commit is contained in:
Dimitri Benin 2018-12-31 16:01:45 +00:00 committed by Wesley Wigham
parent b68f2ee2ac
commit 6e2b0af37a
4 changed files with 106 additions and 344 deletions

View File

@ -1,249 +1,141 @@
// Type definitions for objectPath v0.9.x
// Type definitions for objectPath 0.11
// Project: https://github.com/mariocasciaro/object-path
// Definitions by: Paulo Cesar <https://github.com/pocesar>
// BendingBender <https://github.com/BendingBender>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
declare var objectPath: ObjectPathGlobal.IObjectPathStatic;
declare const objectPath: objectPath.ObjectPathStatic & {
withInheritedProps: objectPath.ObjectPathStatic;
create(options?: objectPath.Options): objectPath.ObjectPathStatic;
};
declare namespace ObjectPathGlobal {
declare namespace objectPath {
interface Options {
includeInheritedProps?: boolean;
}
type IPath = Array<number|string>|number|string;
type IMultiArray = Array<IPath>;
type Path = Array<number | string> | number | string;
interface IObjectPathStatic {
interface ObjectPathStatic {
/**
* Binds an object
*/
<T extends {}>(object: T): IObjectPathBound<T>;
/*======== Del =========*/
<T extends object>(object: T): ObjectPathBound<T>;
/**
* 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;
del(object: object, path: Path): { [key: string]: any };
/*======== 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;
has(object: object, path: Path): 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;
get(object: object, path: Path): any;
get<TResult>(object: object, path: Path, defaultValue: TResult): TResult;
/*======== 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;
set<TResult = any>(
object: object,
path: Path,
value: TResult,
doNotReplace?: boolean
): TResult | undefined;
/*======== 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;
push(object: object, path: Path, ...items: any[]): 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;
coalesce<TResult>(object: object, paths: Path | Path[], defaultValue: TResult): TResult;
coalesce<TResult = any>(
object: object,
paths: Path | Path[],
defaultValue?: TResult
): TResult | undefined;
/*======== 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;
empty(object: object, path: Path): any;
/*======== 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;
ensureExists<TResult>(object: object, path: Path, defaultValue: TResult): TResult;
ensureExists<TResult = any>(
object: object,
path: Path,
defaultValue?: TResult
): TResult | undefined;
/*======== 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;
insert(object: object, path: Path, value: any, at?: number): void;
}
interface IObjectPathBound<T extends {}> {
/*======== Del =========*/
/**
* @see objectPath.ensureExists
*/
del(path: IPath): T;
interface ObjectPathBound<T extends object> {
/**
* @see objectPath.del
*/
del(): T;
del(path: Path): { [key: string]: any };
/*======== Has =========*/
/**
* @see objectPath.ensureExists
*/
has(path: IPath): boolean;
/**
* @see objectPath.has
*/
has(): boolean;
has(path: Path): boolean;
/*======== Get =========*/
/**
* @see objectPath.ensureExists
*/
get<TResult>(path: IPath, defaultValue?: TResult): TResult;
/**
* @see objectPath.get
*/
get(): T;
get(path: Path): any;
get<TResult>(path: Path, defaultValue: TResult): TResult;
/*======== Set =========*/
/**
* @see objectPath.ensureExists
*/
set<TExisting>(path: IPath, value: any, doNotReplace?:boolean): TExisting;
/**
* @see objectPath.set
*/
set(): T;
set<TResult = any>(path: Path, value: TResult, doNotReplace?: boolean): TResult | undefined;
/*======== Push =========*/
/**
* @see objectPath.ensureExists
*/
push(path: IPath, ...args:any[]):void;
/**
* @see objectPath.push
*/
push():void;
push(path: Path, ...items: any[]): void;
/*======== Coalesce =========*/
/**
* @see objectPath.ensureExists
* @see objectPath.coalesce
*/
coalesce<TResult>(paths: IMultiArray, defaultValue?: TResult):TResult;
coalesce<TResult>(paths: Path | Path[], defaultValue: TResult): TResult;
coalesce<TResult = any>(paths: Path | Path[], defaultValue?: TResult): TResult | undefined;
/*======== Empty =========*/
/**
* @see objectPath.ensureExists
*/
empty(path: IPath):T;
/**
* @see objectPath.empty
*/
empty():T;
empty(path: Path): any;
/*======== EnsureExists =========*/
/**
* @see objectPath.ensureExists
*/
ensureExists<TExisting>(path: IPath, value: any):TExisting;
/**
* @see objectPath.ensureExists
*/
ensureExists(): T;
ensureExists<TResult>(path: Path, defaultValue: TResult): TResult;
ensureExists<TResult = any>(path: Path, defaultValue?: TResult): TResult | undefined;
/*======== Insert =========*/
/**
* @see objectPath.insert
*/
insert(path: IPath, value: any, at?: number):void;
insert(path: Path, value: any, at?: number): void;
}
}
// browser version
declare module 'objectPath' {
export = objectPath;
}
// node version
declare module 'object-path' {
export = objectPath;
}
export = objectPath;

View File

@ -1,111 +1,57 @@
import objectPath = require('object-path');
import ObjectPath = require('object-path');
var
object = {
const object = {
one: 1,
two: {
three: 3,
four: ['4']
}
},
array: any[] = [],
Null:any = null;
three: 3,
four: ['4'],
},
};
var obj = ObjectPath(object);
objectPath.get(object, []);
objectPath.get(object, [1, 2]);
objectPath.get(object, [1, '2']);
objectPath.get(object, 1);
objectPath.get(object, '1');
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, 'a'); // $ExpectType { [key: string]: any; }
objectPath.has(object, 'a'); // $ExpectType boolean
objectPath.get(object, 'a'); // $ExpectType any
objectPath.get(object, 'a', 1); // $ExpectType 1
objectPath.set(object, 'a', 1); // $ExpectType 1 | undefined
objectPath.set(object, 'a', 1, true); // $ExpectType 1 | undefined
objectPath.push(object, 'a', 1, 2); // $ExpectType void
objectPath.coalesce(object, 'a', 1); // $ExpectType 1
objectPath.coalesce(object, 'a'); // $ExpectType any
objectPath.coalesce(object, [['a']]); // $ExpectType any
objectPath.coalesce<number>(object, 'a'); // $ExpectType number | undefined
objectPath.empty(object, 'a'); // $ExpectType any
objectPath.ensureExists(object, 'a', 1); // $ExpectType 1
objectPath.ensureExists(object, 'a'); // $ExpectType any
objectPath.ensureExists<number>(object, 'a'); // $ExpectType number | undefined
objectPath.insert(object, 'a', 1); // $ExpectType void
objectPath.insert(object, 'a', 1, 2); // $ExpectType void
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;
const obj = objectPath(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;
obj.del('a'); // $ExpectType { [key: string]: any; }
obj.has('a'); // $ExpectType boolean
obj.get('a'); // $ExpectType any
obj.get('a', 1); // $ExpectType 1
obj.set('a', 1); // $ExpectType 1 | undefined
obj.set('a', 1, true); // $ExpectType 1 | undefined
obj.push('a', 1, 2); // $ExpectType void
obj.coalesce('a', 1); // $ExpectType 1
obj.coalesce('a'); // $ExpectType any
obj.coalesce([['a']]); // $ExpectType any
obj.coalesce<number>('a'); // $ExpectType number | undefined
obj.empty('a'); // $ExpectType any
obj.ensureExists('a', 1); // $ExpectType 1
obj.ensureExists('a'); // $ExpectType any
obj.ensureExists<number>('a'); // $ExpectType number | undefined
obj.insert('a', 1); // $ExpectType void
obj.insert('a', 1, 2); // $ExpectType void
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);
objectPath.withInheritedProps; // $ExpectType ObjectPathStatic
objectPath.create(); // $ExpectType ObjectPathStatic
objectPath.create({ includeInheritedProps: true }); // $ExpectType ObjectPathStatic

View File

@ -6,7 +6,7 @@
],
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": false,
"strictNullChecks": true,
"strictFunctionTypes": true,
"baseUrl": "../",
"typeRoots": [
@ -20,4 +20,4 @@
"index.d.ts",
"object-path-tests.ts"
]
}
}

View File

@ -1,79 +1,3 @@
{
"extends": "dtslint/dt.json",
"rules": {
"adjacent-overload-signatures": false,
"array-type": false,
"arrow-return-shorthand": false,
"ban-types": false,
"callable-types": false,
"comment-format": false,
"dt-header": false,
"eofline": false,
"export-just-namespace": false,
"import-spacing": false,
"interface-name": false,
"interface-over-type-literal": false,
"jsdoc-format": false,
"max-line-length": false,
"member-access": false,
"new-parens": false,
"no-any-union": false,
"no-boolean-literal-compare": false,
"no-conditional-assignment": false,
"no-consecutive-blank-lines": false,
"no-construct": false,
"no-declare-current-package": false,
"no-duplicate-imports": false,
"no-duplicate-variable": false,
"no-empty-interface": false,
"no-for-in-array": false,
"no-inferrable-types": false,
"no-internal-module": false,
"no-irregular-whitespace": false,
"no-mergeable-namespace": false,
"no-misused-new": false,
"no-namespace": false,
"no-object-literal-type-assertion": false,
"no-padding": false,
"no-redundant-jsdoc": false,
"no-redundant-jsdoc-2": false,
"no-redundant-undefined": false,
"no-reference-import": false,
"no-relative-import-in-test": false,
"no-self-import": false,
"no-single-declare-module": false,
"no-string-throw": false,
"no-unnecessary-callback-wrapper": false,
"no-unnecessary-class": false,
"no-unnecessary-generics": false,
"no-unnecessary-qualifier": false,
"no-unnecessary-type-assertion": false,
"no-useless-files": false,
"no-var-keyword": false,
"no-var-requires": false,
"no-void-expression": false,
"no-trailing-whitespace": false,
"object-literal-key-quotes": false,
"object-literal-shorthand": false,
"one-line": false,
"one-variable-per-declaration": false,
"only-arrow-functions": false,
"prefer-conditional-expression": false,
"prefer-const": false,
"prefer-declare-function": false,
"prefer-for-of": false,
"prefer-method-signature": false,
"prefer-template": false,
"radix": false,
"semicolon": false,
"space-before-function-paren": false,
"space-within-parens": false,
"strict-export-declare-modifiers": false,
"trim-file": false,
"triple-equals": false,
"typedef-whitespace": false,
"unified-signatures": false,
"void-return": false,
"whitespace": false
}
"extends": "dtslint/dt.json"
}