@nberlette/utf8
Version:
Blazing fast universal ponyfills for TextEncoder and TextDecoder.
157 lines • 6.12 kB
JavaScript
// deno-lint-ignore-file no-explicit-any no-var
import * as dntShim from "./_dnt.shims.js";
export var undefined;
export const $global = (() => {
if (typeof dntShim.dntGlobalThis === "object")
return dntShim.dntGlobalThis;
if (typeof window === "object")
return window;
if (typeof self === "object")
return self;
if (typeof global === "object")
return global;
if (typeof root === "object")
return root;
if (typeof this === "object")
return this;
// ewww
return (0, eval)("this");
})();
export function isTypedArray(it, type) {
try {
return TypedArrayPrototypeGetToStringTag(it) === type;
}
catch {
return false;
}
}
export const Object = $global.Object;
export const ObjectGetPrototypeOf = Object.getPrototypeOf;
export const ObjectDefineProperty = Object.defineProperty;
export const ObjectGetOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
const Function = $global.Function;
const FunctionPrototype = Function.prototype;
const { bind, call } = FunctionPrototype;
const uncurryThis = (fn) => {
const bound = bind.call(call, fn);
ObjectDefineProperty(bound, "name", { value: fn.name });
return bound;
};
const FunctionPrototypeBind = uncurryThis(bind);
function uncurryGetter(o, p) {
return uncurryThis(lookupGetter(o, p));
}
function lookupGetter(o, p, _allowUndefined) {
return ObjectGetOwnPropertyDescriptor(o, p)?.get ?? (() => undefined);
}
function bindAndRename(fn, thisArg, name = fn.name) {
const bound = FunctionPrototypeBind(fn, thisArg);
ObjectDefineProperty(bound, "name", { value: name });
return bound;
}
export function gracefulDefine(target, key, value) {
if (!(key in target && target[key] !== value)) {
if (typeof value === "function" && typeof key === "string") {
// ensure function names are preserved when minified etc.
ObjectDefineProperty(value, "name", { value: key });
}
ObjectDefineProperty(target, key, {
value,
writable: true,
enumerable: false,
configurable: true,
});
}
}
export const toString = uncurryThis(Object.prototype.toString);
export const Error = $global.Error;
export const TypeError = $global.TypeError;
export const RangeError = $global.RangeError;
export const ReferenceError = $global.ReferenceError;
export const Array = $global.Array;
export const Symbol = $global.Symbol;
export const ArrayBuffer = $global.ArrayBuffer;
export const ArrayBufferIsView = ArrayBuffer.isView;
export const ArrayBufferPrototypeGetByteLength = uncurryGetter(ArrayBuffer.prototype, "byteLength");
export const SharedArrayBuffer = $global.SharedArrayBuffer;
export const SharedArrayBufferPrototypeGetByteLength = uncurryGetter(SharedArrayBuffer?.prototype || {
get byteLength() {
throw new TypeError("SharedArrayBuffer is not supported.");
},
}, "byteLength");
export const Uint8Array = $global.Uint8Array;
export const Uint8ArrayPrototypeSlice = uncurryThis(Uint8Array.prototype.slice);
export const Uint8ArrayPrototypeSubarray = uncurryThis(Uint8Array.prototype.subarray);
export const TypedArray = ObjectGetPrototypeOf(Uint8Array);
export const TypedArrayPrototype = TypedArray
?.prototype;
export const TypedArrayPrototypeGetToStringTag = uncurryGetter(TypedArrayPrototype, Symbol.toStringTag);
export const TypedArrayPrototypeSubarray = uncurryThis(TypedArrayPrototype.subarray);
export const String = $global.String;
export const StringFromCharCode = String.fromCharCode;
export const StringPrototype = String.prototype;
export const StringPrototypeCharCodeAt = uncurryThis(StringPrototype.charCodeAt);
export const StringPrototypeReplace = uncurryThis(StringPrototype.replace);
export const StringPrototypeSlice = uncurryThis(StringPrototype.slice);
export const StringPrototypeCodePointAt = uncurryThis(StringPrototype.codePointAt);
export const StringPrototypeToLowerCase = uncurryThis(StringPrototype.toLowerCase);
export const StringPrototypeTrim = uncurryThis(StringPrototype.trim);
export const Promise = $global.Promise;
export const PromiseResolve = bindAndRename(Promise.resolve, Promise);
export const PromiseReject = bindAndRename(Promise.reject, Promise);
export const TransformStream = $global.TransformStream || function TransformStream() {
throw new TypeError("TransformStream is not supported in this environment");
};
export function getCodePoint(input, index) {
const first = StringPrototypeCharCodeAt(input, index);
if (first >= 0xd800 && first <= 0xdbff) {
const second = StringPrototypeCharCodeAt(input, index + 1);
if (second >= 0xdc00 && second <= 0xdfff) {
return ((first - 0xd800) << 10) + (second - 0xdc00) + 0x10000;
}
}
return first;
}
export function utf8BytesNeeded(codePoint) {
if (codePoint <= 0x7f)
return 1;
if (codePoint <= 0x7ff)
return 2;
if (codePoint <= 0xffff)
return 3;
return 4;
}
export function normalizeEncoding(label) {
let encoding = StringPrototypeToLowerCase(label);
encoding = StringPrototypeTrim(encoding);
if (encoding === "utf8" || encoding === "utf-8")
return "utf-8";
throw new TypeError(`The encoding label provided ('${label}') is invalid.`);
}
export function toUint8Array(input) {
if (input == null) {
return new Uint8Array();
}
else if (isTypedArray(input, "Uint8Array")) {
return input;
}
else if (ArrayBufferIsView(input)) {
return new Uint8Array(input.buffer, input.byteOffset, input.byteLength);
}
else {
try {
ArrayBufferPrototypeGetByteLength(input);
return new Uint8Array(input);
}
catch (_) {
try {
SharedArrayBufferPrototypeGetByteLength(input);
return new Uint8Array(input);
}
catch (_) {
throw new TypeError('The "input" argument must be of type BufferSource');
}
}
}
}
//# sourceMappingURL=_internal.js.map