Created
October 27, 2014 20:01
-
-
Save jnwng/96317c304cba114e752c to your computer and use it in GitHub Desktop.
Changing matching quotes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var fs = require('fs'); | |
var esprima = require('esprima'); | |
var input = process.argv[2]; | |
var content = fs.readFileSync(input, 'utf-8'); | |
var tokens = esprima.parse(content, { | |
tokens: true, | |
range: true | |
}).tokens; | |
function convert(literal) { | |
var result = literal.substring(1, literal.length - 1); | |
result = result.replace(/'/g, '\\\''); | |
return '\'' + result + '\''; | |
} | |
tokens.forEach(function(token) { | |
var str; | |
if (token.type === 'String' && token.value[0] !== '\'') { | |
str = convert(token.value); | |
content = content.substring(0, token.range[0]) + str + | |
content.substring(token.range[1], content.length); | |
} | |
}); | |
fs.writeFileSync(input, content); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment