Socket
Socket
Sign inDemoInstall

unraw

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unraw - npm Package Compare versions

Comparing version 1.2.2 to 1.2.3

5

changelog.md

@@ -5,2 +5,7 @@ # Change Log

## 1.2.3
- Improve UMD documentation
- Export `unraw`, `ErrorType`, and `errorMessages` from main module along with
default export
## 1.2.2

@@ -7,0 +12,0 @@ - Update readme

39

dist/errors.d.ts

@@ -7,6 +7,39 @@ /**

*/
/** The name of an error message. */
export declare type ErrorMessageName = "malformedUnicode" | "malformedHexadecimal" | "codePointLimit" | "octalDeprecation" | "endOfString";
/**
* Keys for possible error messages used by `unraw`.
* Note: These do _not_ map to actual error object types. All errors thrown
* are `SyntaxError`.
*/
export declare enum ErrorType {
/**
* Thrown when a badly formed Unicode escape sequence is found. Possible
* reasons include the code being too short (`"\u25"`) or having invalid
* characters (`"\u2$A5"`).
*/
MalformedUnicode = "MALFORMED_UNICODE",
/**
* Thrown when a badly formed hexadecimal escape sequence is found. Possible
* reasons include the code being too short (`"\x2"`) or having invalid
* characters (`"\x2$"`).
*/
MalformedHexadecimal = "MALFORMED_HEXADECIMAL",
/**
* Thrown when a Unicode code point escape sequence has too high of a code
* point. The maximum code point allowed is `\u{10FFFF}`, so `\u{110000}` and
* higher will throw this error.
*/
CodePointLimit = "CODE_POINT_LIMIT",
/**
* Thrown when an octal escape sequences is encountered and `allowOctals` is
* `false`. For example, `unraw("\234", false)`.
*/
OctalDeprecation = "OCTAL_DEPRECATION",
/**
* Thrown only when a single backslash is found at the end of a string. For
* example, `"\\"` or `"test\\x24\\"`.
*/
EndOfString = "END_OF_STRING"
}
/** Map of error message names to the full text of the message. */
export declare const errorMessages: Map<ErrorMessageName, string>;
export declare const errorMessages: Readonly<Map<ErrorType, string>>;
//# sourceMappingURL=errors.d.ts.map

@@ -18,18 +18,60 @@ /**

Object.defineProperty(exports, "__esModule", { value: true });
// NOTE: don't construct errors here or they'll have the wrong stack trace.
// NOTE: don't make custom error class; the JS engines use `SyntaxError`
/**
* Keys for possible error messages used by `unraw`.
* Note: These do _not_ map to actual error object types. All errors thrown
* are `SyntaxError`.
*/
// Don't use const enum or JS users won't be able to access the enum values
var ErrorType;
(function (ErrorType) {
/**
* Thrown when a badly formed Unicode escape sequence is found. Possible
* reasons include the code being too short (`"\u25"`) or having invalid
* characters (`"\u2$A5"`).
*/
ErrorType["MalformedUnicode"] = "MALFORMED_UNICODE";
/**
* Thrown when a badly formed hexadecimal escape sequence is found. Possible
* reasons include the code being too short (`"\x2"`) or having invalid
* characters (`"\x2$"`).
*/
ErrorType["MalformedHexadecimal"] = "MALFORMED_HEXADECIMAL";
/**
* Thrown when a Unicode code point escape sequence has too high of a code
* point. The maximum code point allowed is `\u{10FFFF}`, so `\u{110000}` and
* higher will throw this error.
*/
ErrorType["CodePointLimit"] = "CODE_POINT_LIMIT";
/**
* Thrown when an octal escape sequences is encountered and `allowOctals` is
* `false`. For example, `unraw("\234", false)`.
*/
ErrorType["OctalDeprecation"] = "OCTAL_DEPRECATION";
/**
* Thrown only when a single backslash is found at the end of a string. For
* example, `"\\"` or `"test\\x24\\"`.
*/
ErrorType["EndOfString"] = "END_OF_STRING";
})(ErrorType = exports.ErrorType || (exports.ErrorType = {}));
/** Map of error message names to the full text of the message. */
exports.errorMessages = new Map([
["malformedUnicode", "malformed Unicode character escape sequence"],
["malformedHexadecimal", "malformed hexadecimal character escape sequence"],
[ErrorType.MalformedUnicode, "malformed Unicode character escape sequence"],
[
"codePointLimit",
ErrorType.MalformedHexadecimal,
"malformed hexadecimal character escape sequence"
],
[
ErrorType.CodePointLimit,
"Unicode codepoint must not be greater than 0x10FFFF in escape sequence"
],
[
"octalDeprecation",
ErrorType.OctalDeprecation,
'"0"-prefixed octal literals and octal escape sequences are deprecated; ' +
'for octal literals use the "0o" prefix instead'
],
["endOfString", "malformed escape sequence at end of string"]
[ErrorType.EndOfString, "malformed escape sequence at end of string"]
]);
});
//# sourceMappingURL=errors.js.map

