# text2jsvar

> A lightweight JavaScript library for converting plain text to JS string variable.

Latest version **1.2.1** (published 2017-02-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install text2jsvar
pnpm add text2jsvar
yarn add text2jsvar
bun add text2jsvar
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.2.1 |
| Published | 2017-02-24 |
| First published | 2017-02-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Sheng Jiang |
| Maintainers | joji |

## Links

- npm: https://www.npmjs.com/package/text2jsvar
- Repository: https://github.com/js1016/text2jsvar
- Homepage: https://github.com/js1016/text2jsvar#readme
- Issues: https://github.com/js1016/text2jsvar/issues
- npm.io page: https://npm.io/package/text2jsvar

## Recent versions

- 1.2.1 (latest) — 2017-02-24
- 1.2.0 — 2017-02-24
- 1.1.7 — 2017-02-23
- 1.1.6 — 2017-02-23
- 1.1.5 — 2017-02-23
- 1.1.4 — 2017-02-23
- 1.1.3 — 2017-02-23
- 1.1.2 — 2017-02-23
- 1.1.1 — 2017-02-23
- 1.1.0 — 2017-02-23
- 1.0.11 — 2017-02-23
- 1.0.10 — 2017-02-23
- 1.0.9 — 2017-02-23
- 1.0.8 — 2017-02-23
- 1.0.7 — 2017-02-23
- … 7 more at https://npm.io/package/text2jsvar/versions

## README

text2jsvar
==========
[![NPM version](http://img.shields.io/npm/v/text2jsvar.svg?style=flat)](https://www.npmjs.com/package/text2jsvar) [![NPM downloads](http://img.shields.io/npm/dm/text2jsvar.svg?style=flat)](https://www.npmjs.com/package/text2jsvar) [![MIT License](http://img.shields.io/badge/license-MIT-blue.svg?style=flat)](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:
```javascript
var sayHello = function() {
    alert("Hello World");
}
```

To:
```javascript
var result = "var sayHello = function() {\n    alert(\"Hello World\");\n}"
```

Online demo: [Text to JS variable converter](http://jstools.io/text-to-js-variable/ "Text to JS variable converter").

## Install

```
npm install text2jsvar
```

## Usage in Node.js

```javascript
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`.

```html
<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
<table>
    <tr>
        <th>Parameter</th>
        <th>Type</th>
        <th>Description</th>
    </tr>
    <tr>
        <td>source</td>
        <td>String</td>
        <td>The source string to be converted.</td>
    </tr>
    <tr>
        <td>double</td>
        <td>Boolean</td>
        <td>Default: false. If <b>double</b> is set to <b>true</b>, the source string will be converted twice.</td>
    </tr>
</table>

### Return value
The converted text.

## text2jsvar.revert(convertedText, double)
Description: Revert the converted text to the source string.

### Arguments
<table>
    <tr>
        <th>Parameter</th>
        <th>Type</th>
        <th>Description</th>
    </tr>
    <tr>
        <td>convertedText</td>
        <td>String</td>
        <td>The converted text to be reverted back.</td>
    </tr>
    <tr>
        <td>double</td>
        <td>Boolean</td>
        <td>Default: false. Set <b>double</b> to <b>true</b> if the text was converted twice.</td>
    </tr>
</table>

### Return value
The source string of the converted text.

---
_Source: https://npm.io/package/text2jsvar · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
