Projektstart
This commit is contained in:
43
backend/node_modules/obliterator/match.js
generated
vendored
Normal file
43
backend/node_modules/obliterator/match.js
generated
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Obliterator Match Function
|
||||
* ===========================
|
||||
*
|
||||
* Function returning an iterator over the matches of the given regex on the
|
||||
* target string.
|
||||
*/
|
||||
var Iterator = require('./iterator.js');
|
||||
|
||||
/**
|
||||
* Match.
|
||||
*
|
||||
* @param {RegExp} pattern - Regular expression to use.
|
||||
* @param {string} string - Target string.
|
||||
* @return {Iterator}
|
||||
*/
|
||||
module.exports = function match(pattern, string) {
|
||||
var executed = false;
|
||||
|
||||
if (!(pattern instanceof RegExp))
|
||||
throw new Error(
|
||||
'obliterator/match: invalid pattern. Expecting a regular expression.'
|
||||
);
|
||||
|
||||
if (typeof string !== 'string')
|
||||
throw new Error('obliterator/match: invalid target. Expecting a string.');
|
||||
|
||||
return new Iterator(function () {
|
||||
if (executed && !pattern.global) {
|
||||
pattern.lastIndex = 0;
|
||||
return {done: true};
|
||||
}
|
||||
|
||||
executed = true;
|
||||
|
||||
var m = pattern.exec(string);
|
||||
|
||||
if (m) return {value: m};
|
||||
|
||||
pattern.lastIndex = 0;
|
||||
return {done: true};
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user