source: node_modules/unraw/readme.md@ d24f17c

main
Last change on this file since d24f17c was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 5.3 KB
Line 
1# `unraw`
2
3```ts
4unraw("\\'\\t\\u{1f601}\\'"); // -> "' 😁'"
5```
6
7`unraw` is a small module that converts raw strings to parsed strings in the same
8manner as the standard JavaScript escaping engine. In essence, it is the exact
9opposite of
10[`String.raw`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw).
11
12## Use Case
13
14Most of the time, you probably don't need this library unless you're working
15directly with raw strings and you need a way to get them back to normal strings.
16Maybe the most signicant use case is when building
17[template literal tags](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#Tagged_templates);
18you can use the `.raw` property of the passed string array to access the raw
19strings, but then you may want to still return normal strings after processing.
20
21The module is also useful for parsing text files written with escape sequences,
22although keep in mind that the JavaScript flavor of escape sequences may differ
23from the flavor used in an input file.
24
25## Getting Started
26
27`unraw` is a UMD module, so it can be used in Node or on the web. Typings are
28included for TypeScript as well.
29
30### Usage in Node.JS
31
32`unraw` is hosted on [npm](https://www.npmjs.com/unraw), so you can install
33with:
34
35```bash
36npm i unraw
37```
38
39To use in code:
40
41```js
42import unraw from "unraw";
43
44unraw("\\n");
45```
46
47If you want to access error messages:
48
49```js
50import {unraw, errorMessages, ErrorType} from "unraw";
51
52unraw("\\n");
53errorMessages.get(ErrorType.MalformedUnicode);
54```
55
56### Usage in the Browser
57
58You can embed it (minified) on a webpage with
59[RequireJS](https://requirejs.org/). The module is available on
60[UNPKG](https://unpkg.com) at https://unpkg.com/unraw:
61
62```html
63<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>
64<script>
65 require(["https://unpkg.com/unraw^1.2.3/dist/index.min.js"], function(unraw) {
66 unraw.unraw("\\n");
67 unraw.errorMessages.get(unraw.ErrorType.MalformedUnicode);
68 });
69</script>
70```
71
72_Note_: Importing via the 'bare' url (`https://unpkg.com/unraw`) is not
73supported as it breaks references to other required files.
74
75## Usage
76
77Usage is simple - the library exports a default function, `unraw`. The first
78argument to `unraw` is the string to parse, and the second is an optional flag
79to allow or disallow octal escapes, which are deprecated (defaults to
80`false`, so the default behavior is to throw an error when octal sequences
81are encountered).
82
83```js
84unraw("\\t\\tThis is indented.");
85// => " This is indented."
86```
87
88The library attempts to mimic the behaviour of standard JavaScript strings as
89closely as possible. This means that invalid escape sequences will throw
90`SyntaxError`s and that every escape sequence that is valid in a normal string
91should be valid when passed to `unraw`.
92
93In some ways this is similar to the behavior of `JSON.parse`.
94
95You can always expect the outcome of calling `unraw` on a raw string to be
96exactly the same as if that string were not raw in the first place:
97
98```js
99`Invalid: \u23` // Throws a SyntaxError
100unraw(String.raw`Invalid: \u23`) // Throws a SyntaxError
101
102`Valid: \u0041` // => `Valid: A`
103unraw(String.raw`Valid: \u0041`) // => `Valid: A`
104
105`Valid: \A` // => `Valid: A`
106unraw(String.raw`Valid: \A`) // => `Valid: A`
107
108`Valid: \\` // => `Valid: \`
109unraw(String.raw`Valid: \\`) // => `Valid: \`
110
111`Valid: \x42` // => `Valid: B`
112unraw(String.raw`Valid: \x42`) // => `Valid: B`
113
114`Octal: \102` // => Throws a SyntaxError
115unraw(String.raw`Octal: \102`) // => Throws a SyntaxError
116unraw(String.raw`Octal: \102`, true) // => Octal: B
117```
118
119### Errors
120
121If desired, you can access the possible error messages to help identify errors:
122
123```ts
124import {unraw, ErrorType, errorMessages} from "unraw";
125
126try {
127 unraw("\\u25");
128} catch (err) {
129 if (err.message === errorMessages.get(ErrorType.MalformedUnicode)) {
130 console.error("String had an invalid Unicode escape sequence.");
131 }
132}
133```
134
135The full list of error message names available through the `ErrorType` enum
136(exposed as a normal object in JavaScript).
137
138## Contributing
139
140Found a bug? Please,
141[submit it here.](https://github.com/iansan5653/unraw/issues)
142
143Pull requests are always welcome, although to increase your chances of your
144contribution being accepted, opening an issue and linking to it is always a
145good idea.
146
147Pull requests will not be merged unless the Azure Pipelines build succeeds.
148This means that all checks must pass and code must be free of lint errors. To
149quickly confirm that it will, just run:
150
151```bash
152npm run check
153```
154
155This checks your formatting, tests, and for TypeScript compiler errors. If the
156task doesn't fail, you should be good to go.
157
158### Other Tasks
159
160For your convenience, some other tasks are also provided in the `package.json`:
161
162- `npm run build` - Compiles TypeScript code to JavaScript
163- `npm run minify` - Generate minified JavaScript files from compiled files
164- `npm run test` - Quickly run tests using TypeScript code without compiling
165- `npm run testWithCoverage` - Run tests and generate coverage report
166- `npm run lint` - Check code for linting errors
167- `npm run check` - Check to ensure code will pass Pipelines checks (see above)
168- `npm run format` - Format code using Prettier
Note: See TracBrowser for help on using the repository browser.