static/js/util.js
/**
 * Scale a number by key.
 * @function scaleNum
 * @param {number} num - number to scale
 * @param {string|number} scale - scale name or power
 * @throws if scale is invalid
 * @returns {number} scaled number
 */
Scale a number by key.
Scale a number by key .
function scaleNum(num, scale) {
  if (scale == null) {
    return num;
  } else if (typeof(scale) === 'number') {
    return Math.pow(num, scale);
  } else if (typeof(scale) === 'string') {
    switch (scale) {
      case 'none':
        return num;
      case 'rank': 
        return NaN;
      case 'square':
        return num ** 2;
      case 'sqrt':
        return Math.sqrt(num);
      case 'cube':
        return num ** 3;
      case 'cbrt':
        return Math.cbrt(num);
      case '4pow':
        return num ** 4;
      case '4rt':
        return num ** 0.25;
      case 'log':
        return Math.log(num);
      case 'exp':
        return Math.exp(num);
      case 'logit':
        return Math.log(num / (1 - num));
      case 'expit':
        return 1 / (1 + Math.exp(-num));
      case 'asin':
        return Math.asin(Math.sqrt(num));
      case 'sin':
        return Math.sign(num) * Math.sin(num) ** 2;
      default:
        throw new Error(`Invalid scale name "${scale}".`);
    }
  } else {
    throw new Error('Invalid scale type.');
  }
}
function scaleNum ( num , scale ) { if ( scale == null ) { return num ; } else if ( typeof ( scale ) === 'number' ) { return Math . pow ( num , scale ) ; } else if ( typeof ( scale ) === 'string' ) { switch ( scale ) { case 'none' : return num ; case 'rank' : return NaN ; case 'square' : return num ** 2 ; case 'sqrt' : return Math . sqrt ( num ) ; case 'cube' : return num ** 3 ; case 'cbrt' : return Math . cbrt ( num ) ; case '4pow' : return num ** 4 ; case '4rt' : return num ** 0.25 ; case 'log' : return Math . log ( num ) ; case 'exp' : return Math . exp ( num ) ; case 'logit' : return Math . log ( num / ( 1 - num ) ) ; case 'expit' : return 1 / ( 1 + Math . exp ( - num ) ) ; case 'asin' : return Math . asin ( Math . sqrt ( num ) ) ; case 'sin' : return Math . sign ( num ) * Math . sin ( num ) ** 2 ; default : throw new Error ( ` ${ scale } ` ) ; } } else { throw new Error ( 'Invalid scale type.' ) ; } }
Scale a number by key.
Scale a number by key .
// cannot rank a single number