-
-
Notifications
You must be signed in to change notification settings - Fork 137
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: refactor code by cursor #135
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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, | ||||||
afc163 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
); | ||||||
Comment on lines
+16
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use more descriptive variable names. I noticed that some of the variable names in the code are not very descriptive, such as 'ps', 'pos', 'acceptation', 'orig', and 'trans'. It would improve the readability of the code if these were renamed to something more descriptive. For example, 'ps' could be renamed to 'phoneticSymbols', 'pos' could be 'partsOfSpeech', 'acceptation' could be 'meanings', 'orig' could be 'originalSentence', and 'trans' could be 'translatedSentence'. This would make it easier for other developers to understand what these variables represent when they read the code.
|
||||||
|
||||||
// phonetic symbol | ||||||
data.ps?.forEach?.((item, i) => { | ||||||
Comment on lines
13
to
21
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code modifies the Recommendation: Create a new object or use destructuring to avoid mutating the input object directly. const { ps, pos, acceptation } = data;
const processedPs = typeof ps === 'string' ? [ps] : ps;
const processedPos = typeof pos === 'string' ? [pos] : pos;
const processedAcceptation = typeof acceptation === 'string' ? [acceptation] : acceptation; |
||||||
|
@@ -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, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactored sentence handling logic. Ensure that the new approach correctly handles all edge cases, such as when
afc163 marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The condition does not handle cases where subItem is an empty array or a non-array object, which could lead to runtime errors. It should explicitly check if subItem is an array and has a length greater than 0. |
||||||
); | ||||||
log(`${chalk.gray(`${i + 1}. `)}${highlight(item.orig, data.key)}`); | ||||||
log(` ${chalk.cyan(item.trans)}`); | ||||||
}); | ||||||
Comment on lines
43
to
51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The nested mapping and conditional logic within the Recommendation: Simplify the mapping logic and add checks for data.sent?.forEach?.((item, i) => {
const orig = typeof item.orig === 'string' ? item.orig.trim() : (item.orig?.[0]?.trim() || '');
const trans = typeof item.trans === 'string' ? item.trans.trim() : (item.trans?.[0]?.trim() || '');
log(`${chalk.gray(`${i + 1}. `)}${highlight(orig, data.key)}`);
log(` ${chalk.cyan(trans)}`);
});
Comment on lines
45
to
51
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add type checking for item.orig and item.trans. It's important to add type checking for item.orig and item.trans before accessing their properties or methods. This will prevent potential errors if these properties are undefined or not of the expected type. Consider modifying the code as follows: data.sent?.forEach?.((item, i) => {
if (item && typeof item === 'object') {
[item.orig, item.trans] = [item.orig, item.trans].map((subItem) =>
Array.isArray(subItem) && subItem[0] ? subItem[0].trim() : (typeof subItem === 'string' ? subItem.trim() : '')
);
log(`${chalk.gray(`${i + 1}. `)}${highlight(item.orig, data.key)}`);
log(` ${chalk.cyan(item.trans)}`);
}
}); This change ensures that the code handles various possible types of item.orig and item.trans, preventing potential runtime errors.
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo fixed in the file path: 'searchHistroy.txt' to 'searchHistory.txt'. Ensure that this change does not affect any other parts of the application that rely on the old file path.
afc163 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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')) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactored logic for filtering and mapping search history data. Ensure that the new approach correctly handles all edge cases, such as when |
||
.filter(([day]) => day >= startDay && day <= endDayValue) | ||
.map(([day, content]) => `${DAY_SPLIT}${day}:\n${content}`); | ||
} | ||
|
||
// 按👇👇👇 格式化 | ||
Comment on lines
13
to
24
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The function Recommendation: Add validation to ensure that the content is in the expected format before processing. If the format is incorrect, handle the error gracefully, possibly by logging a warning or returning an empty array. |
||
|
@@ -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(); | ||
Comment on lines
41
to
46
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The code reads the entire content of the search history file into memory using Recommendation: Consider using a streaming approach to read the file content, which can handle large files more efficiently. For example, you can use |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Refactored array handling and string conversion logic. Ensure that the new approach correctly handles all edge cases, such as when
data.ps
,data.pos
, ordata.acceptation
are undefined or null.