|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +import { |
| 20 | + rebaseToPercentChange, |
| 21 | + rebaseSeriesData, |
| 22 | + snapToNearestX, |
| 23 | +} from '../../src/Timeseries/percentChange'; |
| 24 | + |
| 25 | +test('rebases each column to percent change from its first value', () => { |
| 26 | + const rebased = rebaseToPercentChange( |
| 27 | + [ |
| 28 | + { __timestamp: 1, a: 100, b: 50 }, |
| 29 | + { __timestamp: 2, a: 150, b: 25 }, |
| 30 | + { __timestamp: 3, a: 200, b: null }, |
| 31 | + ], |
| 32 | + '__timestamp', |
| 33 | + ); |
| 34 | + expect(rebased).toEqual([ |
| 35 | + { __timestamp: 1, a: 0, b: 0 }, |
| 36 | + { __timestamp: 2, a: 0.5, b: -0.5 }, |
| 37 | + { __timestamp: 3, a: 1, b: null }, |
| 38 | + ]); |
| 39 | +}); |
| 40 | + |
| 41 | +test('skips leading nulls when picking the baseline', () => { |
| 42 | + const rebased = rebaseToPercentChange( |
| 43 | + [ |
| 44 | + { __timestamp: 1, a: null }, |
| 45 | + { __timestamp: 2, a: 40 }, |
| 46 | + { __timestamp: 3, a: 60 }, |
| 47 | + ], |
| 48 | + '__timestamp', |
| 49 | + ); |
| 50 | + expect(rebased[1].a).toBe(0); |
| 51 | + expect(rebased[2].a).toBeCloseTo(0.5); |
| 52 | +}); |
| 53 | + |
| 54 | +test('yields nulls when the baseline is zero', () => { |
| 55 | + const rebased = rebaseToPercentChange( |
| 56 | + [ |
| 57 | + { __timestamp: 1, a: 0 }, |
| 58 | + { __timestamp: 2, a: 10 }, |
| 59 | + ], |
| 60 | + '__timestamp', |
| 61 | + ); |
| 62 | + expect(rebased.map(r => r.a)).toEqual([null, null]); |
| 63 | +}); |
| 64 | + |
| 65 | +test('re-indexing displayed data matches rebasing raw data to that point', () => { |
| 66 | + // raw 100, 150, 200 rebased to first point: 0, 0.5, 1 |
| 67 | + const displayed: [number, number][] = [ |
| 68 | + [1, 0], |
| 69 | + [2, 0.5], |
| 70 | + [3, 1], |
| 71 | + ]; |
| 72 | + // re-index to x=2 (raw 150): 100/150-1, 0, 200/150-1 |
| 73 | + const reindexed = rebaseSeriesData(displayed, 2); |
| 74 | + expect(reindexed[0][1]).toBeCloseTo(100 / 150 - 1); |
| 75 | + expect(reindexed[1][1]).toBeCloseTo(0); |
| 76 | + expect(reindexed[2][1]).toBeCloseTo(200 / 150 - 1); |
| 77 | +}); |
| 78 | + |
| 79 | +test('re-indexing to the current baseline is a no-op', () => { |
| 80 | + const displayed: [number, number][] = [ |
| 81 | + [1, 0], |
| 82 | + [2, 0.5], |
| 83 | + ]; |
| 84 | + expect(rebaseSeriesData(displayed, 1)).toEqual(displayed); |
| 85 | +}); |
| 86 | + |
| 87 | +test('snaps to the nearest available x', () => { |
| 88 | + expect(snapToNearestX([10, 20, 30], 24)).toBe(20); |
| 89 | + expect(snapToNearestX([10, 20, 30], 26)).toBe(30); |
| 90 | + expect(snapToNearestX([], 5)).toBeUndefined(); |
| 91 | +}); |
0 commit comments