MediaWiki:LAPI.js: Difference between revisions
Content deleted Content added
Fix newline after return; kudos to User:Dudemanfellabra for spotting this |
per tper |
||
(One intermediate revision by one other user not shown) | |||
Line 1:
/*
Small JS library containing stuff I use often.
Line 39 ⟶ 37:
// Note: adding these to the prototype may break other code that assumes that
// {} has no properties at all.
if (!Object.clone) {
Object.clone = function (source, includeInherited) {
if (!source) return null;
var result = {};
for (var key in source) {
if (includeInherited || source.hasOwnProperty (key)) result[key] = source[key];
}
return result;
};
}
if (!Object.merge) {
Object.merge = function (from, into, includeInherited) {
if (!from) return into;
for (var key in from) {
if (includeInherited || from.hasOwnProperty (key)) into[key] = from[key];
}
return into;
};
}
if (!Object.mergeSome) {
Object.mergeSome = function (from, into, includeInherited, predicate) {
if (!from) return into;
if (typeof (predicate) == 'undefined')
return Object.merge (from, into, includeInherited);
for (var key in from) {
if ((includeInherited || from.hasOwnProperty (key)) && predicate (from, into, key))
into[key] = from[key];
}
return into;
};
}
if (!Object.mergeSet) {
Object.mergeSet = function (from, into, includeInherited)
{
return Object.mergeSome
(from, into, includeInherited, function (src, tgt, key) {return src[key] !== null;});
};
}
/** String enhancements (Javascript 1.6) ************/
Line 95 ⟶ 100:
};
}
if (!String.prototype.trimFront)
String.prototype.trimFront = String.prototype.trimLeft; // Synonym // Removes given characters from the end of the string.
Line 105 ⟶ 111:
};
}
if (!String.prototype.trimEnd)
String.prototype.trimEnd = String.prototype.trimRight; // Synonym /** Further String enhancements ************/
// Returns true if the string begins with prefix.
if (!String.prototype.startsWith
return this.indexOf (prefix) === 0;
};
}
// Returns true if the string ends in suffix
if (!String.prototype.endsWith
String.prototype.endsWith = function (suffix) {
var last = this.lastIndexOf (suffix);
return last !== -1 && last + suffix.length == this.length;
};
}
// Returns true if the string contains s.
if (!String.prototype.contains
String.prototype.contains = function (s) {
return this.indexOf (s) >= 0;
};
}
// Replace all occurrences of a string pattern by replacement.
if (!String.prototype.replaceAll) {
String.prototype.replaceAll = function (pattern, replacement) { return this.split (pattern).join (replacement);
};
}
// Escape all backslashes and single or double quotes such that the result can
// be used in Javascript inside quotes or double quotes.
if (!String.prototype.stringifyJS
String.prototype.stringifyJS = function () {
return
.replace (/\n/g, '\\n');
};
}
// Escape all RegExp special characters such that the result can be safely used
// in a RegExp as a literal.
if (!String.prototype.escapeRE
String.prototype.escapeRE = function () {
return this.replace (/([\\{}()|.?*+^$\[\]])/g, "\\$1");
};
}
if (!String.prototype.escapeXML
String.prototype.escapeXML = function (quot, apos) {
var s =
.replace (/
.replace (/
.replace (/>/g, '>');
if (quot) s = s.replace (/\"/g, '"'); // " // Fix syntax coloring if (apos) s = s.replace (/\'/g, '''); // ' // Fix syntax coloring
return s;
};
}
if (!String.prototype.decodeXML
String.prototype.decodeXML = function () {
return
.replace(/&
.replace(/&
.replace(/&
.replace(/&
.replace(/&/g, '&');
};
}
if (!String.prototype.capitalizeFirst
String.prototype.capitalizeFirst = function () {
return this.substring (0, 1).toUpperCase() + this.substring (1);
};
}
if (!String.prototype.lowercaseFirst
String.prototype.lowercaseFirst = function () {
return this.substring (0, 1).toLowerCase() + this.substring (1);
};
}
// This is actually a function on URLs, but since URLs typically are strings in
// Javascript, let's include this one here, too.
if (!String.prototype.getParamValue
String.prototype.getParamValue = function (param) {
var re = new RegExp ('[&?]' + param.escapeRE () + '=([^&#]*)');
var m = re.exec (this);
if (m && m.length >= 2) return decodeURIComponent (m[1]); return null;
};
}
if (!String.getParamValue) {
String.getParamValue = function (param, url)
{
if (typeof (url) == 'undefined' || url === null) url = document.___location.href;
try {
return url.getParamValue (param);
} catch (e) {
return null;
}
};
}
/** Function enhancements ************/
Line 224 ⟶ 257:
};
}
if (!Array.select)
Array.select = Array.filter; // Synonym // Calls iterator on all elements of the array
Line 261 ⟶ 295:
};
}
if (!Array.forAll)
Array.forAll = Array.every; // Synonym // Returns true if predicate is true for at least one element of the array, false otherwise.
Line 279 ⟶ 314:
};
}
if (!Array.exists)
Array.exists = Array.some; // Synonym // Returns a new array built by applying mapper to all elements.
Line 338 ⟶ 374:
/** Additional Array enhancements ************/
if (!Array.remove) {
Array.remove = function (target, elem) { var i = Array.indexOf (target, elem);
if (i >= 0) target.splice (i, 1);
};
}
if (!Array.contains) {
Array.contains = function (target, elem) { return Array.indexOf (target, elem) >= 0;
};
}
if (!Array.flatten
Array.flatten = function (target) {
var result = [];
Array.forEach (target, function (elem) {result = result.concat (elem);}); return result;
};
}
// Calls selector on the array elements until it returns a non-null object
// and then returns that object. If selector always returns null, any also
// returns null. See also Array.map.
if (!Array.any) {
Array.any = function (target, selector, thisObject) {
if (target === null) return null;
if (typeof (selector) != 'function')
throw new Error ('Array.any: selector must be a function');
var l = target.length;
var result = null;
if (thisObject) selector = selector.bind (thisObject);
for (var i=0; l && i < l; i++) {
if (i in target) {
result = selector (target[i], i, target);
if (result != null) return result;
}
}
return null;
}
// Return a contiguous array of the contents of source, which may be an array or pseudo-array,
// basically anything that has a length and can be indexed. (E.g. live HTMLCollections, but also
// Strings, or objects, or the arguments "variable".
if (!Array.make)
Array.make = function (source)
{
if (!source || typeof (source.length) == 'undefined') return null;
for (var i=0; i < l; i++) {
if (i in source) result[result.length] = source[i]; }
return result;
};
}
if (typeof (window.LAPI) == 'undefined') {
Line 2,065 ⟶ 2,111:
} // end if (guard)
|