diff --git a/types/object-path/index.d.ts b/types/object-path/index.d.ts index 535fbe2878..9c3055ab5d 100644 --- a/types/object-path/index.d.ts +++ b/types/object-path/index.d.ts @@ -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 +// 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; - type IMultiArray = Array; + type Path = Array | number | string; - interface IObjectPathStatic { + interface ObjectPathStatic { /** * Binds an object */ - (object: T): IObjectPathBound; - - /*======== Del =========*/ + (object: T): ObjectPathBound; /** * Deletes a member from object or array - * @param {object} object - * @param {string[]|string} path - * @return object */ - del(object: T, path: IPath): T; - /** - * @see objectPath.del - */ - del(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(object: T, path: IPath): boolean; - /** - * @see objectPath.has - */ - has(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(object: T, path: IPath, defaultValue?: TResult): TResult; - /** - * @see objectPath.get - */ - get(object: T): T; - /** - * @see objectPath.get - */ - get():void; + get(object: object, path: Path): any; + get(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(object: T, path: IPath, value: any, doNotReplace?:boolean): TExisting; - /** - * @see objectPath.set - */ - set(object: T): T; - /** - * @see objectPath.set - */ - set():void; + set( + 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(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(object: T, paths: IMultiArray, defaultValue?: TResult):TResult; + coalesce(object: object, paths: Path | Path[], defaultValue: TResult): TResult; + coalesce( + 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(object: T, path: IPath):TResult; - /** - * @see objectPath.empty - */ - empty(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(object: T, path: IPath, value: any):TExisting; - /** - * @see objectPath.ensureExists - */ - ensureExists(object: T): T; - /** - * @see objectPath.ensureExists - */ - ensureExists():void; + ensureExists(object: object, path: Path, defaultValue: TResult): TResult; + ensureExists( + 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(object: T, path: IPath, value: any, at?: number):void; + insert(object: object, path: Path, value: any, at?: number): void; } - interface IObjectPathBound { - /*======== Del =========*/ - - /** - * @see objectPath.ensureExists - */ - del(path: IPath): T; + interface ObjectPathBound { /** * @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(path: IPath, defaultValue?: TResult): TResult; /** * @see objectPath.get */ - get(): T; + get(path: Path): any; + get(path: Path, defaultValue: TResult): TResult; - /*======== Set =========*/ - /** - * @see objectPath.ensureExists - */ - set(path: IPath, value: any, doNotReplace?:boolean): TExisting; /** * @see objectPath.set */ - set(): T; + set(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(paths: IMultiArray, defaultValue?: TResult):TResult; + coalesce(paths: Path | Path[], defaultValue: TResult): TResult; + coalesce(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(path: IPath, value: any):TExisting; - /** - * @see objectPath.ensureExists - */ - ensureExists(): T; + ensureExists(path: Path, defaultValue: TResult): TResult; + ensureExists(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; diff --git a/types/object-path/object-path-tests.ts b/types/object-path/object-path-tests.ts index 4516b336a8..0515af157a 100644 --- a/types/object-path/object-path-tests.ts +++ b/types/object-path/object-path-tests.ts @@ -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(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(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('a'); // $ExpectType number | undefined +obj.empty('a'); // $ExpectType any +obj.ensureExists('a', 1); // $ExpectType 1 +obj.ensureExists('a'); // $ExpectType any +obj.ensureExists('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(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]); -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(object, 0, ['1','2']) === ['1','2']; -objectPath.get(object, 0) === 10; -objectPath.get(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(object, '1.2', true, false) === ['string']; -objectPath.set(object, '1.2', true, false) === object; -obj.set('1.2', true, false) === object; -obj.set(['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); -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); \ No newline at end of file +objectPath.withInheritedProps; // $ExpectType ObjectPathStatic +objectPath.create(); // $ExpectType ObjectPathStatic +objectPath.create({ includeInheritedProps: true }); // $ExpectType ObjectPathStatic diff --git a/types/object-path/tsconfig.json b/types/object-path/tsconfig.json index 14192cc5b0..14d40fa370 100644 --- a/types/object-path/tsconfig.json +++ b/types/object-path/tsconfig.json @@ -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" ] -} \ No newline at end of file +} diff --git a/types/object-path/tslint.json b/types/object-path/tslint.json index a41bf5d19a..f93cf8562a 100644 --- a/types/object-path/tslint.json +++ b/types/object-path/tslint.json @@ -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" }