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" +}