Skip to content

Commit

Permalink
var
Browse files Browse the repository at this point in the history
  • Loading branch information
samthor committed Aug 29, 2022
1 parent 6d61e0b commit 325e185
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 53 deletions.
5 changes: 3 additions & 2 deletions src/buffer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
*/
export function decodeBuffer(bytes, encoding) {
/** @type {Buffer} */
let b;
var b;
if (bytes instanceof Buffer) {
// @ts-ignore
b = bytes;
} else {
b = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
Expand All @@ -20,4 +21,4 @@ export function decodeBuffer(bytes, encoding) {
* @param {string} string
* @return {Uint8Array}
*/
export const encodeBuffer = (string) => Buffer.from(string);
export var encodeBuffer = (string) => Buffer.from(string);
48 changes: 24 additions & 24 deletions src/lowlevel.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
* @return {string}
*/
export function decodeFallback(bytes) {
let inputIndex = 0;
var inputIndex = 0;

// Create a working buffer for UTF-16 code points, but don't generate one
// which is too large for small input sizes. UTF-8 to UCS-16 conversion is
// going to be at most 1:1, if all code points are ASCII. The other extreme
// is 4-byte UTF-8, which results in two UCS-16 points, but this is still 50%
// fewer entries in the output.
const pendingSize = Math.min(256 * 256, bytes.length + 1);
const pending = new Uint16Array(pendingSize);
const chunks = [];
let pendingIndex = 0;
var pendingSize = Math.min(256 * 256, bytes.length + 1);
var pending = new Uint16Array(pendingSize);
var chunks = [];
var pendingIndex = 0;

for (; ;) {
const more = inputIndex < bytes.length;
var more = inputIndex < bytes.length;

// If there's no more data or there'd be no room for two UTF-16 values,
// create a chunk. This isn't done at the end by simply slicing the data
Expand All @@ -28,8 +28,8 @@ export function decodeFallback(bytes) {
// the output code expands pretty fast in this case.
// These extra vars get compiled out: they're just to make TS happy.
// Turns out you can pass an ArrayLike to .apply().
const subarray = pending.subarray(0, pendingIndex);
const arraylike = /** @type {number[]} */ (/** @type {unknown} */ (subarray));
var subarray = pending.subarray(0, pendingIndex);
var arraylike = /** @type {number[]} */ (/** @type {unknown} */ (subarray));
chunks.push(String.fromCharCode.apply(null, arraylike));

if (!more) {
Expand All @@ -46,23 +46,23 @@ export function decodeFallback(bytes) {
// input data is invalid. Here, we blindly parse the data even if it's
// wrong: e.g., if a 3-byte sequence doesn't have two valid continuations.

const byte1 = bytes[inputIndex++];
var byte1 = bytes[inputIndex++];
if ((byte1 & 0x80) === 0) { // 1-byte or null
pending[pendingIndex++] = byte1;
} else if ((byte1 & 0xe0) === 0xc0) { // 2-byte
const byte2 = bytes[inputIndex++] & 0x3f;
var byte2 = bytes[inputIndex++] & 0x3f;
pending[pendingIndex++] = ((byte1 & 0x1f) << 6) | byte2;
} else if ((byte1 & 0xf0) === 0xe0) { // 3-byte
const byte2 = bytes[inputIndex++] & 0x3f;
const byte3 = bytes[inputIndex++] & 0x3f;
var byte2 = bytes[inputIndex++] & 0x3f;
var byte3 = bytes[inputIndex++] & 0x3f;
pending[pendingIndex++] = ((byte1 & 0x1f) << 12) | (byte2 << 6) | byte3;
} else if ((byte1 & 0xf8) === 0xf0) { // 4-byte
const byte2 = bytes[inputIndex++] & 0x3f;
const byte3 = bytes[inputIndex++] & 0x3f;
const byte4 = bytes[inputIndex++] & 0x3f;
var byte2 = bytes[inputIndex++] & 0x3f;
var byte3 = bytes[inputIndex++] & 0x3f;
var byte4 = bytes[inputIndex++] & 0x3f;

// this can be > 0xffff, so possibly generate surrogates
let codepoint = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
var codepoint = ((byte1 & 0x07) << 0x12) | (byte2 << 0x0c) | (byte3 << 0x06) | byte4;
if (codepoint > 0xffff) {
// codepoint &= ~0x10000;
codepoint -= 0x10000;
Expand All @@ -82,19 +82,19 @@ export function decodeFallback(bytes) {
* @return {Uint8Array}
*/
export function encodeFallback(string) {
let pos = 0;
const len = string.length;
var pos = 0;
var len = string.length;

let at = 0; // output position
let tlen = Math.max(32, len + (len >>> 1) + 7); // 1.5x size
let target = new Uint8Array((tlen >>> 3) << 3); // ... but at 8 byte offset
var at = 0; // output position
var tlen = Math.max(32, len + (len >>> 1) + 7); // 1.5x size
var target = new Uint8Array((tlen >>> 3) << 3); // ... but at 8 byte offset

while (pos < len) {
let value = string.charCodeAt(pos++);
var value = string.charCodeAt(pos++);
if (value >= 0xd800 && value <= 0xdbff) {
// high surrogate
if (pos < len) {
const extra = string.charCodeAt(pos);
var extra = string.charCodeAt(pos);
if ((extra & 0xfc00) === 0xdc00) {
++pos;
value = ((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000;
Expand All @@ -111,7 +111,7 @@ export function encodeFallback(string) {
tlen *= (1.0 + (pos / string.length) * 2); // take 2x the remaining
tlen = (tlen >>> 3) << 3; // 8 byte offset

const update = new Uint8Array(tlen);
var update = new Uint8Array(tlen);
update.set(target);
target = update;
}
Expand Down
18 changes: 10 additions & 8 deletions src/o-decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { failedToString, maybeThrowFailedToOption } from './shared.js';
import { hasBufferFrom } from './support.js';
import { decodeSyncXHR } from './xhr.js';

const trySyncXHR = !hasBufferFrom && (typeof Blob === 'function' && typeof URL === 'function' && typeof URL.createObjectURL === 'function');
const validUtfLabels = ['utf-8', 'utf8', 'unicode-1-1-utf-8'];
var trySyncXHR = !hasBufferFrom && (typeof Blob === 'function' && typeof URL === 'function' && typeof URL.createObjectURL === 'function');
var validUtfLabels = ['utf-8', 'utf8', 'unicode-1-1-utf-8'];

/** @type {(bytes: Uint8Array, encoding: string) => string} */
let decodeImpl = decodeFallback;
var decodeImpl = decodeFallback;
if (hasBufferFrom) {
decodeImpl = decodeBuffer;
} else if (trySyncXHR) {
Expand All @@ -22,20 +22,22 @@ if (hasBufferFrom) {
}


const ctorString = `construct 'TextDecoder'`;
const errorPrefix = `${failedToString} ${ctorString}: the `;
var ctorString = `construct 'TextDecoder'`;
var errorPrefix = `${failedToString} ${ctorString}: the `;


/**
* @constructor
* @param {string=} utfLabel
* @param {{fatal: boolean}=} options
*/
export function FastTextDecoder(utfLabel = 'utf-8', options) {
export function FastTextDecoder(utfLabel, options) {
maybeThrowFailedToOption(options && options.fatal, ctorString, 'fatal');

utfLabel = utfLabel || 'utf-8';

/** @type {boolean} */
let ok;
var ok;
if (hasBufferFrom) {
ok = Buffer.isEncoding(utfLabel);
} else {
Expand All @@ -58,7 +60,7 @@ export function FastTextDecoder(utfLabel = 'utf-8', options) {
FastTextDecoder.prototype.decode = function (buffer, options) {
maybeThrowFailedToOption(options && options.stream, 'decode', 'stream');

let bytes;
var bytes;

if (buffer instanceof Uint8Array) {
// Accept Uint8Array instances as-is. This is also a Node buffer.
Expand Down
2 changes: 1 addition & 1 deletion src/o-encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { encodeFallback } from './lowlevel.js';
import { maybeThrowFailedToOption } from './shared.js';
import { hasBufferFrom } from './support.js';

export const encodeImpl = hasBufferFrom ? encodeFallback : encodeBuffer;
export var encodeImpl = hasBufferFrom ? encodeFallback : encodeBuffer;

/**
* @constructor
Expand Down
4 changes: 2 additions & 2 deletions src/shared.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

export const failedToString = 'Failed to ';
export var failedToString = 'Failed to ';

/**
* @param {boolean|undefined} check
* @param {string} operation
* @param {string} fieldName
*/
export const maybeThrowFailedToOption = (check, operation, fieldName) => {
export var maybeThrowFailedToOption = (check, operation, fieldName) => {
if (check) {
throw new Error(`${failedToString}${operation}: the '${fieldName}' option is unsupported.`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/support.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

export const hasBufferFrom = (typeof Buffer === 'function' && Buffer.from);
export var hasBufferFrom = (typeof Buffer === 'function' && Buffer.from);
6 changes: 3 additions & 3 deletions src/xhr.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
* @return {string}
*/
export function decodeSyncXHR(bytes) {
let u;
var u;

// This hack will fail in non-Edgium Edge because sync XHRs are disabled (and
// possibly in other places), so ensure there's a fallback call.
try {
const b = new Blob([bytes], { type: 'text/plain;charset=UTF-8' });
var b = new Blob([bytes], { type: 'text/plain;charset=UTF-8' });
u = URL.createObjectURL(b);

const x = new XMLHttpRequest();
var x = new XMLHttpRequest();
x.open('GET', u, false);
x.send();
return x.responseText;
Expand Down
9 changes: 3 additions & 6 deletions text.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 325e185

Please sign in to comment.