Skip to content

Commit d81a896

Browse files
committed
bundle matcher inside the library
1 parent 9ba878f commit d81a896

File tree

6 files changed

+83
-14
lines changed

6 files changed

+83
-14
lines changed

.babelrc

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
{
22
"presets": ["@babel/preset-env"],
3-
"plugins": ["@babel/plugin-transform-runtime"],
3+
"plugins": [
4+
"@babel/plugin-transform-runtime"
5+
],
46
"comments": false
57
}

dist/vue-wait.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"cross-env": "^5.1.3",
5353
"css-loader": "^0.28.11",
5454
"epic-spinners": "^1.0.3",
55+
"escape-string-regexp": "^1.0.5",
5556
"eslint": "^4.2.0",
5657
"husky": "^0.14.3",
5758
"lint-staged": "^7.1.3",
@@ -68,8 +69,5 @@
6869
},
6970
"peerDependencies": {
7071
"vue": "^2.5.16"
71-
},
72-
"dependencies": {
73-
"matcher": "^1.1.1"
7472
}
7573
}

src/lib/matcher.js

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
// clone of sindresorhus/matcher
2+
'use strict';
3+
const escapeStringRegexp = require('escape-string-regexp');
4+
5+
const reCache = new Map();
6+
7+
function makeRe(pattern, options) {
8+
const opts = Object.assign(
9+
{
10+
caseSensitive: false
11+
},
12+
options
13+
);
14+
15+
const cacheKey = pattern + JSON.stringify(opts);
16+
17+
if (reCache.has(cacheKey)) {
18+
return reCache.get(cacheKey);
19+
}
20+
21+
const negated = pattern[0] === '!';
22+
23+
if (negated) {
24+
pattern = pattern.slice(1);
25+
}
26+
27+
pattern = escapeStringRegexp(pattern).replace(/\\\*/g, '.*');
28+
29+
const re = new RegExp(`^${pattern}$`, opts.caseSensitive ? '' : 'i');
30+
re.negated = negated;
31+
reCache.set(cacheKey, re);
32+
33+
return re;
34+
}
35+
36+
module.exports = (inputs, patterns, options) => {
37+
if (!(Array.isArray(inputs) && Array.isArray(patterns))) {
38+
throw new TypeError(
39+
`Expected two arrays, got ${typeof inputs} ${typeof patterns}`
40+
);
41+
}
42+
43+
if (patterns.length === 0) {
44+
return inputs;
45+
}
46+
47+
const firstNegated = patterns[0][0] === '!';
48+
49+
patterns = patterns.map(x => makeRe(x, options));
50+
51+
const ret = [];
52+
53+
for (const input of inputs) {
54+
// If first pattern is negated we include everything to match user expectation
55+
let matches = firstNegated;
56+
57+
for (const pattern of patterns) {
58+
if (pattern.test(input)) {
59+
matches = !pattern.negated;
60+
}
61+
}
62+
63+
if (matches) {
64+
ret.push(input);
65+
}
66+
}
67+
68+
return ret;
69+
};
70+
71+
module.exports.isMatch = (input, pattern, options) => {
72+
const re = makeRe(pattern, options);
73+
const matches = re.test(input);
74+
return re.negated ? !matches : matches;
75+
};

src/utils.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { isMatch } from 'matcher';
1+
import { isMatch } from './lib/matcher';
22

33
function uniqArray(array) {
44
return array.filter((el, index, arr) => index == arr.indexOf(el));

yarn.lock

+2-8
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@
475475
"@babel/helper-plugin-utils" "7.0.0-beta.51"
476476
"@babel/helper-regex" "7.0.0-beta.51"
477477

478-
478+
"@babel/[email protected]", "@babel/plugin-transform-template-literals@^7.0.0-beta.51":
479479
version "7.0.0-beta.51"
480480
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-template-literals/-/plugin-transform-template-literals-7.0.0-beta.51.tgz#2d0595f56461d4345ba35c38d73033f87ecbbbc8"
481481
dependencies:
@@ -2451,7 +2451,7 @@ escape-string-applescript@^2.0.0:
24512451
version "2.0.0"
24522452
resolved "https://registry.yarnpkg.com/escape-string-applescript/-/escape-string-applescript-2.0.0.tgz#760bca838668e408fe5ee52ce42caf7cb46c5273"
24532453

2454-
escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.4, escape-string-regexp@^1.0.5:
2454+
escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5:
24552455
version "1.0.5"
24562456
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
24572457

@@ -4148,12 +4148,6 @@ map-visit@^1.0.0:
41484148
dependencies:
41494149
object-visit "^1.0.0"
41504150

4151-
matcher@^1.1.1:
4152-
version "1.1.1"
4153-
resolved "https://registry.yarnpkg.com/matcher/-/matcher-1.1.1.tgz#51d8301e138f840982b338b116bb0c09af62c1c2"
4154-
dependencies:
4155-
escape-string-regexp "^1.0.4"
4156-
41574151
math-expression-evaluator@^1.2.14:
41584152
version "1.2.17"
41594153
resolved "https://registry.yarnpkg.com/math-expression-evaluator/-/math-expression-evaluator-1.2.17.tgz#de819fdbcd84dccd8fae59c6aeb79615b9d266ac"

0 commit comments

Comments
 (0)