import { sum } from "d3-array" ; import { slidingWindow } from "../utils" ; import { TMA as defaultOptions } from "./defaultOptionsForComputation" ; export interface TMAOptions { readonly sourcePath ? : string ; readonly windowSize : number ; } export default function ( ) { let options = defaultOptions ; const calculator = ( data ) => { const { windowSize , sourcePath } = options ; const n = Math . floor ( windowSize / 2 ) ; const weight = windowSize % 2 === 0 ? n * ( n + 1 ) : ( n + 1 ) * ( n + 1 ) ; const triaverage = slidingWindow ( ) . windowSize ( windowSize ) . sourcePath ( sourcePath ) . accumulator ( ( values ) => { const total = sum ( values , ( v , i ) => { return i < n ? ( i + 1 ) * v : ( windowSize - i ) * v ; } ) ; return total / weight ; } ) ; return triaverage ( data ) ; } ; calculator . undefinedLength = ( ) => { const { windowSize } = options ; return windowSize - 1 ; } ; calculator . options = ( newOptions ? ) => { if ( newOptions === undefined ) { return options ; } options = { ... defaultOptions , ... newOptions } ; return calculator ; } ; return calculator ; }