Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/morsby/string-escape-spacing
Javascript package that escapes spacing characters (eg. newline) in strings
https://github.com/morsby/string-escape-spacing
Last synced: 23 days ago
JSON representation
Javascript package that escapes spacing characters (eg. newline) in strings
- Host: GitHub
- URL: https://github.com/morsby/string-escape-spacing
- Owner: morsby
- License: mit
- Created: 2020-04-15T09:08:23.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T12:24:26.000Z (over 1 year ago)
- Last Synced: 2024-08-10T09:28:44.721Z (3 months ago)
- Language: TypeScript
- Size: 991 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# string-escape-spacing
## Background
A small javascript package for escaping special characters interpreted as spacing in strings.
Imagine you're writing a code snippet in a template string.
```js
const code = `fmt.Printf("%06d\n",1)`;
```If you then did a `console.log` (or any other outputting), you'd get
```
fmt.Printf("%06d
",1)
```Hey, that's not what you wanted! ðŸ˜
Obviously, you could escape the backslash (`\n` → `\\n`) but that's not how you'd actually write the code.
_Enter **string-escape-spacing**_! It works by looking for unescaped spacing characters (i.e. `\n`, `\r`, `\t`, `\v` ) enclosed in string characters (`"The\tdog\twas\tlazy"`) whilst ignoring the string if it had no quotes around it (`The\tdog\twas\tlazy`).
## Usage
### Installation
```
yarn add string-escape-spacing
npm install string-escape-spacing
```### Example
```js
import escapeString from "string-escape-spacing";const string = `"The\tdog\twas\tlazy"`;
console.log(escapeString(string));
// output:`"The\\tdog\\twas\\tlazy"`
```### API
```js
const escaped = escapeString(stringToEscape, options);
```**Options**
```js
options = {
quotes: "single" | "double",
};
```