Skip to content
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

chore: CSVパーサーのリファクタリング #700

Open
wants to merge 9 commits into
base: main
Choose a base branch
from

Conversation

kiharu3112
Copy link
Contributor

@kiharu3112 kiharu3112 commented Nov 16, 2024

close #637
意図と違ったらごめん

Copy link

cloudflare-workers-and-pages bot commented Nov 16, 2024

Deploying kcmsx with  Cloudflare Pages  Cloudflare Pages

Latest commit: 3ae6dac
Status: ✅  Deploy successful!
Preview URL: https://d0fe50d3.kcmsx.pages.dev
Branch Preview URL: https://chore-csv-parser-to-object.kcmsx.pages.dev

View logs

@kiharu3112 kiharu3112 requested review from tufusa, laminne and speak-mentaiko and removed request for tufusa and laminne November 16, 2024 15:49
@kiharu3112 kiharu3112 enabled auto-merge (squash) November 16, 2024 15:50
Copy link
Member

@tufusa tufusa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

通知の表示は明らかに副作用なので、この関数から分離しているとうれしいです。

};

export const checkData = (data: CSVRow[]) => {
const newErrors = data.map(() => [false, false, false, false, false, false]);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

二重配列でなく、CSVDataのようにオブジェクトにして、値にbooleanではなくエラーコードを仕舞うようにするのがよいでしょう。

Comment on lines +64 to +80
const teamNames = data.map((row) => row.teamName);
const duplicateTeamNames = teamNames.filter(
(name, index) => teamNames.indexOf(name) !== index
);

// 重複するteamNameがあれば、該当する全ての行にエラーフラグを設定
if (duplicateTeamNames.length > 0) {
duplicateTeamNames.forEach((dupName) => {
teamNames.forEach((name, index) => {
if (name === dupName) {
newErrors[index][0] = true;
isError = true;
notifyError("duplicateTeamName");
}
});
});
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

計算量がO(n^2)になっているので、SetHashを使ってください。

});
});
}
return { newErrors, isError };
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return { newErrors, isError };
return { errors, isError };


export const checkData = (data: CSVRow[]) => {
const newErrors = data.map(() => [false, false, false, false, false, false]);
let isError = false;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

letはやめましょう。
この場合、isErrorerrorsから導出できるはずです。

@@ -169,7 +123,7 @@ export const RegisterBulk = () => {

return (
<>
<h1>一括エントリー</h1>
<Text>一括エントリー</Text>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<Text>一括エントリー</Text>
<Title>一括エントリー</Title>

Comment on lines +31 to +32
robotType: RobotType;
departmentType: DepartmentType;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

パースした直後にこの型を使っているので、RobotTypeDepartmentTypeとは確定しないはずです。stringがよいでしょう。

});
};

export const checkData = (data: CSVRow[]) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

React的にはこれはカスタムフックであったほうが嬉しいです。

const {isError, errors} = useCheckData(data);

のような使い方ができるようにしてほしいです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 👀 In review
Development

Successfully merging this pull request may close these issues.

refactor: CSVパースのリファクタリング
2 participants