Skip to content

Commit

Permalink
[New] Uint8Array.prototype.toBase64: add omitPadding option, per …
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Jun 11, 2024
1 parent 1dd5e0a commit e195980
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
"multiline-comment-style": "off",
"new-cap": ["error", {
"capIsNewExceptions": [
"DetachArrayBuffer",
"FromBase64",
"FromHex",
"DetachArrayBuffer",
"Get",
"GetIntrinsic",
"GetOptionsObject",
Expand All @@ -29,6 +29,7 @@
"SetUint8ArrayBytes",
"SetValueInBuffer",
"StringPad",
"ToBoolean",
"TypedArrayLength",
"ValidateUint8Array",
],
Expand Down
15 changes: 9 additions & 6 deletions Uint8Array.prototype.toBase64/implementation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var Get = require('es-abstract/2024/Get');
var GetOptionsObject = require('../aos/GetOptionsObject');
var GetUint8ArrayBytes = require('../aos/GetUint8ArrayBytes');
var ValidateUint8Array = require('../aos/ValidateUint8Array');
var ToBoolean = require('es-abstract/2024/ToBoolean');

var alphabetFromIdentifier = require('../aos/helpers/alphabetFromIdentifier');

Expand All @@ -31,17 +32,19 @@ module.exports = function toBase64() {
throw new $TypeError('Assertion failed: `alphabet` is not `\'base64\'` or `\'base64url\'`: ' + (typeof alphabet === 'string' ? alphabet : typeof alphabet)); // step 6
}

var omitPadding = ToBoolean(Get(opts, 'omitPadding')); // step 7

// eslint-disable-next-line no-unused-vars
var toEncode = GetUint8ArrayBytes(O); // step 7
var toEncode = GetUint8ArrayBytes(O); // step 8

// if (alphabet === 'base64') { // step 8
// if (alphabet === 'base64') { // step 9
// a. Let outAscii be the sequence of code points which results from encoding toEncode according to the base64 encoding specified in section 4 of RFC 4648.
// } else { // step 9
// } else { // step 10
// a. Assert: alphabet is "base64url".
// b. Let outAscii be the sequence of code points which results from encoding toEncode according to the base64url encoding specified in section 5 of RFC 4648.
// }

// return CodePointsToString(outAscii); // step 9
// return CodePointsToString(outAscii); // step 11

// code adapted from https://github.com/tc39/proposal-arraybuffer-base64/blob/22228812214d5a1c2966cd626f43be3576e79290/playground/polyfill-core.mjs
var lookup = alphabetFromIdentifier(alphabet);
Expand All @@ -61,12 +64,12 @@ module.exports = function toBase64() {
result += $charAt(lookup, (triplet >> 18) & 63)
+ $charAt(lookup, (triplet >> 12) & 63)
+ $charAt(lookup, (triplet >> 6) & 63)
+ '=';
+ (omitPadding ? '' : '=');
} else if (i + 1 === O.length) {
triplet = O[i] << 16;
result += $charAt(lookup, (triplet >> 18) & 63)
+ $charAt(lookup, (triplet >> 12) & 63)
+ '==';
+ (omitPadding ? '' : '==');
}
return result;
};
4 changes: 4 additions & 0 deletions test/Uint8Array.prototype.toBase64.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,14 @@ module.exports = {
// standard test vectors from https://datatracker.ietf.org/doc/html/rfc4648#section-10
st.equal(method(new Uint8Array([])), '');
st.equal(method(new Uint8Array([102])), 'Zg==');
st.equal(method(new Uint8Array([102]), { omitPadding: true }), 'Zg');
st.equal(method(new Uint8Array([102, 111])), 'Zm8=');
st.equal(method(new Uint8Array([102, 111]), { omitPadding: true }), 'Zm8');
st.equal(method(new Uint8Array([102, 111, 111])), 'Zm9v');
st.equal(method(new Uint8Array([102, 111, 111, 98])), 'Zm9vYg==');
st.equal(method(new Uint8Array([102, 111, 111, 98]), { omitPadding: true }), 'Zm9vYg');
st.equal(method(new Uint8Array([102, 111, 111, 98, 97])), 'Zm9vYmE=');
st.equal(method(new Uint8Array([102, 111, 111, 98, 97]), { omitPadding: true }), 'Zm9vYmE');
st.equal(method(new Uint8Array([102, 111, 111, 98, 97, 114])), 'Zm9vYmFy');

st.end();
Expand Down

0 comments on commit e195980

Please sign in to comment.