]> git.ipfire.org Git - thirdparty/Chart.js.git/commitdiff
Fix filterBetween (#7211)
authorBen McCann <322311+benmccann@users.noreply.github.com>
Tue, 24 Mar 2020 16:49:48 +0000 (09:49 -0700)
committerGitHub <noreply@github.com>
Tue, 24 Mar 2020 16:49:48 +0000 (12:49 -0400)
* Fix filterBetween
* Update with review suggestions

src/helpers/helpers.collection.js
src/scales/scale.time.js
test/specs/helpers.collection.tests.js

index 06ad935e4cf90971072ece5524e56880b86af9ed..24b0dd5b7efc17cb34f7b5075edd2803377c3c8a 100644 (file)
@@ -68,3 +68,26 @@ export function _rlookupByKey(table, key, value) {
 
        return {lo, hi};
 }
+
+/**
+ * Return subset of `values` between `min` and `max` inclusive.
+ * Values are assumed to be in sorted order.
+ * @param {number[]} values - sorted array of values
+ * @param {number} min - min value
+ * @param {number} max - max value
+ */
+export function _filterBetween(values, min, max) {
+       let start = 0;
+       let end = values.length;
+
+       while (start < end && values[start] < min) {
+               start++;
+       }
+       while (end > start && values[end - 1] > max) {
+               end--;
+       }
+
+       return start > 0 || end < values.length
+               ? values.slice(start, end)
+               : values;
+}
index 08297e0c3a8d6d7edb9b43c8e35c6ddd9c914141..054d76d95cdaef60d50437c247b07d5457016a88 100644 (file)
@@ -3,7 +3,7 @@ import defaults from '../core/core.defaults';
 import {isFinite, isNullOrUndef, mergeIf, valueOrDefault} from '../helpers/helpers.core';
 import {toRadians} from '../helpers/helpers.math';
 import Scale from '../core/core.scale';
-import {_lookup, _lookupByKey} from '../helpers/helpers.collection';
+import {_filterBetween, _lookup, _lookupByKey} from '../helpers/helpers.collection';
 
 /**
  * @typedef { import("../core/core.adapters").Unit } Unit
@@ -494,30 +494,6 @@ function getLabelBounds(scale) {
        return {min, max};
 }
 
-/**
- * Return subset of `timestamps` between `min` and `max`.
- * Timestamps are assumend to be in sorted order.
- * @param {number[]} timestamps - array of timestamps
- * @param {number} min - min value (timestamp)
- * @param {number} max - max value (timestamp)
- */
-function filterBetween(timestamps, min, max) {
-       let start = 0;
-       let end = timestamps.length - 1;
-
-       while (start < end && timestamps[start] < min) {
-               start++;
-       }
-       while (end > start && timestamps[end] > max) {
-               end--;
-       }
-       end++; // slice does not include last element
-
-       return start > 0 || end < timestamps.length
-               ? timestamps.slice(start, end)
-               : timestamps;
-}
-
 const defaultConfig = {
        /**
         * Data distribution along the scale:
@@ -699,7 +675,7 @@ export default class TimeScale extends Scale {
                const min = me.min;
                const max = me.max;
 
-               const ticks = filterBetween(timestamps, min, max);
+               const ticks = _filterBetween(timestamps, min, max);
 
                // PRIVATE
                // determineUnitForFormatting relies on the number of ticks so we don't use it when
index ba08bf79609e436cada9f9b055741ba06da08459..75568f8bdfb8e2b61e0ee892b8ec761d97886da5 100644 (file)
@@ -1,6 +1,6 @@
-import {_lookup, _lookupByKey, _rlookupByKey} from '../../src/helpers/helpers.collection';
+import {_filterBetween, _lookup, _lookupByKey, _rlookupByKey} from '../../src/helpers/helpers.collection';
 
-describe('helpers.interpolation', function() {
+describe('helpers.collection', function() {
        it('Should do binary search', function() {
                const data = [0, 2, 6, 9];
                expect(_lookup(data, 0)).toEqual({lo: 0, hi: 1});
@@ -27,4 +27,10 @@ describe('helpers.interpolation', function() {
                expect(_rlookupByKey(data, 'x', 8)).toEqual({lo: 0, hi: 1});
                expect(_rlookupByKey(data, 'x', 10)).toEqual({lo: 0, hi: 1});
        });
+
+       it('Should filter a sorted array', function() {
+               expect(_filterBetween([1, 2, 3, 4, 5, 6, 7, 8, 9], 5, 8)).toEqual([5, 6, 7, 8]);
+               expect(_filterBetween([1], 1, 1)).toEqual([1]);
+               expect(_filterBetween([1583049600000], 1584816327553, 1585680327553)).toEqual([]);
+       });
 });