2

dist/errors.min.js

@@ -1,1 +0,1 @@

!function(e){if("object"==typeof module&&"object"==typeof module.exports){var o=e(require,exports);void 0!==o&&(module.exports=o)}else"function"==typeof define&&define.amd&&define(["require","exports"],e)}(function(e,o){"use strict";Object.defineProperty(o,"__esModule",{value:!0}),o.errorMessages=new Map([["malformedUnicode","malformed Unicode character escape sequence"],["malformedHexadecimal","malformed hexadecimal character escape sequence"],["codePointLimit","Unicode codepoint must not be greater than 0x10FFFF in escape sequence"],["octalDeprecation",'"0"-prefixed octal literals and octal escape sequences are deprecated; for octal literals use the "0o" prefix instead'],["endOfString","malformed escape sequence at end of string"]])});
!function(e){if("object"==typeof module&&"object"==typeof module.exports){var o=e(require,exports);void 0!==o&&(module.exports=o)}else"function"==typeof define&&define.amd&&define(["require","exports"],e)}(function(e,o){"use strict";var r;Object.defineProperty(o,"__esModule",{value:!0}),function(e){e.MalformedUnicode="MALFORMED_UNICODE",e.MalformedHexadecimal="MALFORMED_HEXADECIMAL",e.CodePointLimit="CODE_POINT_LIMIT",e.OctalDeprecation="OCTAL_DEPRECATION",e.EndOfString="END_OF_STRING"}(r=o.ErrorType||(o.ErrorType={})),o.errorMessages=new Map([[r.MalformedUnicode,"malformed Unicode character escape sequence"],[r.MalformedHexadecimal,"malformed hexadecimal character escape sequence"],[r.CodePointLimit,"Unicode codepoint must not be greater than 0x10FFFF in escape sequence"],[r.OctalDeprecation,'"0"-prefixed octal literals and octal escape sequences are deprecated; for octal literals use the "0o" prefix instead'],[r.EndOfString,"malformed escape sequence at end of string"]])});

@@ -8,2 +8,4 @@ /**

*/
import { ErrorType, errorMessages } from "./errors";
export { ErrorType, errorMessages };
/**

@@ -18,3 +20,4 @@ * Replace raw escape character strings with their escape characters.

*/
export default function unraw(raw: string, allowOctals?: boolean): string;
export declare function unraw(raw: string, allowOctals?: boolean): string;
export default unraw;
//# sourceMappingURL=index.d.ts.map

