Nodejs Base64 Decode fromBase64()

Here you can find the source of fromBase64()

Method Source Code

const CODES = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='

String.prototype.fromBase64 = function() {
  let out = ''//from ww  w  .j a v  a  2s .  c o  m
  for (let i = 0, length = this.length; i < length; i += 4) {
    let [a, b, c, d] = [...this.slice(i, i + 4)].map(c => CODES.indexOf(c))
    bits = a << 18 | b << 12 | c << 6 | d
    out += String.fromCharCode(bits >> 16)
    if (c !== 64) out += String.fromCharCode(bits >> 8 & 0xFF)
    if (d !== 64) out += String.fromCharCode(bits & 0xFF)
  }
  return out
}

Related

  1. fromBase64()
    String.prototype.fromBase64 = function () {
        var converter = new Buffer(this.toString(), 'base64');
        return converter.toString('utf8');
    
  2. fromBase64()
    String.prototype.fromBase64 = function () {
      var str = this;
      var output = str.replace('-', '+').replace('_', '/');
      switch (output.length % 4) {
        case 0:
          break;
        case 2:
          output += '==';
          break;
    ...
    
  3. fromBase64()
    String.prototype.fromBase64 = function(){
      var ordToN = function(ord){
        return ord=="=".charCodeAt(0) ? 0 : (
          ord=="/".charCodeAt(0) ? 63 : (
            ord=="+".charCodeAt(0) ? 62 : (
              ord>="0".charCodeAt(0) && ord<="9".charCodeAt(0) ? ord-"0".charCodeAt(0)+52 : (
                ord>="a".charCodeAt(0) && ord<="z".charCodeAt(0) ? ord-"a".charCodeAt(0)+26 : ord-"A".charCodeAt(0)
              )
            )
    ...
    
  4. fromBase64()
    String.prototype.fromBase64 = function() {
      var m = {};
      var toChar = String.fromCharCode;
      code.forEach(function(value, index) {
        m[value] = index; 
      });
      var decoded = [];
      var a, b, c, d;
      for (var i = 0, len = this.length; i < len; i = i + 4) {
    ...