Created
September 29, 2011 20:40
-
-
Save commuterjoy/1251870 to your computer and use it in GitHub Desktop.
n-way comparison of string parts using Array.every
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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