@@ -20,2 +20,4 @@ /**

const errors_1 = require("./errors");
exports.ErrorType = errors_1.ErrorType;
exports.errorMessages = errors_1.errorMessages;
/**

@@ -57,3 +59,3 @@ * Parse a string as a base-16 number. This is more strict than parseInt as it

function parseHexadecimalCode(code) {
const parsedCode = validateAndParseHex(code, "malformedHexadecimal", 2);
const parsedCode = validateAndParseHex(code, errors_1.ErrorType.MalformedHexadecimal, 2);
return String.fromCharCode(parsedCode);

@@ -71,5 +73,5 @@ }

function parseUnicodeCode(code, surrogateCode) {
const parsedCode = validateAndParseHex(code, "malformedUnicode", 4);
const parsedCode = validateAndParseHex(code, errors_1.ErrorType.MalformedUnicode, 4);
if (surrogateCode !== undefined) {
const parsedSurrogateCode = validateAndParseHex(surrogateCode, "malformedUnicode", 4);
const parsedSurrogateCode = validateAndParseHex(surrogateCode, errors_1.ErrorType.MalformedUnicode, 4);
return String.fromCharCode(parsedCode, parsedSurrogateCode);

@@ -96,6 +98,6 @@ }

if (!isCurlyBraced(codePoint)) {
throw new SyntaxError(errors_1.errorMessages.get("malformedUnicode"));
throw new SyntaxError(errors_1.errorMessages.get(errors_1.ErrorType.MalformedUnicode));
}
const withoutBraces = codePoint.slice(1, -1);
const parsedCode = validateAndParseHex(withoutBraces, "malformedUnicode");
const parsedCode = validateAndParseHex(withoutBraces, errors_1.ErrorType.MalformedUnicode);
try {

@@ -106,3 +108,3 @@ return String.fromCodePoint(parsedCode);

throw err instanceof RangeError
? new SyntaxError(errors_1.errorMessages.get("codePointLimit"))
? new SyntaxError(errors_1.errorMessages.get(errors_1.ErrorType.CodePointLimit))
: err;

@@ -113,3 +115,3 @@ }

if (error) {
throw new SyntaxError(errors_1.errorMessages.get("octalDeprecation"));
throw new SyntaxError(errors_1.errorMessages.get(errors_1.ErrorType.OctalDeprecation));
}

@@ -196,7 +198,8 @@ // The original regex only allows digits so we don't need to have a strict

}
throw new SyntaxError(errors_1.errorMessages.get("endOfString"));
throw new SyntaxError(errors_1.errorMessages.get(errors_1.ErrorType.EndOfString));
});
}
exports.unraw = unraw;
exports.default = unraw;
});
//# sourceMappingURL=index.js.map

@@ -1,1 +0,1 @@

!function(r){if("object"==typeof module&&"object"==typeof module.exports){var e=r(require,exports);void 0!==e&&(module.exports=e)}else"function"==typeof define&&define.amd&&define(["require","exports","./errors"],r)}(function(r,e){"use strict";Object.defineProperty(e,"__esModule",{value:!0});const t=r("./errors");function o(r,e,o){const n=function(r){return r.match(/[^a-f0-9]/i)?NaN:parseInt(r,16)}(r);if(Number.isNaN(n)||o&&r.length!==o)throw new SyntaxError(t.errorMessages.get(e));return n}function n(r,e){const t=o(r,"malformedUnicode",4);if(void 0!==e){const r=o(e,"malformedUnicode",4);return String.fromCharCode(t,r)}return String.fromCharCode(t)}const i=new Map([["b","\b"],["f","\f"],["n","\n"],["r","\r"],["t","\t"],["v","\v"],["0","\0"]]);const f=/\\(?:(\\)|x([\s\S]{0,2})|u(\{[^}]*\}?)|u([\s\S]{4})\\u([^{][\s\S]{0,3})|u([\s\S]{0,4})|([0-3]?[0-7]{1,2})|([\s\S])|$)/g;e.default=function(r,e=!1){return r.replace(f,function(r,f,s,a,u,c,d,m,g){if(void 0!==f)return"\\";if(void 0!==s)return function(r){const e=o(r,"malformedHexadecimal",2);return String.fromCharCode(e)}(s);if(void 0!==a)return function(r){if("{"!==(e=r).charAt(0)||"}"!==e.charAt(e.length-1))throw new SyntaxError(t.errorMessages.get("malformedUnicode"));var e;const n=o(r.slice(1,-1),"malformedUnicode");try{return String.fromCodePoint(n)}catch(r){throw r instanceof RangeError?new SyntaxError(t.errorMessages.get("codePointLimit")):r}}(a);if(void 0!==u)return n(u,c);if(void 0!==d)return n(d);if("0"===m)return"\0";if(void 0!==m)return function(r,e=!1){if(e)throw new SyntaxError(t.errorMessages.get("octalDeprecation"));const o=parseInt(r,8);return String.fromCharCode(o)}(m,!e);if(void 0!==g)return l=g,i.get(l)||l;var l;throw new SyntaxError(t.errorMessages.get("endOfString"))})}});
!function(r){if("object"==typeof module&&"object"==typeof module.exports){var e=r(require,exports);void 0!==e&&(module.exports=e)}else"function"==typeof define&&define.amd&&define(["require","exports","./errors"],r)}(function(r,e){"use strict";Object.defineProperty(e,"__esModule",{value:!0});const o=r("./errors");function t(r,e,t){const n=function(r){return r.match(/[^a-f0-9]/i)?NaN:parseInt(r,16)}(r);if(Number.isNaN(n)||t&&r.length!==t)throw new SyntaxError(o.errorMessages.get(e));return n}function n(r,e){const n=t(r,o.ErrorType.MalformedUnicode,4);if(void 0!==e){const r=t(e,o.ErrorType.MalformedUnicode,4);return String.fromCharCode(n,r)}return String.fromCharCode(n)}e.ErrorType=o.ErrorType,e.errorMessages=o.errorMessages;const i=new Map([["b","\b"],["f","\f"],["n","\n"],["r","\r"],["t","\t"],["v","\v"],["0","\0"]]);const s=/\\(?:(\\)|x([\s\S]{0,2})|u(\{[^}]*\}?)|u([\s\S]{4})\\u([^{][\s\S]{0,3})|u([\s\S]{0,4})|([0-3]?[0-7]{1,2})|([\s\S])|$)/g;function f(r,e=!1){return r.replace(s,function(r,s,f,a,u,c,d,g,p){if(void 0!==s)return"\\";if(void 0!==f)return function(r){const e=t(r,o.ErrorType.MalformedHexadecimal,2);return String.fromCharCode(e)}(f);if(void 0!==a)return function(r){if("{"!==(e=r).charAt(0)||"}"!==e.charAt(e.length-1))throw new SyntaxError(o.errorMessages.get(o.ErrorType.MalformedUnicode));var e;const n=t(r.slice(1,-1),o.ErrorType.MalformedUnicode);try{return String.fromCodePoint(n)}catch(r){throw r instanceof RangeError?new SyntaxError(o.errorMessages.get(o.ErrorType.CodePointLimit)):r}}(a);if(void 0!==u)return n(u,c);if(void 0!==d)return n(d);if("0"===g)return"\0";if(void 0!==g)return function(r,e=!1){if(e)throw new SyntaxError(o.errorMessages.get(o.ErrorType.OctalDeprecation));const t=parseInt(r,8);return String.fromCharCode(t)}(g,!e);if(void 0!==p)return y=p,i.get(y)||y;var y;throw new SyntaxError(o.errorMessages.get(o.ErrorType.EndOfString))})}e.unraw=f,e.default=f});
{
"name": "unraw",
"version": "1.2.2",
"version": "1.2.3",
"description": "Convert raw escape sequences to their respective characters (undo String.raw).",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

@@ -9,2 +9,3 @@ # unraw

## Use Case
Most of the time, you probably don't need this library unless you're working

@@ -17,6 +18,12 @@ directly with raw strings and you need a way to get them back to normal strings.

## Installation
This is a UMD module.
## Getting Started
`unraw` is hosted on [NPM](https://www.npmjs.com/unraw):
`unraw` is a UMD module, so it can be used in Node or on the web. Typings are
included for TypeScript as well.
### Usage in Node.JS
`unraw` is hosted on [npm](https://www.npmjs.com/unraw), so you can install
with:
```bash

@@ -26,7 +33,41 @@ npm i unraw

You can embed it (minified) on a webpage with [UNPKG](https://unpkg.com):
https://unpkg.com/unraw
To use in code:
```js
import unraw from "unraw";
unraw("\\n");
```
If you want to access error messages:
```js
import {unraw, errorMessages, ErrorType} from "unraw";
unraw("\\n");
errorMessages.get(ErrorType.MalformedUnicode);
```
### Usage in the Browser
You can embed it (minified) on a webpage with
[RequireJS](https://requirejs.org/). The module is available on
[UNPKG](https://unpkg.com) at https://unpkg.com/unraw:
```html
<script src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.3.6/require.min.js"></script>
<script>
require(["https://unpkg.com/unraw^1.2.3/dist/index.min.js"], function(unraw) {
unraw.unraw("\\n");
unraw.errorMessages.get(unraw.ErrorType.MalformedUnicode);
});
</script>
```
_Note_: Importing via the 'bare' url (`https://unpkg.com/unraw`) is not
supported as it breaks references to other required files.
## Usage
Usage is simple - the library exports just one function, `unraw`. The first
Usage is simple - the library exports a default function, `unraw`. The first
argument to `unraw` is the string to parse, and the second is an optional flag

@@ -38,4 +79,2 @@ to allow or disallow octal escapes, which are deprecated (defaults to

```js
import unraw from "unraw";
unraw("\\t\\tThis is indented.");

@@ -77,15 +116,23 @@ // => " This is indented."

### Errors
If desired, you can access the error messages for comparison from the `errors`
submodule:
If desired, you can access the possible error messages to help identify errors:
```ts
import {errorMessages} from "unraw/dist/errors";
import {unraw, ErrorType, errorMessages} from "unraw";
const hexErrorMessage = errorMessages.get("malformedHexadecimal");
try {
unraw("\\u25");
} catch (err) {
if (err.message === errorMessages.get(ErrorType.MalformedUnicode)) {
console.error("String had an invalid Unicode escape sequence.");
}
}
```
The full list of error message names available as `errorMessages.keys()` or as
the `ErrorMessageName` type in TypeScript. All errors thrown are `SyntaxError`s.
The full list of error message names available through the `ErrorType` enum
(exposed as a normal object in JavaScript).
## Contributing
Found a bug? Please,
Found a bug? Please,
[submit it here.](https://github.com/iansan5653/compress-tag/issues)

@@ -100,6 +147,20 @@

quickly confirm that it will, just run:
```bash
npm run check
```
This checks your formatting, tests, and for TypeScript compiler errors. If the
task doesn't fail, you should be good to go.
### Other Tasks
For your convenience, some other tasks are also provided in the `package.json`:
- `npm run build` - Compiles TypeScript code to JavaScript
- `npm run minify` - Generate minified JavaScript files from compiled files
- `npm run test` - Quickly run tests using TypeScript code without compiling
- `npm run testWithCoverage` - Run tests and generate coverage report
- `npm run lint` - Check code for linting errors
- `npm run check` - Check to ensure code will pass Pipelines checks (see above)
- `npm run format` - Format code using Prettier

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc