Created
October 6, 2016 04:51
-
-
Save rndomhack/b7df13ad5cb11f6223b49c9d7dd2a5db to your computer and use it in GitHub Desktop.
Get basic info from title
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
const charString = "〇一二三四五六七八九零壱弐参肆伍陸質捌玖壹貳參十百千拾佰仟陌阡"; | |
const chars1 = [ | |
"〇", "一", "二", "三", "四", "五", "六", "七", "八", "九", | |
"零", "壱", "弐", "参", "肆", "伍", "陸", "質", "捌", "玖", | |
void 0, "壹", "貳", "參" | |
]; | |
const chars2 = [ | |
"十", "百", "千", | |
"拾", "佰", "仟", | |
void 0, "陌", "阡" | |
]; | |
function escapeRegString(str) { | |
return str.replace(/([.*+?^=!:${}()|[\]\/\\])/g, "\\$1"); | |
} | |
function getCount(str, counts) { | |
let reg, match; | |
for (const count of counts) { | |
reg = new RegExp(`^${count[0]}([\\d0-9]+)${count[1]}`); | |
match = str.match(reg); | |
if (match !== null) { | |
return { | |
count: Number.parseInt(match[1].replace(/[0-9]/g, s => String.fromCharCode(s.charCodeAt(0) - 0xFEE0)), 10), | |
replaced: str.replace(reg, "").trim() | |
}; | |
} | |
reg = new RegExp(`^${count[0]}([${charString}]+)${count[1]}`); | |
match = str.match(reg); | |
if (match !== null) { | |
const chars = match[1].split(""); | |
let number = 0; | |
let temp = 1; | |
for (let i = 0; i < chars.length; i++) { | |
const char = chars[i]; | |
if (chars1.includes(char)) { | |
temp = chars1.indexOf(char) % 10; | |
if (i === chars.length - 1) { | |
number += temp; | |
} | |
} else { | |
number += Math.pow(10, chars2.indexOf(char) % 3 + 1) * temp; | |
temp = 1; | |
} | |
} | |
return { | |
count: number, | |
replaced: str.replace(reg, "").trim() | |
}; | |
} | |
} | |
return null; | |
} | |
function getSubtitle(str, brackets) { | |
let reg, match; | |
for (const bracket of brackets) { | |
reg = new RegExp(`^${escapeRegString(bracket[0])}(.*?)${escapeRegString(bracket[1])}`); | |
match = str.match(reg); | |
if (match !== null) { | |
return { | |
subtitle: match[1], | |
replaced: str.replace(reg, "").trim() | |
}; | |
} | |
} | |
return null; | |
} | |
function getTitle(str, brackets) { | |
const reg = new RegExp(`^\\S[^\\s${escapeRegString(brackets.map(bracket => bracket[0]).join(""))}]*`); | |
const match = str.match(reg); | |
if (match !== null) { | |
return { | |
title: match[0], | |
replaced: str.replace(reg, "").trim() | |
}; | |
} | |
return null; | |
} | |
function getInfo(str, settings) { | |
const result = { | |
title: null, | |
subtitle: null, | |
count: null | |
}; | |
let reg, match, obj; | |
str = str.trim(); | |
for (const replaceString of settings.replaceStrings) { | |
reg = RegExp(`${escapeRegString(replaceString)}`, "g"); | |
match = str.match(reg); | |
if (match !== null) { | |
str = str.replace(reg, "").trim(); | |
} | |
} | |
for (const replaceBracket of settings.replaceBrackets) { | |
reg = RegExp(`${escapeRegString(replaceBracket[0])}.*?${escapeRegString(replaceBracket[1])}`, "g"); | |
match = str.match(reg); | |
if (match !== null) { | |
str = str.replace(reg, "").trim(); | |
} | |
} | |
while (str !== "") { | |
if ((obj = getCount(str, settings.counts)) !== null) { | |
result.count = obj.count; | |
str = obj.replaced; | |
} else if ((obj = getSubtitle(str, settings.brackets)) !== null) { | |
if (result.title === null) { | |
result.title = obj.subtitle; | |
} else if (result.subtitle === null) { | |
result.subtitle = obj.subtitle; | |
} else { | |
result.subtitle += `, ${obj.subtitle}`; | |
} | |
str = obj.replaced; | |
} else if ((obj = getTitle(str, settings.brackets)) !== null) { | |
if (result.subtitle === null) { | |
if (result.count === null) { | |
if (result.title === null) { | |
result.title = obj.title; | |
} else { | |
result.title += ` ${obj.title}`; | |
} | |
} else { | |
result.subtitle = obj.title; | |
} | |
} | |
str = obj.replaced; | |
} else { | |
break; | |
} | |
} | |
return result; | |
} | |
function main() { | |
const str = process.argv[2]; | |
const settings = { | |
replaceStrings: [ | |
"アニメイズム", | |
"<アニメ>", | |
"TVアニメ・", | |
"TVアニメ", | |
"アニメ・", | |
"アニメ" | |
], | |
replaceBrackets: [ | |
["[", "]"], | |
["【", "】"], | |
["<", ">"], | |
["(", ")"] | |
], | |
brackets: [ | |
["「", "」"], | |
["『", "』"] | |
], | |
counts: [ | |
["第", "話"], | |
["第", "回"], | |
["#", ""], | |
["♯", ""] | |
] | |
}; | |
console.log(getInfo(str, settings)); | |
} | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment