Skip to content

text2jsvar is a lightweight JavaScript library for converting plain text to JS string variable

License

Notifications You must be signed in to change notification settings

js1016/text2jsvar

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

text2jsvar

NPM version NPM downloads MIT License

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

Parameter Type Description
source String The source string to be converted.
double Boolean Default: false. If double is set to true, the source string will be converted twice.

Return value

The converted text.

text2jsvar.revert(convertedText, double)

Description: Revert the converted text to the source string.

Arguments

Parameter Type Description
convertedText String The converted text to be reverted back.
double Boolean Default: false. Set double to true if the text was converted twice.

Return value

The source string of the converted text.

About

text2jsvar is a lightweight JavaScript library for converting plain text to JS string variable

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published