Once again, I got up at 2:30am wide awake... that's what happens when I fall asleep at 9:30 LOL. Anyway, I was bored so I searched for something to do. I found a question on CSS Tricks asking how to add buttons to the jQuery UI Slider. So starting with the side scroll demo as a base, I put together this demo. That is all :P
This blog contains my random dabbling in site design and improvement. Please feel free to browse and use anything on this site. All I ask is you give me some feedback or even advice on how to improve or change anything.
Monday, November 14, 2011
Monday, October 31, 2011
Get All indexOf From An Array - Array.allIndexOf()
This array function is an extension of Array.indexOf(), if it exists, and will return all indexes of the search element. It's named allIndexOf(). This code extends javascript and does not require jQuery. It is designed to work in older versions of IE as well, albeit a tiny bit slower.
Originally if you wanted the second instance of a search element, you'd have to call "indexOf" twice. The second time with the starting index of the first result (plus one), or once with a guessed starting index. This should simplify the process for getting any or all of the indexes.
Originally if you wanted the second instance of a search element, you'd have to call "indexOf" twice. The second time with the starting index of the first result (plus one), or once with a guessed starting index. This should simplify the process for getting any or all of the indexes.
/*
Array.allIndexOf(searchElement)
Array [Array] - the array to search within for the searchElement
searchElement [String] - the desired element with which to find starting indexes
*/
(function(){
Array.prototype.allIndexOf = function(searchElement) {
if (this === null) { return [-1]; }
var len = this.length,
hasIndexOf = Array.prototype.indexOf, // you know, because of IE
i = (hasIndexOf) ? this.indexOf(searchElement) : 0,
n,
indx = 0,
result = [];
if (len === 0 || i === -1) { return [-1]; }
if (hasIndexOf) {
// Array.indexOf does exist
for (n = 0; n <= len; n++) {
i = this.indexOf(searchElement, indx);
if (i !== -1) {
indx = i + 1;
result.push(i);
} else {
return result;
}
}
return result;
} else {
// Array.indexOf doesn't exist
for (n = 0; n <= len; n++) {
if (this[n] === searchElement) {
result.push(n);
}
}
return (result.length > 0) ? result : [-1];
}
};
})();Use it as follows: var s = ["red","green","blue","red","yellow","blue","green","purple","red"];
s.allIndexOf("r"); // result [ -1 ]
s.allIndexOf("red"); // result [ 0,3,8 ]
s.allIndexOf("blue"); // result [ 2,5 ]Try out your own strings in the demo below or full screen.Get All indexOf From A Search String - String.allIndexOf()
This string function is an extension of String.indexOf() and will return all indexes of a search string. It's named allIndexOf. This code extends javascript and does not require jQuery.
Originally if you wanted the second instance of a search string, you'd have to call "indexOf" twice. The second time with the starting index of the first result (plus one), or once with a guessed starting index. This should simplify the process for getting any or all of the indexes. And yes, I woke up with a strange urge to write this function LOL.
Originally if you wanted the second instance of a search string, you'd have to call "indexOf" twice. The second time with the starting index of the first result (plus one), or once with a guessed starting index. This should simplify the process for getting any or all of the indexes. And yes, I woke up with a strange urge to write this function LOL.
/*
String.allIndexOf(searchstring, ignoreCase)
String [String] - the string to search within for the searchstring
searchstring [String] - the desired string with which to find starting indexes
ignoreCase [Boolean] - set to true to make both the string and searchstring case insensitive
*/
(function(){
String.prototype.allIndexOf = function(string, ignoreCase) {
if (this === null) { return [-1]; }
var t = (ignoreCase) ? this.toLowerCase() : this,
s = (ignoreCase) ? string.toString().toLowerCase() : string.toString(),
i = this.indexOf(s),
len = this.length,
n,
indx = 0,
result = [];
if (len === 0 || i === -1) { return [i]; } // "".indexOf("") is 0
for (n = 0; n <= len; n++) {
i = t.indexOf(s, indx);
if (i !== -1) {
indx = i + 1;
result.push(i);
} else {
return result;
}
}
return result;
}
})();Use it as follows: var s = "The rain in Spain stays mainly in the plain";
s.allIndexOf("ain"); // result [ 5,14,25,40 ]
s.allIndexOf("the"); // result [ 34 ]
s.allIndexOf("THE", true); // result [ 0,34 ]Try out your own strings in the demo below or full screen.
Subscribe to:
Posts (Atom)