From 6435478e4d1c6eb147c758d88cb00b938d9c16da Mon Sep 17 00:00:00 2001 From: TANAKA Koichi Date: Sun, 5 Feb 2017 01:39:17 +0900 Subject: [PATCH 1/2] Add definition for env-to-object --- env-to-object/env-to-object-tests.ts | 33 ++++++++++++++++++++++++++ env-to-object/index.d.ts | 35 ++++++++++++++++++++++++++++ env-to-object/tsconfig.json | 20 ++++++++++++++++ env-to-object/tslint.json | 1 + 4 files changed, 89 insertions(+) create mode 100644 env-to-object/env-to-object-tests.ts create mode 100644 env-to-object/index.d.ts create mode 100644 env-to-object/tsconfig.json create mode 100644 env-to-object/tslint.json diff --git a/env-to-object/env-to-object-tests.ts b/env-to-object/env-to-object-tests.ts new file mode 100644 index 0000000000..77ea18327e --- /dev/null +++ b/env-to-object/env-to-object-tests.ts @@ -0,0 +1,33 @@ +import * as envToObject from 'env-to-object'; + +const map = { + 'NODE_ENV': { + 'keypath': 'env', + 'type': 'string' + }, + 'PORT': { + 'keypath': 'server.port', + 'type': 'number' + }, + 'SSL': { + 'keypath': 'server.ssl', + 'type': 'boolean' + }, + 'LOG_LEVEL': { + 'keypath': 'logger.level', + 'type': 'string' + }, + 'INT': { + 'keypath': 'int', + 'type': 'integer', + 'radix': '2' + } +}; + +const result1:any = envToObject(map); +const result2:any = envToObject(map, { + 'my-custom-type': (str: string, opts: any) => { + let foo: any = JSON.parse(str); + return foo; + } +}); diff --git a/env-to-object/index.d.ts b/env-to-object/index.d.ts new file mode 100644 index 0000000000..1bda571f98 --- /dev/null +++ b/env-to-object/index.d.ts @@ -0,0 +1,35 @@ +// Type definitions for env-to-object 1.1 +// Project: https://github.com/kgryte/node-env-to-object#readme +// Definitions by: TANAKA Koichi +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +declare namespace env { + export interface Mappings { + [enviromentVariableName: string]: Mapping; + } + + export type Mapping = IntegerMapping | BooleanMapping | GenericMapping; + + interface GenericMapping { + keypath: string; + type?: string; + [opt: string]: any; + } + + interface IntegerMapping extends GenericMapping { + type: "integer"; + radix: number; + } + + interface BooleanMapping { + type: "boolean"; + strict: boolean; + } + + export interface Parsers { + [parserName: string]: (str: string, opts: any) => any; + } +} + +declare function env(map: env.Mappings, parsers?: env.Parsers): any; +export = env; diff --git a/env-to-object/tsconfig.json b/env-to-object/tsconfig.json new file mode 100644 index 0000000000..68efe0c7ca --- /dev/null +++ b/env-to-object/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "env-to-object-tests.ts" + ] +} diff --git a/env-to-object/tslint.json b/env-to-object/tslint.json new file mode 100644 index 0000000000..377cc837d4 --- /dev/null +++ b/env-to-object/tslint.json @@ -0,0 +1 @@ +{ "extends": "../tslint.json" } From 0a4823399e2cd4fc95585f11d5757c70b05af2c5 Mon Sep 17 00:00:00 2001 From: TANAKA Koichi Date: Sun, 5 Feb 2017 19:23:34 +0900 Subject: [PATCH 2/2] env-to-object: fix 2nd argument Official README is wrong. --- env-to-object/env-to-object-tests.ts | 8 +++++--- env-to-object/index.d.ts | 6 +++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/env-to-object/env-to-object-tests.ts b/env-to-object/env-to-object-tests.ts index 77ea18327e..e2c1fd4e53 100644 --- a/env-to-object/env-to-object-tests.ts +++ b/env-to-object/env-to-object-tests.ts @@ -26,8 +26,10 @@ const map = { const result1:any = envToObject(map); const result2:any = envToObject(map, { - 'my-custom-type': (str: string, opts: any) => { - let foo: any = JSON.parse(str); - return foo; + parsers: { + 'my-custom-type': (str: string, opts: any) => { + let foo: any = JSON.parse(str); + return foo; + } } }); diff --git a/env-to-object/index.d.ts b/env-to-object/index.d.ts index 1bda571f98..1ec7a3c994 100644 --- a/env-to-object/index.d.ts +++ b/env-to-object/index.d.ts @@ -29,7 +29,11 @@ declare namespace env { export interface Parsers { [parserName: string]: (str: string, opts: any) => any; } + + export interface Options { + parsers: Parsers; + } } -declare function env(map: env.Mappings, parsers?: env.Parsers): any; +declare function env(map: env.Mappings, options?: env.Options): any; export = env;