-
Notifications
You must be signed in to change notification settings - Fork 0
/
matcher.js
28 lines (26 loc) · 1 KB
/
matcher.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
const matcherSymbol = Symbol(".matcher");
class Matcher {
/**@param {MatcherTypes} matcher*/
constructor(matcher) {
matcher = matcher.valueOf(); //Converting objects to primitives, if possible.
if (!(typeof matcher === "string" || matcher instanceof RegExp)) {
throw "You need to use either a RegExp object or a string to create a matcher.";
}
this[matcherSymbol] = matcher;
}
/**
* @param {string} string - The string to test the matcher on.
* @returns {boolean} - Returns if matcher matches something on the given string.
*/
testOn(string) {
if (!(this instanceof Matcher)) {
throw "Do not use Matcher functions on another object.";
}
const matcher = this[matcherSymbol];
return typeof matcher === "string" ?
string.indexOf(matcher) > -1 :
matcher.test(string);
}
}
exports = module.exports = Matcher;
/**@typedef {string|RegExp} MatcherTypes */