Skip to content

Instantly share code, notes, and snippets.

@edenworky
Last active June 3, 2017 19:08
Show Gist options
  • Select an option

  • Save edenworky/21b4b7a8042accb772ff280e4762feb3 to your computer and use it in GitHub Desktop.

Select an option

Save edenworky/21b4b7a8042accb772ff280e4762feb3 to your computer and use it in GitHub Desktop.
Go to next image
window.location = makeBumpedUrl();
function makeBumpedUrl() {
const imageNumber = getImageNumberInUrl();
const newImageNumber = bumpTrailingNumber(imageNumber);
return window.location.href.replace(imageNumber, newImageNumber);
}
function getImagePathInUrl() {
const urlComponents = window.location.href.split("/");
const imagePath = urlComponents[urlComponents.length-1];
return imagePath.split(".")[0];
}
function bumpTrailingNumber(s) {
let trailingZeros = numOfTrailingZeros(s);
if (doesBumpAddDigit(s))
trailingZeros--;
return pad(Number(s)+1, trailingZeros);
}
function doesBumpAddDigit(s) {
const num = Number(s);
const bumped = num + 1;
return String(num).length !== String(bumped).length;
}
function numOfTrailingZeros(s) {
const regex = /^0*/;
const matches = s.match(regex);
return matches[0].length;
}
function pad(num, zeros) {
let s = num.toString();
for (let i = 0; i < zeros; i++)
s = "0" + s;
return s;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment