diff --git a/__test__/array.test.ts b/__test__/array.test.ts deleted file mode 100644 index 5fe0c80..0000000 --- a/__test__/array.test.ts +++ /dev/null @@ -1,290 +0,0 @@ -import * as arr from "../src/array"; - -test("forEach", () => { - const arr1: any[] = [1, 2, 3]; - arr.forEach((v, k) => arr1[k] = k, arr1); - expect(arr1).toEqual([0, 1, 2]); - // ArrayLike - arr.forEach((v, k) => arr1[k] = k + k, {0: 1, 1: 2, length: 2}); - expect(arr1).toEqual([0, 2, 2]); - // const arr = thisArg || this; - arr.forEach.call(arr1, (v, k) => arr1[k] = k + 2); - expect(arr1).toEqual([2, 3, 4]); - // if (callbackfn(arr[i], i, arr) === false) break; - arr.forEach((v, k) => { - arr1[k] = k + 1; - return k !== 1; - }, arr1); - expect(arr1).toEqual([1, 2, 4]); - - const arr2: (number | string)[] = [2, 3, 4]; - arr.forEach((v, k) => arr2[k] = "a" + v, arr2); - expect(arr2).toEqual(["a2", "a3", "a4"]); - arr.forEach((v, k) => arr2[k] = "a" + k, arr2); - expect(arr2).toEqual(["a0", "a1", "a2"]); -}); -test("from", () => { - expect(arr.from([1, 2, 3])).toEqual([1, 2, 3]); - // ArrayLike - expect(arr.from({0: 2, 1: 1, length: 2})).toEqual([2, 1]); - // mapFn - expect(arr.from([1, 2, 3], (v, k) => v + "" + k)).toEqual(["10", "21", "32"]); -}); -test("includes", () => { - const list: any[] = ["", undefined, 0, NaN, null]; - expect(arr.includes([1, 2, 3], 10)).toBe(false); - expect(arr.includes(list, NaN)).toBe(true); - expect(arr.includes(list, null)).toBe(true); - expect(arr.includes(list, 0)).toBe(true); - expect(arr.includes(list, undefined)).toBe(true); - expect(arr.includes(list, "")).toBe(true); - expect(arr.includes(list, true)).toBeFalsy(); - expect(arr.includes(list, {})).toBeFalsy(); - expect(arr.includes(list, (item) => !item)).toBe(true); - expect(arr.includes(list, (item) => item === undefined)).toBe(true); - expect(arr.includes.call(list, undefined as any, NaN)).toBe(true); -}); -test("createArray", () => { - expect(arr.createArray({start: 0, end: 2})).toEqual([0, 1]); - expect(arr.createArray({start: 0, len: 2})).toEqual([0, 1]); - expect(arr.createArray({len: 2})).toEqual([0, 1]); - expect(arr.createArray({len: 5, end: 3})).toEqual([0, 1, 2]); - expect(arr.createArray({start: 3, end: 2})).toEqual([]); - expect(arr.createArray({start: 3, len: 2})).toEqual([3, 4]); - expect(arr.createArray({start: 3, len: 5, end: 5})).toEqual([3, 4]); - expect(arr.createArray({start: 3, len: 1, end: 5})).toEqual([3]); - expect(arr.createArray({ - start: 3, - len: 5, - end: 5, - fill(item, index) { - return item + "" + index; - }, - })).toEqual(["30", "41"]); - expect(arr.createArray({start: 3, len: 5, end: 6, fill: 0})).toEqual([0, 0, 0]); -}); -test("filter", () => { - // 未找到 - expect(arr.filter((v, k, arr) => { - return v > 10; - }, [1, 2, 3, 4, 5, 6, 7, 8])).toEqual([]); - // 找到 - expect(arr.filter((v, k, arr) => { - return v < 7 && k > 2 && arr.length === 8; - }, [1, 2, 3, 4, 5, 6, 7, 8])).toEqual([4, 5, 6]); - // call - expect(arr.filter.call([1, 2, 3, 4, 5, 6, 7, 8], (v, k, arr) => { - return (v as number) < 7 && k > 2 && arr.length === 8; - })).toEqual([4, 5, 6]); -}); -test("keys", () => { - // array - expect(arr.keys([1, 2, 3, 4])).toEqual(["0", "1", "2", "3"]); - // object - const keys = arr.keys({a: 1, b: 2}); - const [a, b, c] = keys; - expect(a).toEqual("a"); - expect(b).toEqual("b"); - expect(c).toEqual(undefined); - - expect(arr.keys({a: 1, b: 2})).toEqual(["a", "b"]); - // if (isEmpty(target)) return []; - expect(arr.keys([])).toEqual([]); -}); -test("find", () => { - expect(arr.find((v, k, arr) => { - return v === 3 && k === 2 && arr.length === 4; - }, [1, 2, 3, 4])).toBe(3); - expect(arr.find((v, k, arr) => { - return v === 3 && k === 2 && arr.length === 6; - }, [1, 2, 3, 4])).toBe(undefined); - expect(arr.find((v, k, arr) => { - return v === 3 && k === 2 && arr.length === 6; - }, [])).toBe(undefined); - expect(arr.find((v, k, arr) => { - return v === 3 && k === 2 && arr.length === 6; - })).toBeUndefined(); -}); -test("flat", () => { - expect([1, 2, 3, [1, 2, 3, [1, 2, 3]], [1, 2, 3, [1, 2, 3]]].flat(1)).toEqual([1, 2, 3, 1, 2, 3, [1, 2, 3], 1, 2, 3, [1, 2, 3]]); - - const list = [1, 2, 3, [1, 2, 3, [1, 2, 3]], [1, 2, 3, [1, 2, 3]]]; - - // Array.property.flag(depth=1) - expect(list.flat()).toEqual(list.flat(1)); - expect(arr.flat(list, 1)).toEqual(list.flat()); - expect(arr.flat(list, 1)).toEqual(list.flat(1)); - expect(arr.flat(list, 2)).toEqual(list.flat(2)); - expect(arr.flat(list, 3)).toEqual(list.flat(3)); - - // flat all - expect(arr.flat([1, 2, 3], -1)).toEqual([1, 2, 3]); - expect(arr.flat([1, 2, 3, [1, 2, 3]], -1)).toEqual([1, 2, 3, 1, 2, 3]); - expect(arr.flat([1, 2, 3, [1, 2, 3], [1, 2, 3]], -1)).toEqual([1, 2, 3, 1, 2, 3, 1, 2, 3]); - expect(arr.flat([1, 2, 3, [1, 2, 3, [1, 2, 3]], [1, 2, 3]], -1)).toEqual([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]); - expect(arr.flat([1, 2, 3, [1, 2, 3, [1, 2, 3]], [1, 2, 3, [1, 2, 3]]], -1)).toEqual([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]); - -}); - -test("binaryFind", () => { - const list: { id: number }[] = [...Array(100).keys()].map(i => ({id: i * 2})); - - function find(target: number): { times: number, value: ReturnType } { - let times = 0; - const value = arr.binaryFind(list, function (item, index) { - times++; - // console.log(index); - // 判断index是否正确 - expect(list[index]).toBe(item); - return target - item.id; - }); - return {times, value}; - } - - let res = find(58); - expect(res.times).toBe(5); - expect(res.value).toEqual({id: 58}); - - console.log("----min-----"); - // 判断边缘 min - const first = list[0]; - res = find(first.id); - expect(res.times).toBe(7); - expect(res.value).toEqual(first); - - console.log("----max-----"); - // 判断边缘 max - const maxIndex = list.length - 1; - const last = list[maxIndex]; - res = find(last.id); - expect(res.times).toBe(6); - expect(res.value).toEqual(last); - - // cover - expect(arr.binaryFind([1], i => i)).toBeUndefined(); - expect(find(55).value).toBeUndefined(); - expect(find(400).value).toBeUndefined(); - expect(arr.binaryFind([], i => i)).toBeUndefined(); -}); -test("binaryFind2", () => { - const list: { id: number }[] = [...Array(100).keys()].map(i => ({id: i * 2})); - - function find(target: number): { times: number, value: ReturnType } { - let times = 0; - const value = arr.binaryFind2(list, function (item, index) { - times++; - // console.log(index); - // 判断index是否正确 - expect(list[index]).toBe(item); - return target - item.id; - }); - return {times, value}; - } - - let res = find(58); - expect(res.times).toBe(5); - expect(res.value).toEqual({id: 58}); - - console.log("----min-----"); - // 判断边缘 min - const first = list[0]; - res = find(first.id); - expect(res.times).toBe(7); - expect(res.value).toEqual(first); - - console.log("----max-----"); - // 判断边缘 max - const maxIndex = list.length - 1; - const last = list[maxIndex]; - res = find(last.id); - expect(res.times).toBe(6); - expect(res.value).toEqual(last); - - // cover - expect(arr.binaryFind2([1], i => i)).toBeUndefined(); - expect(find(55).value).toBeUndefined(); - expect(find(400).value).toBeUndefined(); - expect(arr.binaryFind2([], i => i)).toBeUndefined(); -}); -test("binaryFindIndex", () => { - console.log("-----binaryFindIndex------"); - - const list: { id: number }[] = [...Array(100).keys()].map(i => ({id: i * 2})); - - function find(target: number): { times: number, index: ReturnType } { - // 查找次数 - let times = 0; - const index = arr.binaryFindIndex(list, function (item, index, start, end) { - times++; - // console.log(index); - // 判断index是否正确 - expect(list[index]).toBe(item); - // 0 <= (start) < end <= list.length - expect(start).toBeGreaterThanOrEqual(0); - expect(start).toBeLessThan(end); - expect(start).toBeLessThan(list.length); - expect(end).toBeLessThanOrEqual(list.length); - - return target - item.id; - }); - return {times, index}; - } - - let res = find(58); - expect(res.times).toBe(5); - expect(res.index).toBe(29); - - console.log("--------min-------"); - // 判断边缘 min - const minIndex = 0; - const first = list[minIndex]; - res = find(first.id); - expect(res.times).toBe(7); - expect(res.index).toBe(minIndex); - - console.log("----max-----"); - // 判断边缘 max - const maxIndex = list.length - 1; - const last = list[maxIndex]; - res = find(last.id); - expect(res.times).toBe(6); - expect(res.index).toBe(maxIndex); - - // cover - expect(arr.binaryFindIndex([1], (item, index, start, end) => { - // console.log(index, start, end); - return 0 - item; - })).toBe(-1); - expect(arr.binaryFindIndex([], i => i)).toBe(-1); - expect(arr.binaryFindIndex(list, i => 55 - i.id)).toBe(-1); -}); -test("insertToArray", () => { - const arr1 = [1, 2, 3, 4]; - const arr2 = arr.insertToArray(5, 1, arr1); - expect(arr2).toEqual([1, 5, 2, 3, 4]); - const arr3 = arr.insertToArray(5, 0, arr1); - expect(arr3).toEqual([5, 1, 2, 3, 4]); - const arr4 = arr.insertToArray(5, 100, arr1); - expect(arr4).toEqual([1, 2, 3, 4, 5]); -}); -test("unique", () => { - const fn = arr.unique; - expect(fn([1, 1, 2, 1, 3, 4, 1, 1, 1, 1, 1])).toEqual([1, 2, 3, 4]); - const a = {value: 1}; - const b = {value: 2}; - const c = {value: 3}; - const d = {value: 2}; - expect(fn([a, b, c, d])).toEqual([a, b, c, d]); - expect( - fn( - [a, b, c, d], - (v1, v2) => v1.value === v2.value, - ), - ).toEqual([a, b, c]); -}); -test("findIndex", () => { - const fn = arr.findIndex; - expect(fn(v => v === 4, [1, 1, 2, 1, 3, 4, 1, 1, 1, 1, 1])).toEqual(5); - expect(fn(v => v.v === 4, [{v: 1}, {v: 2}])).toEqual(-1); - expect(fn(v => v.v === 2, [{v: 1}, {v: 2}])).toEqual(1); -}); diff --git a/__test__/clone.test.ts b/__test__/clone.test.ts deleted file mode 100644 index 9a826d0..0000000 --- a/__test__/clone.test.ts +++ /dev/null @@ -1,138 +0,0 @@ -import * as clone from "../src/clone"; - - -test('deepClone', () => { - const arr = [1, 2, 3]; - const newArr = clone.deepClone(arr); - // copy == arr - expect(newArr).toEqual(arr); - // copy !== arr - expect(arr === newArr).toBeFalsy(); - const obj = {a: [2, 3], c: 1, d: {f: 123}}; - const newObj = clone.deepClone(obj); - // copy == obj - expect(newObj).toEqual(obj); - // copy !== obj - expect(obj === newObj).toBeFalsy(); - // copy.a == obj.a - expect(obj.a).toEqual(newObj.a); - // copy.a !== obj.a - expect(obj.a === newObj.a).toBeFalsy(); - // 0 === 0 - expect(clone.deepClone(0)).toBe(0); - - const arr2 = [ - () => 100, - () => 200, - ]; - const newArr2 = clone.deepClone(arr2); - // copy == arr2 - expect(arr2 == newArr2).toBe(false); - // copy !== arr2 - expect(newArr2 === arr2).toBeFalsy(); - // copy[0] == arr2[0] - expect(newArr2[0] == arr2[0]).toBe(false); - expect(newArr2[1] == arr2[1]).toEqual(false); - // copy[0] === arr2[0] - expect(newArr2[0] !== arr2[0]).toBeTruthy(); - expect(newArr2[1] !== arr2[1]).toBeTruthy(); - // copy[0]() === arr2[0]() - expect(newArr2[1]() === arr2[1]()).toBeTruthy(); - expect(newArr2[1]()).toEqual(arr2[1]()); - - function Foo() { - this.name = 'foo'; - this.sayHi = function () { - console.log('Say Hi'); - }; - } - - Foo.prototype.sayGoodBy = function () { - console.log('Say Good By'); - }; - let myPro = new Foo(); - expect(myPro.hasOwnProperty('name')).toBeTruthy();//true - expect(myPro.hasOwnProperty('toString')).toBeFalsy();//false - expect(myPro.hasOwnProperty('hasOwnProperty')).toBeFalsy();//fasle - expect(myPro.hasOwnProperty('sayHi')).toBeTruthy();// true - expect(myPro.hasOwnProperty('sayGoodBy')).toBeFalsy();//false - expect('sayGoodBy' in myPro).toBeTruthy();// true - - // test cov if (!(target as any).hasOwnProperty(k)) continue; - const copyFoo = clone.deepClone(myPro); - expect(copyFoo.hasOwnProperty("sayGoodBy")).toBeFalsy(); - - // copy function - function fn(arg: number) { - return arg + fn.data; - } - - fn.data = 100; - - const nFn = clone.deepClone(fn); - - expect(fn(100)).toBe(200); - expect(fn === nFn).toBe(false); - expect(nFn(100)).toBe(200); - expect(nFn.data).toBe(100); - nFn.data = 200; - expect(nFn.data).toBe(200); - expect(fn.data).toBe(100); - - // copy date - const date = new Date("2020-06-05 12:00:00"); - const o = clone.deepClone({date}); - expect(o.date.getFullYear()).toBe(date.getFullYear()); - expect(o.date === date).toBe(false); - - // copy regExp - const re = new RegExp("\\d+", "g"); - const o2 = clone.deepClone({re}); - expect(o2.re.test("123")).toBe(true); - expect(o2.re === re).toBe(false); -}); - -test("cloneFunction", () => { - function test(a, b) { - return a + b; - } - - expect(clone.cloneFunction(test)(50, 50)).toBe(100); - - const test2 = (a, b) => a + b; - expect(clone.cloneFunction(test2)(50, 50)).toBe(100); - expect((function (a, b) { - return a + b; - })(50, 50)).toBe(100); -}); -test("deepCloneBfs", () => { - const obj10086 = {a: 1, b: 2, c: 3, d: 4}; - const nObj = clone.deepCloneBfs(obj10086); - expect(obj10086).toEqual(nObj); - expect(nObj.c).toEqual(3); - expect(nObj === obj10086).toBe(false); - - const obj10000 = {a: 1, b: {c: "123"}}; - const nObj2 = clone.deepCloneBfs(obj10000); - expect(nObj2).toEqual(obj10000); - - const arr = [1, 2, 3]; - const newArr = clone.deepCloneBfs(arr); - // copy == arr - expect(newArr).toEqual(arr); - // copy !== arr - expect(arr === newArr).toBeFalsy(); - const obj = {a: [2, 3], c: 1, d: {f: 123}}; - const newObj = clone.deepCloneBfs(obj); - // copy == obj - expect(newObj).toEqual(obj); - // copy !== obj - expect(obj === newObj).toBeFalsy(); - // copy.a == obj.a - expect(obj.a).toEqual(newObj.a); - // copy.a !== obj.a - expect(obj.a === newObj.a).toBeFalsy(); - // 0 === 0 - expect(clone.deepCloneBfs(0)).toBe(0); - -}); \ No newline at end of file diff --git a/__test__/common.test.ts b/__test__/common.test.ts deleted file mode 100644 index 8a97ab6..0000000 --- a/__test__/common.test.ts +++ /dev/null @@ -1,578 +0,0 @@ -import * as cm from "../src/common"; - -test("forEachByLen", () => { - const arr: number[] = []; - cm.forEachByLen(3, (index) => arr.push(index)); - expect(arr).toEqual([0, 1, 2]); - cm.forEachByLen(7, (index) => arr.push(index)); - expect(arr.length).toEqual(10); - cm.forEachByLen(3, (index): any | false => { - arr.push(index); - if (index === 1) return false; - }); -}); - -test("typeOf", () => { - // 六大基本类型 string boolean number object null undefined - expect(cm.typeOf("")).toBe("string"); - expect(cm.typeOf(true)).toBe("boolean"); - expect(cm.typeOf(0)).toBe("number"); - expect(cm.typeOf(undefined)).toBe("undefined"); - expect(cm.typeOf({})).toBe("object"); - expect(cm.typeOf(null)).toBe("null"); - // 非6 - expect(cm.typeOf(() => { - })).toBe("function"); - expect(cm.typeOf([])).toBe("array"); - expect(cm.typeOf(NaN)).toBe("number"); - expect(cm.typeOf(/abc/)).toBe("regexp"); -}); - -test("randomNumber", () => { - const rand = cm.randomNumber(0, 10); - expect(rand).toBeGreaterThanOrEqual(0); - expect(rand).toBeLessThanOrEqual(10); - // - const rand2 = cm.randomNumber(); - expect(rand2).toBeGreaterThanOrEqual(0); - expect(rand2).toBeLessThanOrEqual(1); - // start as end - const rand3 = cm.randomNumber(5); - expect(rand3).toBeGreaterThanOrEqual(0); - expect(rand3).toBeLessThanOrEqual(5); - // arr - const randArr: number[] = cm.randomNumber(0, 5, 4) as number[]; - expect(randArr.length).toBe(4); - for (let i = 0; i < randArr.length; i++) { - const item = randArr[i]; - expect(item).toBeGreaterThanOrEqual(0); - expect(item).toBeLessThanOrEqual(5); - } - const randArr2: number[] = cm.randomNumber(0, 5, 0); - expect(randArr2).toEqual([]); - - const arr3 = cm.randomNumber(0, 1, 500); - expect(arr3.length).toBe(500); - expect(arr3.some(i => i > 0)).toBeTruthy(); - expect(arr3.some(i => i < 1)).toBeTruthy(); - expect(arr3.some(i => i > 0.3 && i < 0.5)).toBeTruthy(); - expect(arr3.some(i => i >= 0 && i < 0.2)).toBeTruthy(); - expect(arr3.some(i => i > 0.7 && i < 0.9)).toBeTruthy(); - expect(arr3.some(i => i >= 1)).toBeFalsy(); - expect(arr3.some(i => i < 0)).toBeFalsy(); - - const arr4 = cm.randomNumber(-10, 10, 200); - expect(arr4.some(i => i > -10)).toBeTruthy(); - expect(arr4.some(i => i < 10)).toBeTruthy(); - expect(arr4.some(i => i > 5 && i < 6)).toBeTruthy(); - expect(arr4.some(i => i > -6 && i < -1)).toBeTruthy(); - expect(arr4.some(i => i > -5 && i < 5)).toBeTruthy(); - expect(arr4.some(i => i > 10)).toBeFalsy(); - expect(arr4.some(i => i < -10)).toBeFalsy(); - - const arr5 = cm.randomNumber(10, 11, 520); - expect(arr5.length).toBe(520); - expect(arr5.some(i => i < 10)).toBeFalsy(); - expect(arr5.some(i => i < 10.1)).toBeTruthy(); - - const arr6 = cm.randomNumber(0.2, 0.4, 300); - expect(arr6.length).toBe(300); - expect(arr6.some(i => i < 0.2)).toBeFalsy(); - expect(arr6.some(i => i < 0.4)).toBeTruthy(); - expect(arr6.some(i => i >= 0.4)).toBeFalsy(); - expect(arr3.some(i => i > 0.3 && i < 0.4)).toBeTruthy(); -}); -test("strPadStart", () => { - expect(cm.strPadStart("123", 6, "0")).toBe("000123"); - expect(cm.strPadStart("123", 0, "0")).toBe("123"); - expect(cm.strPadStart("123", 4, "hello")).toBe("h123"); - expect(cm.strPadStart("123", 20, "hello")).toBe("hellohellohellohe123"); - expect(cm.strPadStart("123", -1, "0")).toBe("123"); - expect(cm.strPadStart("0", 2, "0")).toBe("00"); -}); -test("strPadEnd", () => { - expect(cm.strPadEnd("123", 6, "0")).toBe("123000"); - expect(cm.strPadEnd("123", 0, "0")).toBe("123"); - expect(cm.strPadEnd("123", 4, "hello")).toBe("123h"); - expect(cm.strPadEnd("123", 20, "hello")).toBe("123hellohellohellohe"); - expect(cm.strPadEnd("123", -1, "0")).toBe("123"); -}); -test("randomColor", () => { - const reg = /#[0-9a-f]{6}$/; - expect(reg.test(cm.randomColor())).toBeTruthy(); - // array - const arr = cm.randomColor(10); - expect(arr.length === 10).toBeTruthy(); - cm.forEachByLen(arr.length, (i) => { - expect(reg.test(arr[i])).toBeTruthy(); - }); -}); - -test("thousandFormat", () => { - expect(cm.thousandFormat(123456789)).toBe("123,456,789"); - expect(cm.thousandFormat(123)).toBe("123"); - expect(cm.thousandFormat(5763423)).toBe("5,763,423"); -}); -test("getChineseNumber", () => { - expect(cm.number2Chinese(123)).toBe("一百二十三"); - expect(cm.number2Chinese(1)).toBe("一"); - expect(cm.number2Chinese(11)).toBe("十一"); - expect(cm.number2Chinese(21)).toBe("二十一"); - expect(cm.number2Chinese(101)).toBe("一百零一"); - expect(cm.number2Chinese(111)).toBe("一百一十一"); - expect(cm.number2Chinese(1001)).toBe("一千零一"); - expect(cm.number2Chinese(12345)).toBe("一万二千三百四十五"); - expect(cm.number2Chinese(23456789)).toBe("二千三百四十五万六千七百八十九"); - expect(cm.number2Chinese(123456789)).toBe("一亿二千三百四十五万六千七百八十九"); -}); -test("getFormatStr", () => { - expect(cm.getFormatStr("hell%s worl%s", "o", "d")).toBe("hello world"); - expect(cm.getFormatStr("hell%s worl%s")).toBe("hell worl"); -}); -test("debounce", (done) => { - let times = 0; - const d = cm.debounce(() => { - times++; - }, 100); - setTimeout(d, 10); - setTimeout(d, 20); - setTimeout(d, 30); - setTimeout(d, 40); - setTimeout(() => { - expect(times).toBe(1); - // 异步代码需要调用done() - done(); - }, 500); -}); - -test("oneByOne", (done) => { - const s = "hello world"; - cm.oneByOne(s, 10, (w, index) => { - expect(w).toBe(s[index]); - if (s.length === index + 1) { - done(); - } - }); - cm.oneByOne(s, 10); -}); -test("generateFunction", () => { - // const args = [1, 2, 3]; - // (new Function(generateFunctionCode(args.length)))(object, property, args); - // expect(cm.strFillPrefix("123", "0", 6)).toBe("000123"); - const value = cm.generateFunction(cm, "strPadStart", ["123", 6, "0"]); - expect(value).toBe("000123"); -}); -test("polling", (done) => { - let t = 0; - const cancel = cm.polling((times) => { - return new Promise((res) => { - expect(times).toBe(t); - t++; - if (times === 10) { - cancel(); - done(); - } - res(); - }); - }, 10, false); -}); - -test("chinese2Number", () => { - expect(cm.chinese2Number("一")).toBe(1); - expect(cm.chinese2Number("十一")).toBe(11); - expect(cm.chinese2Number("九十一")).toBe(91); - expect(cm.chinese2Number("一百九十九")).toBe(199); - expect(cm.chinese2Number("五千一百九十九")).toBe(5199); - expect(cm.chinese2Number("一万零一")).toBe(10001); - expect(cm.chinese2Number("一万零一十")).toBe(10010); - expect(cm.chinese2Number("一万零一十三")).toBe(10013); - expect(cm.chinese2Number("一万零一十三")).toBe(10013); - expect(cm.chinese2Number("十万零一十三")).toBe(100013); - expect(cm.chinese2Number("二百一十万零一十三")).toBe(2100013); - expect(cm.chinese2Number("一千二百一十万零一十三")).toBe(12100013); - expect(cm.chinese2Number("一千二百一十万零一")).toBe(12100001); - expect(cm.chinese2Number("一亿零二百一十万零一十三")).toBe(102100013); - expect(cm.chinese2Number("一亿二千三百四十五万六千七百八十九")).toBe(123456789); - expect(cm.chinese2Number("十一亿零二百一十万零一十三")).toBe(1102100013); - const sbq = ["拾", "佰", "仟"]; - cm.chinese2Number.units = ["", ...sbq, "萬", ...sbq, "亿"]; - cm.chinese2Number.numbers = ["零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"]; - // 千和百未定义 - expect(() => { - cm.chinese2Number("壹亿贰仟叁佰肆拾伍萬陆千柒百捌拾玖"); - }).toThrow(); - expect(cm.chinese2Number("壹亿贰仟叁佰肆拾伍萬陆仟柒佰捌拾玖")).toBe(123456789); -}); -test("getTreeMaxDeep", () => { - expect(cm.getTreeMaxDeep({})).toBe(1); - expect(cm.getTreeMaxDeep({a: 1})).toBe(2); - expect(cm.getTreeMaxDeep(null as any)).toBe(0); - expect(cm.getTreeMaxDeep({a: {b: 1}})).toBe(3); - const obj: any = {a: {a2: 1}, b: 1, c: {c2: {c3: 123, c32: {}}}}; - expect(cm.getTreeMaxDeep(obj)).toBe(4); - expect(cm.getTreeMaxDeep([])).toBe(1); - expect(cm.getTreeMaxDeep([1, 2, 4, 5])).toBe(2); - expect(cm.getTreeMaxDeep([1, 2, 4, 5, {a: 3}])).toBe(3); - - function Fn() { - this.test = {a: 1, b: 2}; - } - - Fn.prototype.c = {a: {b: 2, c: {d: 123}}}; - - const fn = new Fn(); - expect(cm.getTreeMaxDeep(fn)).toBe(3); -}); -test("getTreeNodeLen", () => { - expect(cm.getTreeNodeLen({}, -1)).toBe(0); - expect(cm.getTreeNodeLen({}, 0)).toBe(1); - expect(cm.getTreeNodeLen({})).toBe(0); - expect(cm.getTreeNodeLen({a: 1, b: 2, c: 3})).toBe(3); - expect(cm.getTreeNodeLen({a: 1, b: 2, c: 3, d: {d1: 123, d2: 1, d3: 123}})).toBe(4); - expect(cm.getTreeNodeLen({a: 1, b: {b2: 2, b3: 3}, c: 3, d: {d1: 123, d2: 1, d3: 123}})).toBe(4); - expect(cm.getTreeNodeLen({a: 1, b: {b2: 2, b3: 3}, c: 3, d: {d1: 123, d2: 1, d3: 123}}, 2)).toBe(5); - expect(cm.getTreeNodeLen({a: {a2: 1}, b: {b2: 2}, c: {c2: 3}, d: {d1: 4}}, 2)).toBe(4); - expect(cm.getTreeNodeLen({a: {a2: 1, a3: {a4: 4}}, b: {b2: 2}, c: {c2: 3}, d: {d1: 4}}, 3)).toBe(1); - expect(cm.getTreeNodeLen({ - a: {a2: 1, a3: {a4: 4}}, - b: {b2: 2, b3: {b4: 4}}, - c: {c2: 3}, - d: {d1: 4}, - }, 3)).toBe(2); - expect(cm.getTreeNodeLen([0, 1, [3, 4, 5]], 2)).toBe(3); - expect(cm.getTreeNodeLen([0, 1, {a: 12, b: 1, c: 4}], 2)).toBe(3); - - function Fn() { - this.test = {a: 1, b: 2}; - } - - Fn.prototype.c = {a: {b: 2, c: {d: 123}}}; - - const fn = new Fn(); - expect(cm.getTreeNodeLen(fn, 2)).toBe(2); -}); - -test("merge", () => { - const a = {one: 1, two: 2, three: 3}; - const b = {one: 11, four: 4, five: 5}; - expect(cm.merge(a, b)).toEqual(Object.assign({}, a, b)); - - const c = {...a, test: {a: 1, b: 2, c: 3}}; - expect(cm.merge(c, b)).toEqual(Object.assign({}, c, b)); - expect(cm.merge(c, b).test === c.test && c.test === Object.assign({}, c, b).test).toEqual(true); - - function Fn() { - this.a = 100; - } - - Fn.prototype.b = 200; - const d = new Fn(); - expect(cm.merge(a, d)).toEqual(Object.assign({}, a, d)); - expect(cm.merge(a, d).b).toEqual(undefined); -}); -test("deepMerge", () => { - const a = {one: 1, two: 2, three: 3}; - const b = {one: 11, four: 4, five: 5}; - expect(cm.deepMerge(a, b)).toEqual(Object.assign({}, a, b)); - - const c = {...a, test: {a: 1, b: 2, c: 3}}; - expect(cm.deepMerge(c, b)).toEqual(Object.assign({}, c, b)); - expect(cm.deepMerge(c, b).test.a === 1).toEqual(true); - expect(cm.deepMerge(c, b).test !== c.test && c.test === Object.assign({}, c, b).test).toEqual(true); - - function Fn() { - this.a = 100; - } - - Fn.prototype.b = 200; - const d = new Fn(); - expect(cm.deepMerge(a, d)).toEqual(Object.assign({}, a, d)); - expect(cm.deepMerge(a, d).b).toEqual(undefined); - - expect(cm.deepMerge(a, {a: [{b: 123}]})).toEqual(Object.assign({}, a, {a: [{b: 123}]})); -}); - -test("sleep", async () => { - const date = Date.now(); - await cm.sleep(100); - expect(Date.now() - date).toBeGreaterThan(80); -}); - -test("createUUID", () => { - const uuid = cm.createUUID(10); - - // 判断长度是否正确 - expect(uuid.length === 10).toBeTruthy(); - - const hexDigits = "0123456789abcdef"; - // 判断每个字符是否在范围内 - for (let i = 0; i < uuid.length; i++) { - expect(hexDigits.indexOf(uuid[i]) > -1).toBeTruthy(); - } - - // 判断100次循环中是否有相同的 - for (let i = 0; i < 100; i++) { - const uid = cm.createUUID(10); - expect(uid !== uuid).toBeTruthy(); - } -}); - -test("formatJSON", () => { - const formatJSON = cm.formatJSON; - const str = {a: 1, b: 2}; - expect(formatJSON(str, 4)).toBe(`{\r\n "a":1,\r\n "b":2\r\n}`); - /* const rs = formatJSON({ - ...str, - fn: function () { - }, - }, 4);*/ - // expect(rs).toBe(`{\r\n "a":1,\r\n "b":2,\r\n "fn":"function () {\r\n }"\r\n}`); -}); -const testPickByKeys = (fn: typeof cm.pickByKeys) => { - return () => { - const obj = {a: 1, b: "2", c: ["12313", 111], d: false, e: {a: 1}, f: undefined}; - expect(fn(obj, [])).toEqual({}); - expect(fn(obj, ["a"])).toEqual({a: 1}); - expect(fn(obj, ["b"])).toEqual({b: obj.b}); - expect(fn(obj, ["c"])).toEqual({c: obj.c}); - expect(fn(obj, ["d"])).toEqual({d: obj.d}); - expect(fn(obj, ["e"])).toEqual({e: obj.e}); - expect(fn(obj, ["f"])).toEqual({f: obj.f}); - expect(fn(obj, ["a", "f"])).toEqual({a: obj.a, f: obj.f}); - expect(fn(obj, ["a", "f", "c"])).toEqual({a: obj.a, f: obj.f, c: obj.c}); - - expect(fn(obj, ["a"], (v) => v + 1000)).toEqual({a: 1001}); - expect(fn(obj, ["a", "b"], (v, k) => { - if (k === "a") { - return 2; - } - return "test"; - })).toEqual({a: 2, b: "test"}); - }; -}; -test("pickByKeys", () => { - const fn = cm.pickByKeys; - testPickByKeys(fn)(); -}); -const testPickRename = (fn: typeof cm.pickRename) => { - return () => { - const obj = {a: 1, b: "2", c: ["12313", 111], d: false, e: {a: 1}, f: undefined}; - expect(fn(obj, {})).toEqual({}); - expect(fn(obj, {"A": "a"})).toEqual({A: 1}); - expect(fn(obj, {"B": "b"})).toEqual({B: obj.b}); - expect(fn(obj, {"C": "c"})).toEqual({C: obj.c}); - expect(fn(obj, {"D": "d"})).toEqual({D: obj.d}); - expect(fn(obj, {"E": "e"})).toEqual({E: obj.e}); - expect(fn(obj, {"E": "f"})).toEqual({F: obj.f}); - expect(fn(obj, {"A": "a", "F": "f"})).toEqual({A: obj.a, F: obj.f}); - expect(fn(obj, {"a": "a", "f": "b", "c": "d"})).toEqual({a: obj.a, f: obj.b, c: obj.d}); - - expect(fn(obj, {"A": "a"}, (v) => (v + 1000))).toEqual({A: 1001}); - expect(fn(obj, {"A": "a", "B": "b"}, (v, k) => { - if (k === "a") { - return 2; - } - return "test"; - })).toEqual({A: 2, B: "test"}); - }; -}; -test("pickRename", () => { - const fn = cm.pickRename; - testPickRename(fn)(); -}); -test("pick", () => { - const fn = cm.pick; - // pickByKeys - testPickByKeys(fn)(); - let obj = {a: 1, b: "2", c: ["12313", 111], d: false, e: {a: 1}, f: undefined}; - expect(fn(obj, ["a", "b"], (v, k) => { - if (k === "a") { - return 2; - } - return "test"; - })).toEqual({a: 2, b: "test"}); - - // pickRename - testPickRename(fn)(); - obj = {a: 1, b: "2", c: ["12313", 111], d: false, e: {a: 1}, f: undefined}; - expect(fn(obj, {"A": "a", "B": "b"}, (v, k) => { - if (k === "a") { - return 2; - } - return "test"; - })).toEqual({A: 2, B: "test"}); - const nObj = fn(obj, {"A": "a", "B": "a"}, (v, k) => { - return 2; - }); - expect(nObj).toEqual({A: 2, B: 2}); -}); - -test("promiseAny", async () => { - expect.assertions(12); - const fn = cm.promiseAny; - const isNotIterable = "TypeError: list is not iterable"; - await expect(fn(undefined as any)).rejects.toEqual(isNotIterable); - await expect(fn(null as any)).rejects.toEqual(isNotIterable); - await expect(fn(NaN as any)).rejects.toEqual(isNotIterable); - await expect(fn(0 as any)).rejects.toEqual(isNotIterable); - await expect(fn(true as any)).rejects.toEqual(isNotIterable); - await expect(fn({} as any)).rejects.toEqual(isNotIterable); - - const allReject = "AggregateError: All promises were rejected"; - await expect(fn([])).rejects.toEqual(allReject); - await expect(fn([Promise.reject(0), Promise.reject(1)])).rejects.toEqual(allReject); - await expect(fn([0 as any, Promise.resolve(1)])).resolves.toEqual(0); - await expect(fn([Promise.resolve(1), 0 as any])).resolves.toEqual(0); - await expect(fn([Promise.resolve(0), Promise.resolve(1)])).resolves.toEqual(0); - await expect(fn([Promise.reject(0), Promise.reject(1), Promise.resolve(2)])).resolves.toEqual(2); -}); - -test("debounceAsync", async () => { - expect.assertions(2); - const fn = cm.debounceAsync; - let times = 0; - const cb = () => { - return new Promise(resolve => { - resolve(times++); - }); - }; - - const dbFn = fn(cb, 100); - await cm.promiseAny([dbFn(), dbFn(), dbFn(), dbFn()]); - - expect(times).toEqual(1); - - dbFn(); - await cm.sleep(150); - dbFn(); - await cm.sleep(150); - expect(times).toEqual(3); -}); -test("debounceByPromise", async () => { - expect.assertions(2); - const fn = cm.debounceByPromise; - let times = 0; - const cb = (time = 50) => { - const p = new Promise(resolve => { - setTimeout(() => { - - resolve(time); - }, time); - }); - p.then(() => { - times++; - }); - return p; - }; - - const dbFn = fn(cb); - - await expect(cm.promiseAny([dbFn(40), dbFn(20), dbFn(60), dbFn(30)])).resolves.toEqual(30); - - dbFn(100); - await cm.sleep(150); - dbFn(); - await cm.sleep(150); - // fixme 执行了6次, debounceByPromise无法阻止cb被调用 不推荐使用 - expect(times).toEqual(6); -}); -test("debounceCancelable", async () => { - expect.assertions(2); - const fn = cm.debounceCancelable; - let times = 0; - const cb = () => { - times++; - }; - - const dbFn = fn(cb, 20); - - let cancelFn = dbFn(); - cancelFn(); - - await cm.sleep(30); - - expect(times).toEqual(0); - - dbFn(); - await cm.sleep(150); - expect(times).toEqual(1); -}); -test("getReverseObj", async () => { - const fn = cm.getReverseObj; - - const obj = {a: "aa", b: "bb"}; - expect(fn(obj)).toEqual({aa: "a", bb: "b"}); -}); -test("createEnumByObj", async () => { - const fn = cm.createEnumByObj; - - const obj = {a: "aa", b: "bb"}; - expect(fn(obj)).toEqual({a: "aa", b: "bb", aa: "a", bb: "b"}); - expect(fn({a: 1, b: 2})).toEqual({a: 1, b: 2, 1: "a", 2: "b"}); -}); -test("createEnum", async () => { - const fn = cm.createEnum; - - enum e { - a, - b, - c - } - - expect(fn(["a", "b", "c"])).toEqual(e); -}); -test("forEachObj", () => { - const fn = cm.forEachObj; - - const testFn = (obj: object) => { - let times = 0; - fn(obj, (v, k, o) => { - expect(typeof k).toEqual("string"); - expect(v).toEqual(obj[k]); - expect(obj).toEqual(obj); - times++; - }); - expect(times).toEqual(Object.keys(obj).length); - }; - - testFn({a: 1, b: "2", c: true}); - testFn({a: 1, b: "2", c: {test: 1231}}); -}); -test("reduceObj", () => { - const fn = cm.reduceObj; - const obj = {a: 1, b: 2, c: "3"}; - const result = fn(obj, (r, v, k, o) => { - r[k] = v; - return r; - }, {}); - - expect(result).toEqual(obj); - expect(result === obj).toEqual(false); - - const result2 = fn(obj, (r, v, k, o) => { - r[k] = v + "1"; - return r; - }, {}); - const result3 = Object.keys(obj).reduce((r, key, index, keyArr) => { - const v = obj[key]; - r[key] = v + "1"; - return r; - }, {}); - - expect(result2).toEqual({ - a: "11", b: "21", c: "31", - }); - expect(result2).toEqual(result3); -}); -test("assign", () => { - const fn = cm.assign; - const a = {a: 1, b: 2, c: 3}; - const b = {a: 4, c: 6, d: 7}; - const c = {aa: 4, cc: 6, dd: 7}; - expect(fn({}, a)).toEqual(a); - expect(fn({}, a)).toEqual(Object.assign({}, a)); - expect(fn({}, a, [1, 2, 3])).toEqual(Object.assign({}, a, [1, 2, 3])); - expect(fn({}, a, b, c)).toEqual(Object.assign({}, a, b, c)); - expect(fn({}, a, b, c, a, b, c, a, b)).toEqual(Object.assign({}, a, b, c)); -}); - diff --git a/__test__/coordinate.test.ts b/__test__/coordinate.test.ts deleted file mode 100644 index eb94056..0000000 --- a/__test__/coordinate.test.ts +++ /dev/null @@ -1,41 +0,0 @@ -import * as cd from "../src/coordinate"; - -test("isPointInPath", () => { - expect(cd.isPointInPath([1, 2], [[0, 0], [2, 4]])).toBe(true); - expect(cd.isPointInPath([2, 2], [[0, 0], [2, 4]])).toBe(false); - - expect(cd.isPointInPath([2, 2], [[0, 0], [4, 4]])).toBe(true); - expect(cd.isPointInPath([1, 2], [[0, 0], [4, 4]])).toBe(false); - - expect(cd.isPointInPath([3, 3], [[1, 1], [4, 4]])).toBe(true); -}); - -test("getDistance", () => { - expect(cd.getDistance([0, 0], [3, 4])).toBe(5); - expect(cd.getDistance([0, 0], [1, 1])).toBe(Math.sqrt(2)); - expect(Math.round(cd.getDistance([0, 0], [1, Math.sqrt(3)]))).toBe(2); - expect(cd.getDistance([0, 0], [0, 5])).toBe(5); - expect(cd.getDistance([0, 0], [5, 0])).toBe(5); - expect(cd.getDistance([0, 0], [-5, 0])).toBe(5); - expect(cd.getDistance([0, 0], [0, -5])).toBe(5); -}); -test("getAngle", () => { - expect(cd.getAngle([0, 0], [1, 1])).toBe(135); - expect(cd.getAngle([0, 0], [1, 1], cd.Direct.bottom)).toBe(315); - expect(cd.getAngle([0, 0], [1, 1], cd.Direct.left)).toBe(225); - expect(cd.getAngle([0, 0], [1, 1], cd.Direct.right)).toBe(45); -}); -test("getRotatePoint", () => { - const center: cd.Point = [0, 0]; - expect(cd.getRotatePoint(center, Math.sqrt(2), 135)).toEqual([1, 1]); -}); - -test("getBorderWidthByCos", () => { - expect(cd.getBorderWidthByCos(3,4,90)).toEqual(5); - expect(cd.getBorderWidthByCos(1,1,90).toFixed(2)).toEqual(Math.sqrt(2).toFixed(2)); -}); -test("getBorderWidthByCos", () => { - expect(cd.getBorderWidthBySin(1,45,90).toFixed(2)).toEqual(Math.sqrt(2).toFixed(2)); - expect(cd.getBorderWidthBySin(1,45,45)).toEqual(1); -}); - diff --git a/__test__/date.test.ts b/__test__/date.test.ts deleted file mode 100644 index ed2761e..0000000 --- a/__test__/date.test.ts +++ /dev/null @@ -1,79 +0,0 @@ -import * as dt from "../src/date"; - -test('str2Date', () => { - const t1 = (dt.str2Date("2020-02-02 10:10:10") as Date).getTime(); - const t2 = (dt.str2Date("2020-02-20 10:10:10") as Date).getTime(); - expect(t2).toBeGreaterThan(t1); - expect(dt.str2Date("abcd")).toBeNull(); - - function fn(date: string, format: string) { - return dt.formatDate.call(dt.str2Date(date), format); - } - - expect(fn("2020-02-02", "yyyy")).toBe("2020"); - expect(fn("2020-02-02", "MM")).toBe("02"); - expect(fn("2020-02-02", "dd")).toBe("02"); - expect(fn("2020-02-02", "yy")).toBe("20"); - expect(fn("2020-02-02", "hh")).toBe("00"); - expect(fn("2020-02-02", "mm")).toBe("00"); - expect(fn("2020-02-02", "ss")).toBe("00"); - expect(fn("2019-03", "dd")).toBe("01"); - expect(fn("2020-02-02 12:00:00", "hh")).toBe("12"); - expect(fn("2020-02-02 12:00:00", "mm")).toBe("00"); - expect(fn("2020-02-02 12:00:00", "ss")).toBe("00"); - expect(fn("2020-02-02 12:00:10", "ss")).toBe("10"); - expect(fn("2020-02-02 12:11:10", "mm")).toBe("11"); -}); -test('formatDate', () => { - const date1 = dt.getDateFromStr("2020-02-02 10:10:10"); - expect(dt.formatDate.call(date1, "yyyy-MM-dd")).toBe("2020-02-02"); - expect(dt.formatDate.call(date1, "hh:mm:ss")).toBe("10:10:10"); - expect(dt.formatDate.call(date1, "dd-MM-yyyy")).toBe("02-02-2020"); - // week start - expect(dt.formatDate.call(new Date("2020-01-12"), "周w")).toBe("周日"); - expect(dt.formatDate.call(new Date("2020-01-12"), "w")).toBe("日"); - expect(dt.formatDate.call(new Date("2020-01-13"), "w")).toBe("一"); - expect(dt.formatDate.call(new Date("2020-01-14"), "w")).toBe("二"); - expect(dt.formatDate.call(new Date("2020-01-15"), "w")).toBe("三"); - expect(dt.formatDate.call(new Date("2020-01-16"), "w")).toBe("四"); - expect(dt.formatDate.call(new Date("2020-01-17"), "w")).toBe("五"); - expect(dt.formatDate.call(new Date("2020-01-18"), "w")).toBe("六"); - // week end - // season start - expect(dt.formatDate.call(new Date("2020-01-12"), "q")).toBe("春"); - expect(dt.formatDate.call(new Date("2020-02-12"), "q")).toBe("春"); - expect(dt.formatDate.call(new Date("2020-03-13"), "q")).toBe("春"); - expect(dt.formatDate.call(new Date("2020-04-14"), "q")).toBe("夏"); - expect(dt.formatDate.call(new Date("2020-05-15"), "q")).toBe("夏"); - expect(dt.formatDate.call(new Date("2020-06-16"), "q")).toBe("夏"); - expect(dt.formatDate.call(new Date("2020-07-17"), "q")).toBe("秋"); - expect(dt.formatDate.call(new Date("2020-08-18"), "q")).toBe("秋"); - expect(dt.formatDate.call(new Date("2020-09-18"), "q")).toBe("秋"); - expect(dt.formatDate.call(new Date("2020-10-18"), "q")).toBe("冬"); - expect(dt.formatDate.call(new Date("2020-11-18"), "q")).toBe("冬"); - expect(dt.formatDate.call(new Date("2020-12-18"), "q")).toBe("冬"); - - dt.formatDate.seasonText = ["spring"]; - expect(dt.formatDate.call(new Date("2020-01-12"), "q")).toBe("spring"); - // season end - const date2 = dt.getDateFromStr("2019-12-1 10:10:10"); - expect(dt.formatDate.call(date2, "d-MM-yy")).toBe("1-12-19"); -}); - -test("dateDiff", () => { - const v = dt.dateDiff(new Date("2020-05-01"), new Date("2020-05-06")); - expect(v).toBe("0年5天 00时00分00秒"); - expect(dt.dateDiff(new Date("2020-05-01"), new Date("2020-05-06"), "dd天 hh时mm分ss秒")).toBe("05天 00时00分00秒"); - expect(dt.dateDiff(new Date("2020-05-06"), new Date("2020-05-01 3:20:10"), "d天 hh时mm分ss秒")).toBe("5天 04时39分50秒"); - - // expect(cm.dateDiff(new Date("2020-05-01"), new Date("2020-05-06"), "d天 H时m分s秒")).toBe("5天 0时0分0秒"); - // expect(cm.dateDiff(new Date("2020-05-06"), new Date("2020-05-01 3:20:10"), "d天 H时m分s秒")).toBe("-5天 -4时-39分-50秒"); -}); -test("number2Date", () => { - const fn = dt.number2Date; - expect(fn(1000, "d天hh时")).toBe("0天00时"); - expect(fn(1000)).toBe("0天00时00分01秒"); - expect(fn(60 * 1000)).toBe("0天00时01分00秒"); - expect(fn(60 * 60 * 1000)).toBe("0天01时00分00秒"); - expect(fn(60 * 60 * 24 * 1000)).toBe("1天00时00分00秒"); -}); diff --git a/__test__/decorator.test.ts b/__test__/decorator.test.ts deleted file mode 100644 index 6f87faf..0000000 --- a/__test__/decorator.test.ts +++ /dev/null @@ -1,66 +0,0 @@ -import {sleep} from "../src/common"; -import * as dc from "../src/decorator"; - -test("Debounce", async (done) => { - const Debounce = dc.Debounce; - - const now = Date.now(); - - class Test { - times = 0; - time = 0; - value: string | number = ""; - - @Debounce(1000) - test(value: string | number) { - this.times++; - this.time = Date.now(); - this.value = value; - } - } - - const t = new Test(); - - t.test(1); - t.test(2); - t.test(3); - t.test(4); - t.test(5); - t.test(6); - - await sleep(1100); - - expect(t.times).toBe(1); - expect(t.time - now).toBeGreaterThanOrEqual(100); - expect(t.value).toBeGreaterThanOrEqual(6); - done(); -}); - -test("Polling", async (done) => { - const Polling = dc.Polling; - - class Test { - times = 0; - time = 0; - value: string | number = ""; - - @Polling(100) - test(times?: number) { - return new Promise((res, rej) => { - if (times! >= 5) { - rej(); - } else { - console.log(times); - res(); - } - }); - } - } - - const t = new Test(); - - await t.test(); - - // await sleep(5000); - done(); -}); \ No newline at end of file diff --git a/__test__/eventBus.test.ts b/__test__/eventBus.test.ts deleted file mode 100644 index 57a8aee..0000000 --- a/__test__/eventBus.test.ts +++ /dev/null @@ -1,49 +0,0 @@ -import {EventBus} from "../src/eventBus"; - -test('EventBus', () => { - let result = ""; - let result2 = ""; - EventBus.Ins.on("test", function (...params) { - result = (result + " " + params.join(" ")).trim(); - }); - EventBus.Ins.emit("test", "hello", "world"); - EventBus.Ins.emit("test", "hello"); - EventBus.Ins.emit("test", "world"); - - const eb = new EventBus(); - let cb; - eb.on("test", cb = function (...params) { - result2 = (result2 + " " + params.join(" ")).trim(); - }); - eb.emit("test", "world"); - - eb.off("test", cb); - eb.emit("test", "hello"); - - expect(result).toBe("hello world hello world"); - expect(result2).toBe("world"); -}); -test('once', () => { - let result = ""; - EventBus.Ins.once("test", function (...params) { - result = (result + " " + params.join(" ")).trim(); - }); - EventBus.Ins.emit("test", "hello", "world once"); - EventBus.Ins.emit("test", "hello"); - EventBus.Ins.emit("test", "world"); - - - expect(result).toBe("hello world once"); -}); -test('times', () => { - let result = ""; - EventBus.Ins.times(10, "test", function (...params) { - result = (result + " " + params.join(" ")).trim(); - }); - [...Array(20).keys()].forEach(item => { - EventBus.Ins.emit("test", item); - }); - - - expect(result).toBe([...Array(10).keys()].join(" ")); -}); \ No newline at end of file diff --git a/__test__/img.png b/__test__/img.png deleted file mode 100644 index 601f397..0000000 Binary files a/__test__/img.png and /dev/null differ diff --git a/__test__/img2.png b/__test__/img2.png deleted file mode 100644 index e6cbe33..0000000 Binary files a/__test__/img2.png and /dev/null differ diff --git a/__test__/numberCalc.test.ts b/__test__/numberCalc.test.ts deleted file mode 100644 index 4a012ba..0000000 --- a/__test__/numberCalc.test.ts +++ /dev/null @@ -1,65 +0,0 @@ -import * as numCalc from "../src/numberCalc"; - -test("strip", () => { - expect(numCalc.strip(1.0000000000041083)).toBe(1); - expect(numCalc.strip(1.0000000000001563)).toBe(1); -}); -test('getNumberLenAfterDot', () => { - expect(numCalc.getNumberLenAfterDot(0.12345667)).toBe(8); - expect(numCalc.getNumberLenAfterDot("0.123456789")).toBe(9); - expect(numCalc.getNumberLenAfterDot(12345)).toBe(0); - expect(numCalc.getNumberLenAfterDot("abc")).toBe(0); - expect(numCalc.getNumberLenAfterDot(1.123e5)).toBe(0); - expect(numCalc.getNumberLenAfterDot(1.123e2)).toBe(1); - expect(numCalc.getNumberLenAfterDot(1.123e+2)).toBe(1); - expect(numCalc.getNumberLenAfterDot(1e+2)).toBe(0); -}); - -test('Calc', () => { - const Calc = numCalc.NumberCalc; - // 0.1 + 0.2 = 0.30000000000000004 - expect(0.1 + 0.2).not.toBe(0.3); - const c = new Calc(1); - - // 0.3 - 0.1 = 0.19999999999999998 - expect(0.3 - 0.1).not.toBe(0.2); - expect(Calc.init(0.3).minus(0.1).curVal).toBe(0.2); - // 0.2 * 0.1 = 0.020000000000000004 - expect(0.2 * 0.1).not.toBe(0.02); - expect(Calc.init(0.2).times(0.1).curVal).toBe(0.02); - // 0.3 / 0.1 = 2.9999999999999996 - expect(0.3 / 0.1).not.toBe(3); - expect(Calc.init(0.3).divide(0.1).curVal).toBe(3); - - // 100 / 10 + 5 - 2 = 13 - expect(c.times(100).divide(10).plus(5).minus(2).curVal).toBe(13); - - // 0.3 - 0.1 = 0.19999999999999998 - expect(0.3 - 0.1).not.toBe(0.2); - expect(Calc.init(0.3)["-"](0.1).curVal).toBe(0.2); - // 0.2 * 0.1 = 0.020000000000000004 - expect(0.2 * 0.1).not.toBe(0.02); - expect(Calc.init(0.2)["*"](0.1).curVal).toBe(0.02); - // 0.3 / 0.1 = 2.9999999999999996 - expect(0.3 / 0.1).not.toBe(3); - expect(Calc.init(0.3)["/"](0.1).curVal).toBe(3); - - c.reset(); - // 100 / 10 + 5 - 2 = 13 - expect(c["*"](100)["/"](10)["+"](5)["-"](2).curVal).toBe(13); - c.curVal = 0.1; - // 0.1 + 0.2 - 0.1 = 0.2 - expect(c["+"](0.2)["-"](0.1).curVal).toBe(0.2); - - - // 100 - 20 * 2 = 60 - expect(Calc.init(20)["*"](2).by(100, "-").curVal).toBe(60); - - // 100 - 10 - 20 - 30 - 100 = -60 - expect(Calc.init(100)["-"]([10, 20, 30, 100]).curVal).toBe(-60); - expect(Calc.init(100)["-"]([10, 20, 30, 100]).curVal).toBe(-60); - expect(Calc.init(100)["-"](10, 20, 30, 100).curVal).toBe(-60); - expect(Calc.init(100)["-"]([10, 20], 30, 100).curVal).toBe(-60); - expect(Calc.init(100)["-"]([10, 20, 30], 100).curVal).toBe(-60); -}); - diff --git a/__test__/test.html b/__test__/test.html deleted file mode 100644 index b04a1a4..0000000 --- a/__test__/test.html +++ /dev/null @@ -1,155 +0,0 @@ - - - - - Title - - - - - - -
-

使用copy

-
-

远程图片

- - -

水电费水电费水电费水电费水电费

-
-
-

本地图片

- -

使用copy使用copy使用copy使用copy使用copy

-
-
-
-

使用clipboard

-
-

远程图片

- -

水电费水电费水电费水电费水电费

-
-
-

本地图片

- -

使用clipboard使用clipboard使用clipboard使用clipboard使用clipboard使用clipboard

-
-
- - - - \ No newline at end of file diff --git a/__test__/type.test.ts b/__test__/type.test.ts deleted file mode 100644 index 48aa2c5..0000000 --- a/__test__/type.test.ts +++ /dev/null @@ -1,234 +0,0 @@ -import * as type from "../src/type"; -import * as cm from "../src/common"; - -test('isArrayLike', () => { - expect(type.isArrayLike([1, 2, 3])).toBe(true); - expect(type.isArrayLike([])).toBe(true); - expect(type.isArrayLike({length: 1, 0: 1})).toBe(true); - expect(type.isArrayLike({length: 2, 0: 1})).toBe(false); - expect(type.isArrayLike("")).toBe(false); - expect(type.isArrayLike(1)).toBe(false); - expect(type.isArrayLike(true)).toBe(false); - expect(type.isArrayLike(undefined)).toBe(false); - expect(type.isArrayLike(null)).toBe(false); - expect(type.isArrayLike({})).toBe(false); - expect(type.isArrayLike(() => { - })).toBe(false); -}); -test('isArray', () => { - expect(type.isArray(0.12345667)).toBeFalsy(); - expect(type.isArray("")).toBeFalsy(); - expect(type.isArray({})).toBeFalsy(); - expect(type.isArray({0: 1, 1: 2, length: 2})).toBeFalsy(); - expect(type.isArray(() => { - })).toBeFalsy(); - expect(type.isArray(true)).toBeFalsy(); - expect(type.isArray(NaN)).toBeFalsy(); - expect(type.isArray(undefined)).toBeFalsy(); - expect(type.isArray(null)).toBeFalsy(); - expect(type.isArray([1, 2, 3])).toBeTruthy(); - expect(type.isArray([])).toBeTruthy(); -}); -test('isNumber', () => { - expect(type.isNumber("")).toBeFalsy(); - expect(type.isNumber({})).toBeFalsy(); - expect(type.isNumber({0: 1, 1: 2, length: 2})).toBeFalsy(); - expect(type.isNumber(() => { - })).toBeFalsy(); - expect(type.isNumber(true)).toBeFalsy(); - expect(type.isNumber(undefined)).toBeFalsy(); - expect(type.isNumber(null)).toBeFalsy(); - expect(type.isNumber([1, 2, 3])).toBeFalsy(); - expect(type.isNumber([])).toBeFalsy(); - expect(type.isNumber(NaN)).toBeTruthy(); - expect(type.isNumber(123)).toBeTruthy(); -}); -test('isFunction', () => { - expect(type.isFunction("")).toBeFalsy(); - expect(type.isFunction(() => { - })).toBeTruthy(); -}); -test('isString', () => { - expect(type.isString(123123)).toBeFalsy(); - expect(type.isString("")).toBeTruthy(); -}); -test('isObject', () => { - expect(type.isObject(123123)).toBeFalsy(); - expect(type.isObject(undefined)).toBeFalsy(); - expect(type.isObject(123123)).toBeFalsy(); - expect(type.isObject("")).toBeFalsy(); - // null - expect(typeof null === "object").toBeTruthy(); - expect(type.isObject(null)).toBeFalsy(); - // array - expect(typeof [] === "object").toBeTruthy(); - expect(type.isObject([])).toBeFalsy(); - // - expect(type.isObject({})).toBeTruthy(); - // function - const f = function () { - }; - expect(typeof f === "object").toBeFalsy(); - expect(type.isObject(f)).toBeFalsy(); -}); -test('isBoolean', () => { - expect(type.isBoolean(0)).toBeFalsy(); - expect(type.isBoolean(123123)).toBeFalsy(); - expect(type.isBoolean(undefined)).toBeFalsy(); - expect(type.isBoolean("")).toBeFalsy(); - expect(type.isBoolean(null)).toBeFalsy(); - expect(type.isBoolean([])).toBeFalsy(); - expect(type.isBoolean({})).toBeFalsy(); -}); -test('is', () => { - expect(type.type(0, "string")).toBeFalsy(); - expect(type.type(0, "number")).toBe(true); - expect(type.type(0, ["string", "number"])).toBe(true); - expect(type.type(0, ["string", "function", "object"])).toBe(false); -}); -test('isUndefined', () => { - expect(type.isUndefined(0)).toBeFalsy(); - expect(type.isUndefined(123123)).toBeFalsy(); - expect(type.isUndefined("")).toBeFalsy(); - expect(type.isUndefined(null)).toBeFalsy(); - expect(type.isUndefined([])).toBeFalsy(); - expect(type.isUndefined(undefined)).toBeTruthy(); - let a; - expect(type.isUndefined(a)).toBeTruthy(); -}); -test('isNaN', () => { - expect(NaN === NaN).toBeFalsy(); - expect(type.isNaN(NaN)).toBeTruthy(); - expect(type.isNaN({a: 1})).toBeFalsy(); - expect(type.isNaN(1)).toBeFalsy(); - expect(type.isNaN(0)).toBeFalsy(); - expect(type.isNaN(-1)).toBeFalsy(); - expect(type.isNaN(false)).toBeFalsy(); - expect(type.isNaN(undefined)).toBeFalsy(); - expect(type.isNaN(null)).toBeFalsy(); - expect(type.isNaN("")).toBeFalsy(); - expect(type.isNaN({})).toBeFalsy(); - expect(type.isNaN({a: 1})).toBeFalsy(); - expect(type.isNaN([])).toBeFalsy(); - expect(type.isNaN([1, 2, 3])).toBeFalsy(); - expect(type.isNaN(["bdsdf", 12323])).toBeFalsy(); - expect(type.isNaN("123")).toBeFalsy(); - expect(type.isNaN("NaN")).toBeFalsy(); -}); -test('isEmptyObject', () => { - expect(type.isEmptyObject({})).toBeTruthy(); - expect(type.isEmptyObject({a: 1})).toBeFalsy(); - expect(type.isEmptyObject({true: 1})).toBeFalsy(); - expect(type.isEmptyObject({false: 1})).toBeFalsy(); - expect(type.isEmptyObject({0: 1})).toBeFalsy(); - expect(type.isEmptyObject({undefined: 1})).toBeFalsy(); - expect(type.isEmptyObject({null: 1})).toBeFalsy(); - expect(type.isEmptyObject([])).toBeFalsy(); - expect(type.isEmptyObject(function () { - })).toBeFalsy(); -}); -test('isEmpty', () => { - expect(type.isEmpty(NaN)).toBeTruthy(); - expect(type.isEmpty("")).toBeTruthy(); - expect(type.isEmpty({})).toBeTruthy(); - expect(type.isEmpty([])).toBeTruthy(); - expect(type.isEmpty({a: 1})).toBeFalsy(); - expect(type.isEmpty([1])).toBeFalsy(); - expect(type.isEmpty(0)).toBeFalsy(); - expect(type.isEmpty(function () { - - })).toBeFalsy(); - expect(type.isEmpty({ - a: function () { - - }, - })).toBeFalsy(); -}); -test("isPromiseLike", () => { - expect(type.isPromiseLike({})).toBe(false); - expect(type.isPromiseLike(Promise.resolve())).toBe(true); - expect(type.isPromiseLike(null)).toBe(false); - expect(type.isPromiseLike(null)).toBe(false); - expect(type.isPromiseLike(undefined)).toBe(false); - expect(type.isPromiseLike(0)).toBe(false); - expect(type.isPromiseLike(-42)).toBe(false); - expect(type.isPromiseLike(42)).toBe(false); - expect(type.isPromiseLike('')).toBe(false); - expect(type.isPromiseLike('then')).toBe(false); - expect(type.isPromiseLike(false)).toBe(false); - expect(type.isPromiseLike(true)).toBe(false); - expect(type.isPromiseLike({})).toBe(false); - expect(type.isPromiseLike({then: true})).toBe(false); - expect(type.isPromiseLike([])).toBe(false); - expect(type.isPromiseLike([true])).toBe(false); - expect(type.isPromiseLike(() => { - })).toBe(false); - - const promise = { - then: function () { - }, - }; - expect(type.isPromiseLike(promise)).toBe(true); - - const fn = () => { - }; - fn.then = () => { - }; - expect(type.isPromiseLike(fn)).toBe(true); -}); -test('isEqual', () => { - expect(type.isEqual({a: 1}, {a: 1})).toBeTruthy(); - expect(type.isEqual({a: 1}, {a: 2})).toBeFalsy(); - expect(type.isEqual(1, 1)).toBeTruthy(); - expect(type.isEqual(1, 2)).toBeFalsy(); - expect(type.isEqual(1, "1")).toBeFalsy(); - expect(type.isEqual(0, "")).toBeFalsy(); - expect(type.isEqual(0, false)).toBeFalsy(); - expect(type.isEqual(0, null)).toBeFalsy(); - expect(type.isEqual(0, undefined)).toBeFalsy(); - expect(type.isEqual(null, undefined)).toBeFalsy(); - expect(type.isEqual(false, undefined)).toBeFalsy(); - expect(type.isEqual(false, null)).toBeFalsy(); - expect(type.isEqual(false, true)).toBeFalsy(); - expect(type.isEqual([1, 2], {0: 1, 1: 2, length: 2})).toBeFalsy(); - expect(type.isEqual(() => { - }, () => { - })).toBeFalsy(); - expect(type.isEqual(cm.polling, cm.polling)).toBeTruthy(); - expect(type.isEqual([1, 2], [1, 2])).toBeTruthy(); - expect(type.isEqual(null, null)).toBeTruthy(); - expect(type.isEqual(undefined, undefined)).toBeTruthy(); - expect(type.isEqual(false, false)).toBeTruthy(); - expect(type.isEqual(NaN, NaN)).toBeTruthy(); - expect(type.isEqual("", "")).toBeTruthy(); -}); -test('objectIsEqual', () => { - expect(type.objectIsEqual(cm, cm)).toBeTruthy(); - expect(type.objectIsEqual({a: 1}, {a: 1})).toBeTruthy(); - expect(type.objectIsEqual({a: 1}, {a: 2})).toBeFalsy(); -}); -test('isSameType', () => { - const fn = type.isSameType; - expect(fn(cm, cm)).toBeTruthy(); - expect(fn(1, 2)).toBeTruthy(); - expect(fn("", new String(123))).toBeTruthy(); - expect(fn(1, NaN)).toBeTruthy(); - expect(fn(1, "")).toBeFalsy(); - expect(fn({}, [])).toBeFalsy(); - expect(fn({}, () => 0)).toBeFalsy(); - expect(fn({}, null)).toBeFalsy(); -}); -test('isIterable', () => { - const fn = type.isIterable; - expect(fn(null)).toBeFalsy(); - expect(fn(undefined)).toBeFalsy(); - expect(fn(0)).toBeFalsy(); - expect(fn(true)).toBeFalsy(); - expect(fn({})).toBeFalsy(); - expect(fn(Symbol("123"))).toBeFalsy(); - expect(fn("")).toBeTruthy(); - expect(fn([])).toBeTruthy(); - expect(fn([0, 1])).toBeTruthy(); - expect(fn(new Map())).toBeTruthy(); - expect(fn(new Set())).toBeTruthy(); -}); \ No newline at end of file diff --git a/__test__/urlParse.test.ts b/__test__/urlParse.test.ts deleted file mode 100644 index 4d31d6f..0000000 --- a/__test__/urlParse.test.ts +++ /dev/null @@ -1,28 +0,0 @@ -import {UrlParse} from "../src/urlParse"; - -test('urlParse', () => { - // a[]=123&a[]=on&b[0]=on&b[1]=on&c=1&c=2&d=1,2,3,4,5&pid=19&pname=环球贸易项目基坑地铁2号线隧道结构自动化监测 - const url = "http://www.baidu.com:112332/index.php/admin/MonitorResultManager/monitorData?a%5B%5D=123&a%5B%5D=on&b%5B0%5D=on&b%5B1%5D=on&c=1&c=2&c=3&d=1,2,3,4,5&pid=19&pname=%E7%8E%AF%E7%90%83%E8%B4%B8%E6%98%93%E9%A1%B9%E7%9B%AE%E5%9F%BA%E5%9D%91%E5%9C%B0%E9%93%812%E5%8F%B7%E7%BA%BF%E9%9A%A7%E9%81%93%E7%BB%93%E6%9E%84%E8%87%AA%E5%8A%A8%E5%8C%96%E7%9B%91%E6%B5%8B#test"; - const urlParse = new UrlParse(url); - // console.log(decodeURIComponent(urlParse.queryStr)); - expect(urlParse.schema).toBe("http"); - expect(urlParse.port).toBe("112332"); - expect(urlParse.host).toBe("www.baidu.com"); - expect(urlParse.query.pname).toBe("环球贸易项目基坑地铁2号线隧道结构自动化监测"); - expect(urlParse.query.pid).toBe("19"); - expect(urlParse.query.a).toEqual(["123", "on"]); - expect(urlParse.query.b).toEqual(["on", "on"]); - expect(urlParse.query.c).toEqual(["1", "2", "3"]); - expect(urlParse.query.d).toBe("1,2,3,4,5"); - expect(urlParse.hash).toBe("test"); - expect(urlParse.path).toBe("index.php/admin/MonitorResultManager/monitorData"); - expect(urlParse.parseHost("/")).toBe(""); - expect(urlParse.parsePort("/")).toBe(""); - const urlParse2 = new UrlParse(); - expect(urlParse2.parsePath("/index.php/admin/MonitorResultManager")).toBe("index.php/admin/MonitorResultManager"); - expect(urlParse2.parseSchema("")).toBe(""); - expect(urlParse2.parseHost("/index.php/admin#absdf-23_123")).toBe(""); - expect(urlParse2.parseHost("")).toBe(""); - expect(urlParse2.parseHash("/index.php/admin#absdf-23_123")).toBe("absdf-23_123"); - expect(urlParse2.parseHash("/index.php/admin")).toBe(""); -}); diff --git a/domResizeEvent/test.html b/domResizeEvent/test.html deleted file mode 100644 index 62d6b49..0000000 --- a/domResizeEvent/test.html +++ /dev/null @@ -1,75 +0,0 @@ - - - - - Document - - - -
- demo 内容 -
-
-
-
-
-
-
- - - diff --git a/domResizeEvent/test2.html b/domResizeEvent/test2.html deleted file mode 100644 index ea963c7..0000000 --- a/domResizeEvent/test2.html +++ /dev/null @@ -1,254 +0,0 @@ - - - - - Document - - - - - - -
- -
-
- - - - diff --git a/static/img.png b/static/img.png deleted file mode 100644 index 601f397..0000000 Binary files a/static/img.png and /dev/null differ diff --git a/static/img2.png b/static/img2.png deleted file mode 100644 index e6cbe33..0000000 Binary files a/static/img2.png and /dev/null differ diff --git a/test/merge-img/index.html b/test/merge-img/index.html deleted file mode 100644 index 566549b..0000000 --- a/test/merge-img/index.html +++ /dev/null @@ -1,10 +0,0 @@ - - - - - Title - - - - - \ No newline at end of file diff --git a/test/merge-img/index.ts b/test/merge-img/index.ts deleted file mode 100644 index 4275ce0..0000000 --- a/test/merge-img/index.ts +++ /dev/null @@ -1,19 +0,0 @@ -import {createElement} from "@/dom"; -import {MergeImg} from "../../src/ImgMerge"; - -(async function () { - const mi = await MergeImg.createWithBg("./static/img.png"); - console.log(mi); - // await mi.addImg("./static/img2.png", {left: 20, top: 20}); - // await mi.addImg("./static/img2.png", {right: 20, bottom: 20}); - // await mi.addImg("./static/img2.png", {left: 20, right: 20}); - // await mi.addImg("./static/img2.png", {top: 0, bottom: 0}); - // await mi.addImg("./static/img2.png", {left: 20, right: 20, top: 20, bottom: 20}); - // await mi.addImg("./static/img2.png", {left: 20, right: 20, top: 20, bottom: 20}, [100, undefined]); - // await mi.addImg("./static/img2.png", {left: 20, right: 20, top: 20, bottom: 20}, [, 100]); - // await mi.addImg("./static/img2.png", {left: 20, right: 20, top: 20, bottom: 20}, [100, 50]); - // await mi.addImg("./static/img2.png", {left: 200, right: 200}); - // await mi.addImg("./static/img2.png", {top: 20, bottom: 20}); - await mi.addImg("./static/img2.png", {top: 20, bottom: 20, left: 20, right: 20}); - document.body.append(createElement("img", {src: mi.toDataURL()})); -})(); \ No newline at end of file