// ==UserScript==
// @name GGn Format Title
// @version 1
// @description Format title
// @author ingts
// @match https://gazellegames.net/
// ==/UserScript==
function formatTitle(str, alias) {
const japaneseLowercase = new Map([
["ga", ["が", "ガ"]],
["no", ["の", "ノ"]],
["wa", ["わ", "ワ"]],
["mo", ["も", "モ"]],
["kara", ["から", "カラ"]],
["made", ["まで", "マデ"]],
["to", ["と", "ト"]],
["ya", ["や", "ヤ"]],
["de", ["で", "デ"]],
["ni", ["に", "ニ"]],
["so", ["そ", "ソ"]],
["na", ["な", "ナ"]],
["i", ["い", "イ"]],
["u", ["う", "ウ"]],
["e", ["え", "エ"]],
["o", ["お", "オ"]],
["san", ["さん"]],
["sama", ["さま"]],
["kun", ["くん"]],
["chan", ["ちゃん"]]
])
const smallWords = /^(a|an|and|as|at|but|by|en|for|if|in|nor|of|on|or|per|the|to|v.?|vs.?|via)$/i
const alphanumericPattern = /([A-Za-z0-9\u00C0-\u00FF])/
const wordSeparators = /([ :–—-]|[^a-zA-Z0-9'’])/
const allUppercase = new Set(['rpg', 'fps', 'tps', 'rts', 'tbs', 'mmo', 'mmorpg', 'arpg', 'jrpg', 'pvp', 'pve', 'ntr', 'td', 'vr', 'npc', 'ost'])
return str
.replace(/\s/g, ' ').replace('—', ' - ')
.replace(/~$/, '').replace(/ ~$/, '').replace(/-$/, '').replace(/^-/, '').replace(/ ~ /, ': ').replace(/ ~/, ': ').replace(/ - /, ': ').replace(/ -/, ': ')
.replace('™', '').replace('®', '').replace(' : ', ': ')
.toLowerCase().trim()
.split(wordSeparators)
.map(function (current, index, array) {
if (allUppercase.has(current.trim()) || /\b([IVX])(X{0,3}I{0,3}|X{0,2}VI{0,3}|X{0,2}I?[VX])(?![A-Za-z'])\b/i.test(current))
return current.toUpperCase()
if (alias) {
const jpWords = japaneseLowercase.get(current)
if (jpWords?.some(w => alias.includes(w))) return current
}
if (
/* Check for small words */
current.search(smallWords) > -1 &&
/* Skip first and last word */
index !== 0 &&
index !== array.length - 1 &&
/* Ignore title end and subtitle start */
array[index - 3] !== ':' &&
array[index + 1] !== ':' &&
/* Ignore small words that start a hyphenated phrase */
(array[index + 1] !== '-' ||
(array[index - 1] === '-' && array[index + 1] === '-'))
) {
return current
}
/* Capitalize the first letter */
return current.replace(alphanumericPattern, function (match) {
return match.toUpperCase()
})
})
.join('')
}