1 | # `unraw`
|
---|
2 |
|
---|
3 | ```ts
|
---|
4 | unraw("\\'\\t\\u{1f601}\\'"); // -> "' 😁'"
|
---|
5 | ```
|
---|
6 |
|
---|
7 | `unraw` is a small module that converts raw strings to parsed strings in the same
|
---|
8 | manner as the standard JavaScript escaping engine. In essence, it is the exact
|
---|
9 | opposite of
|
---|
10 | [`String.raw`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/raw).
|
---|
11 |
|
---|
12 | ## Use Case
|
---|
13 |
|
---|
14 | Most of the time, you probably don't need this library unless you're working
|
---|
15 | directly with raw strings and you need a way to get them back to normal strings.
|
---|
16 | Maybe 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);
|
---|
18 | you can use the `.raw` property of the passed string array to access the raw
|
---|
19 | strings, but then you may want to still return normal strings after processing.
|
---|
20 |
|
---|
21 | The module is also useful for parsing text files written with escape sequences,
|
---|
22 | although keep in mind that the JavaScript flavor of escape sequences may differ
|
---|
23 | from 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
|
---|
28 | included 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
|
---|
33 | with:
|
---|
34 |
|
---|
35 | ```bash
|
---|
36 | npm i unraw
|
---|
37 | ```
|
---|
38 |
|
---|
39 | To use in code:
|
---|
40 |
|
---|
41 | ```js
|
---|
42 | import unraw from "unraw";
|
---|
43 |
|
---|
44 | unraw("\\n");
|
---|
45 | ```
|
---|
46 |
|
---|
47 | If you want to access error messages:
|
---|
48 |
|
---|
49 | ```js
|
---|
50 | import {unraw, errorMessages, ErrorType} from "unraw";
|
---|
51 |
|
---|
52 | unraw("\\n");
|
---|
53 | errorMessages.get(ErrorType.MalformedUnicode);
|
---|
54 | ```
|
---|
55 |
|
---|
56 | ### Usage in the Browser
|
---|
57 |
|
---|
58 | You 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
|
---|
73 | supported as it breaks references to other required files.
|
---|
74 |
|
---|
75 | ## Usage
|
---|
76 |
|
---|
77 | Usage is simple - the library exports a default function, `unraw`. The first
|
---|
78 | argument to `unraw` is the string to parse, and the second is an optional flag
|
---|
79 | to allow or disallow octal escapes, which are deprecated (defaults to
|
---|
80 | `false`, so the default behavior is to throw an error when octal sequences
|
---|
81 | are encountered).
|
---|
82 |
|
---|
83 | ```js
|
---|
84 | unraw("\\t\\tThis is indented.");
|
---|
85 | // => " This is indented."
|
---|
86 | ```
|
---|
87 |
|
---|
88 | The library attempts to mimic the behaviour of standard JavaScript strings as
|
---|
89 | closely 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
|
---|
91 | should be valid when passed to `unraw`.
|
---|
92 |
|
---|
93 | In some ways this is similar to the behavior of `JSON.parse`.
|
---|
94 |
|
---|
95 | You can always expect the outcome of calling `unraw` on a raw string to be
|
---|
96 | exactly the same as if that string were not raw in the first place:
|
---|
97 |
|
---|
98 | ```js
|
---|
99 | `Invalid: \u23` // Throws a SyntaxError
|
---|
100 | unraw(String.raw`Invalid: \u23`) // Throws a SyntaxError
|
---|
101 |
|
---|
102 | `Valid: \u0041` // => `Valid: A`
|
---|
103 | unraw(String.raw`Valid: \u0041`) // => `Valid: A`
|
---|
104 |
|
---|
105 | `Valid: \A` // => `Valid: A`
|
---|
106 | unraw(String.raw`Valid: \A`) // => `Valid: A`
|
---|
107 |
|
---|
108 | `Valid: \\` // => `Valid: \`
|
---|
109 | unraw(String.raw`Valid: \\`) // => `Valid: \`
|
---|
110 |
|
---|
111 | `Valid: \x42` // => `Valid: B`
|
---|
112 | unraw(String.raw`Valid: \x42`) // => `Valid: B`
|
---|
113 |
|
---|
114 | `Octal: \102` // => Throws a SyntaxError
|
---|
115 | unraw(String.raw`Octal: \102`) // => Throws a SyntaxError
|
---|
116 | unraw(String.raw`Octal: \102`, true) // => Octal: B
|
---|
117 | ```
|
---|
118 |
|
---|
119 | ### Errors
|
---|
120 |
|
---|
121 | If desired, you can access the possible error messages to help identify errors:
|
---|
122 |
|
---|
123 | ```ts
|
---|
124 | import {unraw, ErrorType, errorMessages} from "unraw";
|
---|
125 |
|
---|
126 | try {
|
---|
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 |
|
---|
135 | The full list of error message names available through the `ErrorType` enum
|
---|
136 | (exposed as a normal object in JavaScript).
|
---|
137 |
|
---|
138 | ## Contributing
|
---|
139 |
|
---|
140 | Found a bug? Please,
|
---|
141 | [submit it here.](https://github.com/iansan5653/unraw/issues)
|
---|
142 |
|
---|
143 | Pull requests are always welcome, although to increase your chances of your
|
---|
144 | contribution being accepted, opening an issue and linking to it is always a
|
---|
145 | good idea.
|
---|
146 |
|
---|
147 | Pull requests will not be merged unless the Azure Pipelines build succeeds.
|
---|
148 | This means that all checks must pass and code must be free of lint errors. To
|
---|
149 | quickly confirm that it will, just run:
|
---|
150 |
|
---|
151 | ```bash
|
---|
152 | npm run check
|
---|
153 | ```
|
---|
154 |
|
---|
155 | This checks your formatting, tests, and for TypeScript compiler errors. If the
|
---|
156 | task doesn't fail, you should be good to go.
|
---|
157 |
|
---|
158 | ### Other Tasks
|
---|
159 |
|
---|
160 | For 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
|
---|