Skip to content

Commit 88e9028

Browse files
authored
Merge pull request #203 from jvanbruegge/prettier
Add Prettier
2 parents d970d1b + 8340958 commit 88e9028

File tree

9 files changed

+814
-706
lines changed

9 files changed

+814
-706
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"ghooks": "^1.2.1",
4848
"mkdirp": "^0.5.1",
4949
"mocha": "^3.4.2",
50+
"prettier": "^1.5.3",
5051
"rxjs": "^5.4.0",
5152
"switch-path": "^1.2.0",
5253
"testem": "^1.16.1",
@@ -58,10 +59,12 @@
5859
},
5960
"config": {
6061
"ghooks": {
61-
"commit-msg": "node ./node_modules/.bin/validate-commit-msg"
62+
"commit-msg": "node ./node_modules/.bin/validate-commit-msg",
63+
"pre-commit": "npm run format"
6264
}
6365
},
6466
"scripts": {
67+
"format": "prettier --tab-width 4 --single-quote --write '{src,test}/**/*.{js,ts,tsx}'",
6568
"lint": "tslint -c tslint.json src/*.ts src/**/*.ts",
6669
"test-node": "mocha -r babel-register test/index.js",
6770
"test-browser": "testem",

src/RouterSource.ts

Lines changed: 70 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,80 @@
1-
import {Location, Pathname} from '@cycle/history';
2-
import {LocationDescriptorObject} from 'history';
3-
import {RouteDefinitionsMap, RouteDefinitionsArray, RouteMatcher} from './interfaces';
1+
import { Location, Pathname } from '@cycle/history';
2+
import { LocationDescriptorObject } from 'history';
3+
import {
4+
RouteDefinitionsMap,
5+
RouteDefinitionsArray,
6+
RouteMatcher
7+
} from './interfaces';
48
import * as util from './util';
5-
import {adapt} from '@cycle/run/lib/adapt';
9+
import { adapt } from '@cycle/run/lib/adapt';
610

711
function isStrictlyInScope(namespace: Pathname[], path: Pathname): boolean {
8-
const pathParts = util.splitPath(path);
9-
return namespace.every((v, i) => {
10-
return pathParts[i] === v;
11-
});
12+
const pathParts = util.splitPath(path);
13+
return namespace.every((v, i) => {
14+
return pathParts[i] === v;
15+
});
1216
}
1317

1418
function getFilteredPath(namespace: Pathname[], path: Pathname): Pathname {
15-
const pathParts = util.splitPath(path);
16-
return '/' + util.filterPath(pathParts, namespace);
19+
const pathParts = util.splitPath(path);
20+
return '/' + util.filterPath(pathParts, namespace);
1721
}
1822

1923
export class RouterSource {
20-
constructor(private _history$: any,
21-
private _namespace: Pathname[],
22-
private _createHref: (path: LocationDescriptorObject) => Pathname,
23-
private _routeMatcher: RouteMatcher) {}
24-
25-
history$ = adapt(this._history$);
26-
27-
path(pathname: Pathname): RouterSource {
28-
const scopedNamespace = this._namespace.concat(util.splitPath(pathname));
29-
const scopedHistory$ = this._history$
30-
.filter(({pathname: _path}: Location) => isStrictlyInScope(scopedNamespace, _path))
31-
.remember();
32-
33-
const createHref = this._createHref;
34-
return new RouterSource(scopedHistory$, scopedNamespace, createHref, this._routeMatcher);
35-
}
36-
37-
define(routes: RouteDefinitionsMap | RouteDefinitionsArray, routeMatcher?: RouteMatcher): any {
38-
const namespace = this._namespace;
39-
const _createHref = this._createHref;
40-
const createHref = util.makeCreateHref(namespace, _createHref);
41-
42-
let match$ = this._history$
43-
.map((location: Location) => {
44-
const matcher = routeMatcher || this._routeMatcher;
45-
const filteredPath = getFilteredPath(namespace, location.pathname);
46-
const {path, value} = matcher(filteredPath, routes);
47-
return {path, value, location, createHref};
48-
})
49-
.remember();
50-
51-
const out$ = adapt(match$);
52-
out$.createHref = createHref;
53-
return out$;
54-
}
55-
56-
createHref(path: Pathname): Pathname {
57-
return util.makeCreateHref(this._namespace, this._createHref)(path);
58-
}
24+
constructor(
25+
private _history$: any,
26+
private _namespace: Pathname[],
27+
private _createHref: (path: LocationDescriptorObject) => Pathname,
28+
private _routeMatcher: RouteMatcher
29+
) {}
30+
31+
history$ = adapt(this._history$);
32+
33+
path(pathname: Pathname): RouterSource {
34+
const scopedNamespace = this._namespace.concat(
35+
util.splitPath(pathname)
36+
);
37+
const scopedHistory$ = this._history$
38+
.filter(({ pathname: _path }: Location) =>
39+
isStrictlyInScope(scopedNamespace, _path)
40+
)
41+
.remember();
42+
43+
const createHref = this._createHref;
44+
return new RouterSource(
45+
scopedHistory$,
46+
scopedNamespace,
47+
createHref,
48+
this._routeMatcher
49+
);
50+
}
51+
52+
define(
53+
routes: RouteDefinitionsMap | RouteDefinitionsArray,
54+
routeMatcher?: RouteMatcher
55+
): any {
56+
const namespace = this._namespace;
57+
const _createHref = this._createHref;
58+
const createHref = util.makeCreateHref(namespace, _createHref);
59+
60+
let match$ = this._history$
61+
.map((location: Location) => {
62+
const matcher = routeMatcher || this._routeMatcher;
63+
const filteredPath = getFilteredPath(
64+
namespace,
65+
location.pathname
66+
);
67+
const { path, value } = matcher(filteredPath, routes);
68+
return { path, value, location, createHref };
69+
})
70+
.remember();
71+
72+
const out$ = adapt(match$);
73+
out$.createHref = createHref;
74+
return out$;
75+
}
76+
77+
createHref(path: Pathname): Pathname {
78+
return util.makeCreateHref(this._namespace, this._createHref)(path);
79+
}
5980
}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
export * from './routerify';
2-
export {RouterSource} from './RouterSource';
2+
export { RouterSource } from './RouterSource';
33
export * from './interfaces';

src/interfaces.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
export interface RouteDefinitionsMap {
2-
[sourcePath: string]: any;
2+
[sourcePath: string]: any;
33
}
44

55
export interface RouteDefinitionsArray {
6-
[sourceIndex: number]: any;
6+
[sourceIndex: number]: any;
77
}
88

99
export interface RouteMatcherReturn {
10-
path: string;
11-
value: any;
10+
path: string;
11+
value: any;
1212
}
1313

1414
export interface RouteMatcher {
15-
(path: string, routes: RouteDefinitionsMap | RouteDefinitionsArray): RouteMatcherReturn;
15+
(
16+
path: string,
17+
routes: RouteDefinitionsMap | RouteDefinitionsArray
18+
): RouteMatcherReturn;
1619
}

src/routerify.ts

Lines changed: 55 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
import xs from 'xstream';
2-
import {adapt} from '@cycle/run/lib/adapt';
3-
import {RouteMatcher} from './interfaces';
4-
import {RouterSource} from './RouterSource';
5-
import {Location, createPath} from 'history';
6-
import {HistoryInput, GenericInput} from '@cycle/history';
7-
import {Stream} from 'xstream';
2+
import { adapt } from '@cycle/run/lib/adapt';
3+
import { RouteMatcher } from './interfaces';
4+
import { RouterSource } from './RouterSource';
5+
import { Location, createPath } from 'history';
6+
import { HistoryInput, GenericInput } from '@cycle/history';
7+
import { Stream } from 'xstream';
88

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

1212
export interface RouterOptions {
13-
basename?: string;
14-
historyName?: string;
15-
routerName?: string;
16-
omitHistory?: boolean;
13+
basename?: string;
14+
historyName?: string;
15+
routerName?: string;
16+
omitHistory?: boolean;
1717
}
1818

1919
/**
@@ -23,50 +23,52 @@ export interface RouterOptions {
2323
* @return {main} The augmented main function
2424
*/
2525
function routerify(
26-
main: (a: any) => any,
27-
routeMatcher: RouteMatcher,
28-
options?: RouterOptions
26+
main: (a: any) => any,
27+
routeMatcher: RouteMatcher,
28+
options?: RouterOptions
2929
) {
30-
if (typeof main !== 'function') {
31-
throw new Error('First argument to routerify must be a valid cycle app');
32-
}
33-
const opts: RouterOptions = {
34-
basename: '/',
35-
historyName: 'history',
36-
routerName: 'router',
37-
omitHistory: true,
38-
...options
39-
};
40-
const createHref = (location: Location) =>
41-
opts.basename + createPath(location);
42-
return function(sources: any): any {
43-
const routerSource = new RouterSource(
44-
xs.from(sources[opts.historyName]),
45-
[],
46-
createHref,
47-
routeMatcher
48-
);
49-
const sinks = main({
50-
...sources,
51-
[opts.routerName]: routerSource,
52-
[opts.historyName]: opts.omitHistory
53-
? undefined
54-
: sources[opts.historyName]
55-
});
56-
return {
57-
...sinks,
58-
[opts.historyName]: adapt(
59-
xs.merge(
60-
sinks[opts.historyName] && !opts.omitHistory
61-
? xs.fromObservable(sinks[opts.historyName])
62-
: xs.never(),
63-
sinks[opts.routerName]
64-
? xs.fromObservable(sinks[opts.routerName])
65-
: xs.never()
66-
)
67-
)
30+
if (typeof main !== 'function') {
31+
throw new Error(
32+
'First argument to routerify must be a valid cycle app'
33+
);
34+
}
35+
const opts: RouterOptions = {
36+
basename: '/',
37+
historyName: 'history',
38+
routerName: 'router',
39+
omitHistory: true,
40+
...options
41+
};
42+
const createHref = (location: Location) =>
43+
opts.basename + createPath(location);
44+
return function(sources: any): any {
45+
const routerSource = new RouterSource(
46+
xs.from(sources[opts.historyName]),
47+
[],
48+
createHref,
49+
routeMatcher
50+
);
51+
const sinks = main({
52+
...sources,
53+
[opts.routerName]: routerSource,
54+
[opts.historyName]: opts.omitHistory
55+
? undefined
56+
: sources[opts.historyName]
57+
});
58+
return {
59+
...sinks,
60+
[opts.historyName]: adapt(
61+
xs.merge(
62+
sinks[opts.historyName] && !opts.omitHistory
63+
? xs.fromObservable(sinks[opts.historyName])
64+
: xs.never(),
65+
sinks[opts.routerName]
66+
? xs.fromObservable(sinks[opts.routerName])
67+
: xs.never()
68+
)
69+
)
70+
};
6871
};
69-
};
7072
}
7173

72-
export {routerify};
74+
export { routerify };

src/switch-path.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
declare module 'switch-path' {
2-
export default function switchPath(sourcePath: string, routes: {}): {};
2+
export default function switchPath(sourcePath: string, routes: {}): {};
33
}

0 commit comments

Comments
 (0)