MCPcopy
hub / github.com/slevithan/xregexp / augment

Function augment

src/xregexp.js:109–135  ·  view source on GitHub ↗

* Attaches extended data and `XRegExp.prototype` properties to a regex object. * * @private * @param {RegExp} regex Regex to augment. * @param {Array} captureNames Array with capture names, or `null`. * @param {String} xSource XRegExp pattern used to generate `regex`, or `null` if N/A. * @para

(regex, captureNames, xSource, xFlags, isInternalOnly)

Source from the content-addressed store, hash-verified

107 * @returns {!RegExp} Augmented regex.
108 */
109function augment(regex, captureNames, xSource, xFlags, isInternalOnly) {
110 regex[REGEX_DATA] = {
111 captureNames
112 };
113
114 if (isInternalOnly) {
115 return regex;
116 }
117
118 // Can't auto-inherit these since the XRegExp constructor returns a nonprimitive value
119 if (regex.__proto__) {
120 regex.__proto__ = XRegExp.prototype;
121 } else {
122 for (const p in XRegExp.prototype) {
123 // An `XRegExp.prototype.hasOwnProperty(p)` check wouldn't be worth it here, since this
124 // is performance sensitive, and enumerable `Object.prototype` or `RegExp.prototype`
125 // extensions exist on `regex.prototype` anyway
126 regex[p] = XRegExp.prototype[p];
127 }
128 }
129
130 regex[REGEX_DATA].source = xSource;
131 // Emulate the ES6 `flags` prop by ensuring flags are in alphabetical order
132 regex[REGEX_DATA].flags = xFlags ? xFlags.split('').sort().join('') : xFlags;
133
134 return regex;
135}
136
137/**
138 * Removes any duplicate characters from the provided string.

Callers 2

copyRegexFunction · 0.85
XRegExpFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…