Hullo JS
Search…
|> route
import { route } from '@hullo/browser'
Use this operator to transform a current window location information into some another. Operator gets a single argument - a (regexp) route to a stream of values.
1
import { route, ofHistory } from '@hullo/browser'
2
import { createBrowserHistory } from 'history';
3
​
4
const history = createBrowserHistory({});
5
​
6
const history$ = ofHistory(history);
7
​
8
history$
9
.pipe(
10
route([
11
{
12
when: /^\/test\/([0-9]+)$/,
13
have: (...args) => ["test", ...args].join("-")
14
},
15
{ when: /^\/[a-z]$/, have: () => "letters" },
16
{ when: /^\/[0-9]$/, have: () => "numbers" },
17
{ when: /^\/$/, have: () => "index" }
18
])
19
)
20
.subscribe({
21
next: v => {
22
console.log('@', v);
23
}
24
});
25
​
26
await history$.next({ pathname: "/d" });
27
// changes the location
28
// but because of derivative stream
29
// also prints "@ letters"
30
​
31
await history$.next({ pathname: "/0" });
32
// changes the location
33
// but because of derivative stream
34
// also prints "@ numbers"
35
​
36
await history$.next({ pathname: "/test/42" });
37
// changes the location
38
// but because of derivative stream
39
// also prints "@ test-42"
Copied!
Last modified 2yr ago
Copy link