From f8ce3b3f9fc26484f43dbf85a311788ed7a7f364 Mon Sep 17 00:00:00 2001 From: Ryan Sonshine Date: Sun, 27 Jan 2019 14:11:52 -0500 Subject: [PATCH 1/6] Added type definitions for dlv --- types/dlv/dlv-tests.ts | 44 +++++++++++++++++++++++++++++++++++++++++ types/dlv/index.d.ts | 22 +++++++++++++++++++++ types/dlv/tsconfig.json | 23 +++++++++++++++++++++ types/dlv/tslint.json | 3 +++ 4 files changed, 92 insertions(+) create mode 100644 types/dlv/dlv-tests.ts create mode 100644 types/dlv/index.d.ts create mode 100644 types/dlv/tsconfig.json create mode 100644 types/dlv/tslint.json diff --git a/types/dlv/dlv-tests.ts b/types/dlv/dlv-tests.ts new file mode 100644 index 0000000000..abe4b259c3 --- /dev/null +++ b/types/dlv/dlv-tests.ts @@ -0,0 +1,44 @@ +import dlv from 'dlv'; + +const obj = { + undef: undefined, + zero: 0, + one: 1, + n: null, + f: false, + a: { + two: 2, + b: { + three: 3, + c: { + four: 4 + } + } + } +}; + +// Test without defaults +dlv(obj, ''); +dlv(obj, 'one'); +dlv(obj, 'one.two'); +dlv(obj, 'a'); +dlv(obj, 'a.two'); +dlv(obj, 'a.b'); +dlv(obj, 'a.b.three'); +dlv(obj, 'a.b.c'); +dlv(obj, 'a.b.c.four'); +dlv(obj, 'n'); +dlv(obj, 'n.badkey'); +dlv(obj, 'f'); +dlv(obj, 'f.badkey'); + +// Test defaults +dlv(obj, '', 'foo'); +dlv(obj, 'undef', 'foo'); +dlv(obj, 'n', null); +dlv(obj, 'n.badkey', 'foo'); +dlv(obj, 'zero', 0); +dlv(obj, 'a.badkey', 'foo'); +dlv(obj, 'a.badkey.anotherbadkey', 'foo'); +dlv(obj, 'f', false); +dlv(obj, 'f.badkey', 'foo'); diff --git a/types/dlv/index.d.ts b/types/dlv/index.d.ts new file mode 100644 index 0000000000..ef84668e7d --- /dev/null +++ b/types/dlv/index.d.ts @@ -0,0 +1,22 @@ +// Type definitions for dlv 1.1.2 +// Project: https://github.com/developit/dlv +// Definitions by: Ryan Sonshine +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 +declare module 'dlv' { + /** + * Safely get a dot-notated path within a nested object, with ability to + * return a default if the full key path does not exist or the value is + * undefined + * + * @param {object} object Object to look up the key on + * @param {(string | string[])} key Key in dot notation to lookup. Can also be an array key. + * @param {T} [defaultValue] Default value to return if the full key path does not exist on the object + * @returns {(any | T))} Value if found in path, the default if not found, undefined if no default provided + */ + export default function dlv( + object : object, + key : string | string[], + defaultValue?: T, + ): any | T; +} diff --git a/types/dlv/tsconfig.json b/types/dlv/tsconfig.json new file mode 100644 index 0000000000..ab250de319 --- /dev/null +++ b/types/dlv/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "dlv-tests.ts" + ] +} diff --git a/types/dlv/tslint.json b/types/dlv/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/dlv/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} From 5afbc6c1d6d3b897f666a937bbad0a5459de328a Mon Sep 17 00:00:00 2001 From: Ryan Sonshine Date: Sun, 27 Jan 2019 14:22:52 -0500 Subject: [PATCH 2/6] Fixes union type for dlv return type --- types/dlv/index.d.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/types/dlv/index.d.ts b/types/dlv/index.d.ts index ef84668e7d..845710e524 100644 --- a/types/dlv/index.d.ts +++ b/types/dlv/index.d.ts @@ -11,12 +11,12 @@ declare module 'dlv' { * * @param {object} object Object to look up the key on * @param {(string | string[])} key Key in dot notation to lookup. Can also be an array key. - * @param {T} [defaultValue] Default value to return if the full key path does not exist on the object - * @returns {(any | T))} Value if found in path, the default if not found, undefined if no default provided + * @param {any} [defaultValue] Default value to return if the full key path does not exist on the object + * @returns {any} Value if found in path, the default if not found, undefined if no default provided */ - export default function dlv( + export default function dlv( object : object, key : string | string[], - defaultValue?: T, - ): any | T; + defaultValue?: any, + ): any; } From 3b7700e7531121e45645d1bce9b19cba2af7eddb Mon Sep 17 00:00:00 2001 From: Ryan Sonshine Date: Sun, 27 Jan 2019 14:33:54 -0500 Subject: [PATCH 3/6] Uses dts-gen to set up module structure --- types/dlv/index.d.ts | 34 ++++++++++++++-------------------- types/dlv/tsconfig.json | 1 - types/dlv/tslint.json | 4 +--- 3 files changed, 15 insertions(+), 24 deletions(-) diff --git a/types/dlv/index.d.ts b/types/dlv/index.d.ts index 845710e524..b8f2608fe9 100644 --- a/types/dlv/index.d.ts +++ b/types/dlv/index.d.ts @@ -1,22 +1,16 @@ -// Type definitions for dlv 1.1.2 -// Project: https://github.com/developit/dlv +// Type definitions for dlv 1.1 +// Project: https://github.com/developit/dlv#readme // Definitions by: Ryan Sonshine // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -// TypeScript Version: 2.8 -declare module 'dlv' { - /** - * Safely get a dot-notated path within a nested object, with ability to - * return a default if the full key path does not exist or the value is - * undefined - * - * @param {object} object Object to look up the key on - * @param {(string | string[])} key Key in dot notation to lookup. Can also be an array key. - * @param {any} [defaultValue] Default value to return if the full key path does not exist on the object - * @returns {any} Value if found in path, the default if not found, undefined if no default provided - */ - export default function dlv( - object : object, - key : string | string[], - defaultValue?: any, - ): any; -} + +/** + * Safely get a dot-notated path within a nested object, with ability to + * return a default if the full key path does not exist or the value is + * undefined + * + * @param {object} object Object to look up the key on + * @param {(string | string[])} key Key in dot notation to lookup. Can also be an array key. + * @param {any} [defaultValue] Default value to return if the full key path does not exist on the object + * @returns {any} Value if found in path, the default if not found, undefined if no default provided +*/ +export default function dlv(object: object, key: string | string[], defaultValue?: any): any; diff --git a/types/dlv/tsconfig.json b/types/dlv/tsconfig.json index ab250de319..34d76ae224 100644 --- a/types/dlv/tsconfig.json +++ b/types/dlv/tsconfig.json @@ -7,7 +7,6 @@ "noImplicitAny": true, "noImplicitThis": true, "strictNullChecks": true, - "strictFunctionTypes": true, "baseUrl": "../", "typeRoots": [ "../" diff --git a/types/dlv/tslint.json b/types/dlv/tslint.json index f93cf8562a..3db14f85ea 100644 --- a/types/dlv/tslint.json +++ b/types/dlv/tslint.json @@ -1,3 +1 @@ -{ - "extends": "dtslint/dt.json" -} +{ "extends": "dtslint/dt.json" } From 36a2c05baab019858e42444c26eb37d402b58de7 Mon Sep 17 00:00:00 2001 From: Ryan Sonshine Date: Sun, 27 Jan 2019 14:40:51 -0500 Subject: [PATCH 4/6] Adds strictFunctionTypes to tsconfig --- types/dlv/tsconfig.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/types/dlv/tsconfig.json b/types/dlv/tsconfig.json index 34d76ae224..222ce070e7 100644 --- a/types/dlv/tsconfig.json +++ b/types/dlv/tsconfig.json @@ -13,7 +13,8 @@ ], "types": [], "noEmit": true, - "forceConsistentCasingInFileNames": true + "forceConsistentCasingInFileNames": true, + "strictFunctionTypes": true }, "files": [ "index.d.ts", From 6025289427c09bc854ceebd6a83dbf9eca474a61 Mon Sep 17 00:00:00 2001 From: Ryan Sonshine Date: Sun, 27 Jan 2019 14:44:42 -0500 Subject: [PATCH 5/6] Adds typescript version to type definition file --- types/dlv/index.d.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/types/dlv/index.d.ts b/types/dlv/index.d.ts index b8f2608fe9..3d86aa91b5 100644 --- a/types/dlv/index.d.ts +++ b/types/dlv/index.d.ts @@ -2,6 +2,7 @@ // Project: https://github.com/developit/dlv#readme // Definitions by: Ryan Sonshine // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 /** * Safely get a dot-notated path within a nested object, with ability to From 73f13bed814d26fdd051a945022959ef81fb8b04 Mon Sep 17 00:00:00 2001 From: Ryan Sonshine Date: Sun, 27 Jan 2019 14:54:17 -0500 Subject: [PATCH 6/6] Removes docstring from dlv type definition --- types/dlv/index.d.ts | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/types/dlv/index.d.ts b/types/dlv/index.d.ts index 3d86aa91b5..5813ec0595 100644 --- a/types/dlv/index.d.ts +++ b/types/dlv/index.d.ts @@ -8,10 +8,5 @@ * Safely get a dot-notated path within a nested object, with ability to * return a default if the full key path does not exist or the value is * undefined - * - * @param {object} object Object to look up the key on - * @param {(string | string[])} key Key in dot notation to lookup. Can also be an array key. - * @param {any} [defaultValue] Default value to return if the full key path does not exist on the object - * @returns {any} Value if found in path, the default if not found, undefined if no default provided -*/ + */ export default function dlv(object: object, key: string | string[], defaultValue?: any): any;