From e0c20152e4129994a40fa0f68e9a1466b73773f6 Mon Sep 17 00:00:00 2001 From: Josh Walker Date: Thu, 10 Oct 2019 17:35:39 -0600 Subject: [PATCH 1/3] Changed var to const and let. Changed += string building with arrays and join. Tests passing. --- src/base64.js | 294 +++++++++++++++++++++++++------------------------- 1 file changed, 147 insertions(+), 147 deletions(-) diff --git a/src/base64.js b/src/base64.js index bb409e6..3436ffe 100644 --- a/src/base64.js +++ b/src/base64.js @@ -1,164 +1,164 @@ /*! https://mths.be/base64 v<%= version %> by @mathias | MIT license */ -;(function(root) { +(function(root) { - // Detect free variables `exports`. - var freeExports = typeof exports == 'object' && exports; + // Detect free variables `exports`. + const freeExports = typeof exports == 'object' && exports; - // Detect free variable `module`. - var freeModule = typeof module == 'object' && module && - module.exports == freeExports && module; + // Detect free variable `module`. + const freeModule = typeof module == 'object' && module && + module.exports == freeExports && module; - // Detect free variable `global`, from Node.js or Browserified code, and use - // it as `root`. - var freeGlobal = typeof global == 'object' && global; - if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) { - root = freeGlobal; - } + // Detect free variable `global`, from Node.js or Browserified code, and use + // it as `root`. + const freeGlobal = typeof global == 'object' && global; + if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) { + root = freeGlobal; + } - /*--------------------------------------------------------------------------*/ + /*--------------------------------------------------------------------------*/ - var InvalidCharacterError = function(message) { - this.message = message; - }; - InvalidCharacterError.prototype = new Error; - InvalidCharacterError.prototype.name = 'InvalidCharacterError'; + const InvalidCharacterError = function(message) { + this.message = message; + }; + InvalidCharacterError.prototype = new Error; + InvalidCharacterError.prototype.name = 'InvalidCharacterError'; - var error = function(message) { - // Note: the error messages used throughout this file match those used by - // the native `atob`/`btoa` implementation in Chromium. - throw new InvalidCharacterError(message); - }; + const error = function(message) { + // Note: the error messages used throughout this file match those used by + // the native `atob`/`btoa` implementation in Chromium. + throw new InvalidCharacterError(message); + }; - var TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; - // http://whatwg.org/html/common-microsyntaxes.html#space-character - var REGEX_SPACE_CHARACTERS = /<%= spaceCharacters %>/g; + const TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; + // http://whatwg.org/html/common-microsyntaxes.html#space-character + const REGEX_SPACE_CHARACTERS = /<%= spaceCharacters %>/g; - // `decode` is designed to be fully compatible with `atob` as described in the - // HTML Standard. http://whatwg.org/html/webappapis.html#dom-windowbase64-atob - // The optimized base64-decoding algorithm used is based on @atk’s excellent - // implementation. https://gist.github.com/atk/1020396 - var decode = function(input) { - input = String(input) - .replace(REGEX_SPACE_CHARACTERS, ''); - var length = input.length; - if (length % 4 == 0) { - input = input.replace(/==?$/, ''); - length = input.length; - } - if ( - length % 4 == 1 || - // http://whatwg.org/C#alphanumeric-ascii-characters - /[^+a-zA-Z0-9/]/.test(input) - ) { - error( - 'Invalid character: the string to be decoded is not correctly encoded.' - ); - } - var bitCounter = 0; - var bitStorage; - var buffer; - var output = ''; - var position = -1; - while (++position < length) { - buffer = TABLE.indexOf(input.charAt(position)); - bitStorage = bitCounter % 4 ? bitStorage * 64 + buffer : buffer; - // Unless this is the first of a group of 4 characters… - if (bitCounter++ % 4) { - // …convert the first 8 bits to a single ASCII character. - output += String.fromCharCode( - 0xFF & bitStorage >> (-2 * bitCounter & 6) - ); - } - } - return output; - }; + // `decode` is designed to be fully compatible with `atob` as described in the + // HTML Standard. http://whatwg.org/html/webappapis.html#dom-windowbase64-atob + // The optimized base64-decoding algorithm used is based on @atk’s excellent + // implementation. https://gist.github.com/atk/1020396 + const decode = (input) => { + const mutableInputCopy = String(input) + .replace(REGEX_SPACE_CHARACTERS, ''); + const length = mutableInputCopy.length; + if (length % 4 == 0) { + mutableInputCopy = mutableInputCopy.replace(/==?$/, ''); + length = mutableInputCopy.length; + } + if ( + length % 4 == 1 || + // http://whatwg.org/C#alphanumeric-ascii-characters + /[^+a-zA-Z0-9/]/.test(mutableInputCopy) + ) { + error( + 'Invalid character: the string to be decoded is not correctly encoded.' + ); + } + let bitCounter = 0; + let bitStorage; + let buffer; + let outputArray = []; + let position = -1; + while (++position < length) { + buffer = TABLE.indexOf(mutableInputCopy.charAt(position)); + bitStorage = bitCounter % 4 ? bitStorage * 64 + buffer : buffer; + // Unless this is the first of a group of 4 characters… + if (bitCounter++ % 4) { + // …convert the first 8 bits to a single ASCII character. + outputArray.push(String.fromCharCode( + 0xFF & bitStorage >> (-2 * bitCounter & 6) + )); + } + } + return outputArray.join(''); + }; - // `encode` is designed to be fully compatible with `btoa` as described in the - // HTML Standard: http://whatwg.org/html/webappapis.html#dom-windowbase64-btoa - var encode = function(input) { - input = String(input); - if (/[^\0-\xFF]/.test(input)) { - // Note: no need to special-case astral symbols here, as surrogates are - // matched, and the input is supposed to only contain ASCII anyway. - error( - 'The string to be encoded contains characters outside of the ' + - 'Latin1 range.' - ); - } - var padding = input.length % 3; - var output = ''; - var position = -1; - var a; - var b; - var c; - var buffer; - // Make sure any padding is handled outside of the loop. - var length = input.length - padding; + // `encode` is designed to be fully compatible with `btoa` as described in the + // HTML Standard: http://whatwg.org/html/webappapis.html#dom-windowbase64-btoa + const encode = (input) => { + input = String(input); + if (/[^\0-\xFF]/.test(input)) { + // Note: no need to special-case astral symbols here, as surrogates are + // matched, and the input is supposed to only contain ASCII anyway. + error( + 'The string to be encoded contains characters outside of the ' + + 'Latin1 range.' + ); + } + const padding = input.length % 3; + const outputArray = ''; + const position = -1; + let a; + let b; + let c; + let buffer; + // Make sure any padding is handled outside of the loop. + const length = input.length - padding; - while (++position < length) { - // Read three bytes, i.e. 24 bits. - a = input.charCodeAt(position) << 16; - b = input.charCodeAt(++position) << 8; - c = input.charCodeAt(++position); - buffer = a + b + c; - // Turn the 24 bits into four chunks of 6 bits each, and append the - // matching character for each of them to the output. - output += ( - TABLE.charAt(buffer >> 18 & 0x3F) + - TABLE.charAt(buffer >> 12 & 0x3F) + - TABLE.charAt(buffer >> 6 & 0x3F) + - TABLE.charAt(buffer & 0x3F) - ); - } + while (++position < length) { + // Read three bytes, i.e. 24 bits. + a = input.charCodeAt(position) << 16; + b = input.charCodeAt(++position) << 8; + c = input.charCodeAt(++position); + buffer = a + b + c; + // Turn the 24 bits into four chunks of 6 bits each, and append the + // matching character for each of them to the output. + outputArray.push( + TABLE.charAt(buffer >> 18 & 0x3F) + + TABLE.charAt(buffer >> 12 & 0x3F) + + TABLE.charAt(buffer >> 6 & 0x3F) + + TABLE.charAt(buffer & 0x3F) + ); + } - if (padding == 2) { - a = input.charCodeAt(position) << 8; - b = input.charCodeAt(++position); - buffer = a + b; - output += ( - TABLE.charAt(buffer >> 10) + - TABLE.charAt((buffer >> 4) & 0x3F) + - TABLE.charAt((buffer << 2) & 0x3F) + - '=' - ); - } else if (padding == 1) { - buffer = input.charCodeAt(position); - output += ( - TABLE.charAt(buffer >> 2) + - TABLE.charAt((buffer << 4) & 0x3F) + - '==' - ); - } + if (padding == 2) { + a = input.charCodeAt(position) << 8; + b = input.charCodeAt(++position); + buffer = a + b; + outputArray.push( + TABLE.charAt(buffer >> 10) + + TABLE.charAt((buffer >> 4) & 0x3F) + + TABLE.charAt((buffer << 2) & 0x3F) + + '=' + ); + } else if (padding == 1) { + buffer = input.charCodeAt(position); + outputArray += ( + TABLE.charAt(buffer >> 2) + + TABLE.charAt((buffer << 4) & 0x3F) + + '==' + ); + } - return output; - }; + return outputArray.join(''); + }; - var base64 = { - 'encode': encode, - 'decode': decode, - 'version': '<%= version %>' - }; + const base64 = { + 'encode': encode, + 'decode': decode, + 'version': '<%= version %>' + }; - // Some AMD build optimizers, like r.js, check for specific condition patterns - // like the following: - if ( - typeof define == 'function' && - typeof define.amd == 'object' && - define.amd - ) { - define(function() { - return base64; - }); - } else if (freeExports && !freeExports.nodeType) { - if (freeModule) { // in Node.js or RingoJS v0.8.0+ - freeModule.exports = base64; - } else { // in Narwhal or RingoJS v0.7.0- - for (var key in base64) { - base64.hasOwnProperty(key) && (freeExports[key] = base64[key]); - } - } - } else { // in Rhino or a web browser - root.base64 = base64; - } + // Some AMD build optimizers, like r.js, check for specific condition patterns + // like the following: + if ( + typeof define == 'function' && + typeof define.amd == 'object' && + define.amd + ) { + define(function() { + return base64; + }); + } else if (freeExports && !freeExports.nodeType) { + if (freeModule) { // in Node.js or RingoJS v0.8.0+ + freeModule.exports = base64; + } else { // in Narwhal or RingoJS v0.7.0- + for (let key in base64) { + base64.hasOwnProperty(key) && (freeExports[key] = base64[key]); + } + } + } else { // in Rhino or a web browser + root.base64 = base64; + } }(this)); From 71a0c1efda722213a9c719b4b49c4b26db76da75 Mon Sep 17 00:00:00 2001 From: Josh Walker Date: Thu, 10 Oct 2019 17:39:11 -0600 Subject: [PATCH 2/3] Forgot one --- src/base64.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/base64.js b/src/base64.js index 3436ffe..30661fb 100644 --- a/src/base64.js +++ b/src/base64.js @@ -123,7 +123,7 @@ ); } else if (padding == 1) { buffer = input.charCodeAt(position); - outputArray += ( + outputArray.push( TABLE.charAt(buffer >> 2) + TABLE.charAt((buffer << 4) & 0x3F) + '==' From 84da8488d6156f561f0d3cb4ae280db2c7e70afe Mon Sep 17 00:00:00 2001 From: Josh Walker Date: Fri, 11 Oct 2019 09:08:35 -0600 Subject: [PATCH 3/3] Changed the top level file to match src --- base64.js | 294 +++++++++++++++++++++++++++--------------------------- 1 file changed, 147 insertions(+), 147 deletions(-) diff --git a/base64.js b/base64.js index 8bd66f8..1c84013 100644 --- a/base64.js +++ b/base64.js @@ -1,164 +1,164 @@ /*! https://mths.be/base64 v0.1.0 by @mathias | MIT license */ -;(function(root) { +(function(root) { - // Detect free variables `exports`. - var freeExports = typeof exports == 'object' && exports; + // Detect free variables `exports`. + const freeExports = typeof exports == 'object' && exports; - // Detect free variable `module`. - var freeModule = typeof module == 'object' && module && - module.exports == freeExports && module; + // Detect free variable `module`. + const freeModule = typeof module == 'object' && module && + module.exports == freeExports && module; - // Detect free variable `global`, from Node.js or Browserified code, and use - // it as `root`. - var freeGlobal = typeof global == 'object' && global; - if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) { - root = freeGlobal; - } + // Detect free variable `global`, from Node.js or Browserified code, and use + // it as `root`. + const freeGlobal = typeof global == 'object' && global; + if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) { + root = freeGlobal; + } - /*--------------------------------------------------------------------------*/ + /*--------------------------------------------------------------------------*/ - var InvalidCharacterError = function(message) { - this.message = message; - }; - InvalidCharacterError.prototype = new Error; - InvalidCharacterError.prototype.name = 'InvalidCharacterError'; + const InvalidCharacterError = function(message) { + this.message = message; + }; + InvalidCharacterError.prototype = new Error; + InvalidCharacterError.prototype.name = 'InvalidCharacterError'; - var error = function(message) { - // Note: the error messages used throughout this file match those used by - // the native `atob`/`btoa` implementation in Chromium. - throw new InvalidCharacterError(message); - }; + const error = function(message) { + // Note: the error messages used throughout this file match those used by + // the native `atob`/`btoa` implementation in Chromium. + throw new InvalidCharacterError(message); + }; - var TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; - // http://whatwg.org/html/common-microsyntaxes.html#space-character - var REGEX_SPACE_CHARACTERS = /[\t\n\f\r ]/g; + const TABLE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; + // http://whatwg.org/html/common-microsyntaxes.html#space-character + const REGEX_SPACE_CHARACTERS = /<%= spaceCharacters %>/g; - // `decode` is designed to be fully compatible with `atob` as described in the - // HTML Standard. http://whatwg.org/html/webappapis.html#dom-windowbase64-atob - // The optimized base64-decoding algorithm used is based on @atk’s excellent - // implementation. https://gist.github.com/atk/1020396 - var decode = function(input) { - input = String(input) - .replace(REGEX_SPACE_CHARACTERS, ''); - var length = input.length; - if (length % 4 == 0) { - input = input.replace(/==?$/, ''); - length = input.length; - } - if ( - length % 4 == 1 || - // http://whatwg.org/C#alphanumeric-ascii-characters - /[^+a-zA-Z0-9/]/.test(input) - ) { - error( - 'Invalid character: the string to be decoded is not correctly encoded.' - ); - } - var bitCounter = 0; - var bitStorage; - var buffer; - var output = ''; - var position = -1; - while (++position < length) { - buffer = TABLE.indexOf(input.charAt(position)); - bitStorage = bitCounter % 4 ? bitStorage * 64 + buffer : buffer; - // Unless this is the first of a group of 4 characters… - if (bitCounter++ % 4) { - // …convert the first 8 bits to a single ASCII character. - output += String.fromCharCode( - 0xFF & bitStorage >> (-2 * bitCounter & 6) - ); - } - } - return output; - }; + // `decode` is designed to be fully compatible with `atob` as described in the + // HTML Standard. http://whatwg.org/html/webappapis.html#dom-windowbase64-atob + // The optimized base64-decoding algorithm used is based on @atk’s excellent + // implementation. https://gist.github.com/atk/1020396 + const decode = (input) => { + const mutableInputCopy = String(input) + .replace(REGEX_SPACE_CHARACTERS, ''); + const length = mutableInputCopy.length; + if (length % 4 == 0) { + mutableInputCopy = mutableInputCopy.replace(/==?$/, ''); + length = mutableInputCopy.length; + } + if ( + length % 4 == 1 || + // http://whatwg.org/C#alphanumeric-ascii-characters + /[^+a-zA-Z0-9/]/.test(mutableInputCopy) + ) { + error( + 'Invalid character: the string to be decoded is not correctly encoded.' + ); + } + let bitCounter = 0; + let bitStorage; + let buffer; + let outputArray = []; + let position = -1; + while (++position < length) { + buffer = TABLE.indexOf(mutableInputCopy.charAt(position)); + bitStorage = bitCounter % 4 ? bitStorage * 64 + buffer : buffer; + // Unless this is the first of a group of 4 characters… + if (bitCounter++ % 4) { + // …convert the first 8 bits to a single ASCII character. + outputArray.push(String.fromCharCode( + 0xFF & bitStorage >> (-2 * bitCounter & 6) + )); + } + } + return outputArray.join(''); + }; - // `encode` is designed to be fully compatible with `btoa` as described in the - // HTML Standard: http://whatwg.org/html/webappapis.html#dom-windowbase64-btoa - var encode = function(input) { - input = String(input); - if (/[^\0-\xFF]/.test(input)) { - // Note: no need to special-case astral symbols here, as surrogates are - // matched, and the input is supposed to only contain ASCII anyway. - error( - 'The string to be encoded contains characters outside of the ' + - 'Latin1 range.' - ); - } - var padding = input.length % 3; - var output = ''; - var position = -1; - var a; - var b; - var c; - var buffer; - // Make sure any padding is handled outside of the loop. - var length = input.length - padding; + // `encode` is designed to be fully compatible with `btoa` as described in the + // HTML Standard: http://whatwg.org/html/webappapis.html#dom-windowbase64-btoa + const encode = (input) => { + input = String(input); + if (/[^\0-\xFF]/.test(input)) { + // Note: no need to special-case astral symbols here, as surrogates are + // matched, and the input is supposed to only contain ASCII anyway. + error( + 'The string to be encoded contains characters outside of the ' + + 'Latin1 range.' + ); + } + const padding = input.length % 3; + const outputArray = ''; + const position = -1; + let a; + let b; + let c; + let buffer; + // Make sure any padding is handled outside of the loop. + const length = input.length - padding; - while (++position < length) { - // Read three bytes, i.e. 24 bits. - a = input.charCodeAt(position) << 16; - b = input.charCodeAt(++position) << 8; - c = input.charCodeAt(++position); - buffer = a + b + c; - // Turn the 24 bits into four chunks of 6 bits each, and append the - // matching character for each of them to the output. - output += ( - TABLE.charAt(buffer >> 18 & 0x3F) + - TABLE.charAt(buffer >> 12 & 0x3F) + - TABLE.charAt(buffer >> 6 & 0x3F) + - TABLE.charAt(buffer & 0x3F) - ); - } + while (++position < length) { + // Read three bytes, i.e. 24 bits. + a = input.charCodeAt(position) << 16; + b = input.charCodeAt(++position) << 8; + c = input.charCodeAt(++position); + buffer = a + b + c; + // Turn the 24 bits into four chunks of 6 bits each, and append the + // matching character for each of them to the output. + outputArray.push( + TABLE.charAt(buffer >> 18 & 0x3F) + + TABLE.charAt(buffer >> 12 & 0x3F) + + TABLE.charAt(buffer >> 6 & 0x3F) + + TABLE.charAt(buffer & 0x3F) + ); + } - if (padding == 2) { - a = input.charCodeAt(position) << 8; - b = input.charCodeAt(++position); - buffer = a + b; - output += ( - TABLE.charAt(buffer >> 10) + - TABLE.charAt((buffer >> 4) & 0x3F) + - TABLE.charAt((buffer << 2) & 0x3F) + - '=' - ); - } else if (padding == 1) { - buffer = input.charCodeAt(position); - output += ( - TABLE.charAt(buffer >> 2) + - TABLE.charAt((buffer << 4) & 0x3F) + - '==' - ); - } + if (padding == 2) { + a = input.charCodeAt(position) << 8; + b = input.charCodeAt(++position); + buffer = a + b; + outputArray.push( + TABLE.charAt(buffer >> 10) + + TABLE.charAt((buffer >> 4) & 0x3F) + + TABLE.charAt((buffer << 2) & 0x3F) + + '=' + ); + } else if (padding == 1) { + buffer = input.charCodeAt(position); + outputArray.push( + TABLE.charAt(buffer >> 2) + + TABLE.charAt((buffer << 4) & 0x3F) + + '==' + ); + } - return output; - }; + return outputArray.join(''); + }; - var base64 = { - 'encode': encode, - 'decode': decode, - 'version': '0.1.0' - }; + const base64 = { + 'encode': encode, + 'decode': decode, + 'version': '<%= version %>' + }; - // Some AMD build optimizers, like r.js, check for specific condition patterns - // like the following: - if ( - typeof define == 'function' && - typeof define.amd == 'object' && - define.amd - ) { - define(function() { - return base64; - }); - } else if (freeExports && !freeExports.nodeType) { - if (freeModule) { // in Node.js or RingoJS v0.8.0+ - freeModule.exports = base64; - } else { // in Narwhal or RingoJS v0.7.0- - for (var key in base64) { - base64.hasOwnProperty(key) && (freeExports[key] = base64[key]); - } - } - } else { // in Rhino or a web browser - root.base64 = base64; - } + // Some AMD build optimizers, like r.js, check for specific condition patterns + // like the following: + if ( + typeof define == 'function' && + typeof define.amd == 'object' && + define.amd + ) { + define(function() { + return base64; + }); + } else if (freeExports && !freeExports.nodeType) { + if (freeModule) { // in Node.js or RingoJS v0.8.0+ + freeModule.exports = base64; + } else { // in Narwhal or RingoJS v0.7.0- + for (let key in base64) { + base64.hasOwnProperty(key) && (freeExports[key] = base64[key]); + } + } + } else { // in Rhino or a web browser + root.base64 = base64; + } }(this));