2021-04-23 04:26:48 +02:00
|
|
|
/*
|
|
|
|
Copyright 2021 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2021-06-29 14:11:58 +02:00
|
|
|
import { clamp, defaultNumber, percentageOf, percentageWithin, sum } from "../../src/utils/numbers";
|
2021-04-23 04:26:48 +02:00
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("numbers", () => {
|
|
|
|
describe("defaultNumber", () => {
|
|
|
|
it("should use the default when the input is not a number", () => {
|
2021-04-23 04:26:48 +02:00
|
|
|
const def = 42;
|
|
|
|
|
|
|
|
let result = defaultNumber(null, def);
|
|
|
|
expect(result).toBe(def);
|
|
|
|
|
|
|
|
result = defaultNumber(undefined, def);
|
|
|
|
expect(result).toBe(def);
|
|
|
|
|
|
|
|
result = defaultNumber(Number.NaN, def);
|
|
|
|
expect(result).toBe(def);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should use the number when it is a number", () => {
|
2021-04-23 04:26:48 +02:00
|
|
|
const input = 24;
|
|
|
|
const def = 42;
|
|
|
|
const result = defaultNumber(input, def);
|
|
|
|
expect(result).toBe(input);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("clamp", () => {
|
|
|
|
it("should clamp high numbers", () => {
|
2021-04-23 04:26:48 +02:00
|
|
|
const input = 101;
|
|
|
|
const min = 0;
|
|
|
|
const max = 100;
|
|
|
|
const result = clamp(input, min, max);
|
|
|
|
expect(result).toBe(max);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should clamp low numbers", () => {
|
2021-04-23 04:26:48 +02:00
|
|
|
const input = -1;
|
|
|
|
const min = 0;
|
|
|
|
const max = 100;
|
|
|
|
const result = clamp(input, min, max);
|
|
|
|
expect(result).toBe(min);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should not clamp numbers in range", () => {
|
2021-04-23 04:26:48 +02:00
|
|
|
const input = 50;
|
|
|
|
const min = 0;
|
|
|
|
const max = 100;
|
|
|
|
const result = clamp(input, min, max);
|
|
|
|
expect(result).toBe(input);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should clamp floats", () => {
|
|
|
|
const min = -0.1;
|
|
|
|
const max = +0.1;
|
2021-04-23 04:26:48 +02:00
|
|
|
|
|
|
|
let result = clamp(-1.2, min, max);
|
|
|
|
expect(result).toBe(min);
|
|
|
|
|
|
|
|
result = clamp(1.2, min, max);
|
|
|
|
expect(result).toBe(max);
|
|
|
|
|
|
|
|
result = clamp(0.02, min, max);
|
|
|
|
expect(result).toBe(0.02);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("sum", () => {
|
|
|
|
it("should sum", () => {
|
|
|
|
// duh
|
2021-04-23 04:26:48 +02:00
|
|
|
const result = sum(1, 2, 1, 4);
|
|
|
|
expect(result).toBe(8);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("percentageWithin", () => {
|
|
|
|
it("should work within 0-100", () => {
|
2021-04-23 04:26:48 +02:00
|
|
|
const result = percentageWithin(0.4, 0, 100);
|
|
|
|
expect(result).toBe(40);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should work within 0-100 when pct > 1", () => {
|
2021-04-23 04:26:48 +02:00
|
|
|
const result = percentageWithin(1.4, 0, 100);
|
|
|
|
expect(result).toBe(140);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should work within 0-100 when pct < 0", () => {
|
2021-04-23 04:26:48 +02:00
|
|
|
const result = percentageWithin(-1.4, 0, 100);
|
|
|
|
expect(result).toBe(-140);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should work with ranges other than 0-100", () => {
|
2021-04-23 04:26:48 +02:00
|
|
|
const result = percentageWithin(0.4, 10, 20);
|
|
|
|
expect(result).toBe(14);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should work with ranges other than 0-100 when pct > 1", () => {
|
2021-04-23 04:26:48 +02:00
|
|
|
const result = percentageWithin(1.4, 10, 20);
|
|
|
|
expect(result).toBe(24);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should work with ranges other than 0-100 when pct < 0", () => {
|
2021-04-23 04:26:48 +02:00
|
|
|
const result = percentageWithin(-1.4, 10, 20);
|
|
|
|
expect(result).toBe(-4);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should work with floats", () => {
|
2021-04-23 04:26:48 +02:00
|
|
|
const result = percentageWithin(0.4, 10.2, 20.4);
|
|
|
|
expect(result).toBe(14.28);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// These are the inverse of percentageWithin
|
2022-12-12 12:24:14 +01:00
|
|
|
describe("percentageOf", () => {
|
|
|
|
it("should work within 0-100", () => {
|
2021-04-23 04:26:48 +02:00
|
|
|
const result = percentageOf(40, 0, 100);
|
|
|
|
expect(result).toBe(0.4);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should work within 0-100 when val > 100", () => {
|
2021-04-23 04:26:48 +02:00
|
|
|
const result = percentageOf(140, 0, 100);
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(result).toBe(1.4);
|
2021-04-23 04:26:48 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should work within 0-100 when val < 0", () => {
|
2021-04-23 04:26:48 +02:00
|
|
|
const result = percentageOf(-140, 0, 100);
|
2022-12-12 12:24:14 +01:00
|
|
|
expect(result).toBe(-1.4);
|
2021-04-23 04:26:48 +02:00
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should work with ranges other than 0-100", () => {
|
2021-04-23 04:26:48 +02:00
|
|
|
const result = percentageOf(14, 10, 20);
|
|
|
|
expect(result).toBe(0.4);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should work with ranges other than 0-100 when val > 100", () => {
|
2021-04-23 04:26:48 +02:00
|
|
|
const result = percentageOf(24, 10, 20);
|
|
|
|
expect(result).toBe(1.4);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should work with ranges other than 0-100 when val < 0", () => {
|
2021-04-23 04:26:48 +02:00
|
|
|
const result = percentageOf(-4, 10, 20);
|
|
|
|
expect(result).toBe(-1.4);
|
|
|
|
});
|
|
|
|
|
2022-12-12 12:24:14 +01:00
|
|
|
it("should work with floats", () => {
|
2021-04-23 04:26:48 +02:00
|
|
|
const result = percentageOf(14.28, 10.2, 20.4);
|
|
|
|
expect(result).toBe(0.4);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|