static/js/util.js
/**
 * Scale an array by key.
 * @function scaleArr
 * @param {number[]} arr - array to scale
 * @param {string|number} scale - scale name or power
 * @returns {number[]} scaled array 
 * @throws if scale is invalid
 */
Scale an array by key.
Scale an array by key .
function scaleArr(arr, scale) {
  const n = arr.length;
  const res = Array(n).fill(NaN);
  if (scale == null) {
    return arr;
  } else if (typeof(scale) === 'number') {
    for (let i = 0; i < n; i++) res[i] = arr[i] ** scale;
  } else if (typeof(scale) === 'string') {
    let num;
    switch (scale) {
      case 'none':
        return arr;
      case 'rank':
        return rankdata(arr);
      case 'square':
        for (let i = 0; i < n; i++) res[i] = arr[i] ** 2;
        break;
      case 'sqrt':
        for (let i = 0; i < n; i++) res[i] = Math.sqrt(arr[i]);
        break;
      case 'cube':
        for (let i = 0; i < n; i++) res[i] = arr[i] ** 3;
        break;
      case 'cbrt':
        for (let i = 0; i < n; i++) res[i] = Math.cbrt(arr[i]);
        break;
      case '4pow':
        for (let i = 0; i < n; i++) res[i] = arr[i] ** 4;
        break;
      case '4rt':
        for (let i = 0; i < n; i++) res[i] = arr[i] ** 0.25;
        break;
      case 'log':
        for (let i = 0; i < n; i++) res[i] = Math.log(arr[i]);
        break;
      case 'exp':
        for (let i = 0; i < n; i++) res[i] = Math.exp(arr[i]);
        break;
      case 'logit':
        for (let i = 0; i < n; i++) {
          num = arr[i];
          res[i] = Math.log(num / (1 - num));
        }
        break;
      case 'expit':
        for (let i = 0; i < n; i++) res[i] = 1 / (1 + Math.exp(-arr[i]));
        break;
      case 'asin':
        for (let i = 0; i < n; i++) res[i] = Math.asin(Math.sqrt(arr[i]));
        break;
      case 'sin':
        for (let i = 0; i < n; i++) {
          num = arr[i];
          res[i] = Math.sign(num) * Math.sin(num) ** 2;
        }
        break;
      default:
        throw new Error(`Invalid scale name "${scale}".`);
    }
  } else {
    throw new Error('Invalid scale type.');
  }
  return res;
}
function scaleArr ( arr , scale ) { const n = arr . length ; const res = Array ( n ) . fill ( NaN ) ; if ( scale == null ) { return arr ; } else if ( typeof ( scale ) === 'number' ) { for ( let i = 0 ; i < n ; i ++ ) res [ i ] = arr [ i ] ** scale ; } else if ( typeof ( scale ) === 'string' ) { let num ; switch ( scale ) { case 'none' : return arr ; case 'rank' : return rankdata ( arr ) ; case 'square' : for ( let i = 0 ; i < n ; i ++ ) res [ i ] = arr [ i ] ** 2 ; break ; case 'sqrt' : for ( let i = 0 ; i < n ; i ++ ) res [ i ] = Math . sqrt ( arr [ i ] ) ; break ; case 'cube' : for ( let i = 0 ; i < n ; i ++ ) res [ i ] = arr [ i ] ** 3 ; break ; case 'cbrt' : for ( let i = 0 ; i < n ; i ++ ) res [ i ] = Math . cbrt ( arr [ i ] ) ; break ; case '4pow' : for ( let i = 0 ; i < n ; i ++ ) res [ i ] = arr [ i ] ** 4 ; break ; case '4rt' : for ( let i = 0 ; i < n ; i ++ ) res [ i ] = arr [ i ] ** 0.25 ; break ; case 'log' : for ( let i = 0 ; i < n ; i ++ ) res [ i ] = Math . log ( arr [ i ] ) ; break ; case 'exp' : for ( let i = 0 ; i < n ; i ++ ) res [ i ] = Math . exp ( arr [ i ] ) ; break ; case 'logit' : for ( let i = 0 ; i < n ; i ++ ) { num = arr [ i ] ; res [ i ] = Math . log ( num / ( 1 - num ) ) ; } break ; case 'expit' : for ( let i = 0 ; i < n ; i ++ ) res [ i ] = 1 / ( 1 + Math . exp ( - arr [ i ] ) ) ; break ; case 'asin' : for ( let i = 0 ; i < n ; i ++ ) res [ i ] = Math . asin ( Math . sqrt ( arr [ i ] ) ) ; break ; case 'sin' : for ( let i = 0 ; i < n ; i ++ ) { num = arr [ i ] ; res [ i ] = Math . sign ( num ) * Math . sin ( num ) ** 2 ; } break ; default : throw new Error ( ` ${ scale } ` ) ; } } else { throw new Error ( 'Invalid scale type.' ) ; } return res ; }
Scale an array by key.
Scale an array by key .