From 2b42a4123b6343a3adf176bcdecbcb70decb850b Mon Sep 17 00:00:00 2001 From: Thomas Lent Date: Tue, 24 Oct 2017 14:07:17 -0400 Subject: [PATCH] Complete definition for js-base64 and update to version 2.3 (#20929) * Update version number * Format existing code * Add name to authors * Add tslint.json * Rewrite to match module.d.ts template * Add missing functions * Add extendString and related String augmentation * Correct uriSafe parameter to be optional * Correct extended functions to be required * Add new tests and fix global Base64 * Add missing uriSafe parameter to toBase64 * Enable strictNullChecks * Add missing test case --- types/js-base64/index.d.ts | 81 +++++++++---------- types/js-base64/js-base64-tests.ts | 10 --- types/js-base64/test/js-base64-global.test.ts | 19 +++++ types/js-base64/test/js-base64-module.test.ts | 21 +++++ types/js-base64/tsconfig.json | 15 ++-- types/js-base64/tslint.json | 3 + 6 files changed, 85 insertions(+), 64 deletions(-) delete mode 100644 types/js-base64/js-base64-tests.ts create mode 100644 types/js-base64/test/js-base64-global.test.ts create mode 100644 types/js-base64/test/js-base64-module.test.ts create mode 100644 types/js-base64/tslint.json diff --git a/types/js-base64/index.d.ts b/types/js-base64/index.d.ts index bd195e3885..00a77fe462 100644 --- a/types/js-base64/index.d.ts +++ b/types/js-base64/index.d.ts @@ -1,54 +1,45 @@ -// Type definitions for js-base64 v2.1.9 +// Type definitions for js-base64 2.3 // Project: https://github.com/dankogai/js-base64 -// Definitions by: Denis Carriere +// Definitions by: Denis Carriere , Tommy Lent // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped -/** -## TODO +export namespace Base64 { + const VERSION: string; -add methods: -- [x] encode -- [x] encodeURI -- [x] decode -- [ ] atob -- [ ] btoa -- [ ] fromBase64 -- [ ] toBase64 -- [ ] utob -- [ ] btou -- [ ] noConflict - */ + function encode(s: string, uriSafe?: boolean): string; -declare module 'js-base64' { - namespace JSBase64 { - const Base64: Base64Static - interface Base64Static { - /** - * .encode - * @param {String} string - * @return {String} - */ - encode(base64: string): string; + function encodeURI(s: string): string; - /** - * .encodeURI - * @param {String} string - * @return {String} - */ - encodeURI(base64: string): string + function decode(base64: string): string; - /** - * .decode - * @param {String} string - * @return {String} - */ - decode(base64: string): string + function atob(base64: string): string; - /** - * Library version - */ - VERSION:string - } - } - export = JSBase64 + function btoa(s: string): string; + + function fromBase64(base64: string): string; + + function toBase64(s: string, uriSafe?: boolean): string; + + function btou(s: string): string; + + function utob(s: string): string; + + function noConflict(): typeof Base64; + + function extendString(): void; +} + +// Helper to allow referencing Base64 from inside the global declaration without creating a self reference +/* tslint:disable:strict-export-declare-modifiers */ +type Base64_ = typeof Base64; +/* tslint:enable:strict-export-declare-modifiers */ + +declare global { + interface String { + fromBase64(): string; + toBase64(uriSafe?: boolean): string; + toBase64URI(): string; + } + + const Base64: Base64_; } diff --git a/types/js-base64/js-base64-tests.ts b/types/js-base64/js-base64-tests.ts deleted file mode 100644 index 275547d296..0000000000 --- a/types/js-base64/js-base64-tests.ts +++ /dev/null @@ -1,10 +0,0 @@ -import { Base64 } from 'js-base64' - -Base64.encode('dankogai'); // ZGFua29nYWk= -Base64.encode('小飼弾'); // 5bCP6aO85by+ -Base64.encodeURI('小飼弾'); // 5bCP6aO85by- - -Base64.decode('ZGFua29nYWk='); // dankogai -Base64.decode('5bCP6aO85by+'); // 小飼弾 -// note .decodeURI() is unnecessary since it accepts both flavors -Base64.decode('5bCP6aO85by-'); // 小飼弾 diff --git a/types/js-base64/test/js-base64-global.test.ts b/types/js-base64/test/js-base64-global.test.ts new file mode 100644 index 0000000000..25c7837697 --- /dev/null +++ b/types/js-base64/test/js-base64-global.test.ts @@ -0,0 +1,19 @@ +Base64.encode(''); // $ExpectType string +Base64.encode('', true); // $ExpectType string +Base64.encodeURI(''); // $ExpectType string +Base64.decode(''); // $ExpectType string +Base64.atob(''); // $ExpectType string +Base64.btoa(''); // $ExpectType string +Base64.fromBase64(''); // $ExpectType string +Base64.toBase64(''); // $ExpectType string +Base64.toBase64('', true); // $ExpectType string +Base64.btou(''); // $ExpectType string +Base64.utob(''); // $ExpectType string + +Base64.noConflict(); // $ExpectType typeof Base64 + +Base64.extendString(); // $ExpectType void +''.toBase64(); // $ExpectType string +''.toBase64(true); // $ExpectType string +''.toBase64URI(); // $ExpectType string +''.fromBase64(); // $ExpectType string diff --git a/types/js-base64/test/js-base64-module.test.ts b/types/js-base64/test/js-base64-module.test.ts new file mode 100644 index 0000000000..fc7f7a696e --- /dev/null +++ b/types/js-base64/test/js-base64-module.test.ts @@ -0,0 +1,21 @@ +import { Base64 } from 'js-base64'; + +Base64.encode(''); // $ExpectType string +Base64.encode('', true); // $ExpectType string +Base64.encodeURI(''); // $ExpectType string +Base64.decode(''); // $ExpectType string +Base64.atob(''); // $ExpectType string +Base64.btoa(''); // $ExpectType string +Base64.fromBase64(''); // $ExpectType string +Base64.toBase64(''); // $ExpectType string +Base64.toBase64('', true); // $ExpectType string +Base64.btou(''); // $ExpectType string +Base64.utob(''); // $ExpectType string + +Base64.noConflict(); // $ExpectType typeof Base64 + +Base64.extendString(); // $ExpectType void +''.toBase64(); // $ExpectType string +''.toBase64(true); // $ExpectType string +''.toBase64URI(); // $ExpectType string +''.fromBase64(); // $ExpectType string diff --git a/types/js-base64/tsconfig.json b/types/js-base64/tsconfig.json index f0622b7bc8..2e325222f7 100644 --- a/types/js-base64/tsconfig.json +++ b/types/js-base64/tsconfig.json @@ -1,23 +1,20 @@ { "compilerOptions": { "module": "commonjs", - "lib": [ - "es6" - ], + "lib": ["es6"], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "strictFunctionTypes": true, "baseUrl": "../", - "typeRoots": [ - "../" - ], + "typeRoots": ["../"], "types": [], "noEmit": true, "forceConsistentCasingInFileNames": true }, "files": [ "index.d.ts", - "js-base64-tests.ts" + "test/js-base64-module.test.ts", + "test/js-base64-global.test.ts" ] -} \ No newline at end of file +} diff --git a/types/js-base64/tslint.json b/types/js-base64/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/js-base64/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +}