Comparing version 1.0.0 to 1.1.0
@@ -5,4 +5,9 @@ # Change Log | ||
## 1.1.0 | ||
- Switch to UMD for broader compatibility | ||
- Remove circular dependency with `compress-tag` | ||
## 1.0.0 | ||
- Initial release |
@@ -1,2 +0,1 @@ | ||
"use strict"; | ||
/** | ||
@@ -9,190 +8,196 @@ * @file **unraw** | Convert raw escape sequences to their respective characters | ||
*/ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const compress_tag_1 = require("compress-tag"); | ||
/** | ||
* Parse a string as a base-16 number. This is more strict than parseInt as it | ||
* will not allow any other characters, including (for example) "+", "-", and | ||
* ".". | ||
* @param hex A string containing a hexadecimal number. | ||
*/ | ||
function hexToInt(hex) { | ||
if (hex.match(/[^a-f0-9]/i) !== null) { | ||
// Matches the first non-hex symbol in the string | ||
return NaN; | ||
(function (factory) { | ||
if (typeof module === "object" && typeof module.exports === "object") { | ||
var v = factory(require, exports); | ||
if (v !== undefined) module.exports = v; | ||
} | ||
else { | ||
return parseInt(hex, 16); | ||
else if (typeof define === "function" && define.amd) { | ||
define(["require", "exports"], factory); | ||
} | ||
} | ||
/** | ||
* Parse a hexadecimal escape code. | ||
* @param code The two-character hex code that represents the character to | ||
* output. | ||
* @throws {SyntaxError} If the code is not valid hex or is not the right | ||
* length. | ||
*/ | ||
function parseHexadecimalCode(code) { | ||
const codeNumber = hexToInt(code); | ||
if (code.length !== 2 || Number.isNaN(codeNumber)) { | ||
// ie, "\xF" or "\x$$" | ||
throw new SyntaxError("malformed hexadecimal character escape sequence"); | ||
})(function (require, exports) { | ||
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
/** | ||
* Parse a string as a base-16 number. This is more strict than parseInt as it | ||
* will not allow any other characters, including (for example) "+", "-", and | ||
* ".". | ||
* @param hex A string containing a hexadecimal number. | ||
*/ | ||
function hexToInt(hex) { | ||
if (hex.match(/[^a-f0-9]/i) !== null) { | ||
// Matches the first non-hex symbol in the string | ||
return NaN; | ||
} | ||
else { | ||
return parseInt(hex, 16); | ||
} | ||
} | ||
return String.fromCharCode(codeNumber); | ||
} | ||
/** | ||
* Parse a Unicode escape code. | ||
* @param code The four-digit unicode number that represents the character to | ||
* output. | ||
* @param surrogateCode The four-digit unicode surrogate that represents the | ||
* character to output. | ||
* @throws {SyntaxError} If the codes are not valid hex or are not the right | ||
* length. | ||
*/ | ||
function parseUnicodeCode(code, surrogateCode) { | ||
const parsedCode = hexToInt(code); | ||
if (code.length !== 4 || Number.isNaN(parsedCode)) { | ||
// ie, "\u$$$$" or "\uF8" | ||
throw new SyntaxError("malformed Unicode character escape sequence"); | ||
/** | ||
* Parse a hexadecimal escape code. | ||
* @param code The two-character hex code that represents the character to | ||
* output. | ||
* @throws {SyntaxError} If the code is not valid hex or is not the right | ||
* length. | ||
*/ | ||
function parseHexadecimalCode(code) { | ||
const codeNumber = hexToInt(code); | ||
if (code.length !== 2 || Number.isNaN(codeNumber)) { | ||
// ie, "\xF" or "\x$$" | ||
throw new SyntaxError("malformed hexadecimal character escape sequence"); | ||
} | ||
return String.fromCharCode(codeNumber); | ||
} | ||
if (surrogateCode !== undefined) { | ||
let parsedSurrogateCode = undefined; | ||
parsedSurrogateCode = parseInt(surrogateCode, 16); | ||
if (surrogateCode.length !== 4 || Number.isNaN(parsedSurrogateCode)) { | ||
// ie, "\u00FF\uF" or "\u00FF\u$$$$" | ||
/** | ||
* Parse a Unicode escape code. | ||
* @param code The four-digit unicode number that represents the character to | ||
* output. | ||
* @param surrogateCode The four-digit unicode surrogate that represents the | ||
* character to output. | ||
* @throws {SyntaxError} If the codes are not valid hex or are not the right | ||
* length. | ||
*/ | ||
function parseUnicodeCode(code, surrogateCode) { | ||
const parsedCode = hexToInt(code); | ||
if (code.length !== 4 || Number.isNaN(parsedCode)) { | ||
// ie, "\u$$$$" or "\uF8" | ||
throw new SyntaxError("malformed Unicode character escape sequence"); | ||
} | ||
return String.fromCharCode(parsedCode, parsedSurrogateCode); | ||
} | ||
return String.fromCharCode(parsedCode); | ||
} | ||
/** | ||
* Parse a Unicode code point escape code. | ||
* @param codePoint A unicode escape code, including the surrounding curly | ||
* braces. | ||
* @throws {SyntaxError} If the code is not valid hex or does not have the | ||
* surrounding curly braces. | ||
*/ | ||
function parseUnicodeCodePointCode(codePoint) { | ||
const lastIndex = codePoint.length - 1; | ||
const parsedCode = hexToInt(codePoint.substring(1, lastIndex)); | ||
if (codePoint.charAt(0) !== "{" || | ||
codePoint.charAt(lastIndex) !== "}" || | ||
Number.isNaN(parsedCode)) { | ||
// ie, "\u$$$$" or "\uF8" | ||
throw new SyntaxError("malformed Unicode character escape sequence"); | ||
} | ||
try { | ||
return String.fromCodePoint(parsedCode); | ||
} | ||
catch (err) { | ||
if (err instanceof RangeError) { | ||
throw new SyntaxError(compress_tag_1.c ` | ||
Unicode codepoint must not be greater than 0x10FFFF in escape sequence | ||
`); | ||
if (surrogateCode !== undefined) { | ||
let parsedSurrogateCode = undefined; | ||
parsedSurrogateCode = parseInt(surrogateCode, 16); | ||
if (surrogateCode.length !== 4 || Number.isNaN(parsedSurrogateCode)) { | ||
// ie, "\u00FF\uF" or "\u00FF\u$$$$" | ||
throw new SyntaxError("malformed Unicode character escape sequence"); | ||
} | ||
return String.fromCharCode(parsedCode, parsedSurrogateCode); | ||
} | ||
else { | ||
throw err; | ||
} | ||
} | ||
} | ||
function parseOctalCode(code, error = false) { | ||
if (error) { | ||
throw new SyntaxError(compress_tag_1.c ` | ||
"0"-prefixed octal literals and octal escape sequences are deprecated; | ||
for octal literals use the "0o" prefix instead | ||
`); | ||
} | ||
else { | ||
// The original regex only allows digits so we don't need to have a strict | ||
// octal parser like hexToInt | ||
const parsedCode = parseInt(code, 8); | ||
return String.fromCharCode(parsedCode); | ||
} | ||
} | ||
/** | ||
* Parse a single character escape sequence and return the matching character. | ||
* If none is matched, returns `code`. Naively assumes `code.length == 1`. | ||
* @param code A single character code. | ||
*/ | ||
function parseSingleCharacterCode(code) { | ||
switch (code) { | ||
case "b": | ||
return "\b"; | ||
case "f": | ||
return "\f"; | ||
case "n": | ||
return "\n"; | ||
case "r": | ||
return "\r"; | ||
case "t": | ||
return "\t"; | ||
case "v": | ||
return "\v"; | ||
case "0": | ||
return "\0"; | ||
default: | ||
// Handles quotes and backslashes as well as anything else | ||
return code; | ||
} | ||
} | ||
/** | ||
* Matches every escape sequence possible, including invalid ones. | ||
* | ||
* All capture groups (described below) are unique (only one will match), except | ||
* for 3 and 4 which always match together. | ||
* | ||
* **Capture Groups:** | ||
* 0. A single backslash | ||
* 1. Hexadecimal code | ||
* 2. Unicode code point code with surrounding curly braces | ||
* 3. Unicode escape code with surrogate | ||
* 4. Surrogate code | ||
* 5. Unicode escape code without surrogate | ||
* 6. Octal code _NOTE: includes "0"._ | ||
* 7. A single character (will never be \, x, u, or 0-3) | ||
*/ | ||
const escapeMatch = /\\(?:(\\)|x([\s\S]{0,2})|u(\{[^}]*\}?)|u([\s\S]{4})\\u([\s\S]{0,4})|u([\s\S]{0,4})|([0-3]?[0-7]{1,2})|([\s\S])|$)/g; | ||
/** | ||
* Replace raw escape character strings with their escape characters. | ||
* @param raw A string where escape characters are represented as raw string | ||
* values like `\'` rather than `'`. | ||
* @param allowOctals If `true`, will process the now-deprecated octal escape | ||
* sequences (ie, `\111`). | ||
* @returns The processed string, with escape characters replaced by their | ||
* respective actual Unicode characters. | ||
*/ | ||
function unraw(raw, allowOctals = false) { | ||
return raw.replace(escapeMatch, function (_, backslash, hex, codePoint, unicodeWithSurrogate, surrogate, unicode, octal, singleCharacter) { | ||
// Compare groups to undefined because empty strings mean different errors | ||
// Otherwise, `\u` would fail the same as `\` which is wrong. | ||
if (backslash !== undefined) { | ||
return "\\"; | ||
/** | ||
* Parse a Unicode code point escape code. | ||
* @param codePoint A unicode escape code, including the surrounding curly | ||
* braces. | ||
* @throws {SyntaxError} If the code is not valid hex or does not have the | ||
* surrounding curly braces. | ||
*/ | ||
function parseUnicodeCodePointCode(codePoint) { | ||
const lastIndex = codePoint.length - 1; | ||
const parsedCode = hexToInt(codePoint.substring(1, lastIndex)); | ||
if (codePoint.charAt(0) !== "{" || | ||
codePoint.charAt(lastIndex) !== "}" || | ||
Number.isNaN(parsedCode)) { | ||
// ie, "\u$$$$" or "\uF8" | ||
throw new SyntaxError("malformed Unicode character escape sequence"); | ||
} | ||
else if (singleCharacter !== undefined) { | ||
return parseSingleCharacterCode(singleCharacter); | ||
try { | ||
return String.fromCodePoint(parsedCode); | ||
} | ||
else if (hex !== undefined) { | ||
return parseHexadecimalCode(hex); | ||
catch (err) { | ||
if (err instanceof RangeError) { | ||
throw new SyntaxError("Unicode codepoint must not be greater than 0x10FFFF in escape sequence"); | ||
} | ||
else { | ||
throw err; | ||
} | ||
} | ||
else if (codePoint !== undefined) { | ||
return parseUnicodeCodePointCode(codePoint); | ||
} | ||
function parseOctalCode(code, error = false) { | ||
if (error) { | ||
throw new SyntaxError('"0"-prefixed octal literals and octal escape sequences are ' + | ||
'deprecated; for octal literals use the "0o" prefix instead'); | ||
} | ||
else if (unicodeWithSurrogate !== undefined) { | ||
return parseUnicodeCode(unicodeWithSurrogate, surrogate); | ||
else { | ||
// The original regex only allows digits so we don't need to have a strict | ||
// octal parser like hexToInt | ||
const parsedCode = parseInt(code, 8); | ||
return String.fromCharCode(parsedCode); | ||
} | ||
else if (unicode !== undefined) { | ||
return parseUnicodeCode(unicode); | ||
} | ||
/** | ||
* Parse a single character escape sequence and return the matching character. | ||
* If none is matched, returns `code`. Naively assumes `code.length == 1`. | ||
* @param code A single character code. | ||
*/ | ||
function parseSingleCharacterCode(code) { | ||
switch (code) { | ||
case "b": | ||
return "\b"; | ||
case "f": | ||
return "\f"; | ||
case "n": | ||
return "\n"; | ||
case "r": | ||
return "\r"; | ||
case "t": | ||
return "\t"; | ||
case "v": | ||
return "\v"; | ||
case "0": | ||
return "\0"; | ||
default: | ||
// Handles quotes and backslashes as well as anything else | ||
return code; | ||
} | ||
else if (octal === "0") { | ||
return "\0"; | ||
} | ||
else if (octal !== undefined) { | ||
return parseOctalCode(octal, !allowOctals); | ||
} | ||
else { | ||
throw new SyntaxError("malformed escape sequence at end of string"); | ||
} | ||
}); | ||
} | ||
exports.default = unraw; | ||
} | ||
/** | ||
* Matches every escape sequence possible, including invalid ones. | ||
* | ||
* All capture groups (described below) are unique (only one will match), except | ||
* for 3 and 4 which always match together. | ||
* | ||
* **Capture Groups:** | ||
* 0. A single backslash | ||
* 1. Hexadecimal code | ||
* 2. Unicode code point code with surrounding curly braces | ||
* 3. Unicode escape code with surrogate | ||
* 4. Surrogate code | ||
* 5. Unicode escape code without surrogate | ||
* 6. Octal code _NOTE: includes "0"._ | ||
* 7. A single character (will never be \, x, u, or 0-3) | ||
*/ | ||
const escapeMatch = /\\(?:(\\)|x([\s\S]{0,2})|u(\{[^}]*\}?)|u([\s\S]{4})\\u([\s\S]{0,4})|u([\s\S]{0,4})|([0-3]?[0-7]{1,2})|([\s\S])|$)/g; | ||
/** | ||
* Replace raw escape character strings with their escape characters. | ||
* @param raw A string where escape characters are represented as raw string | ||
* values like `\'` rather than `'`. | ||
* @param allowOctals If `true`, will process the now-deprecated octal escape | ||
* sequences (ie, `\111`). | ||
* @returns The processed string, with escape characters replaced by their | ||
* respective actual Unicode characters. | ||
*/ | ||
function unraw(raw, allowOctals = false) { | ||
return raw.replace(escapeMatch, function (_, backslash, hex, codePoint, unicodeWithSurrogate, surrogate, unicode, octal, singleCharacter) { | ||
// Compare groups to undefined because empty strings mean different errors | ||
// Otherwise, `\u` would fail the same as `\` which is wrong. | ||
if (backslash !== undefined) { | ||
return "\\"; | ||
} | ||
else if (singleCharacter !== undefined) { | ||
return parseSingleCharacterCode(singleCharacter); | ||
} | ||
else if (hex !== undefined) { | ||
return parseHexadecimalCode(hex); | ||
} | ||
else if (codePoint !== undefined) { | ||
return parseUnicodeCodePointCode(codePoint); | ||
} | ||
else if (unicodeWithSurrogate !== undefined) { | ||
return parseUnicodeCode(unicodeWithSurrogate, surrogate); | ||
} | ||
else if (unicode !== undefined) { | ||
return parseUnicodeCode(unicode); | ||
} | ||
else if (octal === "0") { | ||
return "\0"; | ||
} | ||
else if (octal !== undefined) { | ||
return parseOctalCode(octal, !allowOctals); | ||
} | ||
else { | ||
throw new SyntaxError("malformed escape sequence at end of string"); | ||
} | ||
}); | ||
} | ||
exports.default = unraw; | ||
}); | ||
//# sourceMappingURL=index.js.map |
@@ -1,6 +0,1 @@ | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:!0});const compress_tag_1=require("compress-tag");function hexToInt(e){return null!==e.match(/[^a-f0-9]/i)?NaN:parseInt(e,16)}function parseHexadecimalCode(e){const r=hexToInt(e);if(2!==e.length||Number.isNaN(r))throw new SyntaxError("malformed hexadecimal character escape sequence");return String.fromCharCode(r)}function parseUnicodeCode(e,r){const t=hexToInt(e);if(4!==e.length||Number.isNaN(t))throw new SyntaxError("malformed Unicode character escape sequence");if(void 0!==r){let e=void 0;if(e=parseInt(r,16),4!==r.length||Number.isNaN(e))throw new SyntaxError("malformed Unicode character escape sequence");return String.fromCharCode(t,e)}return String.fromCharCode(t)}function parseUnicodeCodePointCode(e){const r=e.length-1,t=hexToInt(e.substring(1,r));if("{"!==e.charAt(0)||"}"!==e.charAt(r)||Number.isNaN(t))throw new SyntaxError("malformed Unicode character escape sequence");try{return String.fromCodePoint(t)}catch(e){throw e instanceof RangeError?new SyntaxError(compress_tag_1.c` | ||
Unicode codepoint must not be greater than 0x10FFFF in escape sequence | ||
`):e}}function parseOctalCode(e,r=!1){if(r)throw new SyntaxError(compress_tag_1.c` | ||
"0"-prefixed octal literals and octal escape sequences are deprecated; | ||
for octal literals use the "0o" prefix instead | ||
`);{const r=parseInt(e,8);return String.fromCharCode(r)}}function parseSingleCharacterCode(e){switch(e){case"b":return"\b";case"f":return"\f";case"n":return"\n";case"r":return"\r";case"t":return"\t";case"v":return"\v";case"0":return"\0";default:return e}}const escapeMatch=/\\(?:(\\)|x([\s\S]{0,2})|u(\{[^}]*\}?)|u([\s\S]{4})\\u([\s\S]{0,4})|u([\s\S]{0,4})|([0-3]?[0-7]{1,2})|([\s\S])|$)/g;function unraw(e,r=!1){return e.replace(escapeMatch,function(e,t,n,a,o,c,s,i,u){if(void 0!==t)return"\\";if(void 0!==u)return parseSingleCharacterCode(u);if(void 0!==n)return parseHexadecimalCode(n);if(void 0!==a)return parseUnicodeCodePointCode(a);if(void 0!==o)return parseUnicodeCode(o,c);if(void 0!==s)return parseUnicodeCode(s);if("0"===i)return"\0";if(void 0!==i)return parseOctalCode(i,!r);throw new SyntaxError("malformed escape sequence at end of string")})}exports.default=unraw; | ||
!function(e){if("object"==typeof module&&"object"==typeof module.exports){var r=e(require,exports);void 0!==r&&(module.exports=r)}else"function"==typeof define&&define.amd&&define(["require","exports"],e)}(function(e,r){"use strict";function t(e){return null!==e.match(/[^a-f0-9]/i)?NaN:parseInt(e,16)}function n(e,r){const n=t(e);if(4!==e.length||Number.isNaN(n))throw new SyntaxError("malformed Unicode character escape sequence");if(void 0!==r){let e=void 0;if(e=parseInt(r,16),4!==r.length||Number.isNaN(e))throw new SyntaxError("malformed Unicode character escape sequence");return String.fromCharCode(n,e)}return String.fromCharCode(n)}Object.defineProperty(r,"__esModule",{value:!0});const o=/\\(?:(\\)|x([\s\S]{0,2})|u(\{[^}]*\}?)|u([\s\S]{4})\\u([\s\S]{0,4})|u([\s\S]{0,4})|([0-3]?[0-7]{1,2})|([\s\S])|$)/g;r.default=function(e,r=!1){return e.replace(o,function(e,o,a,i,c,u,s,f,d){if(void 0!==o)return"\\";if(void 0!==d)return function(e){switch(e){case"b":return"\b";case"f":return"\f";case"n":return"\n";case"r":return"\r";case"t":return"\t";case"v":return"\v";case"0":return"\0";default:return e}}(d);if(void 0!==a)return function(e){const r=t(e);if(2!==e.length||Number.isNaN(r))throw new SyntaxError("malformed hexadecimal character escape sequence");return String.fromCharCode(r)}(a);if(void 0!==i)return function(e){const r=e.length-1,n=t(e.substring(1,r));if("{"!==e.charAt(0)||"}"!==e.charAt(r)||Number.isNaN(n))throw new SyntaxError("malformed Unicode character escape sequence");try{return String.fromCodePoint(n)}catch(e){throw e instanceof RangeError?new SyntaxError("Unicode codepoint must not be greater than 0x10FFFF in escape sequence"):e}}(i);if(void 0!==c)return n(c,u);if(void 0!==s)return n(s);if("0"===f)return"\0";if(void 0!==f)return function(e,r=!1){if(r)throw new SyntaxError('"0"-prefixed octal literals and octal escape sequences are deprecated; for octal literals use the "0o" prefix instead');{const r=parseInt(e,8);return String.fromCharCode(r)}}(f,!r);throw new SyntaxError("malformed escape sequence at end of string")})}}); |
{ | ||
"name": "unraw", | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"description": "Convert raw escape sequences to their respective characters (undo String.raw).", | ||
@@ -49,5 +49,3 @@ "main": "dist/index.js", | ||
}, | ||
"dependencies": { | ||
"compress-tag": "^1.1.6" | ||
}, | ||
"dependencies": {}, | ||
"nyc": { | ||
@@ -54,0 +52,0 @@ "include": [ |
@@ -16,2 +16,4 @@ # unraw | ||
## Installation | ||
This is a UMD module. | ||
`unraw` is hosted on [NPM](https://www.npmjs.com/unraw): | ||
@@ -22,6 +24,4 @@ ```bash | ||
You can embed it (minified) on a webpage with [UNPKG](https://unpkg.com): | ||
```html | ||
<script src="https://unpkg.com/unraw"></script> | ||
``` | ||
You can embed it (minified) on a webpage with [UNPKG](https://unpkg.com): | ||
https://unpkg.com/unraw | ||
@@ -28,0 +28,0 @@ ## Usage |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
21895
0
1
218
- Removedcompress-tag@^1.1.6
- Removedcompress-tag@1.2.2(transitive)
- Removedunraw@1.2.5(transitive)