From ea345679fb124e9a31572ff022be85b2b0e9cb9c Mon Sep 17 00:00:00 2001 From: Mikal Madsen Date: Tue, 2 May 2017 00:19:16 +0200 Subject: [PATCH] Add Recase typings (#15744) * Add Recase typings * Add relaxed-json type definitions * Update Recase and Relaxed-JSON types as per PR review * Update Recase and Relaxed-JSON types --- types/recase/index.d.ts | 14 +++++++ types/recase/recase-tests.ts | 33 ++++++++++++++++ types/recase/tsconfig.json | 22 +++++++++++ types/recase/tslint.json | 1 + types/relaxed-json/index.d.ts | 16 ++++++++ types/relaxed-json/relaxed-json-tests.ts | 49 ++++++++++++++++++++++++ types/relaxed-json/tsconfig.json | 22 +++++++++++ types/relaxed-json/tslint.json | 1 + 8 files changed, 158 insertions(+) create mode 100644 types/recase/index.d.ts create mode 100644 types/recase/recase-tests.ts create mode 100644 types/recase/tsconfig.json create mode 100644 types/recase/tslint.json create mode 100644 types/relaxed-json/index.d.ts create mode 100644 types/relaxed-json/relaxed-json-tests.ts create mode 100644 types/relaxed-json/tsconfig.json create mode 100644 types/relaxed-json/tslint.json diff --git a/types/recase/index.d.ts b/types/recase/index.d.ts new file mode 100644 index 0000000000..29808f6130 --- /dev/null +++ b/types/recase/index.d.ts @@ -0,0 +1,14 @@ +// Type definitions for Recase 1.0 +// Project: https://www.npmjs.com/package/recase +// Definitions by: Mikal Madsen +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export interface Recase { + camelCopy(orig: any): any; + snakeCopy(orig: any): any; +} +export function create(opts: { + exceptions?: { + [origKey: string]: string; + }; +}): Recase; diff --git a/types/recase/recase-tests.ts b/types/recase/recase-tests.ts new file mode 100644 index 0000000000..fe554ecb4b --- /dev/null +++ b/types/recase/recase-tests.ts @@ -0,0 +1,33 @@ +import * as Recase from 'recase'; +const recase = Recase.create({exceptions: {FOO: 'foo'}}); + +const snake = recase.snakeCopy( + { + FOO: 1, // Exception + abcXyz: { // Normal + _abcXyz: [ // private + {__abcXyz: 1}, // very private + {___abcXyz: 1}, // very very private + ], + }, + }, +); + +const camel = recase.camelCopy( + { + foo: 1, // Exception + abc_xyz: { // Normal + _abc_xyz: [ // private + {__abc_xyz: 1}, // very private + {___abc_xyz: 1}, // very very private + ], + }, + }, +); + +recase.snakeCopy(1); +recase.camelCopy(2); +recase.snakeCopy('a'); +recase.camelCopy('b'); +recase.snakeCopy(['c', '1']); +recase.snakeCopy(['d', '1']); diff --git a/types/recase/tsconfig.json b/types/recase/tsconfig.json new file mode 100644 index 0000000000..fe94c39f52 --- /dev/null +++ b/types/recase/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "recase-tests.ts" + ] +} diff --git a/types/recase/tslint.json b/types/recase/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/recase/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" } diff --git a/types/relaxed-json/index.d.ts b/types/relaxed-json/index.d.ts new file mode 100644 index 0000000000..7b081d9acf --- /dev/null +++ b/types/relaxed-json/index.d.ts @@ -0,0 +1,16 @@ +// Type definitions for relaxed-json 1.0 +// Project: https://github.com/phadej/relaxed-json +// Definitions by: Mikal Madsen +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +export type Reviver = (this: {}, key: string, value: any) => any; +export function transform(text: string): string; +export function parse(text: string, reviver: Reviver): {}; +export function parse(text: string, opts?: { + reviver?: Reviver, + relaxed?: boolean, + warnings?: boolean, + tolerant?: boolean, + duplicate?: boolean, +}): {}; +export function stringify(obj: any): string; diff --git a/types/relaxed-json/relaxed-json-tests.ts b/types/relaxed-json/relaxed-json-tests.ts new file mode 100644 index 0000000000..dc07398f9f --- /dev/null +++ b/types/relaxed-json/relaxed-json-tests.ts @@ -0,0 +1,49 @@ +import * as RJSON from 'relaxed-json'; + +const relaxedString = ` + { // comment + foo: "bar", // identifier + "arr": [1,2,3,], // trailing comma in array + "single-quoted": 'string', + 'trailing': 4, + }, +`; + +RJSON.transform(relaxedString); + +function revive(key: string, value: any): any { + return typeof value === 'number' + ? value * 2 // return value * 2 for numbers + : value; // return everything else unchanged +} + +RJSON.parse(relaxedString, revive); + +RJSON.parse(relaxedString, { + reviver: revive, + relaxed: true, + warnings: true, + tolerant: true, + duplicate: true, +}); + +RJSON.parse(relaxedString, { + tolerant: false, + duplicate: true, +}); + +RJSON.parse(relaxedString, { + reviver: revive, +}); + +RJSON.parse(relaxedString, {}); + +const parsed = RJSON.parse(relaxedString); +RJSON.stringify(parsed); + +RJSON.stringify({ + foo: 'bar', +}); +RJSON.stringify([1, 2, 3]); +RJSON.stringify(3); +RJSON.stringify('a'); diff --git a/types/relaxed-json/tsconfig.json b/types/relaxed-json/tsconfig.json new file mode 100644 index 0000000000..235aa056fa --- /dev/null +++ b/types/relaxed-json/tsconfig.json @@ -0,0 +1,22 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "relaxed-json-tests.ts" + ] +} diff --git a/types/relaxed-json/tslint.json b/types/relaxed-json/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/relaxed-json/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }