interval-management
Advanced tools
@@ -179,6 +179,3 @@ "use strict"; | ||
| }; | ||
| interval.find = (compare, end) => { | ||
| if (typeof compare !== 'function' && !interval.has(compare)) { | ||
| return null; | ||
| } | ||
| const findHelper = (compare, end) => { | ||
| if (!end && end !== 0 && interval.end === infinity) { | ||
@@ -190,2 +187,12 @@ throw Error('Cannot seek inside infinite interval without boundary'); | ||
| const copyInterval = generalInterval(interval.start, trueEnd, interval.usedNext); | ||
| return { | ||
| trueCompare, | ||
| copyInterval, | ||
| }; | ||
| }; | ||
| interval.find = (compare, end) => { | ||
| if (typeof compare !== 'function' && !interval.has(compare)) { | ||
| return null; | ||
| } | ||
| const { trueCompare, copyInterval } = findHelper(compare, end); | ||
| while (!copyInterval.done()) { | ||
@@ -199,2 +206,16 @@ if (trueCompare(copyInterval.val())) { | ||
| }; | ||
| interval.all = (compare, end) => { | ||
| if (typeof compare !== 'function' && !interval.has(compare)) { | ||
| return []; | ||
| } | ||
| const { trueCompare, copyInterval } = findHelper(compare, end); | ||
| const aggregate = []; | ||
| while (!copyInterval.done()) { | ||
| if (trueCompare(copyInterval.val())) { | ||
| aggregate.push(copyInterval.val()); | ||
| } | ||
| copyInterval.next(); | ||
| } | ||
| return aggregate; | ||
| }; | ||
| interval.convert = (to, next) => { | ||
@@ -201,0 +222,0 @@ let nextEnd; |
+1
-1
| { | ||
| "name": "interval-management", | ||
| "version": "2.1.1", | ||
| "version": "2.2.0", | ||
| "description": "No dependency interval management library, able to work with numbers, string, Dates or special objects", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
+5
-0
@@ -157,2 +157,7 @@ # Intervals documentation | ||
| ### Changes since 2.2.0: | ||
| Next to a find function you can now find 'all'. Function will return an array of all elements matching a certain pattern. | ||
| Will fail, similarly to 'find', if you apply it on an infinite interval without an 'end' item to finish the search. | ||
| This library does NOT allow you to: | ||
@@ -159,0 +164,0 @@ |
+7
-0
@@ -57,2 +57,9 @@ export interface Interval<T = number> { | ||
| /** | ||
| * Does an array-level search inside the interval. Will fail in case of an infinite interval without end param. | ||
| * Will return all elements matching. | ||
| * @param compare Can either be a function, which returns a match, or an item of interval | ||
| * @param end at which element should the search stop? | ||
| */ | ||
| all: (compare: ((item: T) => boolean) | T, end?: T) => T[]; | ||
| /** | ||
| * Substracts interval by limits. Returns null if interval is inside the diffing one | ||
@@ -59,0 +66,0 @@ */ |
30461
4.88%585
5.03%173
2.98%