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

重複チェックスクリプトを実装する #1

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
.vscode
Thumbs.db
node_modules
REPORT.json
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
# decomoji-duplicate-checker

CSV とデコモジ本体の全体リストを突合して重複があるかないかをチェックするスクリプト。
デコモジ DB からダウンロードした CSV とデコモジ本体の全体リストを突合し、重複があるかないかをチェックするスクリプト。

```
npm ci
node main.js[ <path-to-csv>[ <path-to-exists-json>]]
```

| label | example | default |
| :------------------ | :-------------------------------- | :-------------------------------------------- |
| path-to-csv | `~/Downloads/my-collection.csv` | `./src/candidate.csv` |
| path-to-exists-json | `~/Downloads/his-collection.json` | `./node_modules/decomoji/configs/v5_all.json` |

## ライセンス

Expand Down
61 changes: 61 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const fs = require("fs");
const isStringOfNotEmpty = require("./utilities/isStringOfNotEmpty");
const writeJsonFileSync = require("./utilities/writeJsonFileSync");

const default_path = {
csv: "./src/candidate.csv",
all: "./node_modules/decomoji/configs/v5_all.json",
};

const CSV_PATH = process.argv[2] || default_path.csv;
const ALL_PATH = process.argv[3] || default_path.all;

// CSV をパースして行ごとの配列にする
const csv = fs.readFileSync(CSV_PATH).toString().split("\n");

// 既存リストの JSON
const exists = JSON.parse(fs.readFileSync(ALL_PATH));

// 行を配列にしてキーと値群に分ける
const [header, ...values] = csv.map((v) => v.replace("\r", "").split(","));

// header[i] をキーにしたオブジェクトの配列を作る
const labelled = values.map((row) => {
const data = {};
row.forEach((value, i) => {
// 値が空文字列だったら何もしない
if (!isStringOfNotEmpty(value)) return;
data[header[i]] = value;
});
return data;
});

// `ignore: TRUE` を取り除く
const candidates = labelled.filter((v) => !v.ignore);

// 登録しようとしているリスト(candidates)と既存リスト(exists)を突合して重複しているものを作る
const duplicates = candidates.filter((c) => {
return exists.findIndex((e) => e.name === c.yomi) > -1;
});

const amount = duplicates.length;
const result = amount > 0 ? "FAIL" : "PASS";

// レポートオブジェクト
const report = {
result,
amount,
csv: CSV_PATH,
duplicates,
};

const quick_report =
amount > 0
? `[FAIL🛑]: ${amount} items duplicated! See also REPORT.json`
: "[PASS🎉]: No duplicate";

// REPORT.json を書き出す
writeJsonFileSync(report, "./REPORT.json", "silent");

// クリックレポートを出力する
console.log(quick_report);
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"name": "duplicate-checker",
"version": "0.1.0",
"description": "CSVとデコモジ本体の全体リストを突合して重複があるかないかをチェックするスクリプト。",
"description": "デコモジDBからダウンロードした CSV とデコモジ本体の全体リストを突合し、重複があるかないかをチェックするスクリプト。",
"main": "index.js",
"scripts": {
"check": "node index.js"
"update:decomoji": "rm -rf node_modules/decomoji && npm i -d decomoji/decomoji"
},
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions utilities/writeJsonFileSync.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const fs = require("fs");

// fs.writeFileSync を try catch する
const writeJsonFileSync = (buffer, filepath) => {
const writeJsonFileSync = (buffer, filepath, silent) => {
try {
fs.writeFileSync(`${filepath}`, JSON.stringify(buffer));
console.log(`${filepath} has been saved!`);
!silent && console.log(`${filepath} has been saved!`);
} catch (err) {
throw err;
}
Expand Down