From a64a94ef7839ce253b4e46db414a58d8e112580e Mon Sep 17 00:00:00 2001 From: karak Date: Fri, 16 Mar 2018 14:24:48 +0900 Subject: [PATCH 1/6] [riot-route] Created type definitions of "riot-route". --- types/riot-route/index.d.ts | 93 ++++++++++++++++++++++++++++ types/riot-route/riot-route-tests.ts | 73 ++++++++++++++++++++++ types/riot-route/tsconfig.json | 24 +++++++ types/riot-route/tslint.json | 3 + 4 files changed, 193 insertions(+) create mode 100644 types/riot-route/index.d.ts create mode 100644 types/riot-route/riot-route-tests.ts create mode 100644 types/riot-route/tsconfig.json create mode 100644 types/riot-route/tslint.json diff --git a/types/riot-route/index.d.ts b/types/riot-route/index.d.ts new file mode 100644 index 0000000000..fa1127e600 --- /dev/null +++ b/types/riot-route/index.d.ts @@ -0,0 +1,93 @@ +// Type definitions for riot-route +// Project: https://github.com/riot/route +// Definitions by: karak +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +declare module 'riot-route' { + interface RiotRoute { + /** + * Execute the given callback when the URL changes. + */ + (callback: (...params: string[]) => void): void; + + /** + * Execute the given callback when the URL changes and it match the filter. + * @since v2.3 + */ + (filter: string, callback: (...params: string[]) => void): void; + + /** + * Returns a new routing context. + * @since v2.3 + */ + create(): RiotSubRoute; + + /** + * Changes the browser URL and notifies all the listeners assigned with `route(callback)`. + */ + (to: string, title?: string, shouldReplace?: boolean): void; + + /** + * Start listening the url changes. + */ + start(): void; + + /** + * Start listening the url changes and also exec routing on the current url. + */ + start(autoStart: boolean): void; + + /** + * Stop all the routings. It'll removes the listeners and clear also the callbacks. + */ + stop(): void; + + /** + * Study the current browser path "in place" and emit routing without waiting for it to change. + */ + exec(): void; + + /** + * This is an utility function to extract the query from the url. + * @since v2.3 + */ + query(): { [name: string]: string; }; + + /** + * Change the base path. + * + * @param arg - a new base or '#' or '#!' + */ + base(arg?: string|RegExp): void; + + /** + * Changes the default parser to a custom one. + * @param parser + * @param secondParser + */ + parser( + parser: (path: string) => T[], + secondParser?: (path: string, filter: string) => string[] | undefined + ): void; + } + + interface RiotSubRoute extends RiotRoute { + /** + * Stop only subRoute's routings. It'll removes the listeners and clear also the callbacks. + * + * @since v2.3 + */ + stop(): void; + } + + const route: RiotRoute; + + export default route; +} + +declare module 'riot-route/lib/tag' { + import route from 'riot-route'; + + export default route; +} diff --git a/types/riot-route/riot-route-tests.ts b/types/riot-route/riot-route-tests.ts new file mode 100644 index 0000000000..19d9f59386 --- /dev/null +++ b/types/riot-route/riot-route-tests.ts @@ -0,0 +1,73 @@ +import { default as route } from 'riot-route'; +import routeFromTag from 'riot-route/lib/tag'; + +/* () */ +route(function(collection, id, action) { + +}); + +route('/fruit', function(name) { + console.log('The list of fruits') +}); + +/* create() */ +var subRoute = route.create(); +subRoute('/fruit/apple', function() { /* */ }); + +/* () */ +route('customers/267393/edit'); +route('customers/267393/edit', 'Editing customer page'); +route('not-found', 'Not found', true); + +/* start() */ +route.start(); +route.start(true); + +/* stop() */ +route.stop(); + +/* exec() */ +route.exec(); + +/* query() */ +route('/search..', function() { + var q = route.query() + console.log('Search keyword: ' + q.keyword) + console.log('Search limit: ' + q.limit) +}); + +/* base() */ +route.base('/app'); +route.base('#!'); +route.base(); //reset + +/* parser() */ +route.parser(function(path) { + var raw = path.slice(2).split('?'), + uri = raw[0].split('/') as (string | { [name: string]: string })[], + qs = raw[1], + params = {} as { [name: string]: string }; + + if (qs) { + qs.split('&').forEach(function(v) { + var c = v.split('='); + params[c[0]] = c[1]; + }) + } + uri.push(params); + return uri +}); + +(function () { + function first(path: string) { + return[]; + } + + function second(path: string, filter: string) { + let args: RegExpMatchArray | null; + var re = new RegExp('^' + filter.replace(/\*/g, '([^/?#]+?)').replace(/\.\./, '.*') + '$') + if (args = path.match(re)) return args.slice(1) + } + + route.parser(first, second) +})(); diff --git a/types/riot-route/tsconfig.json b/types/riot-route/tsconfig.json new file mode 100644 index 0000000000..b9307bfcbb --- /dev/null +++ b/types/riot-route/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "es6", + "allowSyntheticDefaultImports": true, + "esModuleInterop": true, + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strict": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true + }, + "files": [ + "index.d.ts", + "riot-route-tests.ts" + ] +} diff --git a/types/riot-route/tslint.json b/types/riot-route/tslint.json new file mode 100644 index 0000000000..f93cf8562a --- /dev/null +++ b/types/riot-route/tslint.json @@ -0,0 +1,3 @@ +{ + "extends": "dtslint/dt.json" +} From d17063ea8dd3766d6788062530ad2bcfced67eb4 Mon Sep 17 00:00:00 2001 From: karak Date: Fri, 16 Mar 2018 14:41:46 +0900 Subject: [PATCH 2/6] [riot-route] Modified tsconfig.json subsequent to dtslint. --- types/riot-route/tsconfig.json | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/types/riot-route/tsconfig.json b/types/riot-route/tsconfig.json index b9307bfcbb..919f11b1a6 100644 --- a/types/riot-route/tsconfig.json +++ b/types/riot-route/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "module": "es6", + "module": "commonjs", "allowSyntheticDefaultImports": true, "esModuleInterop": true, "lib": [ @@ -9,7 +9,9 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strict": true, + "strictNullChecks": false, + "strictFunctionTypes": true, + "forceConsistentCasingInFileNames": true, "baseUrl": "../", "typeRoots": [ "../" From 1e7aa1d80ba62781fb53f963d4f60fbdb97c81d4 Mon Sep 17 00:00:00 2001 From: karak Date: Fri, 16 Mar 2018 14:57:02 +0900 Subject: [PATCH 3/6] [riot-route] Fixed errors by dtslint. --- types/riot-route/index.d.ts | 90 +--------------------------- types/riot-route/lib/index.d.ts | 79 ++++++++++++++++++++++++ types/riot-route/lib/tag.d.ts | 3 + types/riot-route/riot-route-tests.ts | 55 ++++++++--------- 4 files changed, 113 insertions(+), 114 deletions(-) create mode 100644 types/riot-route/lib/index.d.ts create mode 100644 types/riot-route/lib/tag.d.ts diff --git a/types/riot-route/index.d.ts b/types/riot-route/index.d.ts index fa1127e600..453bb185fa 100644 --- a/types/riot-route/index.d.ts +++ b/types/riot-route/index.d.ts @@ -1,93 +1,9 @@ -// Type definitions for riot-route +// Type definitions for riot-route 3.1 // Project: https://github.com/riot/route // Definitions by: karak // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.3 -declare module 'riot-route' { - interface RiotRoute { - /** - * Execute the given callback when the URL changes. - */ - (callback: (...params: string[]) => void): void; +import route from './lib/index'; - /** - * Execute the given callback when the URL changes and it match the filter. - * @since v2.3 - */ - (filter: string, callback: (...params: string[]) => void): void; - - /** - * Returns a new routing context. - * @since v2.3 - */ - create(): RiotSubRoute; - - /** - * Changes the browser URL and notifies all the listeners assigned with `route(callback)`. - */ - (to: string, title?: string, shouldReplace?: boolean): void; - - /** - * Start listening the url changes. - */ - start(): void; - - /** - * Start listening the url changes and also exec routing on the current url. - */ - start(autoStart: boolean): void; - - /** - * Stop all the routings. It'll removes the listeners and clear also the callbacks. - */ - stop(): void; - - /** - * Study the current browser path "in place" and emit routing without waiting for it to change. - */ - exec(): void; - - /** - * This is an utility function to extract the query from the url. - * @since v2.3 - */ - query(): { [name: string]: string; }; - - /** - * Change the base path. - * - * @param arg - a new base or '#' or '#!' - */ - base(arg?: string|RegExp): void; - - /** - * Changes the default parser to a custom one. - * @param parser - * @param secondParser - */ - parser( - parser: (path: string) => T[], - secondParser?: (path: string, filter: string) => string[] | undefined - ): void; - } - - interface RiotSubRoute extends RiotRoute { - /** - * Stop only subRoute's routings. It'll removes the listeners and clear also the callbacks. - * - * @since v2.3 - */ - stop(): void; - } - - const route: RiotRoute; - - export default route; -} - -declare module 'riot-route/lib/tag' { - import route from 'riot-route'; - - export default route; -} +export default route; diff --git a/types/riot-route/lib/index.d.ts b/types/riot-route/lib/index.d.ts new file mode 100644 index 0000000000..cf3bea4c40 --- /dev/null +++ b/types/riot-route/lib/index.d.ts @@ -0,0 +1,79 @@ +interface RiotRoute { + /** + * Execute the given callback when the URL changes. + */ + (callback: (...params: string[]) => void): void; + + /** + * Execute the given callback when the URL changes and it match the filter. + * @since v2.3 + */ + (filter: string, callback: (...params: string[]) => void): void; + + /** + * Changes the browser URL and notifies all the listeners assigned with `route(callback)`. + */ + (to: string, title?: string, shouldReplace?: boolean): void; + + /** + * Returns a new routing context. + * @since v2.3 + */ + create(): RiotSubRoute; + + /** + * Start listening the url changes. + */ + start(): void; + + /** + * Start listening the url changes and also exec routing on the current url. + */ + start(autoStart: boolean): void; + + /** + * Stop all the routings. It'll removes the listeners and clear also the callbacks. + */ + stop(): void; + + /** + * Study the current browser path "in place" and emit routing without waiting for it to change. + */ + exec(): void; + + /** + * This is an utility function to extract the query from the url. + * @since v2.3 + */ + query(): { [name: string]: string; }; + + /** + * Change the base path. + * + * @param arg - a new base or '#' or '#!' + */ + base(arg?: string|RegExp): void; + + /** + * Changes the default parser to a custom one. + * @param parser + * @param secondParser + */ + parser( + parser: (path: string) => T[], + secondParser?: (path: string, filter: string) => string[] | undefined + ): void; +} + +interface RiotSubRoute extends RiotRoute { + /** + * Stop only subRoute's routings. It'll removes the listeners and clear also the callbacks. + * + * @since v2.3 + */ + stop(): void; +} + +declare const route: RiotRoute; + +export default route; diff --git a/types/riot-route/lib/tag.d.ts b/types/riot-route/lib/tag.d.ts new file mode 100644 index 0000000000..15c5d884fa --- /dev/null +++ b/types/riot-route/lib/tag.d.ts @@ -0,0 +1,3 @@ +import route from './index'; + +export default route; diff --git a/types/riot-route/riot-route-tests.ts b/types/riot-route/riot-route-tests.ts index 19d9f59386..fab6a2b0df 100644 --- a/types/riot-route/riot-route-tests.ts +++ b/types/riot-route/riot-route-tests.ts @@ -2,17 +2,16 @@ import { default as route } from 'riot-route'; import routeFromTag from 'riot-route/lib/tag'; /* () */ -route(function(collection, id, action) { - +route((collection, id, action) => { }); -route('/fruit', function(name) { - console.log('The list of fruits') +route('/fruit', (name) => { + console.log('The list of fruits'); }); /* create() */ -var subRoute = route.create(); -subRoute('/fruit/apple', function() { /* */ }); +const subRoute = route.create(); +subRoute('/fruit/apple', () => { /* */ }); /* () */ route('customers/267393/edit'); @@ -30,44 +29,46 @@ route.stop(); route.exec(); /* query() */ -route('/search..', function() { - var q = route.query() - console.log('Search keyword: ' + q.keyword) - console.log('Search limit: ' + q.limit) +route('/search..', () => { + const q = route.query(); + console.log('Search keyword: ' + q.keyword); + console.log('Search limit: ' + q.limit); }); /* base() */ route.base('/app'); route.base('#!'); -route.base(); //reset +route.base(); // reset /* parser() */ -route.parser(function(path) { - var raw = path.slice(2).split('?'), - uri = raw[0].split('/') as (string | { [name: string]: string })[], - qs = raw[1], - params = {} as { [name: string]: string }; +route.parser((path) => { + const raw = path.slice(2).split('?'); + const uri = raw[0].split('/') as Array; + const qs = raw[1]; + const params: { [name: string]: string } = {}; if (qs) { - qs.split('&').forEach(function(v) { - var c = v.split('='); + qs.split('&').forEach((v) => { + const c = v.split('='); params[c[0]] = c[1]; - }) + }); } uri.push(params); - return uri + return uri; }); -(function () { - function first(path: string) { - return[]; +(() => { + function first(path: string): any[] { + return []; } function second(path: string, filter: string) { - let args: RegExpMatchArray | null; - var re = new RegExp('^' + filter.replace(/\*/g, '([^/?#]+?)').replace(/\.\./, '.*') + '$') - if (args = path.match(re)) return args.slice(1) + const re = new RegExp(`^${filter.replace(/\*/g, '([^/?#]+?)').replace(/\.\./, '.*')}$`); + const args = path.match(re); + if (args) { + return args.slice(1); + } } - route.parser(first, second) + route.parser(first, second); })(); From 3dd0180b621e31969d3ecf46f4f184663b3875a3 Mon Sep 17 00:00:00 2001 From: karak Date: Fri, 16 Mar 2018 15:07:27 +0900 Subject: [PATCH 4/6] [riot-route] Added new files separated. --- types/riot-route/tsconfig.json | 2 ++ 1 file changed, 2 insertions(+) diff --git a/types/riot-route/tsconfig.json b/types/riot-route/tsconfig.json index 919f11b1a6..161e5b28d7 100644 --- a/types/riot-route/tsconfig.json +++ b/types/riot-route/tsconfig.json @@ -21,6 +21,8 @@ }, "files": [ "index.d.ts", + "lib/index.d.ts", + "lib/tag.d.ts", "riot-route-tests.ts" ] } From f9b690e45ab61f2bb72444ef9094652783a4d2c5 Mon Sep 17 00:00:00 2001 From: karak Date: Fri, 16 Mar 2018 15:21:25 +0900 Subject: [PATCH 5/6] [riot-route] Fixed errors again. --- types/riot-route/lib/index.d.ts | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/types/riot-route/lib/index.d.ts b/types/riot-route/lib/index.d.ts index cf3bea4c40..d824e0a643 100644 --- a/types/riot-route/lib/index.d.ts +++ b/types/riot-route/lib/index.d.ts @@ -22,14 +22,10 @@ interface RiotRoute { create(): RiotSubRoute; /** - * Start listening the url changes. + * Start listening the url changes + * and also exec routing on the current url. */ - start(): void; - - /** - * Start listening the url changes and also exec routing on the current url. - */ - start(autoStart: boolean): void; + start(autoStart?: boolean): void; /** * Stop all the routings. It'll removes the listeners and clear also the callbacks. @@ -59,8 +55,8 @@ interface RiotRoute { * @param parser * @param secondParser */ - parser( - parser: (path: string) => T[], + parser( + parser: (path: string) => any[], secondParser?: (path: string, filter: string) => string[] | undefined ): void; } From a807455d599b471a20adbadec4b806f8508a39ab Mon Sep 17 00:00:00 2001 From: karak Date: Fri, 16 Mar 2018 16:55:20 +0900 Subject: [PATCH 6/6] [riot-route] Regenereate configs. --- types/riot-route/tsconfig.json | 7 +++---- types/riot-route/tslint.json | 4 +--- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/types/riot-route/tsconfig.json b/types/riot-route/tsconfig.json index 161e5b28d7..cc6b807db4 100644 --- a/types/riot-route/tsconfig.json +++ b/types/riot-route/tsconfig.json @@ -1,7 +1,6 @@ { "compilerOptions": { "module": "commonjs", - "allowSyntheticDefaultImports": true, "esModuleInterop": true, "lib": [ "es6", @@ -9,15 +8,15 @@ ], "noImplicitAny": true, "noImplicitThis": true, - "strictNullChecks": false, + "strictNullChecks": true, "strictFunctionTypes": true, - "forceConsistentCasingInFileNames": true, "baseUrl": "../", "typeRoots": [ "../" ], "types": [], - "noEmit": true + "noEmit": true, + "forceConsistentCasingInFileNames": true }, "files": [ "index.d.ts", diff --git a/types/riot-route/tslint.json b/types/riot-route/tslint.json index f93cf8562a..3db14f85ea 100644 --- a/types/riot-route/tslint.json +++ b/types/riot-route/tslint.json @@ -1,3 +1 @@ -{ - "extends": "dtslint/dt.json" -} +{ "extends": "dtslint/dt.json" }