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

feat: input parser #115

Merged
merged 13 commits into from
Jan 20, 2020
11 changes: 11 additions & 0 deletions .circleci/.editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
root = true

[*]

indent_style = space
indent_size = 2

end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false
6 changes: 6 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ jobs:
when: always
command: |
npm run eslint:ci
# テスト
- run:
name: test
when: always
command: |
npm test -- --tap | npx tap-xunit > results/ava.xml
- store_test_results:
path: results
- store_artifacts:
Expand Down
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,13 @@ end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = false

[package.json]

indent_style = space
indent_size = 2
blhsrwznrghfzpr marked this conversation as resolved.
Show resolved Hide resolved

end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
5 changes: 5 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
"ecmaVersion": 2018,
"sourceType": "module"
},
"settings": {
"jsdoc": {
"mode": "typescript"
}
},
"rules": {
"semi": [
"error",
Expand Down
44 changes: 44 additions & 0 deletions js/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,50 @@ function $(id) {
return document.getElementById(id);
}

/**
* @param {string} input 入力文字列
* @param {(tootIds: string[])=>void} onPURL 入力がまとめのURLだったときのcallback
* @param {(tootId: string)=>void} onToot 入力がTooT URLかToot IDだったときのcallback
* @returns {void} callbackが返した値
yumetodo marked this conversation as resolved.
Show resolved Hide resolved
*/
export function inputParser(input, onPURL, onToot) {
Copy link
Contributor

Choose a reason for hiding this comment

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

これ設計的な話ですけど、onPURLとonTootを渡した方がいいですか?
引数をinputだけにして、戻り値をstring[]だけにする(tootIdだけの場合もarrayにしちゃう)か、戻り値にPURLかTootかの区分を持たせるようにして、callbackを渡さないようにしたほうが、色々処理しやすそうな気はしました。
(強制したいということではないので、判断に従います)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

処理フローが違うので、戻り値で色々やるのは二度手間かなと思ったりしてました。

/**
* @param {string} i
* @returns {string}
*/
const assumeIsTootId = i => {
if (/[^0-9]/.test(i) || isNaN(parseInt(i))) {
throw new Error("invalid id syntax.");
}
return i;
};
/** @type {URL} */
let url;
try {
url = new URL(input);
} catch (_) {
// console.log("a", input);
onToot(UncompressOrPassThroughTootId(input));
return;
}
if (/twitter/.test(url.hostname)) {
throw new Error("Twitter URL is not allowed.");
}
if (url.searchParams != null && url.searchParams.has("t")) {
// console.log("b", input);
const t = url.searchParams.get("t").split(",");
onPURL(t.map(v => UncompressOrPassThroughTootId(v)));
} else {
// pathnameは常に"/"から始まる
const p = url.pathname;
if (!p.startsWith("/web/statuses")) {
throw new Error("This is not a mastodon's toot URL.");
}
// console.log("c", p, input);
onToot(assumeIsTootId(p.substring(p.lastIndexOf("/") + 1)));
}
}

/**
* @param {number} index
* @param {string} prefix
Expand Down
Loading