import { expect } from "chai";
import {
convertArray,
firstUpperCase,
matchStr,
sleep,
compose,
randomRange,
} from "../src/index";
describe("convertArray", function () {
it("二维数组转换为一维数组 [ [{a: 1}], [{a: 2}] to [{a: 1}, {a: 2}]", function () {
const result = convertArray([[{ a: 1 }], [{ a: 2 }]]);
expect([{ a: 1 }, { a: 2 }]).to.deep.equal(result);
});
it("二维数组转换为一维数组 [{a:1}] to [{a:1}]", function () {
const result = convertArray([{ a: 1 }]);
expect([{ a: 1 }]).to.deep.equal(result);
});
});
describe("firstUpperCase", function () {
it("首字母大写 xiaotian to Xiaotian", function () {
const result = firstUpperCase("xiaotian");
expect("Xiaotian").to.equal(result);
});
});
describe("matchStr", function () {
it(" match 不包含 AB 的字符 --aaaaaaaa!! to aaaaaaaa", function () {
const result = matchStr("--aaaaaaaa!!", "--", "!!");
const resultStr = result?.length ? result[0] : "";
expect("aaaaaaaa").to.equal(resultStr);
});
});
describe("sleep", function () {
it("sleep Xms", async function () {
console.log("sleep", Date.now());
const result = await sleep(1);
console.log("sleep", Date.now());
expect("sleep").to.equal("sleep");
});
});
describe("compose", function () {
it("compose 0 to 3", function () {
const add = (x: number) => ++x;
const result = compose(add, add, add)(0);
expect(3).to.equal(result);
});
it("compose 0 to 1", function () {
const add = (x: number) => ++x;
const result = compose(add)(0);
expect(1).to.equal(result);
});
it("compose 0 to 0", function () {
const result = compose()(0);
expect(0).to.equal(result);
});
});
describe("randomRange", () => {
it("random 1 - 5", () => {
const result = randomRange(1, 5);
expect([1, 2, 3, 4, 5]).to.include(result);
});
});