Nodejs String from Ascii85 fromAscii85()

Here you can find the source of fromAscii85()

Method Source Code

String.prototype.fromAscii85 = function() {
  var str = this.slice(2, -2).replace(/z/g, '!!!!!').replace(/\s/g, '');
  var decoded = '';
  
  
  for(var i = 0, len = str.length; i < len; i+=5) {
    var n = 0;/*from   w w  w  . j ava2s. com*/
    var us = 0;
    for(var j = 4; j >= 0; j--) {
      var c = str[j+i] || (us++, 'u'),
          code = c.charCodeAt(0) - 33;

      n += code * Math.pow(85, Math.abs(j-4));
    }
    
    var bin = stretch(n.toString(2), 32);
    
    for(var j = 0; j < 32; j+=8) {
      var char = parseInt(bin.slice(j, j+8), 2);
      decoded += String.fromCharCode(char);
    }
    decoded = decoded.slice(0, us > 0 ? -us : undefined);
  }
  
  return decoded;
}

function to8bit(n) {
  var bin = stretch(n.toString(2), 8);
  
  return bin;
}

function stretch(s, n) {
  var r = s;
  while(r.length < n) {
    r = '0' + r;
  }
  
  return r;
}

Related

  1. fromAscii85()
    String.prototype.fromAscii85 = function() {
      var str = this.slice(2, -2).replace(/\s/g, ''), result = ''
      while (str.length) {
        if (str[0] == 'z') {
          result += '\u0000\u0000\u0000\u0000'
          str = str.substr(1)
        } else {
          let num = 0, j = 5, i = 4
          while (j--) {
    ...
    
  2. fromAscii85()
    String.prototype.fromAscii85 = function() {
      var ret = "";
      var str = this.replace(/\s+/g, "").replace("z", "!!!!!").slice(2, -2);
      for(var i=0; i<str.length; i+=5){
        var deficit = Math.max(0, 5+i-str.length);
        var nnn = 0;
        for(var j=0; j<5; j++){
          nnn = nnn*85 + (str.charCodeAt(i+j) || 117) - 33;
        var codes = [];
        for(var j=0; j<4; j++){
          codes.push(nnn%256);
          nnn = nnn/256 | 0;
        codes.reverse();
        var chunk = String.fromCharCode.apply(undefined, codes).slice(0, 4-deficit);
        ret+= chunk;
      return ret;
    
  3. fromAscii85()
    String.prototype.fromAscii85 = function() {
        console.log(this)
        let pad = 'u', addnum = 0, s = this.substring(2, this.length - 2).replace(/\s/g, ''), str = '', res = '';
        for (let i = 0; i < s.length; i++)
            str += s[i] === 'z' ? '!!!!!' : s[i];
        while (str.length % 5 !== 0)
            str += pad;
            addnum ++;
    ...