Merge pull request #24331 from karak/riot-route

Added a new definition of 'riot-route'
This commit is contained in:
Arthur Ozga
2018-03-16 13:34:30 -07:00
committed by GitHub
6 changed files with 189 additions and 0 deletions
+9
View File
@@ -0,0 +1,9 @@
// Type definitions for riot-route 3.1
// Project: https://github.com/riot/route
// Definitions by: karak <https://github.com/karak>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
import route from './lib/index';
export default route;
+75
View File
@@ -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;
+3
View File
@@ -0,0 +1,3 @@
import route from './index';
export default route;
+74
View File
@@ -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<string | { [name: string]: string }>;
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);
})();
+27
View File
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }