Skip to content

Commit

Permalink
some stylistic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zloirock committed Dec 4, 2023
1 parent 86f6f87 commit d27aa04
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 38 deletions.
10 changes: 10 additions & 0 deletions packages/core-js/internals/an-object-or-undefined.js
Original file line number Diff line number Diff line change
@@ -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');
};
1 change: 1 addition & 0 deletions packages/core-js/internals/base64-map.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 8 additions & 0 deletions packages/core-js/internals/get-alphabet-option.js
Original file line number Diff line number Diff line change
@@ -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');
};
35 changes: 15 additions & 20 deletions packages/core-js/modules/esnext.uint8-array.from-base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,71 @@
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
// https://github.com/tc39/proposal-arraybuffer-base64
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--;
}

Expand Down
3 changes: 1 addition & 2 deletions packages/core-js/modules/esnext.uint8-array.from-hex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
25 changes: 10 additions & 15 deletions packages/core-js/modules/esnext.uint8-array.to-base64.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/core-js/modules/esnext.uint8-array.to-hex.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit d27aa04

Please sign in to comment.