Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"ghooks": "^1.2.1",
"mkdirp": "^0.5.1",
"mocha": "^3.4.2",
"prettier": "^1.5.3",
"rxjs": "^5.4.0",
"switch-path": "^1.2.0",
"testem": "^1.16.1",
Expand All @@ -58,10 +59,12 @@
},
"config": {
"ghooks": {
"commit-msg": "node ./node_modules/.bin/validate-commit-msg"
"commit-msg": "node ./node_modules/.bin/validate-commit-msg",
"pre-commit": "npm run format"
}
},
"scripts": {
"format": "prettier --tab-width 4 --single-quote --write '{src,test}/**/*.{js,ts,tsx}'",
"lint": "tslint -c tslint.json src/*.ts src/**/*.ts",
"test-node": "mocha -r babel-register test/index.js",
"test-browser": "testem",
Expand Down
119 changes: 70 additions & 49 deletions src/RouterSource.ts
Original file line number Diff line number Diff line change
@@ -1,59 +1,80 @@
import {Location, Pathname} from '@cycle/history';
import {LocationDescriptorObject} from 'history';
import {RouteDefinitionsMap, RouteDefinitionsArray, RouteMatcher} from './interfaces';
import { Location, Pathname } from '@cycle/history';
import { LocationDescriptorObject } from 'history';
import {
RouteDefinitionsMap,
RouteDefinitionsArray,
RouteMatcher
} from './interfaces';
import * as util from './util';
import {adapt} from '@cycle/run/lib/adapt';
import { adapt } from '@cycle/run/lib/adapt';

function isStrictlyInScope(namespace: Pathname[], path: Pathname): boolean {
const pathParts = util.splitPath(path);
return namespace.every((v, i) => {
return pathParts[i] === v;
});
const pathParts = util.splitPath(path);
return namespace.every((v, i) => {
return pathParts[i] === v;
});
}

function getFilteredPath(namespace: Pathname[], path: Pathname): Pathname {
const pathParts = util.splitPath(path);
return '/' + util.filterPath(pathParts, namespace);
const pathParts = util.splitPath(path);
return '/' + util.filterPath(pathParts, namespace);
}

export class RouterSource {
constructor(private _history$: any,
private _namespace: Pathname[],
private _createHref: (path: LocationDescriptorObject) => Pathname,
private _routeMatcher: RouteMatcher) {}

history$ = adapt(this._history$);

path(pathname: Pathname): RouterSource {
const scopedNamespace = this._namespace.concat(util.splitPath(pathname));
const scopedHistory$ = this._history$
.filter(({pathname: _path}: Location) => isStrictlyInScope(scopedNamespace, _path))
.remember();

const createHref = this._createHref;
return new RouterSource(scopedHistory$, scopedNamespace, createHref, this._routeMatcher);
}

define(routes: RouteDefinitionsMap | RouteDefinitionsArray, routeMatcher?: RouteMatcher): any {
const namespace = this._namespace;
const _createHref = this._createHref;
const createHref = util.makeCreateHref(namespace, _createHref);

let match$ = this._history$
.map((location: Location) => {
const matcher = routeMatcher || this._routeMatcher;
const filteredPath = getFilteredPath(namespace, location.pathname);
const {path, value} = matcher(filteredPath, routes);
return {path, value, location, createHref};
})
.remember();

const out$ = adapt(match$);
out$.createHref = createHref;
return out$;
}

createHref(path: Pathname): Pathname {
return util.makeCreateHref(this._namespace, this._createHref)(path);
}
constructor(
private _history$: any,
private _namespace: Pathname[],
private _createHref: (path: LocationDescriptorObject) => Pathname,
private _routeMatcher: RouteMatcher
) {}

history$ = adapt(this._history$);

path(pathname: Pathname): RouterSource {
const scopedNamespace = this._namespace.concat(
util.splitPath(pathname)
);
const scopedHistory$ = this._history$
.filter(({ pathname: _path }: Location) =>
isStrictlyInScope(scopedNamespace, _path)
)
.remember();

const createHref = this._createHref;
return new RouterSource(
scopedHistory$,
scopedNamespace,
createHref,
this._routeMatcher
);
}

define(
routes: RouteDefinitionsMap | RouteDefinitionsArray,
routeMatcher?: RouteMatcher
): any {
const namespace = this._namespace;
const _createHref = this._createHref;
const createHref = util.makeCreateHref(namespace, _createHref);

let match$ = this._history$
.map((location: Location) => {
const matcher = routeMatcher || this._routeMatcher;
const filteredPath = getFilteredPath(
namespace,
location.pathname
);
const { path, value } = matcher(filteredPath, routes);
return { path, value, location, createHref };
})
.remember();

const out$ = adapt(match$);
out$.createHref = createHref;
return out$;
}

createHref(path: Pathname): Pathname {
return util.makeCreateHref(this._namespace, this._createHref)(path);
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
export * from './routerify';
export {RouterSource} from './RouterSource';
export { RouterSource } from './RouterSource';
export * from './interfaces';
13 changes: 8 additions & 5 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
export interface RouteDefinitionsMap {
[sourcePath: string]: any;
[sourcePath: string]: any;
}

export interface RouteDefinitionsArray {
[sourceIndex: number]: any;
[sourceIndex: number]: any;
}

export interface RouteMatcherReturn {
path: string;
value: any;
path: string;
value: any;
}

export interface RouteMatcher {
(path: string, routes: RouteDefinitionsMap | RouteDefinitionsArray): RouteMatcherReturn;
(
path: string,
routes: RouteDefinitionsMap | RouteDefinitionsArray
): RouteMatcherReturn;
}
108 changes: 55 additions & 53 deletions src/routerify.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import xs from 'xstream';
import {adapt} from '@cycle/run/lib/adapt';
import {RouteMatcher} from './interfaces';
import {RouterSource} from './RouterSource';
import {Location, createPath} from 'history';
import {HistoryInput, GenericInput} from '@cycle/history';
import {Stream} from 'xstream';
import { adapt } from '@cycle/run/lib/adapt';
import { RouteMatcher } from './interfaces';
import { RouterSource } from './RouterSource';
import { Location, createPath } from 'history';
import { HistoryInput, GenericInput } from '@cycle/history';
import { Stream } from 'xstream';

export declare type HistoryAction = HistoryInput | GenericInput | string;
export declare type RouterSink = Stream<HistoryAction>;

export interface RouterOptions {
basename?: string;
historyName?: string;
routerName?: string;
omitHistory?: boolean;
basename?: string;
historyName?: string;
routerName?: string;
omitHistory?: boolean;
}

/**
Expand All @@ -23,50 +23,52 @@ export interface RouterOptions {
* @return {main} The augmented main function
*/
function routerify(
main: (a: any) => any,
routeMatcher: RouteMatcher,
options?: RouterOptions
main: (a: any) => any,
routeMatcher: RouteMatcher,
options?: RouterOptions
) {
if (typeof main !== 'function') {
throw new Error('First argument to routerify must be a valid cycle app');
}
const opts: RouterOptions = {
basename: '/',
historyName: 'history',
routerName: 'router',
omitHistory: true,
...options
};
const createHref = (location: Location) =>
opts.basename + createPath(location);
return function(sources: any): any {
const routerSource = new RouterSource(
xs.from(sources[opts.historyName]),
[],
createHref,
routeMatcher
);
const sinks = main({
...sources,
[opts.routerName]: routerSource,
[opts.historyName]: opts.omitHistory
? undefined
: sources[opts.historyName]
});
return {
...sinks,
[opts.historyName]: adapt(
xs.merge(
sinks[opts.historyName] && !opts.omitHistory
? xs.fromObservable(sinks[opts.historyName])
: xs.never(),
sinks[opts.routerName]
? xs.fromObservable(sinks[opts.routerName])
: xs.never()
)
)
if (typeof main !== 'function') {
throw new Error(
'First argument to routerify must be a valid cycle app'
);
}
const opts: RouterOptions = {
basename: '/',
historyName: 'history',
routerName: 'router',
omitHistory: true,
...options
};
const createHref = (location: Location) =>
opts.basename + createPath(location);
return function(sources: any): any {
const routerSource = new RouterSource(
xs.from(sources[opts.historyName]),
[],
createHref,
routeMatcher
);
const sinks = main({
...sources,
[opts.routerName]: routerSource,
[opts.historyName]: opts.omitHistory
? undefined
: sources[opts.historyName]
});
return {
...sinks,
[opts.historyName]: adapt(
xs.merge(
sinks[opts.historyName] && !opts.omitHistory
? xs.fromObservable(sinks[opts.historyName])
: xs.never(),
sinks[opts.routerName]
? xs.fromObservable(sinks[opts.routerName])
: xs.never()
)
)
};
};
};
}

export {routerify};
export { routerify };
2 changes: 1 addition & 1 deletion src/switch-path.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
declare module 'switch-path' {
export default function switchPath(sourcePath: string, routes: {}): {};
export default function switchPath(sourcePath: string, routes: {}): {};
}
Loading