All files / src/timing TimingFunction.js

95% Statements 19/20
85.71% Branches 12/14
100% Functions 3/3
94.73% Lines 18/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60            1x 962809x                         8x 8x     8x               42x     14x 14x       14x 11x   3x     2x 2x 2x   2x       12x 2x   10x    
// @ts-check
 
import { cubicBezier } from './cubicBezier';
import { argSepRe } from '../utils';
 
/** @type {{ [name: string]: (t: number) => number }} */
export const builtinTimingFunctions = {
  linear: function(/** @type {number} */ t) { return +t; },
  ease: cubicBezier(0.25, 0.1, 1.25, 1),
  'ease-in': cubicBezier(0.42, 0, 1, 1),
  'ease-out': cubicBezier(0, 0, 0.58, 1),
  'ease-in-out': cubicBezier(0.42, 0, 0.58, 1)
};
 
/**
 * @param {string} tfunc
 * @param {string} str
 * @return {number}
 */
function _parseFloat(tfunc, str) {
  const num = Number.parseFloat(str);
  Iif (Number.isNaN(num)) {
    throw new SyntaxError('invalid number in timing function: ' + tfunc);
  }
  return num;
}
 
/**
 * @param  {string|null|undefined} tfunc
 * @return {((t: number) => number) | null}
 */
export function parseTimingFunction(tfunc) {
  if (!tfunc) { return null; }
  /*:: (tfunc: string) */
 
  tfunc = tfunc.trim();
  const idx = tfunc.indexOf('(');
  /** @type {((t: number) => number)|undefined} */
  var ret;
 
  if (idx === -1) {
    ret = builtinTimingFunctions[tfunc];
  }
  else if ((tfunc.slice(0, idx) === 'cubic-bezier') &&
           (tfunc[tfunc.length - 1] === ')')) {
    /** @type {any[]} */
    var values = tfunc.slice(idx + 1, -1).split(argSepRe);
    values = values.map(_parseFloat.bind(null, tfunc));
    Eif (values.length === 4) {
      // @ts-ignore
      return cubicBezier(...values);
    }
  }
  // @ts-ignore
  if (!ret) {
    throw new SyntaxError('invalid timing function: ' + tfunc);
  }
  return ret;
}