Skip to content

Instantly share code, notes, and snippets.

@redbluish
Created November 17, 2013 01:29
Show Gist options
  • Save redbluish/7507846 to your computer and use it in GitHub Desktop.
Save redbluish/7507846 to your computer and use it in GitHub Desktop.
String#succ_prec.js
String.prototype.succ = function () {
if (!/^[a-z]+$/.test(this)) return '';
var parts = this.split(''),
i = parts.length - 1;
do {
parts[i] = String.fromCharCode(97 + (parts[i].charCodeAt(0) - 70) % 26);
if (parts[i] !== 'a') break;
else if (i === 0) parts.unshift('a');
} while (i--);
return parts.join('');
};
String.prototype.prec = function () {
if (!/^[a-z]+$/.test(this)) return '';
var parts = this.split(''),
i = parts.length - 1;
do {
parts[i] = String.fromCharCode(97 + (parts[i].charCodeAt(0) - 72) % 26);
if (parts[i] !== 'z') break;
else if (i === 0) parts.shift();
} while (i--);
return parts.join('');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment