@jrc03c/js-math-tools
Version:
some math tools for JS
90 lines (82 loc) • 1.72 kB
JavaScript
import { clamp } from "./clamp.mjs"
import { DataFrame, Series } from "./dataframe/index.mjs"
import { expect, test } from "@jrc03c/fake-jest"
import { forEach } from "./for-each.mjs"
import { isEqual } from "./is-equal.mjs"
import { normal } from "./normal.mjs"
test("tests that values can be clamped correctly", () => {
const s = new Series({ hello: normal(100) })
const d = new DataFrame({ foo: normal(100), bar: normal(100) })
const rights = [
[],
[],
[],
[-2.3, 0],
[],
[-Infinity, 0],
[
[],
[],
],
[
[
[],
[-5, 6, -7],
],
[
[],
[],
],
],
[],
[-234n, 0n],
[
[],
[],
],
[],
[],
]
forEach(rights, pair => {
expect(isEqual(clamp(pair[0], 0, 1), pair[1])).toBe(true)
})
const wrongs = [
NaN,
"foo",
true,
false,
null,
undefined,
Symbol.for("Hello, world!"),
x => x,
function (x) {
return x
},
{ hello: "world" },
]
forEach(wrongs, v => {
expect(clamp(v, 0, 1)).toBeNaN()
})
expect(
isEqual(
clamp(new Series(["a", "b", "c"]), 0, 1),
new Series([NaN, NaN, NaN]),
),
).toBe(true)
expect(
isEqual(
clamp(
new DataFrame([
["a", "d"],
["b", "e"],
["c", "f"],
]),
),
new DataFrame([
[],
[],
[],
]),
),
).toBe(true)
})