import { assert } from 'chai' ; import * as dataForge from 'data-forge' ; import { IDataFrame , DataFrame , ISeries , Series } from 'data-forge' ; import "./direction" ; declare module "data-forge/build/lib/series" { interface ISeries < IndexT , ValueT > { streaks ( period ) : ISeries < IndexT , number > ; } interface Series < IndexT , ValueT > { streaks ( period ) : ISeries < IndexT , number > ; } } function streaks < IndexT = any > ( this , period = 2 ) { return this . direction ( period ) . groupSequentialBy ( direction => direction ) . selectMany ( group => { const direction = group . first ( ) ; if ( direction === 0 ) { return dataForge . replicate ( 0 , group . count ( ) ) . withIndex ( group . getIndex ( ) ) . toPairs ( ) ; } else if ( direction < 0 ) { return dataForge . range ( 1 , group . count ( ) ) . select ( value => - value ) . withIndex ( group . getIndex ( ) ) . toPairs ( ) ; } else { return dataForge . range ( 1 , group . count ( ) ) . withIndex ( group . getIndex ( ) ) . toPairs ( ) ; } } ) . withIndex ( pair => pair [ 0 ] ) . select ( pair => pair [ 1 ] ) . bake ( ) ; } Series . prototype . streaks = streaks ;