Skip to content

Commit 315be7c

Browse files
committed
Remove reliance on Object.assign() (breaks in phantomjs)
1 parent 56fbcd8 commit 315be7c

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/index.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { cloneElement, h, Component } from 'preact';
2-
import { exec, pathRankSort } from './util';
2+
import { exec, pathRankSort, assign } from './util';
33

44
let customHistory = null;
55

@@ -94,9 +94,10 @@ function routeFromLink(node) {
9494

9595

9696
function handleLinkClick(e) {
97-
if (e.button !== 0) return;
98-
routeFromLink(e.currentTarget || e.target || this);
99-
return prevent(e);
97+
if (e.button==0) {
98+
routeFromLink(e.currentTarget || e.target || this);
99+
return prevent(e);
100+
}
100101
}
101102

102103

@@ -152,7 +153,7 @@ class Router extends Component {
152153
}
153154

154155
this.state = {
155-
url: this.props.url || getCurrentUrl()
156+
url: props.url || getCurrentUrl()
156157
};
157158

158159
initEventListeners();
@@ -215,12 +216,7 @@ class Router extends Component {
215216
if (matches) {
216217
if (invoke!==false) {
217218
let newProps = { url, matches };
218-
// copy matches onto props
219-
for (let i in matches) {
220-
if (matches.hasOwnProperty(i)) {
221-
newProps[i] = matches[i];
222-
}
223-
}
219+
assign(newProps, matches);
224220
return cloneElement(vnode, newProps);
225221
}
226222
return vnode;
@@ -254,7 +250,7 @@ class Router extends Component {
254250
}
255251

256252
const Link = (props) => (
257-
h('a', Object.assign({}, props, { onClick: handleLinkClick }))
253+
h('a', assign({ onClick: handleLinkClick }, props))
258254
);
259255

260256
const Route = props => h(props.component, props);

src/util.js

+8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11

22
const EMPTY = {};
33

4+
export function assign(obj, props) {
5+
// eslint-disable-next-line guard-for-in
6+
for (let i in props) {
7+
obj[i] = props[i];
8+
}
9+
return obj;
10+
}
11+
412
export function exec(url, route, opts=EMPTY) {
513
let reg = /(?:\?([^#]*))?(#.*)?$/,
614
c = url.match(reg),

0 commit comments

Comments
 (0)