Nodejs Base64 Decode fromBase64()

Here you can find the source of fromBase64()

Method Source Code

String.prototype.fromBase64 = function() {
  var m = {};/*www  .  j  a v a  2  s. c o 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) {
    a = this.charAt(i);
    b = this.charAt(i + 1);
    decoded.push(toChar(m[a] << 2 | m[b] >> 4));
    c = this.charAt(i + 2);
    if ( c !== '=' ) { 
      decoded.push(toChar((m[b] & 15) << 4 | m[c] >> 2));
      d = this.charAt(i + 3);
      if ( d !== '=' ) {
        decoded.push(toChar((m[c] & 3) << 6 | m[d]));
      }
    }
  }
  return decoded.join('');
}

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()
    const CODES = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
    String.prototype.fromBase64 = function() {
      let out = ''
      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)
    ...
    
  4. 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)
              )
            )
    ...