Comparing version 1.1.0 to 1.2.0
# Tippex changelog | ||
## 1.2.0 | ||
* Add `tippex.replace` method | ||
## 1.1.0 | ||
@@ -4,0 +8,0 @@ |
@@ -188,4 +188,16 @@ function find(str) { | ||
function makeGlobalRegExp(original) { | ||
var flags = 'g'; | ||
if (original.multiline) flags += 'm'; | ||
if (original.ignoreCase) flags += 'i'; | ||
if (original.sticky) flags += 'y'; | ||
if (original.unicode) flags += 'u'; | ||
return new RegExp(original.source, flags); | ||
} | ||
function match(str, pattern, callback) { | ||
if (!pattern.global) throw new Error('regular expression must have the g (global) flag'); | ||
var g = pattern.global; | ||
if (!g) pattern = makeGlobalRegExp(pattern); | ||
@@ -211,3 +223,5 @@ var found = find(str); | ||
if (!chunk || chunk.start > match.index) { | ||
callback.apply(null, match); | ||
var args = [].slice.call(match).concat(match.index, str); | ||
callback.apply(null, args); | ||
if (!g) break; | ||
} | ||
@@ -217,3 +231,33 @@ } | ||
export { find, erase, match }; | ||
function replace(str, pattern, callback) { | ||
var replacements = []; | ||
match(str, pattern, function (match) { | ||
var start = arguments[arguments.length - 2]; | ||
var end = start + match.length; | ||
var content = callback.apply(null, arguments); | ||
replacements.push({ start: start, end: end, content: content }); | ||
}); | ||
var replaced = ''; | ||
var lastIndex = 0; | ||
for (var i = 0; i < replacements.length; i += 1) { | ||
var _replacements$i = replacements[i]; | ||
var start = _replacements$i.start; | ||
var end = _replacements$i.end; | ||
var content = _replacements$i.content; | ||
replaced += str.slice(lastIndex, start) + content; | ||
lastIndex = end; | ||
} | ||
replaced += str.slice(lastIndex); | ||
return replaced; | ||
} | ||
export { find, erase, match, replace }; | ||
//# sourceMappingURL=tippex.es6.js.map |
@@ -194,4 +194,16 @@ (function (global, factory) { | ||
function makeGlobalRegExp(original) { | ||
var flags = 'g'; | ||
if (original.multiline) flags += 'm'; | ||
if (original.ignoreCase) flags += 'i'; | ||
if (original.sticky) flags += 'y'; | ||
if (original.unicode) flags += 'u'; | ||
return new RegExp(original.source, flags); | ||
} | ||
function match(str, pattern, callback) { | ||
if (!pattern.global) throw new Error('regular expression must have the g (global) flag'); | ||
var g = pattern.global; | ||
if (!g) pattern = makeGlobalRegExp(pattern); | ||
@@ -217,3 +229,5 @@ var found = find(str); | ||
if (!chunk || chunk.start > match.index) { | ||
callback.apply(null, match); | ||
var args = [].slice.call(match).concat(match.index, str); | ||
callback.apply(null, args); | ||
if (!g) break; | ||
} | ||
@@ -223,7 +237,38 @@ } | ||
function replace(str, pattern, callback) { | ||
var replacements = []; | ||
match(str, pattern, function (match) { | ||
var start = arguments[arguments.length - 2]; | ||
var end = start + match.length; | ||
var content = callback.apply(null, arguments); | ||
replacements.push({ start: start, end: end, content: content }); | ||
}); | ||
var replaced = ''; | ||
var lastIndex = 0; | ||
for (var i = 0; i < replacements.length; i += 1) { | ||
var _replacements$i = replacements[i]; | ||
var start = _replacements$i.start; | ||
var end = _replacements$i.end; | ||
var content = _replacements$i.content; | ||
replaced += str.slice(lastIndex, start) + content; | ||
lastIndex = end; | ||
} | ||
replaced += str.slice(lastIndex); | ||
return replaced; | ||
} | ||
exports.find = find; | ||
exports.erase = erase; | ||
exports.match = match; | ||
exports.replace = replace; | ||
})); | ||
//# sourceMappingURL=tippex.umd.js.map |
{ | ||
"name": "tippex", | ||
"description": "Find and erase strings and comments in JavaScript code", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"author": "Rich Harris", | ||
@@ -6,0 +6,0 @@ "main": "dist/tippex.umd.js", |
@@ -60,3 +60,5 @@ # Tippex | ||
...or download from npmcdn.com ([UMD version](https://npmcdn.com/tippex), [ES6 exports version](https://npmcdn.com/tippex/dist/tippex.es6.js)). | ||
## Usage | ||
@@ -106,3 +108,12 @@ | ||
To replace occurrences of a pattern that aren't inside strings or comments, use `tippex.replace`: | ||
```js | ||
code = tippex.replace( code, importPattern, ( match, name, source ) => { | ||
return `var ${name} = require('${source}')`; | ||
}); | ||
``` | ||
## License | ||
@@ -109,0 +120,0 @@ |
@@ -173,4 +173,16 @@ export function find ( str ) { | ||
function makeGlobalRegExp ( original ) { | ||
let flags = 'g'; | ||
if ( original.multiline ) flags += 'm'; | ||
if ( original.ignoreCase ) flags += 'i'; | ||
if ( original.sticky ) flags += 'y'; | ||
if ( original.unicode ) flags += 'u'; | ||
return new RegExp( original.source, flags ); | ||
} | ||
export function match ( str, pattern, callback ) { | ||
if ( !pattern.global ) throw new Error( 'regular expression must have the g (global) flag' ); | ||
const g = pattern.global; | ||
if ( !g ) pattern = makeGlobalRegExp( pattern ); | ||
@@ -196,5 +208,33 @@ const found = find( str ); | ||
if ( !chunk || chunk.start > match.index ) { | ||
callback.apply( null, match ); | ||
const args = [].slice.call( match ).concat( match.index, str ); | ||
callback.apply( null, args ); | ||
if ( !g ) break; | ||
} | ||
} | ||
} | ||
export function replace ( str, pattern, callback ) { | ||
let replacements = []; | ||
match( str, pattern, function ( match ) { | ||
const start = arguments[ arguments.length - 2 ]; | ||
const end = start + match.length; | ||
const content = callback.apply( null, arguments ); | ||
replacements.push({ start, end, content }); | ||
}); | ||
let replaced = ''; | ||
let lastIndex = 0; | ||
for ( let i = 0; i < replacements.length; i += 1 ) { | ||
const { start, end, content } = replacements[i]; | ||
replaced += str.slice( lastIndex, start ) + content; | ||
lastIndex = end; | ||
} | ||
replaced += str.slice( lastIndex ); | ||
return replaced; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
47305
581
124