All files / site time_funs.ts

21.42% Statements 3/14
0% Branches 0/8
0% Functions 0/7
21.42% Lines 3/14

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    2x                 2x                             2x                                
import type {VariableFilterParsed} from '../types'
import type {DataViewParsed} from './dataview'
import {patterns} from './patterns'
 
type TimeFuns = {
  number: Function
  first: Function
  selected: Function
  last: Function
}
 
const time_funs = {
  number: function (this: number, v: VariableFilterParsed) {
    return this - v.range[0]
  },
  first: function (v: VariableFilterParsed) {
    return 0
  },
  selected: function (v: VariableFilterParsed, p: DataViewParsed) {
    return p.time_agg - v.range[0]
  },
  last: function (v: VariableFilterParsed) {
    return v.range[1] - v.range[0]
  },
}
 
export function component_fun(times: number[], c: string | number): Function {
  Iif ('string' === typeof c && patterns.number.test(c)) {
    c = times.indexOf(+c)
    Iif (-1 === c)
      return function () {
        return NaN
      }
  }
  return 'number' === typeof c
    ? time_funs.number.bind(c)
    : c in time_funs
    ? time_funs[c as keyof TimeFuns]
    : function () {
        return NaN
      }
}