added more content
This commit is contained in:
88
node_modules/match-at/lib/__tests__/matchAt-test.js
generated
vendored
Normal file
88
node_modules/match-at/lib/__tests__/matchAt-test.js
generated
vendored
Normal file
@@ -0,0 +1,88 @@
|
||||
describe('matchAt', function () {
|
||||
|
||||
var matchAt;
|
||||
|
||||
beforeEach(function () {
|
||||
matchAt = require('../matchAt.js');
|
||||
});
|
||||
|
||||
it('matches a simple regex', function () {
|
||||
expect(matchAt(/l/, 'hello', 0)).toBe(null);
|
||||
expect(matchAt(/l/, 'hello', 1)).toBe(null);
|
||||
expect(matchAt(/l/, 'hello', 4)).toBe(null);
|
||||
expect(matchAt(/l/, 'hello', 5)).toBe(null);
|
||||
|
||||
var match = matchAt(/l/, 'hello', 2);
|
||||
expect(Array.isArray(match)).toBe(true);
|
||||
expect(match.index).toBe(2);
|
||||
expect(match.input).toBe('hello');
|
||||
expect(match[0]).toBe('l');
|
||||
expect(match[1]).toBe(undefined);
|
||||
expect(match.length).toBe(1);
|
||||
|
||||
var match = matchAt(/l/, 'hello', 3);
|
||||
expect(Array.isArray(match)).toBe(true);
|
||||
expect(match.index).toBe(3);
|
||||
expect(match.input).toBe('hello');
|
||||
expect(match[0]).toBe('l');
|
||||
expect(match[1]).toBe(undefined);
|
||||
expect(match.length).toBe(1);
|
||||
});
|
||||
|
||||
it('matches a zero-length regex', function () {
|
||||
expect(matchAt(/(?=l)/, 'hello', 0)).toBe(null);
|
||||
expect(matchAt(/(?=l)/, 'hello', 1)).toBe(null);
|
||||
expect(matchAt(/(?=l)/, 'hello', 4)).toBe(null);
|
||||
expect(matchAt(/(?=l)/, 'hello', 5)).toBe(null);
|
||||
|
||||
var match = matchAt(/(?=l)/, 'hello', 2);
|
||||
expect(Array.isArray(match)).toBe(true);
|
||||
expect(match.index).toBe(2);
|
||||
expect(match.input).toBe('hello');
|
||||
expect(match[0]).toBe('');
|
||||
expect(match[1]).toBe(undefined);
|
||||
expect(match.length).toBe(1);
|
||||
|
||||
var match = matchAt(/(?=l)/, 'hello', 3);
|
||||
expect(Array.isArray(match)).toBe(true);
|
||||
expect(match.index).toBe(3);
|
||||
expect(match.input).toBe('hello');
|
||||
expect(match[0]).toBe('');
|
||||
expect(match[1]).toBe(undefined);
|
||||
expect(match.length).toBe(1);
|
||||
});
|
||||
|
||||
it('matches a regex with capturing groups', function () {
|
||||
expect(matchAt(/(l)(l)?/, 'hello', 0)).toBe(null);
|
||||
expect(matchAt(/(l)(l)?/, 'hello', 1)).toBe(null);
|
||||
expect(matchAt(/(l)(l)?/, 'hello', 4)).toBe(null);
|
||||
expect(matchAt(/(l)(l)?/, 'hello', 5)).toBe(null);
|
||||
|
||||
var match = matchAt(/(l)(l)?/, 'hello', 2);
|
||||
expect(Array.isArray(match)).toBe(true);
|
||||
expect(match.index).toBe(2);
|
||||
expect(match.input).toBe('hello');
|
||||
expect(match[0]).toBe('ll');
|
||||
expect(match[1]).toBe('l');
|
||||
expect(match[2]).toBe('l');
|
||||
expect(match.length).toBe(3);
|
||||
|
||||
var match = matchAt(/(l)(l)?/, 'hello', 3);
|
||||
expect(Array.isArray(match)).toBe(true);
|
||||
expect(match.index).toBe(3);
|
||||
expect(match.input).toBe('hello');
|
||||
expect(match[0]).toBe('l');
|
||||
expect(match[1]).toBe('l');
|
||||
expect(match[2]).toBe(undefined);
|
||||
expect(match.length).toBe(3);
|
||||
});
|
||||
|
||||
it('copies flags over', function () {
|
||||
expect(matchAt(/L/i, 'hello', 0)).toBe(null);
|
||||
expect(matchAt(/L/i, 'hello', 1)).toBe(null);
|
||||
expect(matchAt(/L/i, 'hello', 2)).not.toBe(null);
|
||||
expect(matchAt(/L/i, 'hello', 3)).not.toBe(null);
|
||||
expect(matchAt(/L/i, 'hello', 4)).toBe(null);
|
||||
expect(matchAt(/L/i, 'hello', 5)).toBe(null);
|
||||
});
|
||||
});
|
38
node_modules/match-at/lib/matchAt.js
generated
vendored
Normal file
38
node_modules/match-at/lib/matchAt.js
generated
vendored
Normal file
@@ -0,0 +1,38 @@
|
||||
function getRelocatable(re) {
|
||||
// In the future, this could use a WeakMap instead of an expando.
|
||||
if (!re.__matchAtRelocatable) {
|
||||
// Disjunctions are the lowest-precedence operator, so we can make any
|
||||
// pattern match the empty string by appending `|()` to it:
|
||||
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-patterns
|
||||
var source = re.source + '|()';
|
||||
|
||||
// We always make the new regex global.
|
||||
var flags = 'g' + (re.ignoreCase ? 'i' : '') + (re.multiline ? 'm' : '') + (re.unicode ? 'u' : '')
|
||||
// sticky (/.../y) doesn't make sense in conjunction with our relocation
|
||||
// logic, so we ignore it here.
|
||||
;
|
||||
|
||||
re.__matchAtRelocatable = new RegExp(source, flags);
|
||||
}
|
||||
return re.__matchAtRelocatable;
|
||||
}
|
||||
|
||||
function matchAt(re, str, pos) {
|
||||
if (re.global || re.sticky) {
|
||||
throw new Error('matchAt(...): Only non-global regexes are supported');
|
||||
}
|
||||
var reloc = getRelocatable(re);
|
||||
reloc.lastIndex = pos;
|
||||
var match = reloc.exec(str);
|
||||
// Last capturing group is our sentinel that indicates whether the regex
|
||||
// matched at the given location.
|
||||
if (match[match.length - 1] == null) {
|
||||
// Original regex matched.
|
||||
match.length = match.length - 1;
|
||||
return match;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = matchAt;
|
40
node_modules/match-at/lib/matchAt.js.flow
generated
vendored
Normal file
40
node_modules/match-at/lib/matchAt.js.flow
generated
vendored
Normal file
@@ -0,0 +1,40 @@
|
||||
/** @flow */
|
||||
|
||||
function getRelocatable(re: RegExp): RegExp {
|
||||
// In the future, this could use a WeakMap instead of an expando.
|
||||
if (!(re: any).__matchAtRelocatable) {
|
||||
// Disjunctions are the lowest-precedence operator, so we can make any
|
||||
// pattern match the empty string by appending `|()` to it:
|
||||
// https://people.mozilla.org/~jorendorff/es6-draft.html#sec-patterns
|
||||
var source = re.source + '|()';
|
||||
|
||||
// We always make the new regex global.
|
||||
var flags = 'g' + (re.ignoreCase ? 'i' : '') + (re.multiline ? 'm' : '') + ((re: any).unicode ? 'u' : '')
|
||||
// sticky (/.../y) doesn't make sense in conjunction with our relocation
|
||||
// logic, so we ignore it here.
|
||||
;
|
||||
|
||||
(re: any).__matchAtRelocatable = new RegExp(source, flags);
|
||||
}
|
||||
return (re: any).__matchAtRelocatable;
|
||||
}
|
||||
|
||||
function matchAt(re: RegExp, str: string, pos: number): any {
|
||||
if (re.global || (re: any).sticky) {
|
||||
throw new Error('matchAt(...): Only non-global regexes are supported');
|
||||
}
|
||||
var reloc = getRelocatable(re);
|
||||
reloc.lastIndex = pos;
|
||||
var match: Array<string> = reloc.exec(str);
|
||||
// Last capturing group is our sentinel that indicates whether the regex
|
||||
// matched at the given location.
|
||||
if (match[match.length - 1] == null) {
|
||||
// Original regex matched.
|
||||
match.length = match.length - 1;
|
||||
return match;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = matchAt;
|
Reference in New Issue
Block a user