mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-08-30 13:02:53 +00:00
104 lines
2.1 KiB
JavaScript
Executable File
104 lines
2.1 KiB
JavaScript
Executable File
//--
|
|
//-- Augmented methods for the JavaScript Array() object
|
|
//--
|
|
|
|
// Add indexOf function if browser does not support it
|
|
if(!Array.indexOf) {
|
|
Array.prototype.indexOf = function(item,from)
|
|
{
|
|
if(!from)
|
|
from = 0;
|
|
var i;
|
|
for(i=from; i<this.length; i++) {
|
|
if(this[i] === item)
|
|
return i;
|
|
}
|
|
return -1;
|
|
};}
|
|
|
|
// Find an entry in a given field of the members of an array
|
|
Array.prototype.findByField = function(field,value)
|
|
{
|
|
var t;
|
|
for(t=0; t<this.length; t++) {
|
|
if(this[t][field] === value)
|
|
return t;
|
|
}
|
|
return null;
|
|
};
|
|
|
|
// Return whether an entry exists in an array
|
|
Array.prototype.contains = function(item)
|
|
{
|
|
return this.indexOf(item) != -1;
|
|
};
|
|
|
|
// Adds, removes or toggles a particular value within an array
|
|
// value - value to add
|
|
// mode - +1 to add value, -1 to remove value, 0 to toggle it
|
|
Array.prototype.setItem = function(value,mode)
|
|
{
|
|
var p = this.indexOf(value);
|
|
if(mode == 0)
|
|
mode = (p == -1) ? +1 : -1;
|
|
if(mode == +1) {
|
|
if(p == -1)
|
|
this.push(value);
|
|
} else if(mode == -1) {
|
|
if(p != -1)
|
|
this.splice(p,1);
|
|
}
|
|
};
|
|
|
|
// Return whether one of a list of values exists in an array
|
|
Array.prototype.containsAny = function(items)
|
|
{
|
|
var i;
|
|
for(i=0; i<items.length; i++) {
|
|
if(this.indexOf(items[i]) != -1)
|
|
return true;
|
|
}
|
|
return false;
|
|
};
|
|
|
|
// Return whether all of a list of values exists in an array
|
|
Array.prototype.containsAll = function(items)
|
|
{
|
|
var i;
|
|
for(i = 0; i<items.length; i++) {
|
|
if(this.indexOf(items[i]) == -1)
|
|
return false;
|
|
}
|
|
return true;
|
|
};
|
|
|
|
// Push a new value into an array only if it is not already present in the array. If the optional unique parameter is false, it reverts to a normal push
|
|
Array.prototype.pushUnique = function(item,unique)
|
|
{
|
|
if(unique === false) {
|
|
this.push(item);
|
|
} else {
|
|
if(this.indexOf(item) == -1)
|
|
this.push(item);
|
|
}
|
|
};
|
|
|
|
Array.prototype.remove = function(item)
|
|
{
|
|
var p = this.indexOf(item);
|
|
if(p != -1)
|
|
this.splice(p,1);
|
|
};
|
|
|
|
if(!Array.prototype.map) {
|
|
Array.prototype.map = function(fn,thisObj)
|
|
{
|
|
var scope = thisObj || window;
|
|
var i,j,a = [];
|
|
for(i=0, j=this.length; i < j; ++i) {
|
|
a.push(fn.call(scope,this[i],i,this));
|
|
}
|
|
return a;
|
|
};}
|
|
|