@3r/tool
Version:
🏃包含一些常用方法例如对象深克隆、递归调用、一一对比/数组交集、并集、差集/二维向量点乘、叉乘/股票KDJ、MACD、RSI、BOLL/验证为空、车牌号、邮箱、身份证、统一社会信用代码、手机号、版本对比/转换日期、星座、身份证解析、字节/随机颜色、手机号、身份证号码、统一社会信用代码...持续更新整合
751 lines (745 loc) • 23.5 kB
JavaScript
import { InterchangeFlag, Maths } from "../index.js";
let description = function () {
return ["#### Maths 数学模块", "包含一些与数学的方法.", "", "以下是相关示例:"];
};
let run = function () {
console.log("获取整数12的所有因数", Maths.getFactors(12));
console.log("获取整数12的所有因数通过接近程度排序", Maths.getFactorsByApproach(12));
console.log("数组求和", Maths.sum([1, 2, 3, 4]));
console.log("判断a与b符号是否相同", Maths.sameSign(1, -1));
console.log("角度转弧度", Maths.degreeToRad(45));
console.log("弧度转角度", Maths.radToDegree(0.7853981633974483));
console.log("交集 A∩B", Maths.intersection([{ x: 1 }, { y: 2 }, { a: 2, z: 3 }, false, true, 1, 3, 5, { a: 2, c: [1, 2] }], [true, 5, 5, { z: 3, a: 2 }]));
console.log("对象是否相等", Maths.equal({ a: 2, z: 3 }, { z: 3, a: 2 }));
console.log("删除重复项", Maths.removeRepeat([{ x: 1, y: 2 }, 2, 3, { y: 2, x: 1 }, 1, 4, 5, 6, 2, 3, 4, 4, 45, 4, 31]));
console.log("补集", Maths.complementarySet([{ x: 1 }, { y: 2 }, { a: 2, z: 3 }, false, true, 1, 3, 5, { a: 2, c: [1, 2] }], [true, 5, 5, { z: 3, a: 2 }, 8, { z: 3, a: 3 }]));
console.log("并集", Maths.union([1, 23, 4, 556, 14, 124], [123, 452, 231, 1, 14]));
console.log("数组 通过下标改变位置 从3的位置移到1的位置", Maths.interchange([1, 2, 3, 4], 3, 1));
console.log("阶乘 10!", Maths.iterationFactorial(10));
console.log("勾股定理", Maths.pythagorasTheorem(3, 4));
console.log("在可及的范围内", Maths.inRange(4, 1, 5));
const tmp1 = [
"25952000000082304387",
"25952000000087201733",
"25952000000090118196",
"25942000000019659408",
"25612000000054155084",
"25532000000052842129",
"25532000000052851936",
"25532000000054146405",
"25532000000055013765",
"25532000000059848250",
"25532000000060442367",
"25532000000060601106",
"25532000000060608726",
"25517000000000359888",
"25517000000000359919",
"25512000000097428790",
"25512000000100563543",
"25512000000102424320",
"25512000000102597085",
"25512000000102752873",
"25512000000103419767",
"25512000000103572164",
"25512000000104032208",
"25512000000106769094",
"25512000000112158725",
"25502000000047112733",
"25502000000048343682",
"25502000000048451040",
"25502000000050714684",
"25452000000044654007",
"25452000000049599946",
"25442000000297735183",
"25442000000245640421",
"25442000000261397155",
"25442000000266690977",
"25442000000266693510",
"25442000000273172407",
"25432000000059485890",
"25422000000083037311",
"25422000000085258326",
"25422000000085279165",
"25422000000085303128",
"25422000000085306097",
"25422000000085330902",
"25417000000168993891",
"25417000000168993892",
"25417000000168993893",
"25417000000168993894",
"25417000000168993895",
"25417000000168993897",
"25417000000168993902",
"25417000000168993911",
"25417000000168993912",
"25417000000168993918",
"25417000000169201213",
"25417000000172073521",
"25417000000172133659",
"25417000000172133660",
"25417000000163598104",
"25417000000163598106",
"25417000000165292644",
"25417000000168993889",
"25417000000168993890",
"25417000000160844554",
"25417000000157367762",
"25417000000057223411",
"25417000000057223535",
"25417000000057223714",
"25417000000057223790",
"25417000000057223233",
"25414000000001072755",
"25412000000122340263",
"25412000000118945318",
"25412000000119089726",
"25412000000119107803",
"25412000000119204248",
"25412000000119471463",
"25412000000120297750",
"25412000000120328465",
"25442000000290226876",
"25412000000120941958",
"25412000000121395275",
"25412000000116199711",
"25412000000116254488",
"25412000000116616304",
"25412000000116697807",
"25412000000116715679",
"25412000000116946845",
"25412000000116983804",
"25412000000116983289",
"25412000000117741916",
"25412000000118086888",
"25412000000118124809",
"25412000000114439180",
"25412000000114488762",
"25412000000114743795",
"25412000000114811926",
"25412000000115620620",
"25412000000115998089",
"25412000000112554675",
"25412000000112602766",
"25412000000112589056",
"25412000000112615542",
"25412000000112746786",
"25412000000112959256",
"25412000000113032169",
"25412000000112984330",
"25412000000113041284",
"25412000000113244483",
"25412000000113459027",
"25412000000113698929",
"25412000000113770066",
"25412000000120009281",
"25412000000111288624",
"25412000000111327318",
"25412000000111414716",
"25412000000111434307",
"25412000000111455754",
"25412000000111544638",
"25412000000111663701",
"25412000000111697430",
"25412000000111698291",
"25412000000111989237",
"25412000000110212570",
"25412000000110213523",
"25412000000110244229",
"25412000000110319454",
"25412000000110444409",
"25412000000110577087",
"25412000000110802083",
"25412000000110845541",
"25412000000111063897",
"25412000000111126682",
"25412000000111146121",
"25412000000111196614",
"25412000000111262940",
"25412000000109236561",
"25412000000109270067",
"25412000000109337588",
"25412000000109662043",
"25412000000109671362",
"25412000000109684766",
"25412000000109711499",
"25412000000109723893",
"25412000000109884804",
"25412000000110159900",
"25412000000110168569",
"25412000000110178137",
"25412000000110187354",
"25412000000110187358",
"25412000000110192612",
"25412000000110211625",
"25412000000107722432",
"25412000000107833168",
"25412000000108088614",
"25412000000108090206",
"25412000000106564348",
"25412000000111297763",
"25412000000106592248",
"25412000000106632530",
"25412000000106649582",
"25412000000106703164",
"25412000000106787151",
"25412000000106788788",
"25412000000106878407",
"25412000000106904698",
"25412000000106953568",
"25412000000106954558",
"25412000000106967881",
"25412000000106968069",
"25412000000106976929",
"25412000000107029619",
"25412000000107043417",
"25412000000107076557",
"25412000000107092189",
"25412000000107141523",
"25412000000107331283",
"25412000000107429470",
"25412000000107570612",
"25412000000105217246",
"25412000000105266650",
"25412000000105533417",
"25412000000105683746",
"25412000000105859899",
"25412000000105887890",
"25412000000105934174",
"25412000000106036783",
"25412000000106071103",
"25412000000106419358",
"25412000000106419802",
"25412000000106448741",
"25412000000106457218",
"25412000000106464948",
"25412000000106475485",
"25412000000106482315",
"25412000000106521156",
"25412000000103835543",
"25412000000103960002",
"25412000000103998248",
"25412000000104177710",
"25412000000104436247",
"25412000000104503667",
"25412000000104508070",
"25412000000104677580",
"25412000000104740242",
"25412000000104801455",
"25412000000104929261",
"25412000000104929884",
"25412000000104959360",
"25412000000104978437",
"25412000000105009069",
"25412000000105021962",
"25412000000105046777",
"25412000000105068952",
"25412000000105099468",
"25412000000105160008",
"25412000000100995278",
"25412000000101181885",
"25412000000101630310",
"25412000000101983348",
"25412000000102702184",
"25412000000102887134",
"25412000000103069845",
"25412000000103079198",
"25412000000103079392",
"25412000000103106647",
"25412000000103125983",
"25412000000103143714",
"25412000000103150915",
"25412000000113727880",
"25412000000113753623",
"25412000000103170377",
"25412000000098748140",
"25412000000098952272",
"25412000000099238205",
"25412000000099267434",
"25412000000099379174",
"25412000000099482674",
"25412000000099728916",
"25412000000100447474",
"25412000000100513833",
"25412000000100736615",
"25412000000097525872",
"25412000000097809830",
"25412000000097818671",
"25412000000097836197",
"25412000000097836728",
"25412000000097818478",
"25412000000097827144",
"25412000000097845732",
"25412000000097871708",
"25412000000098030644",
"25412000000098346543",
"25412000000098441886",
"25412000000096420388",
"25412000000096873965",
"25412000000094963967",
"25412000000095372422",
"25412000000092542950",
"25412000000092775129",
"25412000000101259484",
"25412000000096845024",
"25412000000096468768",
"25412000000091735331",
"25412000000089832029",
"25412000000107610084",
"25412000000082031943",
"25412000000081371831",
"25412000000082431775",
"25412000000074913691",
"25412000000078272510",
"25372000000124804435",
"25372000000127043168",
"25372000000127222257",
"25372000000128679104",
"25372000000109922220",
"25372000000116226163",
"25372000000116255328",
"25372000000117593885",
"25372000000118460477",
"25372000000119530458",
"25372000000122506393",
"25362000000043380971",
"25352000000050816437",
"25352000000051012092",
"25352000000053646485",
"25352000000054025265",
"25352000000055569527",
"25342000000078116860",
"25342000000081613546",
"25342000000068247744",
"25342000000073302165",
"25342000000068255762",
"25342000000073278718",
"25342000000068263076",
"25342000000073302408",
"25342000000068272238",
"25342000000073302052",
"25342000000068272491",
"25342000000073288155",
"25342000000073236015",
"25342000000073794167",
"25332000000196814163",
"25332000000193466382",
"25332000000193944462",
"25332000000180515792",
"25332000000180525326",
"25332000000180535127",
"25332000000180535162",
"25332000000180545182",
"25332000000180554716",
"25332000000180554743",
"25332000000180564495",
"25332000000180704007",
"25332000000180995864",
"25332000000186995911",
"25332000000180387654",
"25332000000180426883",
"25332000000180446594",
"25332000000180466135",
"25332000000180485942",
"25332000000180485967",
"25332000000180496014",
"25332000000180496015",
"25322000000218664109",
"25322000000205128240",
"25317000000721940568",
"25317000000721940571",
"25317000000721940573",
"25317000000721940574",
"25317000000721940576",
"25317000000504939561",
"25317000000721940567",
"25317000000526098455",
"25317000000526099194",
"25317000000662122242",
"25317000000696364977",
"25317000000721938436",
"25312000000158466697",
"25312000000164515067",
"25312000000137799160",
"25312000000137805560",
"25312000000137809580",
"25312000000137843758",
"25312000000138737239",
"25312000000138164446",
"25312000000138749510",
"25312000000138755615",
"25312000000138759004",
"25312000000138798918",
"25312000000139159530",
"25312000000140074503",
"25312000000140077117",
"25312000000140681415",
"25312000000142292193",
"25312000000142307977",
"25312000000144720040",
"25312000000146916144",
"25312000000146916947",
"25312000000146917064",
"25312000000146918874",
"25312000000146919297",
"25312000000147290645",
"25312000000148737901",
"25312000000151411300",
"25312000000151460377",
"25312000000123973265",
"25312000000130428919",
"25312000000131900862",
"25312000000133227198",
"25312000000137900354",
"25312000000137908475",
"25312000000136195715",
"25312000000136701006",
"25312000000136705734",
"25312000000136706149",
"25312000000136707777",
"25312000000136707916",
"25312000000136709588",
"25222000000020254699",
"25232000000026682674",
"25212000000031701906",
"25212000000032953201",
"25212000000034620471",
"25212000000034759535",
"25212000000034875864",
"25212000000036604682",
"25137000000019463535",
"25132000000075626847",
"25132000000075663346",
"25132000000076140669",
"25132000000076643157",
"25132000000078999367",
"25132000000079181609",
"25132000000079423581",
"25132000000080082485",
"25132000000081136346",
"25132000000062351933",
"25132000000065971211",
"25132000000073704878",
"25127000000064144461",
"25122000000023196445",
"25122000000024451346",
"25122000000029856905",
"25412000000096918633",
"25122000000030548297",
"25122000000032224331",
"25332000000205928593",
"25412000000117163664",
"25412000000120878591",
"25504000000000704920",
"25504000000000734449",
"25504000000000744473",
"25534000000001763690",
"25534000000001803373",
"25534000000001833205",
"25112000000104695679",
"25112000000107017145",
"25112000000100874341",
"25112000000101532845",
"25112000000101831707",
"25112000000091460962",
"25112000000092172136",
"25112000000095602164",
"25112000000096609167",
"25112000000096364579",
"25112000000096399911",
"25112000000096586112",
"25112000000099059395",
"25112000000084553030",
"25112000000010088068",
"25212000000022019534",
"25372000000066742094",
];
const tmp2 = [
"25317000000526098455",
"25317000000526099194",
"25122000000023196445",
"25412000000089832029",
"25122000000024451346",
"25317000000662122242",
"25412000000092775129",
"25317000000696364977",
"25452000000044654007",
"25317000000721938436",
"25412000000098030644",
"25412000000098441886",
"25372000000109922220",
"25414000000001072755",
"25312000000130428919",
"25412000000101259484",
"25312000000131900862",
"25412000000101630310",
"25512000000100563543",
"25312000000133227198",
"25517000000000359888",
"25442000000245640421",
"25222000000020254699",
"25342000000073794167",
"25412000000103835543",
"25412000000103960002",
"25512000000102424320",
"25412000000103998248",
"25412000000104177710",
"25412000000104740242",
"25512000000102752873",
"25372000000118460477",
"25312000000136701006",
"25512000000102597085",
"25212000000034620471",
"25512000000103572164",
"25412000000104436247",
"25412000000104508070",
"25137000000019463535",
"25122000000029856905",
"25372000000117593885",
"25502000000047112733",
"25312000000136705734",
"25412000000104867253",
"25412000000104677580",
"25132000000075663346",
"25312000000136195715",
"25312000000136709588",
"25412000000105099468",
"25532000000052851936",
"25312000000136706149",
"25512000000103419767",
"25412000000105217246",
"25132000000075626847",
"25412000000105266650",
"25412000000104801455",
"25312000000136707777",
"25312000000136707916",
"25532000000052842129",
"25952000000087201733",
"25412000000105046777",
"25412000000105160008",
"25112000000091460962",
"25362000000043380971",
"25412000000106036783",
"25332000000185452616",
"25132000000076643157",
"25412000000105859899",
"25412000000105683746",
"25312000000137900354",
"25312000000137843758",
"25452000000049599946",
"25212000000034875864",
"25512000000104032208",
"25212000000034759535",
"25312000000138164446",
"25312000000137805560",
"25412000000105887890",
"25412000000105533417",
"25372000000119530458",
"25112000000092172136",
"25412000000106071103",
"25312000000137908475",
"25312000000137809580",
"25312000000137799160",
"25132000000076140669",
"25322000000205128240",
"25412000000106954558",
"25412000000106564348",
"25412000000106703164",
"25412000000107076557",
"25442000000261397155",
"25312000000138798918",
"25412000000106649582",
"25412000000107170222",
"25312000000139159530",
"25412000000106967881",
"25432000000059485890",
"25312000000138737239",
"25502000000048343682",
"25312000000138755615",
"25412000000106968069",
"25412000000106904698",
"25412000000106592248",
"25412000000106953568",
"25122000000030548297",
"25412000000106448741",
"25312000000138759004",
"25312000000138749510",
"25412000000106521156",
"25422000000083037311",
"25412000000107043417",
"25412000000107145950",
"25412000000107092189",
"25352000000050816437",
"25412000000106632530",
"25532000000054146405",
"25412000000107715915",
"25512000000106769094",
"25412000000107833168",
"25352000000051012092",
"25412000000107743303",
"25412000000107722432",
"25312000000140077117",
"25512000000106431906",
"25312000000140074503",
"25412000000108090206",
"25412000000108114960",
"25442000000266690977",
"25372000000122506393",
"25442000000266693510",
"25342000000078116860",
"25412000000109236561",
"25332000000193944462",
"25412000000109684766",
"25332000000193466382",
"25312000000142292193",
"25112000000095602164",
"25232000000026682674",
"25412000000109454869",
"25132000000078999367",
"25412000000109337588",
"25412000000109434866",
"25612000000052930826",
"25372000000124804435",
"25312000000144720040",
"25612000000052821767",
"25212000000036604682",
"25442000000273172407",
"25112000000096364579",
"25502000000050714684",
"25112000000096609167",
"25412000000110802083",
"25412000000111434307",
"25412000000111297763",
"25412000000111663701",
"25132000000080414346",
"25412000000111288624",
"25412000000111314968",
"25412000000111697430",
"25122000000032224331",
"25412000000111455754",
"25412000000112047432",
"25152000000025133577",
"25312000000146916947",
"25412000000112602766",
"25312000000146916144",
"25312000000146918874",
"25512000000112158725",
"25612000000054155084",
"25372000000127043168",
"25517000000000359919",
"25352000000053646485",
"25412000000112554675",
"25312000000146917064",
"25312000000146919297",
"25322000000218664109",
"25372000000127222257",
"25132000000081136346",
"25412000000112308777",
"25412000000113361704",
"25342000000081613546",
"25412000000112746786",
"25352000000054025265",
"25412000000113459027",
"25372000000128679104",
"25412000000113271529",
"25442000000281235458",
"25312000000151411300",
"25312000000151460377",
"25532000000060442367",
"25112000000100874341",
"25412000000114743795",
"25417000000157367762",
"25412000000114488762",
"25532000000060608726",
"25352000000055569527",
"25412000000115998089",
"25412000000116697807",
"25412000000116618730",
"25412000000116616304",
"25412000000116715679",
"25412000000118410627",
"25112000000104695679",
"25412000000118837096",
"25442000000297735183",
"25312000000158466697",
"25312000000158864758",
"25412000000120941958",
"25412000000121129510",
"25312000000164515067",
"25372000000117818458",
"25112000000107017145",
];
// console.log(Maths.removeRepeat(tmp1).toString());
const tmp3 = Maths.intersection(tmp1, tmp2);
const tmp4 = Maths.complementarySet(tmp3, tmp2);
const tmp5 = Maths.removeRepeat(tmp4);
console.log(tmp5.toString());
};
try {
describe("数学模块", function () {
it("获取整数12的所有因数", function () {
expect(Maths.getFactors(12)).toEqual([
{ a: 1, b: 12 },
{ a: 2, b: 6 },
{ a: 3, b: 4 },
{ a: 4, b: 3 },
{ a: 6, b: 2 },
]);
});
it("获取整数12的所有因数通过接近程度排序", function () {
expect(Maths.getFactorsByApproach(12)).toEqual([
{ a: 3, b: 4 },
{ a: 4, b: 3 },
{ a: 2, b: 6 },
{ a: 6, b: 2 },
{ a: 1, b: 12 },
]);
});
it("数组求和", function () {
expect(Maths.sum([1, 2, 3, 4])).toEqual(10);
});
it("判断a与b符号是否相同", function () {
expect(Maths.sameSign(1, -1)).toEqual(false);
});
it("角度转弧度", function () {
expect(Maths.degreeToRad(45)).toEqual(0.7853981633974483);
});
it("弧度转角度", function () {
expect(Maths.radToDegree(0.7853981633974483)).toEqual(45);
});
it("交集 A∩B", function () {
expect(Maths.intersection([{ x: 1 }, { y: 2 }, { a: 2, z: 3 }, false, true, 1, 3, 5, { a: 2, c: [1, 2] }], [true, 5, 5, { z: 3, a: 2 }])).toEqual([{ a: 2, z: 3 }, true, 5]);
});
it("对象是否相等", function () {
expect(Maths.equal({ a: 2, z: 3 }, { z: 3, a: 2 })).toEqual(true);
});
it("删除重复项", function () {
expect(Maths.removeRepeat([{ x: 1, y: 2 }, 2, 3, { y: 2, x: 1 }, 1, 4, 5, 6, 2, 3, 4, 4, 45, 4, 31])).toEqual([{ x: 1, y: 2 }, 2, 3, 1, 4, 5, 6, 45, 31]);
});
it("补集", function () {
expect(Maths.complementarySet([{ x: 1 }, { y: 2 }, { a: 2, z: 3 }, false, true, 1, 3, 5, { a: 2, c: [1, 2] }], [true, 5, 5, { z: 3, a: 2 }, 8, { z: 3, a: 3 }])).toEqual([8, { z: 3, a: 3 }]);
});
it("并集", function () {
expect(Maths.union([1, 23, 4, 556, 14, 124], [123, 452, 231, 1, 14])).toEqual([1, 23, 4, 556, 14, 124, 123, 452, 231]);
});
it("数组 通过下标改变位置 从3的位置移到1的位置", function () {
expect(Maths.interchange([1, 2, 3, 4], 3, 1)).toEqual([1, 4, 2, 3]);
expect(Maths.interchange([1, 2, 3, 4], 3, 1, InterchangeFlag.Change)).toEqual([1, 4, 3, 2]);
});
it("阶乘", function () {
expect(Maths.iterationFactorial(10)).toEqual(3628800);
});
it("勾股定理", function () {
expect(Maths.pythagorasTheorem(3, 4)).toEqual(5);
});
it("在可及的范围内", function () {
let val = 10;
expect(val.inRange(1, 10)).toEqual(val);
expect(val.inRange(1, 9)).toEqual(9);
expect(val.inRange(11, 91)).toEqual(11);
expect(val.inRange(10, 91)).toEqual(10);
});
});
} catch (error) {
// describe is not defined 无需理会 调用方式不一致
}
export { run, description };