Skip to content

Commit 4277cc5

Browse files
committed
moved endsWith from router utils to general utils; added startsWith util; refactored makeExactMatcher to use both
1 parent a245a2b commit 4277cc5

File tree

4 files changed

+20
-11
lines changed

4 files changed

+20
-11
lines changed

src/core/router/history/hash.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { noop } from '../../util/core';
22
import { on } from '../../util/dom';
3-
import { parseQuery, cleanPath, replaceSlug, endsWith } from '../util';
3+
import { endsWith } from '../../util/str';
4+
import { parseQuery, cleanPath, replaceSlug } from '../util';
45
import { History } from './base';
56

67
function replaceHash(path) {

src/core/router/util.js

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,3 @@ export function getPath(...args) {
113113
export const replaceSlug = cached(path => {
114114
return path.replace('#', '?id=');
115115
});
116-
117-
export function endsWith(str, suffix) {
118-
return str.indexOf(suffix, str.length - suffix.length) !== -1;
119-
}

src/core/util/str.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export function startsWith(str, prefix) {
2+
return str.indexOf(prefix) === 0;
3+
}
4+
5+
export function endsWith(str, suffix) {
6+
return str.indexOf(suffix, str.length - suffix.length) !== -1;
7+
}
Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
1+
import { startsWith, endsWith } from '../util/str';
2+
13
/**
24
* Adds beginning of input (^) and end of input ($) assertions if needed into a regex string
35
* @param {string} matcher the string to match
46
* @returns {string}
57
*/
68
export function makeExactMatcher(matcher) {
7-
const matcherWithBeginningOfInput =
8-
matcher.slice(0, 1) === '^' ? matcher : `^${matcher}`;
9+
const matcherWithBeginningOfInput = startsWith(matcher, '^')
10+
? matcher
11+
: `^${matcher}`;
912

10-
const matcherWithBeginningAndEndOfInput =
11-
matcherWithBeginningOfInput.slice(-1) === '$'
12-
? matcherWithBeginningOfInput
13-
: `${matcherWithBeginningOfInput}$`;
13+
const matcherWithBeginningAndEndOfInput = endsWith(
14+
matcherWithBeginningOfInput,
15+
'$'
16+
)
17+
? matcherWithBeginningOfInput
18+
: `${matcherWithBeginningOfInput}$`;
1419

1520
return matcherWithBeginningAndEndOfInput;
1621
}

0 commit comments

Comments
 (0)