Nodejs Base64 Encode toBase64(()

Here you can find the source of toBase64(()

Method Source Code

//http://www.codewars.com/dojo/katas/5270f22f862516c686000161
String.prototype.toBase64 = (function(){
  var b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  var b64hash = {}; b64chars.split('').forEach(function(v,i){b64hash[v]=i});
  var b64pos = [18, 12, 6, 0];
  var b64and = b64pos.map(function(v){return 63<<v});
  return function toBase64 () {
    var length = this.length;
    var count, encoded = [], b64string = "";
    var i, extra;
    for (var index=0;index<length;index++) {
      if (!(index % 3)) count = 0;
      count <<= 8;/*w  w  w.  ja  va 2s  . c o m*/
      count |= this.charCodeAt(index);
      if (index % 3 == 2) encoded.push(count);
      }
    encoded.forEach(function(v){
      for (i=0;i<4;i++)
        b64string += b64chars[ (v & b64and[i]) >> b64pos[i] ];
      });
    if (extra = length % 3) {
      count <<= 24 - 8 * extra;
      for (i=0;i<extra+1;i++)
        b64string += b64chars[ (count & b64and[i]) >> b64pos[i] ];
      for (i=0;i<3-extra;i++)
        b64string += '=';
      }
    return b64string
    }
  })();
  
String.prototype.fromBase64 = (function(){
  var b64chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  var b64hash = {}; b64chars.split('').forEach(function(v,i){b64hash[v]=i});
  var asciiPos = [16, 8, 0];
  var asciiAnd = asciiPos.map(function(v){return 255<<v});
  return function fromBase64 () {
    var offset = +(this[this.length-1]=='=') + +(this[this.length-2]=='=');
    var string = this.replace(/=/g,'');
    var length = string.length;
    var count, decoded = [], asciiString = "";
    for (var index=0;index<length;index++) {
      if (!(index % 4)) count = 0;
      count <<= 6;
      count |= b64hash[string[index]];
      if (index % 4 == 3) decoded.push(count);
      }
    if (offset) decoded.push(count << (offset * 6));
    var decLen = decoded.length-1;
    decoded.forEach(function(v,I){
      asciiString += String.fromCharCode.apply(null, 
        (decLen==I ? asciiPos.slice(0,3-offset) : asciiPos)
          .map(function(c,i){return (v & asciiAnd[i]) >> c}))  
      });
    return asciiString
    }
  })()

Related

  1. toBase64()
    String.prototype.toBase64 = function () {
        var converter = new Buffer(this.toString(), 'utf8');
        return converter.toString('base64');
    
  2. toBase64()
    const CODES = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
    String.prototype.toBase64 = function() {
      let out = ''
      for (let i = 0, length = this.length; i < length; i += 3) {
        let [a, b, c] = [...this.slice(i, i + 3)].map(c => c.charCodeAt(0))
        bits = a << 16 | b << 8 | c
        out += CODES[bits >> 18 & 0x3F]
        out += CODES[bits >> 12 & 0x3F]
        out += b ? CODES[bits >> 6 & 0x3F] : '='
    ...
    
  3. toBase64()
    String.prototype.toBase64 = function() {
      var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
      var output = ""
      var chr1, chr2, chr3
      var enc1, enc2, enc3, enc4
       var i = 0
       do {
          chr1 = this.charCodeAt(i++)
          chr2 = this.charCodeAt(i++)
    ...
    
  4. toBase64()
    String.prototype.toBase64 = function(){
      var nToOrd = function(n){
        return n < 26 ? "A".charCodeAt(0) + n : (
          n < 52 ? "a".charCodeAt(0) - 26 + n : (
            n < 62 ? "0".charCodeAt(0) - 52 + n : (
              n==62 ? "+".charCodeAt(0) : "/".charCodeAt(0)
            )
          )
        );
    ...