package/package.json000644 001133 000024 0000001205 13153650653013010 0ustar00000000 000000 { "name": "match-at", "version": "0.1.1", "description": "Relocatable regular expressions.", "repository": "sophiebits/match-at", "main": "lib/matchAt.js", "files": [ "lib/" ], "devDependencies": { "babel-cli": "^6.26.0", "babel-jest": "^21.0.0", "babel-plugin-syntax-flow": "^6.18.0", "babel-plugin-transform-flow-strip-types": "^6.22.0", "jest": "^21.0.1" }, "scripts": { "prepublish": "babel --no-babelrc --plugins syntax-flow -d lib/ src/ && mv lib/matchAt.js lib/matchAt.js.flow && babel -d lib/ src/", "test": "jest" }, "babel": { "plugins": ["transform-flow-strip-types"] } } package/README.md000644 001133 000024 0000001376 13153642254012010 0ustar00000000 000000 # match-at [![Build Status](https://travis-ci.org/spicyj/match-at.svg?branch=master)](https://travis-ci.org/spicyj/match-at) ## Introduction Like `String.prototype.match` if it only checked the regex at the given index instead of searching the entire string. ```js matchAt(/world/, 'hello world', 6); // ['world'] matchAt(/world/, 'hello world', 0); // null ``` Almost like `'hello world'.slice(i).match(/^world/)` except the resulting match object's `.index` property corresponds to the original string, and it doesn't actually slice the string. Most engines optimize taking a substring so this probably isn't particularly valuable in practice, but it was an entertaining exercise and could be useful if you reminisce about these semantics. ## License MIT. package/LICENSE000644 001133 000024 0000002070 13153651736011533 0ustar00000000 000000 The MIT License (MIT) Copyright (c) 2017 Sophie Alpert Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. package/lib/matchAt.js000644 001133 000024 0000002400 13153653526013207 0ustar00000000 000000 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;package/lib/__tests__/matchAt-test.js000644 001133 000024 0000005603 13153653525016131 0ustar00000000 000000 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); }); });package/lib/matchAt.js.flow000644 001133 000024 0000002555 13153653525014167 0ustar00000000 000000 /** @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 = 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;