From 3b053628b3ab016485dde6fe49333b0a5cd202f5 Mon Sep 17 00:00:00 2001 From: Neila Date: Sat, 8 Jun 2024 21:12:00 +0800 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20=E9=87=8D=E5=86=99for?= =?UTF-8?q?=E5=92=8CforEach=E4=B8=BAmap?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/core/prebuild.ts | 18 ++---- .../core/src/app/index/sidebar/searchBase.ts | 10 ++- packages/shared/Recently.ts | 5 -- packages/shared/matrix/base.ts | 9 +-- packages/shared/range.ts | 5 ++ packages/tools/tool-cylinder/makeCylinder.ts | 9 +-- packages/tools/tool-jigsaw/page.tsx | 5 +- packages/tools/tool-mathgen/calcMath.ts | 62 +++++++++---------- packages/tools/tool-pi/generatePis.ts | 9 +-- packages/tools/tool-pillar/page.tsx | 1 - packages/tools/tool-probability/page.tsx | 11 ++-- packages/tools/tool-shaizi/Cube.tsx | 15 +---- packages/tools/tool-shaizi/page.tsx | 4 +- packages/tools/tool-shaizi/shaiziCanvas.tsx | 2 +- .../tools/tool-speech/getShortTimeEnergy.ts | 13 ++-- 15 files changed, 75 insertions(+), 103 deletions(-) create mode 100644 packages/shared/range.ts diff --git a/packages/core/prebuild.ts b/packages/core/prebuild.ts index db637cae..040c605f 100644 --- a/packages/core/prebuild.ts +++ b/packages/core/prebuild.ts @@ -27,22 +27,12 @@ async function devMain() { return oldPackage; } async function publicMain() { - const pages = ChildProcess.execSync(`find ./src/app -name '*page.tsx'`).toString().replaceAll("./src/app", "").replaceAll("page.tsx", "").split("\n"); - pages.forEach((single, index) => { + const pages = ChildProcess.execSync(`find ./src/app -name '*page.tsx'`).toString().replaceAll("./src/app", "").replaceAll("page.tsx", "").split("\n").map(single => { if (single !== "/") { - pages[index] = pages[index].substr(0, pages[index].length - 1); + return single.substr(0, single.length - 1); } - }); - pages.forEach((single, index) => { - if (single === "/handle") { - pages.splice(index, 1); - } - }); - pages.forEach((single, index) => { - if (single === "") { - pages.splice(index, 1); - } - }); + return "/"; + }).filter(single => single !== ""); const pagesJSON = JSON.stringify(pages, null, 4); fs.writeFileSync("./src/pages.json", pagesJSON); const NextConfig = await build({ diff --git a/packages/core/src/app/index/sidebar/searchBase.ts b/packages/core/src/app/index/sidebar/searchBase.ts index a60be152..f952660f 100644 --- a/packages/core/src/app/index/sidebar/searchBase.ts +++ b/packages/core/src/app/index/sidebar/searchBase.ts @@ -3,11 +3,9 @@ import { tool } from "tools/info"; export default function searchBase(sortedTools: tool[], search: string) { - const calcTools: tool[] = [], - lower = String(search).toLowerCase(); - sortedTools.forEach(tool => { + const lower = String(search).toLowerCase(); + return sortedTools.filter(tool => { const to = String(tool.to); - if (tool.desc.toLowerCase().includes(lower) || to.includes(search) || tool.name.toLowerCase().includes(lower)) calcTools.push(tool); - }); - return calcTools; + return tool.desc.toLowerCase().includes(lower) || to.includes(search) || tool.name.toLowerCase().includes(lower); + }) } diff --git a/packages/shared/Recently.ts b/packages/shared/Recently.ts index e2296296..79233e04 100644 --- a/packages/shared/Recently.ts +++ b/packages/shared/Recently.ts @@ -27,11 +27,6 @@ export default class Recently extends Set { super.add(name); } if (super.size > this.max) { - this.get().forEach((item, index) => { - if (index === 0) { - super.delete(item); - } - }); super.delete(this.get()[0]); } return this; diff --git a/packages/shared/matrix/base.ts b/packages/shared/matrix/base.ts index c881e6a7..656f1035 100644 --- a/packages/shared/matrix/base.ts +++ b/packages/shared/matrix/base.ts @@ -1,6 +1,7 @@ import { Palette } from "@mui/material"; +import range from "../range"; import { block } from "./matrix"; @@ -24,8 +25,8 @@ export default function drawMatrixBase(edge: number, n: number, blocks: block[], dos.forEach((item, index) => { cxt.fillStyle = item[1]; const path = new Path2D(), pBlock = item[0].map(item => item.toString()); - for (let i = 0; i <= n; i++) { - for (let j = 0; j < n; j++) { + for (let i of range(n)) { + for (let j of range(n)) { if (pBlock.includes([i, j].toString())) { path.rect(size * j, size * i, size, size); } @@ -34,8 +35,8 @@ export default function drawMatrixBase(edge: number, n: number, blocks: block[], cxt.fill(path); }); if (!onlyPos) { - for (let i = 0; i < n; i++) { - for (let j = 0; j < n; j++) { + for (let i of range(n - 1)) { + for (let j of range(n - 1)) { cxt.rect(size * j, size * i, size, size); } } diff --git a/packages/shared/range.ts b/packages/shared/range.ts new file mode 100644 index 00000000..2e7d3b34 --- /dev/null +++ b/packages/shared/range.ts @@ -0,0 +1,5 @@ +export default function* range(top: number, initial: number = 0) { + for (let i = initial; i <= top; i++) { + yield i; + } +} diff --git a/packages/tools/tool-cylinder/makeCylinder.ts b/packages/tools/tool-cylinder/makeCylinder.ts index d1cfee16..24da44c2 100644 --- a/packages/tools/tool-cylinder/makeCylinder.ts +++ b/packages/tools/tool-cylinder/makeCylinder.ts @@ -1,6 +1,7 @@ import { block } from "@verkfi/shared/matrix/matrix"; +import range from "@verkfi/shared/range"; /** * 生成一个圆。 * ![cylinder](./cylinder.png) @@ -35,7 +36,7 @@ export default class Cylinder extends Array { let nextMinXn = 0; const minInvRadiusX = 1 / (radiusX - thickness), minInvRadiusZ = 1 / (radiusZ - thickness); - forX: for (let x = 0; x <= ceilRadiusX; ++x) { + forX: for (let x of range(ceilRadiusX)) { const xn = nextXn, dx2 = nextMinXn * nextMinXn; nextXn = (x + 1) * invRadiusX; @@ -43,7 +44,7 @@ export default class Cylinder extends Array { let nextZn = 0, nextMinZn = 0; xSqr = xn * xn; - forZ: for (let z = 0; z <= ceilRadiusZ; ++z) { + for (let z of range(ceilRadiusZ)) { const zn = nextZn, dz2 = nextMinZn * nextMinZn; nextZn = (z + 1) * invRadiusZ; @@ -63,12 +64,12 @@ export default class Cylinder extends Array { } } } else { - forX: for (let x = 0; x <= ceilRadiusX; ++x) { + forX: for (let x of range(ceilRadiusX)) { const xn = nextXn; nextXn = (x + 1) * invRadiusX; let nextZn = 0; xSqr = xn * xn; - forZ: for (let z = 0; z <= ceilRadiusZ; ++z) { + for (let z of range(ceilRadiusZ)) { const zn = nextZn; nextZn = (z + 1) * invRadiusZ; zSqr = zn * zn; diff --git a/packages/tools/tool-jigsaw/page.tsx b/packages/tools/tool-jigsaw/page.tsx index 8c4ab7f5..23039126 100644 --- a/packages/tools/tool-jigsaw/page.tsx +++ b/packages/tools/tool-jigsaw/page.tsx @@ -45,6 +45,7 @@ import { } from "react-intl-universal"; import useStoragedState from "@verkfi/shared/reader/useStoragedState"; import canvasToBlob from "./canvasToBlob"; +import range from "@verkfi/shared/range"; type block = Blob & { rotation: number; }; @@ -116,9 +117,9 @@ export default function JigsawEntry(): JSX.Element { splited: Blob[][] = []; canvas.width = img.width / width; canvas.height = img.height / height; - for (let y = 0; y < height; y++) { + for (let y of range(height - 1)) { const thisBuffer: Blob[] = []; - for (let x = 0; x < width; x++) { + for (let x of range(width - 1)) { context.drawImage(img, x * img.width / width, y * img.height / height, img.width / width, img.height / height, 0, 0, img.width / width, img.height / height); thisBuffer.push(await canvasToBlob(canvas)); context.clearRect(0, 0, img.width / width, img.height / height); diff --git a/packages/tools/tool-mathgen/calcMath.ts b/packages/tools/tool-mathgen/calcMath.ts index 378f5544..17eddb57 100644 --- a/packages/tools/tool-mathgen/calcMath.ts +++ b/packages/tools/tool-mathgen/calcMath.ts @@ -8,39 +8,37 @@ import genNumber from "./genNumber"; import { evaluate } from "mathjs"; -export default function calcMath(calcs: calc[], subtractionCheck: boolean, divisionCheck: boolean, max: number, min: number, itemCount: number, setMath: setState) { - const calcMaths: string[] = []; - calcs.forEach(function (mode) { - const modeS = mode.replace("×", "*").replace("÷", "/"); - function genMathS(): [number, number, number] { - const one = genNumber(max, min); - let two = genNumber(max, min); - if (subtractionCheck || divisionCheck) { - switch (mode) { - case "-": - case "÷": - while (two > one) { - two = genNumber(max, min); - } - break; - default: - break; - } +import range from "@verkfi/shared/range"; +const calcMath = (calcs: calc[], subtractionCheck: boolean, divisionCheck: boolean, max: number, min: number, itemCount: number, setMath: setState) => setMath([...new Set(calcs.map(mode => { + const modeS = mode.replace("×", "*").replace("÷", "/"); + function genMathS(): [number, number, number] { + const one = genNumber(max, min); + let two = genNumber(max, min); + if (subtractionCheck || divisionCheck) { + switch (mode) { + case "-": + case "÷": + while (two > one) { + two = genNumber(max, min); + } + break; + default: + break; } - return [one, two, evaluate(`${one} ${modeS} ${two}`)]; } - for (let step = 1; step < (itemCount / (calcs.length)); step++) { - let [one, two, out] = genMathS(), - math = `${one}${mode}${two}=${out}`; - function reGenMath() { - [one, two, out] = genMathS(); - math = `${one}${mode}${two}=${out}`; - } - while (out > max || calcMaths.includes(math) || (mode === "%" && two === 0)) { - reGenMath(); - } - calcMaths.push(math); + return [one, two, evaluate(`${one} ${modeS} ${two}`)]; + } + return [...range(itemCount / (calcs.length) - 1, 1)].map(step => { + let [one, two, out] = genMathS(), + math = `${one}${mode}${two}=${out}`; + function reGenMath() { + [one, two, out] = genMathS(); + math = `${one}${mode}${two}=${out}`; + } + while (out > max || (mode === "%" && two === 0)) { + reGenMath(); } + return math; }); - return setMath(calcMaths); -} +})).keys()].flat(14)); // 14 没有什么意义 +export default calcMath; diff --git a/packages/tools/tool-pi/generatePis.ts b/packages/tools/tool-pi/generatePis.ts index f3766abe..f32fa972 100644 --- a/packages/tools/tool-pi/generatePis.ts +++ b/packages/tools/tool-pi/generatePis.ts @@ -1,10 +1,7 @@ "use client"; +import range from "@verkfi/shared/range"; import generateDigitsOfPi from "./generateDigitsOfPi"; export default function generatePis(digit: number) { - let ret = "3.", - generaterInstance = generateDigitsOfPi(); - for (let i = 0; i < digit; i++) { - ret += (generaterInstance.next().value as number).toString(); - } - return ret; + const generaterInstance = generateDigitsOfPi(); + return `3.${[...range(digit - 1)].map(() => (generaterInstance.next().value as number).toString()).join("")}`; } diff --git a/packages/tools/tool-pillar/page.tsx b/packages/tools/tool-pillar/page.tsx index bf4a6433..85027a52 100644 --- a/packages/tools/tool-pillar/page.tsx +++ b/packages/tools/tool-pillar/page.tsx @@ -87,7 +87,6 @@ export default function Pillar(): JSX.Element { [length, setLength] = useState(0), [filterRules, setFilterRules] = useState(emptyFilterRules as filterRules), choosesId = useId(), - valueSelectLabelId = useId(), sizeLabelId = useId(), pillars = calcPillars(type, length), filteredPillars = pillars.filter(single => Object.entries(filterRules).every(singleRule => ( diff --git a/packages/tools/tool-probability/page.tsx b/packages/tools/tool-probability/page.tsx index a6657603..1f56baee 100644 --- a/packages/tools/tool-probability/page.tsx +++ b/packages/tools/tool-probability/page.tsx @@ -30,6 +30,7 @@ import { VictoryBar, VictoryChart } from "victory"; +import range from "@verkfi/shared/range"; export default function Probability(): JSX.Element { const [code, setCode] = useState(`// ${get("probability.tip")}`), theme = useTheme(), @@ -40,7 +41,7 @@ export default function Probability(): JSX.Element { x: any, y: number }[] = []; - datas.map(data => { + datas.forEach(data => { const index = objectDatas.findIndex(objectData => objectData.x === data); if (index === -1) { return objectDatas.push({ @@ -63,12 +64,8 @@ export default function Probability(): JSX.Element { {get("probability.import")}