text2jsvar
v1.2.1
Published
A lightweight JavaScript library for converting plain text to JS string variable.
Downloads
24
Readme
text2jsvar
text2jsvar is a lightweight JavaScript library for converting plain text to JS string variable.
It can easily transfer the following JavaScript function text into a JavaScript string variable.
Convert:
var sayHello = function() {
alert("Hello World");
}
To:
var result = "var sayHello = function() {\n alert(\"Hello World\");\n}"
Online demo: Text to JS variable converter.
Install
npm install text2jsvar
Usage in Node.js
var text2jsvar = require('text2jsvar');
text2jsvar.convert('a\nb'); // return 'a\\nb'
Usage in browsers
You can find the minified JS file for browsers at ~/node_modules/text2jsvar/min/text2jsvar.min.js
. Copy this file to your web project and include it using <script>
tag, it will declare a global variable: text2jsvar
.
<script src="text2jsvar.min.js"></script>
<script>
text2jsvar.convert('a\nb'); // return 'a\\nb'
</script>
text2jsvar.convert(source, double)
Description: Convert text to JavaScript string variable.
Arguments
Return value
The converted text.
text2jsvar.revert(convertedText, double)
Description: Revert the converted text to the source string.
Arguments
Return value
The source string of the converted text.