Bläddra i källkod

refactor: 优化reduce写法

Michael Wang 3 år sedan
förälder
incheckning
5950dea2fa
2 ändrade filer med 14 tillägg och 10 borttagningar
  1. 8 5
      src/features/mark/CommonMarkBody.vue
  2. 6 5
      src/utils/utils.ts

+ 8 - 5
src/features/mark/CommonMarkBody.vue

@@ -317,11 +317,14 @@ async function processSplitConfig() {
   }
   // 裁切块,可能是一块,两块,三块... [start, width ...] => [0, 0.3] | [0, 0.55, 0.45, 0.55] | [0, 0.35, 0.33, 0.35, 0.66, 0.35]
   // 要转变为 [[0, 0.3]] | [[0, 0.55], [0.45, 0.55]] | [[0, 0.35], [0.33, 0.35], [0.66, 0.35]]
-  const splitConfigPairs = store.setting.splitConfig
-    .map<[number, number]>((v, index, ary) =>
-      index % 2 === 0 ? [v, ary[index + 1]] : [-1, -1]
-    )
-    .filter((v) => v[0] > -1);
+  const splitConfigPairs = store.setting.splitConfig.reduce<[number, number][]>(
+    (a, v, index) => {
+      // 偶数位组成数组的第一位,奇数位组成数组的第二位
+      index % 2 === 0 ? a.push([v, -1]) : (a.at(-1)![1] = v);
+      return a;
+    },
+    []
+  );
 
   // 最大的 splitConfig 的宽度
   const maxSplitConfig = Math.max(

+ 6 - 5
src/utils/utils.ts

@@ -215,11 +215,12 @@ export async function preDrawImage(_currentTask: Task) {
       images.push(image);
     }
 
-    const splitConfigPairs = store.setting.splitConfig
-      .map<[number, number]>((v, index, ary) =>
-        index % 2 === 0 ? [v, ary[index + 1]] : [-1, -1]
-      )
-      .filter((v) => v[0] > -1);
+    const splitConfigPairs = store.setting.splitConfig.reduce<
+      [number, number][]
+    >((a, v, index) => {
+      index % 2 === 0 ? a.push([v, -1]) : (a.at(-1)![1] = v);
+      return a;
+    }, []);
 
     const maxSplitConfig = Math.max(...store.setting.splitConfig);
     maxSliceWidth =