Skip to content

Instantly share code, notes, and snippets.

@commuterjoy
Created September 29, 2011 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save commuterjoy/1251870 to your computer and use it in GitHub Desktop.
Save commuterjoy/1251870 to your computer and use it in GitHub Desktop.
n-way comparison of string parts using Array.every
/**
* Splits a string on a given separator and compares each part
* @returns {Boolean} True is all parts are equal, false if not
*/
String.prototype.equate = function (separator) {
return this.split(separator).every(function(element, index, array){
return (element === array[0]);
});
}
// true
console.log("this this this".equate(' '));
// false
console.log("this that this".equate(' '));
// true
console.log("bbbbbbbbb".equate(''));
// true
console.log("1 2 3 4 5 6 7 8 9".equate(/(d)/));
// true (no separator = one string)
console.log("whateverwewant".equate());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment