interval-management
Advanced tools
@@ -159,2 +159,5 @@ "use strict"; | ||
| } | ||
| if (interval.done()) { | ||
| return []; | ||
| } | ||
| var aggregate = []; | ||
@@ -376,2 +379,31 @@ var copyInterval = interval.copy(); | ||
| }; | ||
| interval.filter = function (by) { | ||
| var prevUsed = interval.usedNext; | ||
| interval.usedNext = function (current) { | ||
| var val = prevUsed(current); | ||
| if (by(val)) { | ||
| return val; | ||
| } | ||
| return interval.usedNext(val); | ||
| }; | ||
| var nextStart = interval.start; | ||
| while (!by(nextStart)) { | ||
| nextStart = prevUsed(nextStart); | ||
| if (less(interval.end, nextStart)) { | ||
| interval.isDone = true; | ||
| return interval; | ||
| } | ||
| } | ||
| interval.start = nextStart; | ||
| return interval; | ||
| }; | ||
| interval.shuffle = function () { | ||
| var acc = []; | ||
| var orig = interval.array(); | ||
| while (orig.length !== 0) { | ||
| var index = Math.floor(Math.random() * (orig.length - 1)); | ||
| acc.push.apply(acc, orig.splice(index, 1)); | ||
| } | ||
| return acc; | ||
| }; | ||
| return interval; | ||
@@ -378,0 +410,0 @@ }; |
+6
-2
| { | ||
| "name": "interval-management", | ||
| "version": "2.3.0", | ||
| "version": "2.4.0", | ||
| "description": "No dependency interval management library, able to work with numbers, string, Dates or special objects", | ||
@@ -10,3 +10,7 @@ "main": "index.js", | ||
| "man": ["./README.md"], | ||
| "keywords": ["interval", "intervals", "management", "library", "number", "string", "Date", "utility"], | ||
| "keywords": ["interval", "intervals", "management", "library", | ||
| "number", "string", "Date", "utility", | ||
| "range", "numbers", "compare", "util", "utils", "utilities", | ||
| "no dependency", "template", "typescript", "customizable", | ||
| "simple", "between"], | ||
| "dependencies": { | ||
@@ -13,0 +17,0 @@ }, |
+9
-0
@@ -195,2 +195,11 @@ # Intervals documentation | ||
| ### Changes since 2.4.0 | ||
| Added .filter and .shuffle functions. Filter will work with infinite intervals, provided that at least one of the values will match to criteria. | ||
| Shuffle will only work with finite intervals and it will generate values from interval in randomized order. | ||
| Fixed a bug in .array() function where it returns a value even after .done() evaluates as true. | ||
| This library does NOT allow you to: | ||
@@ -197,0 +206,0 @@ |
+13
-0
@@ -96,2 +96,15 @@ export interface Interval<T = number> { | ||
| /** | ||
| * Filters values in array by a selector. Will work on infinite intervals. | ||
| * When working with infinite interval, | ||
| * it is possible that this will cause an infinite loop, | ||
| * under these conditions: | ||
| * interval.start is NOT evaluated as true by the parameter AND | ||
| * NO other value inside this infinite interval will evaluate as true | ||
| */ | ||
| filter: (by: (item: T) => boolean) => Interval<T>; | ||
| /** | ||
| * Returns an array of values from interval in random order. Will fail on infinite intervals | ||
| */ | ||
| shuffle: () => T[]; | ||
| /** | ||
| * Splits interval into several smaller ones. Will fail on infinite intervals. | ||
@@ -98,0 +111,0 @@ * @param by When this function returns false, an interval with 'current' as its end will be created. |
36914
6.41%687
7.01%215
4.37%