1.0.1 • Published 6 years ago
find-streaks v1.0.1
find-streaks
Finds streaks of equal/same item, based on a monotonically increasing "time" property of them.
The following table shows how find-streaks works with a streak length of 3.
monotonic
timer bucket output
-------------------------
0 A A starts
1 A
2 B B starts
2 C C starts
5 C A ends
6 B B ends, B starts
end C ends, B endsInstallation
npm install find-streaksUsage
These items represent the chart above:
const item1 = [0, 'a']
const item2 = [1, 'a']
const item3 = [2, 'b']
const item4 = [2, 'c']
const item4 = [5, 'c']
const item5 = [6, 'b']Two items can be part of a streak if they have the same bucket(item), which must return a string or null.
find-streaks will tell if two items are witin a streak using monotonic(item), which must return an integer.
const streakLength = 3
const bucket = item => item[1]
const monotonic = item => item[0]keeping only the last item of each streak
const keepLastOfStreaks = require('find-streaks')
const {check, flush} = keepLastOfStreaks(streakLength, bucket, monotonic)
check(item1) // []
check(item2) // []
check(item3) // []
check(item4) // []
check(item5) // [item2]
check(item6) // [item3]
flush() // [item5, item6]as a transform stream
const keepLastStream = require('find-streaks/stream')
const keepLast = keepLastStream(streakLength, bucket, monotonic)
keepLast.on('data', console.log)
keepLast.write(item1)
// …
keepLast.end()as a callbag operator
const keepLastCallbag = require('find-streaks/callbag')
const {pipe, fromIter, forEach} = require('callbag-basics')
const keepLast = keepLastCallbag(streakLength, bucket, monotonic)
pipe(
fromIter([item1, item2, item3, item4, item5, item6]),
keepLast,
forEach(console.log)
)finding raw starts & ends of streaks
const findStartsEnds = require('find-streaks/starts-ends')
const {START, END} = findStartsEnds
const {check, flush} = findStartsEnds(streakLength, bucket, monotonic)
check(item1) // ['a', START]
check(item2) // []
check(item3) // ['b', START]
check(item4) // ['c', START]
check(item5) // ['a', END]
check(item6) // ['b', END, 'b', START]
flush() // ['c', END, 'b', END]Related
lodash.debouncelodash.throttlecallbag-keep-sequences– A callbag operator that passes through only sequences with minimum length.flatten-overlapping-ranges– Flatten overlapping ranges into a sequence of sections.
Contributing
If you have a question or need support using find-streaks, please double-check your code and setup first. If you think you have found a bug or want to propose a feature, use the issues page.