Comparing version
@@ -188,3 +188,30 @@ function find(str) { | ||
export { find, erase }; | ||
function match(str, pattern, callback) { | ||
if (!pattern.global) throw new Error('regular expression must have the g (global) flag'); | ||
var found = find(str); | ||
var match = undefined; | ||
var chunkIndex = 0; | ||
while (match = pattern.exec(str)) { | ||
var chunk = undefined; | ||
do { | ||
chunk = found[chunkIndex]; | ||
if (chunk && chunk.end < match.index) { | ||
chunkIndex += 1; | ||
} else { | ||
break; | ||
} | ||
} while (chunk); | ||
if (!chunk || chunk.start > match.index) { | ||
callback.apply(null, match); | ||
} | ||
} | ||
} | ||
export { find, erase, match }; | ||
//# sourceMappingURL=tippex.es6.js.map |
@@ -194,6 +194,34 @@ (function (global, factory) { | ||
function match(str, pattern, callback) { | ||
if (!pattern.global) throw new Error('regular expression must have the g (global) flag'); | ||
var found = find(str); | ||
var match = undefined; | ||
var chunkIndex = 0; | ||
while (match = pattern.exec(str)) { | ||
var chunk = undefined; | ||
do { | ||
chunk = found[chunkIndex]; | ||
if (chunk && chunk.end < match.index) { | ||
chunkIndex += 1; | ||
} else { | ||
break; | ||
} | ||
} while (chunk); | ||
if (!chunk || chunk.start > match.index) { | ||
callback.apply(null, match); | ||
} | ||
} | ||
} | ||
exports.find = find; | ||
exports.erase = erase; | ||
exports.match = match; | ||
})); | ||
//# sourceMappingURL=tippex.umd.js.map |
{ | ||
"name": "tippex", | ||
"description": "Find and erase strings and comments in JavaScript code", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"author": "Rich Harris", | ||
@@ -6,0 +6,0 @@ "main": "dist/tippex.umd.js", |
@@ -79,3 +79,29 @@ # Tippex | ||
Sometimes you might need to match a regular expression against the original string, but ignoring comments etc. For that you can use `tippex.match`: | ||
```js | ||
var code = ` | ||
import a from './a.js'; | ||
// import b from './b.js'; TODO do we need this? | ||
`; | ||
var importPattern = /import (.+?) from '([^']+)'/g; // must have 'g' flag | ||
var importDeclarations = []; | ||
tippex.match( code, importPattern, ( match, name, source ) => { | ||
// this callback will be called for each match that *doesn't* begin | ||
// inside a comment, string or regular expression | ||
importDeclarations.push({ name, source }); | ||
}); | ||
console.log( importDeclarations ); | ||
// -> [{ | ||
// name: 'a', | ||
// source: './a.js' | ||
// }] | ||
``` | ||
(A complete regular expression for ES6 imports would be a bit more complicated; this is for illustrative purposes.) | ||
## License | ||
@@ -82,0 +108,0 @@ |
@@ -172,1 +172,28 @@ export function find ( str ) { | ||
} | ||
export function match ( str, pattern, callback ) { | ||
if ( !pattern.global ) throw new Error( 'regular expression must have the g (global) flag' ); | ||
const found = find( str ); | ||
let match; | ||
let chunkIndex = 0; | ||
while ( match = pattern.exec( str ) ) { | ||
let chunk; | ||
do { | ||
chunk = found[ chunkIndex ]; | ||
if ( chunk && chunk.end < match.index ) { | ||
chunkIndex += 1; | ||
} else { | ||
break; | ||
} | ||
} while ( chunk ); | ||
if ( !chunk || chunk.start > match.index ) { | ||
callback.apply( null, match ); | ||
} | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
39114
13.36%9
12.5%487
14.32%113
29.89%