Skip to content

Commit 8340958

Browse files
committed
chore(META): Apply prettier formatting
1 parent 31f270f commit 8340958

File tree

8 files changed

+810
-705
lines changed

8 files changed

+810
-705
lines changed

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
}

src/util.ts

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
1-
import {Pathname} from '@cycle/history';
2-
import {LocationDescriptorObject} from 'history';
1+
import { Pathname } from '@cycle/history';
2+
import { LocationDescriptorObject } from 'history';
33

44
function splitPath(path: Pathname): Pathname[] {
5-
return path.split('/').filter(p => p.length > 0);
5+
return path.split('/').filter(p => p.length > 0);
66
}
77

88
function filterPath(pathParts: Pathname[], namespace: Pathname[]): Pathname {
9-
return pathParts.filter(part => namespace.indexOf(part) < 0).join('/');
9+
return pathParts.filter(part => namespace.indexOf(part) < 0).join('/');
1010
}
1111

1212
const startsWith = (param: string, value: string) => param[0] === value;
1313

1414
const startsWith2 = (param: string, value1: string, value2: string) =>
15-
param[0] === value1 && param[1] === value2;
15+
param[0] === value1 && param[1] === value2;
1616

17-
function makeCreateHref(namespace: Pathname[], _createHref: (pathname: LocationDescriptorObject) => Pathname) {
18-
/**
17+
function makeCreateHref(
18+
namespace: Pathname[],
19+
_createHref: (pathname: LocationDescriptorObject) => Pathname
20+
) {
21+
/**
1922
* Function used to create HREFs that are properly namespaced
2023
* @typedef {createHref}
2124
* @name createHref
@@ -25,33 +28,31 @@ function makeCreateHref(namespace: Pathname[], _createHref: (pathname: LocationD
2528
* @return {string} a fully qualified HREF composed from the current
2629
* namespace and the path provided
2730
*/
28-
return function createHref(location: Pathname | LocationDescriptorObject): Pathname {
29-
if (typeof location === 'object' && location !== null) {
30-
const fullPath = `${namespace.join('/')}${location.pathname}`;
31-
return startsWith(fullPath, '/') || startsWith2(fullPath, '#', '/')
32-
? _createHref({
33-
...location,
34-
pathname: fullPath,
35-
})
36-
: _createHref({
37-
...location,
38-
pathname: '/' + fullPath
39-
});
40-
} else if (typeof location === 'string') {
41-
const fullPath = `${namespace.join('/')}${location}`;
42-
return startsWith(fullPath, '/') || startsWith2(fullPath, '#', '/')
43-
? _createHref({
44-
pathname: fullPath
45-
})
46-
: _createHref({
47-
pathname: '/' + fullPath
48-
});
49-
}
50-
};
31+
return function createHref(
32+
location: Pathname | LocationDescriptorObject
33+
): Pathname {
34+
if (typeof location === 'object' && location !== null) {
35+
const fullPath = `${namespace.join('/')}${location.pathname}`;
36+
return startsWith(fullPath, '/') || startsWith2(fullPath, '#', '/')
37+
? _createHref({
38+
...location,
39+
pathname: fullPath
40+
})
41+
: _createHref({
42+
...location,
43+
pathname: '/' + fullPath
44+
});
45+
} else if (typeof location === 'string') {
46+
const fullPath = `${namespace.join('/')}${location}`;
47+
return startsWith(fullPath, '/') || startsWith2(fullPath, '#', '/')
48+
? _createHref({
49+
pathname: fullPath
50+
})
51+
: _createHref({
52+
pathname: '/' + fullPath
53+
});
54+
}
55+
};
5156
}
5257

53-
export {
54-
splitPath,
55-
filterPath,
56-
makeCreateHref,
57-
}
58+
export { splitPath, filterPath, makeCreateHref };

0 commit comments

Comments
 (0)