Skip to content

Commit

Permalink
refactor: refactor code by cursor (#135)
Browse files Browse the repository at this point in the history
  • Loading branch information
afc163 authored Sep 21, 2024
1 parent b711317 commit f061005
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 29 deletions.
21 changes: 6 additions & 15 deletions lib/print.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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)}`);
});
Expand Down
20 changes: 6 additions & 14 deletions lib/searchHistory.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ' ';
Expand All @@ -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}`);
}

// 按👇👇👇 格式化
Expand All @@ -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();
Expand Down

0 comments on commit f061005

Please sign in to comment.