Category Programming

String.prototype.strint

Do you also have the bad habit of storing identificational values as an HTML element attribute? I do, because sometimes $.data(); just doesn’t cut it. In order to have enough ID’s for different types of elements (for my Affinity prototype I used images, groups and titles, all with ID’s), you need some sort of prefix. But then what, how do you get the integer from the ID attribute when it’s a non-parseInt() String? Like this:

String.prototype.strint = function()
{
	return parseInt(this.replace(/[^0-9.]/g, ""));
};

Usage:

var s = "group_42";
var i = s.strint();
// i = 42

Array.prototype.subset

It’s for finding subsets in an array. Can anyone find fault in this?

Array.prototype.subset = function(b)
{
	if(b.length > this.length)
		return false; // Needle is longer than haystack

	var s = this.indexOf(b[0]);	

	if(s >= 0)
	{
		for(var i = 0; i < b.length; i++)
		{
			if(this[(i + s)] == b[i])
				continue;
			else return false;
		}
		return true;
	} else return false;
}