Files
MartinandMarco Pasqualetti d476f8cbe1 extract "core" package, small improvements (#137)
This PR mainly refactors the code by extracting a "core" package which
is then used by the "backend" and "frontend" apps.

Apart from this the PR also contains several smaller changes like fixing
dependabot, upgrading dependencies, aligned "package.json" files, added
tests, ...

addresses step 1 of #32 
closes #136

---------

Co-authored-by: Marco Pasqualetti <marco.pasqualetti@live.com>
2026-04-18 10:37:26 +02:00

91 lines
2.9 KiB
JavaScript

import { describe, expect, it } from "vitest";
import {
chunkArray,
clampValue,
dateDiff,
lowercaseTrim,
parseArray,
parseBoolean,
parseEmojis,
} from "../src/common/ops.js";
describe("Test ops.js", () => {
it("should test parseBoolean", () => {
expect(parseBoolean(true)).toBe(true);
expect(parseBoolean(false)).toBe(false);
expect(parseBoolean("true")).toBe(true);
expect(parseBoolean("false")).toBe(false);
expect(parseBoolean("True")).toBe(true);
expect(parseBoolean("False")).toBe(false);
expect(parseBoolean("TRUE")).toBe(true);
expect(parseBoolean("FALSE")).toBe(false);
expect(parseBoolean("1")).toBe(undefined);
expect(parseBoolean("0")).toBe(undefined);
expect(parseBoolean("")).toBe(undefined);
// @ts-ignore
expect(parseBoolean(undefined)).toBe(undefined);
});
it("should test parseArray", () => {
expect(parseArray("a,b,c")).toEqual(["a", "b", "c"]);
expect(parseArray("a, b, c")).toEqual(["a", " b", " c"]); // preserves spaces
expect(parseArray("")).toEqual([]);
// @ts-ignore
expect(parseArray(undefined)).toEqual([]);
});
it("should test clampValue", () => {
expect(clampValue(5, 1, 10)).toBe(5);
expect(clampValue(0, 1, 10)).toBe(1);
expect(clampValue(15, 1, 10)).toBe(10);
// string inputs are coerced numerically by Math.min/Math.max
// @ts-ignore
expect(clampValue("7", 1, 10)).toBe(7);
// non-numeric and NaN fall back to min
// @ts-ignore
expect(clampValue("abc", 1, 10)).toBe(1);
expect(clampValue(NaN, 2, 5)).toBe(2);
});
it("should test lowercaseTrim", () => {
expect(lowercaseTrim(" Hello World ")).toBe("hello world");
expect(lowercaseTrim("already lower")).toBe("already lower");
});
it("should test chunkArray", () => {
expect(chunkArray([1, 2, 3, 4, 5], 2)).toEqual([[1, 2], [3, 4], [5]]);
expect(chunkArray([1, 2, 3, 4, 5], 1)).toEqual([[1], [2], [3], [4], [5]]);
expect(chunkArray([1, 2, 3, 4, 5], 10)).toEqual([[1, 2, 3, 4, 5]]);
});
it("should test parseEmojis", () => {
// unknown emoji name is stripped
expect(parseEmojis("Hello :nonexistent:")).toBe("Hello ");
// common emoji names should be replaced (at least token removed)
const out = parseEmojis("I :heart: OSS");
expect(out).not.toContain(":heart:");
expect(out.startsWith("I ")).toBe(true);
expect(out.endsWith(" OSS")).toBe(true);
expect(() => parseEmojis("")).toThrow(/parseEmoji/);
// @ts-ignore
expect(() => parseEmojis()).toThrow(/parseEmoji/);
});
it("should test dateDiff", () => {
const a = new Date("2020-01-01T00:10:00Z");
const b = new Date("2020-01-01T00:00:00Z");
expect(dateDiff(a, b)).toBe(10);
const c = new Date("2020-01-01T00:00:00Z");
const d = new Date("2020-01-01T00:10:30Z");
// rounds to nearest minute
expect(dateDiff(c, d)).toBe(-10);
});
});