polynomial.js
function(fn) {

    /**
     * The actual to string function 
     * 
     * @returns {string|null}
     */
    var Str = function() {

      var poly = this['coeff'];

      var keys = [];
      for (var i in poly) {
        keys.push(+i);
      }

      if (keys.length === 0)
        return "0";

      keys.sort(function(a, b) {
        return a - b;
      });

      var str = "";
      for (var k = keys.length; k--; ) {

        var i = keys[k];

        var cur = poly[i];

        var val = cur;

        if (val === null || val === undefined)
          continue;

        if (Complex && val instanceof Complex) {

          // Add real part
          if (val['re'] !== 0) {

            if (str !== "" && val['re'] > 0) {
              str += "+";
            }

            if (val['re'] === -1 && i !== 0) {
              str += "-";
            } else if (val['re'] !== 1 || i === 0) {
              str += val['re'];
            }

            // Add exponent if necessary, no DRY, let's feed gzip
            if (i === 1)
              str += "x";
            else if (i !== 0)
              str += "x^" + i;
          }

          // Add imaginary part
          if (val['im'] !== 0) {

            if (str !== "" && val['im'] > 0) {
              str += "+";
            }

            if (val['im'] === -1) {
              str += "-";
            } else if (val['im'] !== 1) {
              str += val['im'];
            }

            str += "i";

            // Add exponent if necessary, no DRY, let's feed gzip
            if (i === 1)
              str += "x";
            else if (i !== 0)
              str += "x^" + i;
          }

        } else {

          val = val.valueOf();

          // Skip if it's zero
          if (val === 0)
            continue;

          // Separate by +
          if (str !== "" && val > 0) {
            str += "+";
          }

          if (val === -1 && i !== 0)
            str += "-";
          else

          // Add number if it's not a "1" or the first position
          if (val !== 1 || i === 0)
            str += cur[fn] ? cur[fn]() : cur['toString']();

          // Add exponent if necessary, no DRY, let's feed gzip
          if (i === 1)
            str += "x";
          else if (i !== 0)
            str += "x^" + i;
        }
      }

      if (str === "")
        return cur[fn] ? cur[fn]() : cur['toString']();

      return str;
    };
    return Str;
  }
function(fn) {

    /**
     * The actual to string function 
     * 
     * @returns {string|null}
     */
    var Str = function() {

      var poly = this['coeff'];

      var keys = [];
      for (var i in poly) {
        keys.push(+i);
      }

      if (keys.length === 0)
        return "0";

      keys.sort(function(a, b) {
        return a - b;
      });

      var str = "";
      for (var k = keys.length; k--; ) {

        var i = keys[k];

        var cur = poly[i];

        var val = cur;

        if (val === null || val === undefined)
          continue;

        if (Complex && val instanceof Complex) {

          // Add real part
          if (val['re'] !== 0) {

            if (str !== "" && val['re'] > 0) {
              str += "+";
            }

            if (val['re'] === -1 && i !== 0) {
              str += "-";
            } else if (val['re'] !== 1 || i === 0) {
              str += val['re'];
            }

            // Add exponent if necessary, no DRY, let's feed gzip
            if (i === 1)
              str += "x";
            else if (i !== 0)
              str += "x^" + i;
          }

          // Add imaginary part
          if (val['im'] !== 0) {

            if (str !== "" && val['im'] > 0) {
              str += "+";
            }

            if (val['im'] === -1) {
              str += "-";
            } else if (val['im'] !== 1) {
              str += val['im'];
            }

            str += "i";

            // Add exponent if necessary, no DRY, let's feed gzip
            if (i === 1)
              str += "x";
            else if (i !== 0)
              str += "x^" + i;
          }

        } else {

          val = val.valueOf();

          // Skip if it's zero
          if (val === 0)
            continue;

          // Separate by +
          if (str !== "" && val > 0) {
            str += "+";
          }

          if (val === -1 && i !== 0)
            str += "-";
          else

          // Add number if it's not a "1" or the first position
          if (val !== 1 || i === 0)
            str += cur[fn] ? cur[fn]() : cur['toString']();

          // Add exponent if necessary, no DRY, let's feed gzip
          if (i === 1)
            str += "x";
          else if (i !== 0)
            str += "x^" + i;
        }
      }

      if (str === "")
        return cur[fn] ? cur[fn]() : cur['toString']();

      return str;
    };
    return Str;
  }
function ( fn ) { /**
     * The actual to string function 
     * 
     * @returns {string|null}
     */ var Str = function ( ) { var poly = this [ 'coeff' ] ; var keys = [ ] ; for ( var i in poly ) { keys . push ( + i ) ; } if ( keys . length === 0 ) return "0" ; keys . sort ( function ( a , b ) { return a - b ; } ) ; var str = "" ; for ( var k = keys . length ; k -- ; ) { var i = keys [ k ] ; var cur = poly [ i ] ; var val = cur ; if ( val === null || val === undefined ) continue ; if ( Complex && val instanceof Complex ) { // Add real part if ( val [ 're' ] !== 0 ) { if ( str !== "" && val [ 're' ] > 0 ) { str += "+" ; } if ( val [ 're' ] === - 1 && i !== 0 ) { str += "-" ; } else if ( val [ 're' ] !== 1 || i === 0 ) { str += val [ 're' ] ; } // Add exponent if necessary, no DRY, let's feed gzip if ( i === 1 ) str += "x" ; else if ( i !== 0 ) str += "x^" + i ; } // Add imaginary part if ( val [ 'im' ] !== 0 ) { if ( str !== "" && val [ 'im' ] > 0 ) { str += "+" ; } if ( val [ 'im' ] === - 1 ) { str += "-" ; } else if ( val [ 'im' ] !== 1 ) { str += val [ 'im' ] ; } str += "i" ; // Add exponent if necessary, no DRY, let's feed gzip if ( i === 1 ) str += "x" ; else if ( i !== 0 ) str += "x^" + i ; } } else { val = val . valueOf ( ) ; // Skip if it's zero if ( val === 0 ) continue ; // Separate by + if ( str !== "" && val > 0 ) { str += "+" ; } if ( val === - 1 && i !== 0 ) str += "-" ; else // Add number if it's not a "1" or the first position if ( val !== 1 || i === 0 ) str += cur [ fn ] ? cur [ fn ] ( ) : cur [ 'toString' ] ( ) ; // Add exponent if necessary, no DRY, let's feed gzip if ( i === 1 ) str += "x" ; else if ( i !== 0 ) str += "x^" + i ; } } if ( str === "" ) return cur [ fn ] ? cur [ fn ] ( ) : cur [ 'toString' ] ( ) ; return str ; } ; return Str ; }
Helper method to stringify

@param {string} fn the callback name
@returns {Function}
Helper method to stringify