https://github.com/derhuerst/find-streaks
Finds streaks of equal/same item, based on a "time" property of them.
https://github.com/derhuerst/find-streaks
debounce streak
Last synced: 8 months ago
JSON representation
Finds streaks of equal/same item, based on a "time" property of them.
- Host: GitHub
- URL: https://github.com/derhuerst/find-streaks
- Owner: derhuerst
- License: isc
- Created: 2020-03-01T00:55:12.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2020-03-01T02:30:31.000Z (over 5 years ago)
- Last Synced: 2025-02-08T15:07:01.345Z (8 months ago)
- Topics: debounce, streak
- Language: JavaScript
- Homepage: https://github.com/derhuerst/find-streaks#find-streaks
- Size: 223 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
# find-streaks
**Finds streaks of equal/same item**, based on a [monotonically](https://en.wikipedia.org/wiki/Monotonic_function) increasing "time" property of them.
[](https://www.npmjs.com/package/find-streaks)
[](https://travis-ci.org/derhuerst/find-streaks)


[](https://gitter.im/derhuerst)
[](https://patreon.com/derhuerst)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 ends
```## Installation
```shell
npm install find-streaks
```## Usage
These items represent the chart above:
```js
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.
```js
const streakLength = 3
const bucket = item => item[1]
const monotonic = item => item[0]
```## keeping only the last item of each streak
```js
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](https://nodejs.org/api/stream.html#stream_class_stream_transform)
```js
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](https://github.com/callbag/callbag#callbag-) operator
```js
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
```js
const findStartsEnds = require('find-streaks/starts-ends')
const {START, END} = findStartsEndsconst {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.debounce`](https://lodash.com/docs/4.17.15#debounce)
- [`lodash.throttle`](https://lodash.com/docs/4.17.15#throttle)
- [`callbag-keep-sequences`](https://github.com/derhuerst/callbag-keep-sequences) – A callbag operator that passes through only sequences with minimum length.
- [`flatten-overlapping-ranges`](https://github.com/derhuerst/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](https://github.com/derhuerst/find-streaks/issues).