Skip to content

Instantly share code, notes, and snippets.

@werrpy
Last active May 19, 2020 02:19
Show Gist options
  • Save werrpy/3e59914374fb800878a276ebee101a1b to your computer and use it in GitHub Desktop.
Save werrpy/3e59914374fb800878a276ebee101a1b to your computer and use it in GitHub Desktop.
dots..............................
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<title>Hello, world!</title>
</head>
<body>
<h1>Hello, world!</h1>
<div class="container">
<div class="form-group">
<label for="input">Shitty title</label>
<input type="text" class="form-control" id="input">
</div>
<div class="form-group">
<label for="output">Good title</label>
<input type="text" class="form-control" id="output" readonly>
</div>
</div>
<script>
// Picket.Fences.S03.NTSC.DVD.DD.2.0-CultFilms™
// https://stackoverflow.com/a/10710400
// get all indexes of ch in str
function getAllIndexes(str, ch) {
var indices = [];
for (var i = 0; i < str.length; i++) {
if (str[i] === ch) indices.push(i);
}
return indices;
}
// is index in range of str?
function inRange(str, index) {
return (index >= 0 && index < str.length);
}
// https://stackoverflow.com/a/8935675
function is_numeric(str) {
return /^\d+$/.test(str)
}
// https://www.geeksforgeeks.org/how-to-replace-a-character-at-a-particular-index-in-javascript/
function replaceChar(origString, replaceChar, index) {
let firstPart = origString.substr(0, index);
let lastPart = origString.substr(index + 1);
let newString = firstPart + replaceChar + lastPart;
return newString;
}
function removeDots(title) {
// replace dots with spaces that are between:
// 1) letter and letter
// 2) number and letter
// 3) . 4 numbers (.year)
// 4) 4 numbers and 3 or 4 numbers followed by i or p (year and resolution, 2020.1080p)
// 5) S 2 numbers . 3 or 4 numbers followed by i or p (season and resolution, S01.720p)
// 6) 2 letters . number . number (DD.2.0 or AAC.2.0 or DD+.2.0 or DTS-X.7.1)
let newTitle = title;
const indexes = getAllIndexes(title, ".")
console.log(indexes);
// each dot location
for (const i of indexes) {
let before, after;
if (inRange(title, i - 1) && inRange(title, i + 1)) {
// characters before and after dot
before = title[i - 1]
after = title[i + 1]
if (!is_numeric(before) && !is_numeric(after)) {
// Case 1: letter and letter
newTitle = replaceChar(newTitle, " ", i)
continue;
} else if (is_numeric(before) && !is_numeric(after)) {
// Case 2: number and letter
newTitle = replaceChar(newTitle, " ", i)
continue;
}
}
// Case 3: . 4 numbers (.year)
if (inRange(title, i + 5)) {
after = title.substring(i + 1, i + 5)
if (is_numeric(after)) {
newTitle = replaceChar(newTitle, " ", i)
continue;
}
}
// Case 4: 4 numbers and 3 numbers followed by i or p (year and resolution, 2020.720p)
if (inRange(title, i - 4) && inRange(title, i + 4)) {
const beforeNum = title.substring(i - 4, i) // ex. 1987
const afterNum = title.substring(i + 1, i + 4) // ex. 480, 576 or 720
const afterResolution = title.charAt(i + 4).toLowerCase() // i or p
if (is_numeric(beforeNum) && is_numeric(afterNum) && (afterResolution == "i" || afterResolution == "p")) {
newTitle = replaceChar(newTitle, " ", i)
continue;
}
}
// Case 5: S 2 numbers . 3 or 4 numbers followed by i or p (season and resolution, S01.720p)
if (inRange(title, i - 3) && inRange(i + 4)) {
const beforeNum = title.substring(i - 3, i - 2).toLowerCase() // ex. S
const seasonNum = title.substring(i - 2, i).toLowerCase() // ex. 01
const afterNum = title.substring(i + 1, i + 4) // ex. 480, 576 or 720
const afterResolution = title.charAt(i + 4).toLowerCase() // i or p
if (beforeNum == "s" && is_numeric(seasonNum) && (afterResolution == "i" || afterResolution == "p")) {
newTitle = replaceChar(newTitle, " ", i)
continue;
}
}
// Case 6: 2 letters . number (DD.2.0 or AAC.2.0 or DD+.2.0 or DTS-X.7.1)
// looking at it from the first dot only
if (inRange(title, i - 1) && inRange(title, i + 1)) {
const before = title.substring(i - 1, i).toLowerCase() // ex. DD or AC or D+ or -X
const after = title.charAt(i + 1) // number
if (!is_numeric(before) && is_numeric(after)) {
newTitle = replaceChar(newTitle, " ", i)
continue;
}
}
}
return newTitle;
}
document.addEventListener("DOMContentLoaded", () => {
// it did
const input = document.getElementById("input");
const output = document.getElementById("output");
input.addEventListener('change', (event) => {
const newTitle = removeDots(event.target.value);
output.value = newTitle;
});
})
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment