From 9dbbc5d8cfc5cc76c595be6de4c79961dc6d0e7e Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Mon, 14 Aug 2017 20:34:44 +0200 Subject: [PATCH] [js-cookie] update typings to v2.1, enable strict null checks and linting --- types/js-cookie/index.d.ts | 16 +++--- types/js-cookie/js-cookie-tests.ts | 82 +++++++++++++----------------- types/js-cookie/tsconfig.json | 4 +- types/js-cookie/tslint.json | 1 + 4 files changed, 49 insertions(+), 54 deletions(-) create mode 100644 types/js-cookie/tslint.json diff --git a/types/js-cookie/index.d.ts b/types/js-cookie/index.d.ts index a54f4f4864..f587e1df88 100644 --- a/types/js-cookie/index.d.ts +++ b/types/js-cookie/index.d.ts @@ -1,7 +1,9 @@ -// Type definitions for js-cookie v2.0 +// Type definitions for js-cookie 2.1 // Project: https://github.com/js-cookie/js-cookie // Definitions by: Theodore Brown +// BendingBender // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 declare namespace Cookies { interface CookieAttributes { @@ -39,12 +41,12 @@ declare namespace Cookies { /** * Create a cookie */ - set(name: string, value: string | any, options?: CookieAttributes): void; + set(name: string, value: string | object, options?: CookieAttributes): void; /** * Read cookie */ - get(name: string): string; + get(name: string): string | undefined; /** * Read all available cookies @@ -55,7 +57,7 @@ declare namespace Cookies { * Returns the parsed representation of the string * stored in the cookie according to JSON.parse */ - getJSON(name: string): any; + getJSON(name: string): object; /** * Returns the parsed representation of @@ -85,11 +87,13 @@ declare namespace Cookies { * will run the converter first for each cookie. The returned * string will be used as the cookie value. */ - withConverter(converter: (value: string, name: string) => string): CookiesStatic; + withConverter(converter: CookieConverter | { write: CookieConverter; read: CookieConverter; }): CookiesStatic; } + + type CookieConverter = (value: string, name: string) => string; } -declare var Cookies: Cookies.CookiesStatic; +declare const Cookies: Cookies.CookiesStatic; export = Cookies; export as namespace Cookies; diff --git a/types/js-cookie/js-cookie-tests.ts b/types/js-cookie/js-cookie-tests.ts index 3b2509fe8a..dbc3306a1a 100644 --- a/types/js-cookie/js-cookie-tests.ts +++ b/types/js-cookie/js-cookie-tests.ts @@ -1,67 +1,57 @@ import Cookies = require("js-cookie"); -// Create a cookie, valid across the entire site + Cookies.set('name', 'value'); - -// Create a cookie that expires 7 days from now, valid across the entire site Cookies.set('name', 'value', { expires: 7 }); - -// Create an expiring cookie, valid to the path of the current page +Cookies.set('name', 'value', { expires: new Date() }); Cookies.set('name', 'value', { expires: 7, path: '' }); - -// Read cookie -Cookies.get('name'); // => 'value' -Cookies.get('nothing'); // => undefined - -// Read all available cookies -Cookies.get(); // => { name: 'value' } - -// Delete cookie -Cookies.remove('name'); - -// Delete a cookie valid to the path of the current page +Cookies.set('name', 'value', { expires: 7, path: '', domain: '' }); +Cookies.set('name', 'value', { expires: 7, path: '', domain: '', secure: true }); +Cookies.set('name', 'value', { secure: true }); +Cookies.set('name', 'value', { domain: '' }); Cookies.set('name', 'value', { path: '' }); -Cookies.remove('name'); // fail! -Cookies.remove('name', { path: '' }); // removed! -// Assign the js-cookie api to a different variable -// and restore the original "window.Cookies" -var Cookies2 = Cookies.noConflict(); -Cookies2.set('name', 'value'); +// $ExpectType string | undefined +Cookies.get('name'); + +// $ExpectType { [key: string]: string; } +Cookies.get(); + +Cookies.remove('name'); +Cookies.remove('name', { path: '' }); + +const Cookies2 = Cookies.noConflict(); +Cookies2; // $ExpectType CookiesStatic -// When creating a cookie you can pass an Array or Object Literal -// instead of a string in the value. If you do so, js-cookie will -// store the string representation of the object according to JSON.stringify Cookies.set('name', { foo: 'bar' }); -// When reading a cookie with the Cookies.getJSON api, you receive -// the parsed representation of the string stored in the cookie -// according to JSON.parse -Cookies.getJSON('name'); // => { foo: 'bar' } +// $ExpectType object +Cookies.getJSON('name'); -// Define the domain where the cookie is available -Cookies.set('name', 'value', { domain: 'sub.domain.com' }); -Cookies.get('name'); // => undefined (need to read at 'sub.domain.com') - -// Indicate that the cookie transmission requires (https) -Cookies.set('name', 'value', { secure: true }); -Cookies.get('name'); // => 'value' -Cookies.remove('name', { secure: true }); +// $ExpectType { [key: string]: any; } +Cookies.getJSON(); document.cookie = 'escaped=%u5317'; document.cookie = 'default=%E5%8C%97'; -var cookies = Cookies.withConverter(function (value, name) { +const cookies = Cookies.withConverter((value, name) => { if ( name === 'escaped' ) { return decodeURIComponent(value); } + return value; }); -cookies.get('escaped'); // 北 -cookies.get('default'); // 北 -cookies.get(); // { escaped: '北', default: '北' } +cookies.get('escaped'); -// To remove, set or declare defaults to the path of the -// current page, you just need to declare it as empty: Cookies.defaults.path = ''; - -// Deleting the property will fallback to the path: / internally: delete Cookies.defaults.path; + +const PHPCookies = Cookies.withConverter({ + write(value) { + return encodeURIComponent(value) + .replace(/%(23|24|26|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent); + }, + read(value) { + return value + .replace(/\+/g, ' ') + .replace(/(%[0-9A-Z]{2})+/g, decodeURIComponent); + } +}); diff --git a/types/js-cookie/tsconfig.json b/types/js-cookie/tsconfig.json index affebaaebb..486bcb98e4 100644 --- a/types/js-cookie/tsconfig.json +++ b/types/js-cookie/tsconfig.json @@ -7,7 +7,7 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "baseUrl": "../", "typeRoots": [ "../" @@ -20,4 +20,4 @@ "index.d.ts", "js-cookie-tests.ts" ] -} \ No newline at end of file +} diff --git a/types/js-cookie/tslint.json b/types/js-cookie/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/js-cookie/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }