/
prototypes.js
110 lines (100 loc) · 2.87 KB
/
prototypes.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*!
* XRegExp Prototypes 2.1.0-rc
* <http://xregexp.com/>
* Steven Levithan © 2012 MIT License
*/
/**
* Adds a collection of methods to `XRegExp.prototype`. RegExp objects copied by XRegExp are also
* augmented with any `XRegExp.prototype` methods. Hence, the following work equivalently:
*
* XRegExp('[a-z]', 'ig').xexec('abc');
* XRegExp(/[a-z]/ig).xexec('abc');
* XRegExp.globalize(/[a-z]/i).xexec('abc');
*/
(function (XRegExp) {
'use strict';
// Shortcut
var proto = XRegExp.prototype;
/**
* Implicitly calls the regex's `test` method with the first value in the provided `args` array.
* @memberOf XRegExp.prototype
* @param {*} context Ignored. Accepted only for congruity with `Function.prototype.apply`.
* @param {Array} args Array with the string to search as its first value.
* @returns {Boolean} Whether the regex matched the provided value.
* @example
*
* XRegExp('[a-z]').apply(null, ['abc']); // -> true
*/
proto.apply = function (context, args) {
return this.test(args[0]);
};
/**
* Implicitly calls the regex's `test` method with the provided string.
* @memberOf XRegExp.prototype
* @param {*} context Ignored. Accepted only for congruity with `Function.prototype.call`.
* @param {String} str String to search.
* @returns {Boolean} Whether the regex matched the provided value.
* @example
*
* XRegExp('[a-z]').call(null, 'abc'); // -> true
*/
proto.call = function (context, str) {
return this.test(str);
};
/**
* Implicitly calls {@link #XRegExp.forEach}.
* @memberOf XRegExp.prototype
* @example
*
* XRegExp('\\d').forEach('1a2345', function (match, i) {
* if (i % 2) this.push(+match[0]);
* }, []);
* // -> [2, 4]
*/
proto.forEach = function (str, callback, context) {
return XRegExp.forEach(str, this, callback, context);
};
/**
* Implicitly calls {@link #XRegExp.globalize}.
* @memberOf XRegExp.prototype
* @example
*
* var globalCopy = XRegExp('regex').globalize();
* globalCopy.global; // -> true
*/
proto.globalize = function () {
return XRegExp.globalize(this);
};
/**
* Implicitly calls {@link #XRegExp.match}.
* @memberOf XRegExp.prototype
* @example
*
* XRegExp('\\d').match('1a23', 'all');
* // -> ['1', '2', '3']
*/
proto.match = function (str, scope) {
return XRegExp.match(str, this, scope);
};
/**
* Implicitly calls {@link #XRegExp.exec}.
* @memberOf XRegExp.prototype
* @example
*
* var match = XRegExp('U\\+(?<hex>[0-9A-F]{4})').xexec('U+2620');
* match.hex; // -> '2620'
*/
proto.xexec = function (str, pos, sticky) {
return XRegExp.exec(str, this, pos, sticky);
};
/**
* Implicitly calls {@link #XRegExp.test}.
* @memberOf XRegExp.prototype
* @example
*
* XRegExp('c').xtest('abc'); // -> true
*/
proto.xtest = function (str, pos, sticky) {
return XRegExp.test(str, this, pos, sticky);
};
}(XRegExp));