diff --git a/types/riot-route/index.d.ts b/types/riot-route/index.d.ts new file mode 100644 index 0000000000..453bb185fa --- /dev/null +++ b/types/riot-route/index.d.ts @@ -0,0 +1,9 @@ +// 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 + +import route from './lib/index'; + +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..d824e0a643 --- /dev/null +++ b/types/riot-route/lib/index.d.ts @@ -0,0 +1,75 @@ +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 + * 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) => any[], + 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 new file mode 100644 index 0000000000..fab6a2b0df --- /dev/null +++ b/types/riot-route/riot-route-tests.ts @@ -0,0 +1,74 @@ +import { default as route } from 'riot-route'; +import routeFromTag from 'riot-route/lib/tag'; + +/* () */ +route((collection, id, action) => { +}); + +route('/fruit', (name) => { + console.log('The list of fruits'); +}); + +/* create() */ +const subRoute = route.create(); +subRoute('/fruit/apple', () => { /* */ }); + +/* () */ +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..', () => { + 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 + +/* parser() */ +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((v) => { + const c = v.split('='); + params[c[0]] = c[1]; + }); + } + uri.push(params); + return uri; +}); + +(() => { + function first(path: string): any[] { + return []; + } + + function second(path: string, filter: string) { + const re = new RegExp(`^${filter.replace(/\*/g, '([^/?#]+?)').replace(/\.\./, '.*')}$`); + const args = path.match(re); + if (args) { + 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..cc6b807db4 --- /dev/null +++ b/types/riot-route/tsconfig.json @@ -0,0 +1,27 @@ +{ + "compilerOptions": { + "module": "commonjs", + "esModuleInterop": true, + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "lib/index.d.ts", + "lib/tag.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..3db14f85ea --- /dev/null +++ b/types/riot-route/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }