import { functor , path } from "./index" ; import { noop } from "./noop" ; interface SlidingWindow { ( data ) : any [ ] ; misc ( ) : any ; misc ( x ) : SlidingWindow ; accumulator ( ) : any ; accumulator ( x ) : SlidingWindow ; skipInitial ( ) : number ; skipInitial ( x ) : SlidingWindow ; source ( ) : any ; source ( source ) : SlidingWindow ; sourcePath ( ) : any ; sourcePath ( x ) : SlidingWindow ; windowSize ( ) : number ; windowSize ( windowSize ) : SlidingWindow ; undefinedValue ( ) : any ; undefinedValue ( x ) : SlidingWindow ; } export default function ( ) { let undefinedValue ; let windowSize = 10 ; let accumulator = noop ; let sourcePath ; let source ; let skipInitial = 0 ; let misc ; const slidingWindow = ( data ) => { const sourceFunction = source || path ( sourcePath ) ; const size = functor ( windowSize ) . apply ( this , arguments ) ; const windowData = data . slice ( skipInitial , size + skipInitial ) . map ( sourceFunction ) ; let accumulatorIdx = 0 ; const undef = functor ( undefinedValue ) ; return data . map ( ( d , i ) => { if ( i < skipInitial + size - 1 ) { return undef ( sourceFunction ( d ) , i , misc ) ; } if ( i >= skipInitial + size ) { windowData . shift ( ) ; windowData . push ( sourceFunction ( d , i ) ) ; } return accumulator ( windowData , i , accumulatorIdx ++ , misc ) ; } ) ; } ; slidingWindow . undefinedValue = function ( x ) { if ( ! arguments . length ) { return undefinedValue ; } undefinedValue = x ; return slidingWindow ; } ; slidingWindow . windowSize = function ( x ) { if ( ! arguments . length ) { return windowSize ; } windowSize = x ; return slidingWindow ; } ; slidingWindow . misc = function ( x ) { if ( ! arguments . length ) { return misc ; } misc = x ; return slidingWindow ; } ; slidingWindow . accumulator = function ( x ) { if ( ! arguments . length ) { return accumulator ; } accumulator = x ; return slidingWindow ; } ; slidingWindow . skipInitial = function ( x ) { if ( ! arguments . length ) { return skipInitial ; } skipInitial = x ; return slidingWindow ; } ; slidingWindow . sourcePath = function ( x ) { if ( ! arguments . length ) { return sourcePath ; } sourcePath = x ; return slidingWindow ; } ; slidingWindow . source = function ( x ) { if ( ! arguments . length ) { return source ; } source = x ; return slidingWindow ; } ; return slidingWindow as SlidingWindow ; }