-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
42 lines (35 loc) · 868 Bytes
/
index.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
'use strict'
/**
* Determine if a given string matches a given pattern.
*
* @param {String} pattern
* @param {String} value
* @returns {Boolean}
* @throws {TypeError}
*/
function is(pattern, value) {
if (typeof pattern !== 'string' || typeof value !== 'string') {
throw new TypeError('Expected all arguments to be strings')
}
if (pattern === value) {
return true
}
pattern = '^' + pattern.replace('*', '.*') + '$'
var regexp = new RegExp(pattern)
return regexp.test(value)
}
if (typeof String.prototype.is === 'undefined') {
/**
* Add the "is" method to the String class.
*
* @param {String} pattern
* @returns {Boolean}
* @throws {TypeError}
*/
String.prototype.is = function (pattern) {
return is(pattern, this)
}
}
module.exports = function (pattern, value) {
return is(pattern, value)
}