diff options
author | Marcos Casagrande <marcoscvp90@gmail.com> | 2020-06-28 18:27:02 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-06-28 12:27:02 -0400 |
commit | 0374eadcf7ecb054dae1b1587843a2006fdf4c2d (patch) | |
tree | ef341276515bcfbd8b1991b798cd4004411a58d7 /std/node/buffer.ts | |
parent | 96b4a5f837fe984a91c03babc398fb55a6210610 (diff) |
feat(std/node): Add Buffer.isEncoding (#6521)
Diffstat (limited to 'std/node/buffer.ts')
-rw-r--r-- | std/node/buffer.ts | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/std/node/buffer.ts b/std/node/buffer.ts index b878092dc..9ec7c04ff 100644 --- a/std/node/buffer.ts +++ b/std/node/buffer.ts @@ -1,8 +1,7 @@ import * as hex from "../encoding/hex.ts"; import * as base64 from "../encoding/base64.ts"; -import { notImplemented } from "./_utils.ts"; +import { notImplemented, normalizeEncoding } from "./_utils.ts"; -const validEncodings = ["utf8", "hex", "base64"]; const notImplementedEncodings = [ "utf16le", "latin1", @@ -17,20 +16,16 @@ function checkEncoding(encoding = "utf8", strict = true): string { throw new TypeError(`Unkown encoding: ${encoding}`); } - encoding = encoding.toLowerCase(); - if (encoding === "utf-8" || encoding === "") { - return "utf8"; - } + const normalized = normalizeEncoding(encoding); + + if (normalized === undefined) + throw new TypeError(`Unkown encoding: ${encoding}`); if (notImplementedEncodings.includes(encoding)) { notImplemented(`"${encoding}" encoding`); } - if (!validEncodings.includes(encoding)) { - throw new TypeError(`Unkown encoding: ${encoding}`); - } - - return encoding; + return normalized; } /** @@ -181,6 +176,15 @@ export default class Buffer extends Uint8Array { return obj instanceof Buffer; } + // eslint-disable-next-line @typescript-eslint/no-explicit-any + static isEncoding(encoding: any): boolean { + return ( + typeof encoding === "string" && + encoding.length !== 0 && + normalizeEncoding(encoding) !== undefined + ); + } + /** * Copies data from a region of buf to a region in target, even if the target * memory region overlaps with buf. |