From ba72727fba05a6163ac9ce156caa09d27d189df2 Mon Sep 17 00:00:00 2001 From: coderaiser Date: Wed, 22 May 2019 11:44:56 +0300 Subject: [PATCH] refactor(select-by-pattern) forEach -> for-of --- client/dom/select-by-pattern.js | 71 ++++++++++++++++----------------- common/util.js | 2 - 2 files changed, 34 insertions(+), 39 deletions(-) diff --git a/client/dom/select-by-pattern.js b/client/dom/select-by-pattern.js index 32f7ab7d..32469b34 100644 --- a/client/dom/select-by-pattern.js +++ b/client/dom/select-by-pattern.js @@ -1,51 +1,48 @@ 'use strict'; -/* global DOM */ - let SelectType = '*.*'; const {getRegExp} = require('../../common/util'); +const { + alert, + prompt, +} = require('./dialog'); -module.exports = (msg, files) => { - const allMsg = `Specify file type for ${msg} selection`; - const cancel = false; - const {Dialog} = DOM; +const {DOM} = require('.'); + +module.exports = async (msg, files) => { + if (!files) + return; - Dialog.prompt(allMsg, SelectType, {cancel}).then((type) => { - SelectType = type; + const allMsg = `Specify file type for ${msg} selection`; + + const type = await prompt(allMsg, SelectType); + /* eslint require-atomic-updates: 0 */ + SelectType = type; + + const regExp = getRegExp(type); + + let matches = 0; + + for (const current of files) { + const name = DOM.getCurrentName(current); - const regExp = getRegExp(type); + if (name === '..' || !regExp.test(name)) + continue; - if (!files) - return; + ++matches; - let matches = 0; + let isSelected = DOM.isSelected(current); + const shouldSel = msg === 'expand'; - files.forEach((current) => { - const name = DOM.getCurrentName(current); - - if (name === '..') - return; - - const isMatch = regExp.test(name); - - if (!isMatch) - return; - - ++matches; - - let isSelected = DOM.isSelected(current); - const shouldSel = msg === 'expand'; - - if (shouldSel) - isSelected = !isSelected; - - if (isSelected) - DOM.toggleSelectedFile(current); - }); + if (shouldSel) + isSelected = !isSelected; - if (!matches) - Dialog.alert('Select Files', 'No matches found!'); - }); + if (isSelected) + DOM.toggleSelectedFile(current); + } + + if (!matches) + alert('No matches found!'); }; diff --git a/common/util.js b/common/util.js index 09d64db5..1e28d3a5 100644 --- a/common/util.js +++ b/common/util.js @@ -15,8 +15,6 @@ module.exports.escapeRegExp = (str) => { * get regexp from wild card */ module.exports.getRegExp = (wildcard) => { - wildcard = wildcard || '*'; - const escaped = '^' + wildcard // search from start of line .replace('.', '\\.') .replace('*', '.*')