From d27aa045217345357a7f1ab5463d5fdd07df58f1 Mon Sep 17 00:00:00 2001 From: Denis Pushkarev Date: Sun, 26 Nov 2023 10:50:46 +0700 Subject: [PATCH] some stylistic changes --- .../internals/an-object-or-undefined.js | 10 ++++++ packages/core-js/internals/base64-map.js | 1 + .../core-js/internals/get-alphabet-option.js | 8 +++++ .../modules/esnext.uint8-array.from-base64.js | 35 ++++++++----------- .../modules/esnext.uint8-array.from-hex.js | 3 +- .../modules/esnext.uint8-array.to-base64.js | 25 ++++++------- .../modules/esnext.uint8-array.to-hex.js | 2 +- 7 files changed, 46 insertions(+), 38 deletions(-) create mode 100644 packages/core-js/internals/an-object-or-undefined.js create mode 100644 packages/core-js/internals/get-alphabet-option.js diff --git a/packages/core-js/internals/an-object-or-undefined.js b/packages/core-js/internals/an-object-or-undefined.js new file mode 100644 index 000000000000..3138e11478ef --- /dev/null +++ b/packages/core-js/internals/an-object-or-undefined.js @@ -0,0 +1,10 @@ +'use strict'; +var isObject = require('../internals/is-object'); + +var $String = String; +var $TypeError = TypeError; + +module.exports = function (argument) { + if (argument === undefined || isObject(argument)) return argument; + throw new $TypeError($String(argument) + ' is not an object or undefined'); +}; diff --git a/packages/core-js/internals/base64-map.js b/packages/core-js/internals/base64-map.js index b2e521524066..2bda13a71388 100644 --- a/packages/core-js/internals/base64-map.js +++ b/packages/core-js/internals/base64-map.js @@ -4,6 +4,7 @@ var base64Alphabet = commonAlphabet + '+/'; var base64UrlAlphabet = commonAlphabet + '-_'; var inverse = function (characters) { + // TODO: use `Object.create(null)` in `core-js@4` var result = {}; var index = 0; for (; index < 64; index++) result[characters.charAt(index)] = index; diff --git a/packages/core-js/internals/get-alphabet-option.js b/packages/core-js/internals/get-alphabet-option.js new file mode 100644 index 000000000000..216d169273ae --- /dev/null +++ b/packages/core-js/internals/get-alphabet-option.js @@ -0,0 +1,8 @@ +'use strict'; +var $TypeError = TypeError; + +module.exports = function (options) { + var alphabet = options && options.alphabet; + if (alphabet === undefined || alphabet === 'base64' || alphabet === 'base64url') return alphabet || 'base64'; + throw new $TypeError('Incorrect `alphabet` option'); +}; diff --git a/packages/core-js/modules/esnext.uint8-array.from-base64.js b/packages/core-js/modules/esnext.uint8-array.from-base64.js index 5509bba6b544..778afc1259c9 100644 --- a/packages/core-js/modules/esnext.uint8-array.from-base64.js +++ b/packages/core-js/modules/esnext.uint8-array.from-base64.js @@ -2,24 +2,23 @@ var $ = require('../internals/export'); var global = require('../internals/global'); var uncurryThis = require('../internals/function-uncurry-this'); +var anObjectOrUndefined = require('../internals/an-object-or-undefined'); var aString = require('../internals/a-string'); -var isObject = require('../internals/is-object'); -var has = require('../internals/has-own-property'); +var hasOwn = require('../internals/has-own-property'); var arrayFromConstructorAndList = require('../internals/array-from-constructor-and-list'); var base64Map = require('../internals/base64-map'); +var getAlphabetOption = require('../internals/get-alphabet-option'); var base64Alphabet = base64Map.c2i; var base64UrlAlphabet = base64Map.c2iUrl; var Uint8Array = global.Uint8Array; var SyntaxError = global.SyntaxError; -var TypeError = global.TypeError; var charAt = uncurryThis(''.charAt); var replace = uncurryThis(''.replace); var stringSlice = uncurryThis(''.slice); var push = uncurryThis([].push); var SPACES = /[\t\n\f\r ]/g; -var BASE64 = 'base64'; var EXTRA_BITS = 'Extra bits'; // `Uint8Array.fromBase64` method @@ -27,51 +26,47 @@ var EXTRA_BITS = 'Extra bits'; if (Uint8Array) $({ target: 'Uint8Array', stat: true, forced: true }, { fromBase64: function fromBase64(string /* , options */) { aString(string); - var options = arguments.length > 1 ? arguments[1] : undefined; - if (options !== undefined && !isObject(options)) throw new TypeError('Incorrect options'); - var $alphabet = options && options.alphabet; - if ($alphabet === undefined) $alphabet = BASE64; - if ($alphabet !== BASE64 && $alphabet !== 'base64url') throw new TypeError('Incorrect `alphabet` option'); - var alphabet = $alphabet === BASE64 ? base64Alphabet : base64UrlAlphabet; - var $strict = options ? !!options.strict : false; + var options = arguments.length > 1 ? anObjectOrUndefined(arguments[1]) : undefined; + var alphabet = getAlphabetOption(options) === 'base64' ? base64Alphabet : base64UrlAlphabet; + var strict = options ? !!options.strict : false; - var input = $strict ? string : replace(string, SPACES, ''); + var input = strict ? string : replace(string, SPACES, ''); if (input.length % 4 === 0) { if (stringSlice(input, -2) === '==') input = stringSlice(input, 0, -2); else if (stringSlice(input, -1) === '=') input = stringSlice(input, 0, -1); - } else if ($strict) throw new SyntaxError('Input is not correctly padded'); + } else if (strict) throw new SyntaxError('Input is not correctly padded'); var lastChunkSize = input.length % 4; switch (lastChunkSize) { case 1: throw new SyntaxError('Bad input length'); - case 2: input += 'A'; // break omitted - case 3: input += 'A'; // 'AA' for 2 + case 2: input += 'AA'; break; + case 3: input += 'A'; } var bytes = []; var i = 0; var inputLength = input.length; - var get = function (shift) { + var at = function (shift) { var chr = charAt(input, i + shift); - if (!has(alphabet, chr)) throw new SyntaxError('Bad char in input: "' + chr + '"'); + if (!hasOwn(alphabet, chr)) throw new SyntaxError('Bad char in input: "' + chr + '"'); return alphabet[chr] << (18 - 6 * shift); }; for (; i < inputLength; i += 4) { - var triplet = get(0) + get(1) + get(2) + get(3); + var triplet = at(0) + at(1) + at(2) + at(3); push(bytes, (triplet >> 16) & 255, (triplet >> 8) & 255, triplet & 255); } var byteLength = bytes.length; if (lastChunkSize === 2) { - if ($strict && bytes[byteLength - 2] !== 0) throw new SyntaxError(EXTRA_BITS); + if (strict && bytes[byteLength - 2] !== 0) throw new SyntaxError(EXTRA_BITS); byteLength -= 2; } else if (lastChunkSize === 3) { - if ($strict && bytes[byteLength - 1] !== 0) throw new SyntaxError(EXTRA_BITS); + if (strict && bytes[byteLength - 1] !== 0) throw new SyntaxError(EXTRA_BITS); byteLength--; } diff --git a/packages/core-js/modules/esnext.uint8-array.from-hex.js b/packages/core-js/modules/esnext.uint8-array.from-hex.js index 61648b0510e9..59767b1d3a2e 100644 --- a/packages/core-js/modules/esnext.uint8-array.from-hex.js +++ b/packages/core-js/modules/esnext.uint8-array.from-hex.js @@ -19,8 +19,7 @@ if (Uint8Array) $({ target: 'Uint8Array', stat: true, forced: true }, { var stringLength = string.length; if (stringLength % 2) throw new SyntaxError('String should have an even number of characters'); if (exec(NOT_HEX, string)) throw new SyntaxError('String should only contain hex characters'); - var resultLength = stringLength / 2; - var result = new Uint8Array(resultLength); + var result = new Uint8Array(stringLength / 2); for (var i = 0; i < stringLength; i += 2) { result[i / 2] = parseInt(stringSlice(string, i, i + 2), 16); } diff --git a/packages/core-js/modules/esnext.uint8-array.to-base64.js b/packages/core-js/modules/esnext.uint8-array.to-base64.js index 583c2cd7fa76..faa17d4e3a96 100644 --- a/packages/core-js/modules/esnext.uint8-array.to-base64.js +++ b/packages/core-js/modules/esnext.uint8-array.to-base64.js @@ -2,49 +2,44 @@ var $ = require('../internals/export'); var global = require('../internals/global'); var uncurryThis = require('../internals/function-uncurry-this'); -var isObject = require('../internals/is-object'); +var anObjectOrUndefined = require('../internals/an-object-or-undefined'); var anUint8Array = require('../internals/an-uint8-array'); var base64Map = require('../internals/base64-map'); +var getAlphabetOption = require('../internals/get-alphabet-option'); var base64Alphabet = base64Map.i2c; var base64UrlAlphabet = base64Map.i2cUrl; var Uint8Array = global.Uint8Array; -var TypeError = global.TypeError; var charAt = uncurryThis(''.charAt); -var BASE64 = 'base64'; -// `Uint8Array..prototype.toBase64` method +// `Uint8Array.prototype.toBase64` method // https://github.com/tc39/proposal-arraybuffer-base64 if (Uint8Array) $({ target: 'Uint8Array', proto: true, forced: true }, { toBase64: function toBase64(/* options */) { var array = anUint8Array(this); - var options = arguments.length ? arguments[0] : undefined; - if (options !== undefined && !isObject(options)) throw new TypeError('Incorrect options'); - var $alphabet = options && options.alphabet; - if ($alphabet === undefined) $alphabet = BASE64; - if ($alphabet !== BASE64 && $alphabet !== 'base64url') throw new TypeError('Incorrect `alphabet` option'); - var alphabet = $alphabet === BASE64 ? base64Alphabet : base64UrlAlphabet; + var options = arguments.length ? anObjectOrUndefined(arguments[0]) : undefined; + var alphabet = getAlphabetOption(options) === 'base64' ? base64Alphabet : base64UrlAlphabet; var result = ''; var i = 0; var length = array.length; var triplet; - var at = function (bit) { - return charAt(alphabet, (triplet >> bit) & 63); + var at = function (shift) { + return charAt(alphabet, (triplet >> (6 * shift)) & 63); }; for (; i + 2 < length; i += 3) { triplet = (array[i] << 16) + (array[i + 1] << 8) + array[i + 2]; - result += at(18) + at(12) + at(6) + at(0); + result += at(3) + at(2) + at(1) + at(0); } if (i + 2 === length) { triplet = (array[i] << 16) + (array[i + 1] << 8); - result += at(18) + at(12) + at(6) + '='; + result += at(3) + at(2) + at(1) + '='; } else if (i + 1 === length) { triplet = array[i] << 16; - result += at(18) + at(12) + '=='; + result += at(3) + at(2) + '=='; } return result; diff --git a/packages/core-js/modules/esnext.uint8-array.to-hex.js b/packages/core-js/modules/esnext.uint8-array.to-hex.js index fd379b632817..54e05a582145 100644 --- a/packages/core-js/modules/esnext.uint8-array.to-hex.js +++ b/packages/core-js/modules/esnext.uint8-array.to-hex.js @@ -7,7 +7,7 @@ var anUint8Array = require('../internals/an-uint8-array'); var Uint8Array = global.Uint8Array; var numberToString = uncurryThis(1.0.toString); -// `Uint8Array..prototype.toHex` method +// `Uint8Array.prototype.toHex` method // https://github.com/tc39/proposal-arraybuffer-base64 if (Uint8Array) $({ target: 'Uint8Array', proto: true, forced: true }, { toHex: function toHex() {