[riot-route] Created type definitions of "riot-route".

This commit is contained in:
karak
2018-03-16 14:24:48 +09:00
parent 17c3e0a996
commit a64a94ef78
4 changed files with 193 additions and 0 deletions
+93
View File
@@ -0,0 +1,93 @@
// Type definitions for riot-route
// Project: https://github.com/riot/route
// Definitions by: karak <https://github.com/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<T>(
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;
}
+73
View File
@@ -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)
})();
+24
View File
@@ -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"
]
}
+3
View File
@@ -0,0 +1,3 @@
{
"extends": "dtslint/dt.json"
}