diff --git a/lib/print.js b/lib/print.js index 45ffa64..b771c27 100644 --- a/lib/print.js +++ b/lib/print.js @@ -13,15 +13,9 @@ exports.iciba = (data, options = {}) => { firstLine += `${data.key} `; // maybe string - if (typeof data.ps === 'string') { - data.ps = [data.ps]; - } - if (typeof data.pos === 'string') { - data.pos = [data.pos]; - } - if (typeof data.acceptation === 'string') { - data.acceptation = [data.acceptation]; - } + [data.ps, data.pos, data.acceptation] = [data.ps, data.pos, data.acceptation].map((item) => + typeof item === 'string' ? [item] : item, + ); // phonetic symbol data.ps?.forEach?.((item, i) => { @@ -49,12 +43,9 @@ exports.iciba = (data, options = {}) => { } data.sent?.forEach?.((item, i) => { - if (typeof item.orig !== 'string' && item.orig[0]) { - item.orig = item.orig[0].trim(); - } - if (typeof item.trans !== 'string' && item.trans[0]) { - item.trans = item.trans[0].trim(); - } + [item.orig, item.trans] = [item.orig, item.trans].map((subItem) => + typeof subItem !== 'string' && subItem[0] ? subItem[0].trim() : subItem, + ); log(`${chalk.gray(`${i + 1}. `)}${highlight(item.orig, data.key)}`); log(` ${chalk.cyan(item.trans)}`); }); diff --git a/lib/searchHistory.js b/lib/searchHistory.js index 607c4ad..0a9581f 100644 --- a/lib/searchHistory.js +++ b/lib/searchHistory.js @@ -4,7 +4,7 @@ const chalk = require('chalk'); const dayjs = require('dayjs'); const homedir = process.env.HOME || require('node:os').homedir(); -const searchFilePath = path.resolve(homedir, '.config', 'fanyi', 'searchHistroy.txt'); +const searchFilePath = path.resolve(homedir, '.config', 'fanyi', 'searchHistory.txt'); const DAY_SPLIT = 'day-'; const WORD_MEAN_SPLIT = ' '; @@ -13,19 +13,12 @@ const today = dayjs().format('YYYY-MM-DD'); // @return Array<`day+words`> function getTargetContent(content, startDay, endDay) { - // 无结束日期,则为查指定日期 const endDayValue = endDay || startDay; - const allDays = content.split(DAY_SPLIT); - const targetData = []; - for (const item of allDays) { - if (item.length) { - const [day] = item.split(':\n'); - if (day >= startDay && day <= endDayValue) { - targetData.push(item); - } - } - } - return targetData; + return content + .split(DAY_SPLIT) + .map((item) => item.split(':\n')) + .filter(([day]) => day >= startDay && day <= endDayValue) + .map(([day, content]) => `${DAY_SPLIT}${day}:\n${content}`); } // 按👇👇👇 格式化 @@ -48,7 +41,6 @@ exports.searchList = (args) => { console.log(chalk.gray('fanyi history:')); console.log(); let targetContent; - // 与配置放在一起 fs.ensureFileSync(searchFilePath); const data = fs.readFileSync(searchFilePath); const content = data.toString();