소스 검색

core: remove use of `Array.prototype.reduce` where possible (#3016)

Antoine du Hamel 3 년 전
부모
커밋
632ac377f6
3개의 변경된 파일20개의 추가작업 그리고 25개의 파일을 삭제
  1. 4 6
      examples/transloadit-textarea/main.js
  2. 12 13
      packages/@uppy/provider-views/src/SharedHandler.js
  3. 4 6
      website/src/examples/markdown-snippets/app.es6

+ 4 - 6
examples/transloadit-textarea/main.js

@@ -94,12 +94,10 @@ class MarkdownTextarea {
       }
       }
     })
     })
 
 
-    return Object.keys(filesById).reduce((acc, key) => {
-      const file = filesById[key]
-      const thumb = thumbsById[key]
-      acc.push({ file, thumb })
-      return acc
-    }, [])
+    return Object.keys(filesById).map((key) => ({
+      file : filesById[key],
+      thumb : thumbsById[key],
+    }))
   }
   }
 
 
   uploadFiles (files) {
   uploadFiles (files) {

+ 12 - 13
packages/@uppy/provider-views/src/SharedHandler.js

@@ -36,29 +36,28 @@ module.exports = class SharedHandler {
     // Shift-clicking selects a single consecutive list of items
     // Shift-clicking selects a single consecutive list of items
     // starting at the previous click and deselects everything else.
     // starting at the previous click and deselects everything else.
     if (this.lastCheckbox && e.shiftKey) {
     if (this.lastCheckbox && e.shiftKey) {
-      let currentSelection
       const prevIndex = items.indexOf(this.lastCheckbox)
       const prevIndex = items.indexOf(this.lastCheckbox)
       const currentIndex = items.indexOf(file)
       const currentIndex = items.indexOf(file)
-      if (prevIndex < currentIndex) {
-        currentSelection = items.slice(prevIndex, currentIndex + 1)
-      } else {
-        currentSelection = items.slice(currentIndex, prevIndex + 1)
-      }
+      const currentSelection = (prevIndex < currentIndex)
+        ? items.slice(prevIndex, currentIndex + 1)
+        : items.slice(currentIndex, prevIndex + 1)
+      const reducedCurrentSelection = []
+
       // Check restrictions on each file in currentSelection,
       // Check restrictions on each file in currentSelection,
       // reduce it to only contain files that pass restrictions
       // reduce it to only contain files that pass restrictions
-      currentSelection = currentSelection.reduce((reducedCurrentSelection, item) => {
-        const uppy = this.plugin.uppy
+      for (const item of currentSelection) {
+        const { uppy } = this.plugin
         const validatedRestrictions = uppy.validateRestrictions(
         const validatedRestrictions = uppy.validateRestrictions(
           remoteFileObjToLocal(item),
           remoteFileObjToLocal(item),
           [...uppy.getFiles(), ...reducedCurrentSelection]
           [...uppy.getFiles(), ...reducedCurrentSelection]
         )
         )
-        if (!validatedRestrictions.result) {
+        if (validatedRestrictions.result) {
+          reducedCurrentSelection.push(item)
+        } else {
           uppy.info({ message: validatedRestrictions.reason }, 'error', uppy.opts.infoTimeout)
           uppy.info({ message: validatedRestrictions.reason }, 'error', uppy.opts.infoTimeout)
-          return reducedCurrentSelection
         }
         }
-        return [...reducedCurrentSelection, item]
-      })
-      this.plugin.setPluginState({ currentSelection })
+      }
+      this.plugin.setPluginState({ currentSelection: reducedCurrentSelection })
       return
       return
     }
     }
 
 

+ 4 - 6
website/src/examples/markdown-snippets/app.es6

@@ -98,12 +98,10 @@ class MarkdownTextarea {
       }
       }
     })
     })
 
 
-    return Object.keys(filesById).reduce((acc, key) => {
-      const file = filesById[key]
-      const thumb = thumbsById[key]
-      acc.push({ file, thumb })
-      return acc
-    }, [])
+    return Object.keys(filesById).map((key) => ({
+      file : filesById[key],
+      thumb : thumbsById[key],
+    }))
   }
   }
 
 
   uploadFiles (files) {
   uploadFiles (files) {