Subversion Repositories JSX

Compare Revisions

Last modification

Regard whitespace Rev 480 → Rev 481

/trunk/array.js
30,29 → 30,109
 
if (typeof jsx.array == "undefined")
{
jsx.array = {};
}
 
/**
* @type jsx.array
* @memberOf __jsx.array
* @namespace
*/
jsx.array = {};
jsx.array = (/** @constructor */ function () {
/* Imports */
var _jsx_object = jsx.object;
var _isArray = _jsx_object.isArray;
 
/**
* @param {Array} a (optional)
* Array to which an element should be appended. Is used instead
* of the <code>Array</code> object the function is applied to.
* @param value (optional)
* @return {Array}
* The changed array.
*/
function _array_push (a, value)
{
if (!_isArray(a) && _isArray(this))
{
value = a;
a = this;
}
 
jsx.array.version = "0.1.$Rev$";
jsx.array.copyright = "Copyright \xA9 2004-2013";
jsx.array.author = "Thomas Lahn";
jsx.array.email = "js@PointedEars.de";
jsx.array.path = "http://pointedears.de/scripts/";
// jsx.array.docURL = jsx.array.path + "array.htm";
for (var i = 0, len = arguments.length; i < len; i++)
{
a[a.length] = arguments[i];
}
 
if (typeof jsx.array.emulate == "undefined")
return a;
}
 
/**
* Takes input array <code>a</code> or the <code>Array</code> object
* it is applied to as method and returns a new <code>Array</code>
* object with all string elements (optionally all elements regardless
* of their type) either lowercased or uppercased.
*
* @param {Array} a (optional)
* Array which elements should be changed. Is used instead
* of the Array object the function is applied to.
* @param {boolean} bUppercase = false
* If <code>false</code> (default), changes the elements to
* be all lowercase. If <code>true</code>, changes them to
* be all uppercase.
* @param {boolean} bConvertNonStrings = false
* If <code>false</code> default, changes only the case of
* string elements. If <code>true</code>, converts non-string
* elements to <code>string</code> and changes their case.
* @return {Array}
* A copy of <code>a</code> or the Array object with its
* elements' value uppercased or lowercased. If <code>a</code>
* has no elements, an empty array is returned.
*/
function _changeCase (a, bUppercase, bConvertNonStrings) {
if (!_isArray(a) && _isArray(this))
{
jsx.array.emulate = false;
bConvertNonStrings = bUppercase;
bUpperCase = a;
a = this;
}
 
if (_isArray(a))
{
for (var i = 0; i < a.length; i++)
{
if (bConvertNonStrings || typeof a[i] == "string")
{
if (bUppercase)
{
a[i] = String(a[i]).toUpperCase();
}
else
{
a[i] = String(a[i]).toLowerCase();
}
}
}
 
return a;
}
 
return [];
}
 
return {
version: "0.1.$Rev$",
copyright: "Copyright \xA9 2004-2013",
author: "Thomas Lahn",
email: "js@PointedEars.de",
path: "http://pointedears.de/scripts/",
 
/**
* @memberOf jsx.array
* @param {string} sMsg (optional)
* @return {boolean} false
*/
jsx.array.ArrayError = function(sMsg) {
ArrayError: function(sMsg) {
alert(
"array.js "
+ Array.version
65,20 → 145,12
+ ">\n\n"
+ sMsg);
return false;
};
},
 
/**
* Splits the array <code>a</code> into several arrays with
* <code>iSize</code> values in them.
*
* @author (C) 2004 Thomas Lahn &lt;js@PointedEars.de&gt;
* @requires jsx.object#isArray()
* @function
*/
jsx.array.chunk = (function () {
var _isArray = jsx.object.isArray;
 
/**
* @param {Array} a
* (optional) Array which should be split. Is used instead of
* the <code>Array</code> instance the function is applied to.
86,7 → 158,7
* Maximum size of the resulting arrays.
* An array of arrays indexed with numbers starting from zero.
*/
return function (a, iSize) {
chunk: function (a, iSize) {
if (!_isArray(a) && _isArray(this))
{
iSize = a;
93,7 → 165,7
a = this;
}
 
var arrays = new Array(new Array());
var arrays = new Array([]);
 
var i = 0;
if (_jsx_object.isMethod(a, "slice"))
111,40 → 183,31
{
if (arrays[index].length == iSize)
{
arrays[++index] = new Array();
arrays[++index] = [];
}
 
jsx.array.push(arrays[index], a[i]);
_array_push(arrays[index], a[i]);
}
}
};
}());
},
 
/**
* Returns an object using the values of the array <code>a</code>
* as properties and their frequency in <code>a</code> as values.
*
* @author (C) 2004 Thomas Lahn &lt;js@PointedEars.de&gt;
* @requires jsx.object#isArray()
* @function
*/
jsx.array.countValues = (function () {
var _isArray = jsx.object.isArray;
 
/**
* @param {Array} a
* (optional) Array which values should be counted. Is used
* instead of the <code>Array</code> instance the function is
* applied to.
* @return Object
* @return {Object}
*/
return function (a) {
countValues: function (a) {
if (!_isArray(a) && _isArray(this))
{
a = this;
}
 
var o = new Object();
var o = {};
 
for (var i = 0; i < a.length; i++)
{
159,8 → 222,7
}
 
return o;
};
}());
},
 
/**
* Fills an array with <code>iNumber</code> entries of the value
167,14 → 229,6
* of the <code>value</code> argument, indexes starting at the
* value of the <code>iStart</code> argument.
*
* @author (C) 2004 Thomas Lahn &lt;js@PointedEars.de&gt;
* @requires jsx.object#isArray()
* @function
*/
jsx.array.fill = (function () {
var _isArray = jsx.object.isArray;
 
/**
* @param {Array} a
* (optional) Array which should be filled. Is used instead
* of the {@link Array} object the function is applied to.
186,15 → 240,15
* The value the array should be filled with
* @return {Array} The filled array
*/
return function (a, iStart, iNumber, value) {
fill: function (a, iStart, iNumber, value) {
if (!_isArray(a) && _isArray(this))
{
a = this;
}
 
if (!jsx.object.isArray(a))
if (!_isArray(a))
{
a = new Array();
a = [];
}
 
for (var i = iStart; i < iStart + iNumber; i++)
203,21 → 257,12
}
 
return a;
};
}());
},
 
/**
* Returns an array containing all the elements of <code>a</code>
* filtered according a callback function <code>fCallback</code>.
* Returns an array containing all the elements of another
* array filtered according to a callback function.
*
* @author (C) 2004 Thomas Lahn &lt;js@PointedEars.de&gt;
* @requires jsx.object#isArray()
* @function
*/
jsx.array.filter = (function () {
var _isArray = jsx.object.isArray;
 
/**
* @param {Function} fCallback
* A function accepting a single argument that returns
* a value to be interpreted either as <code>true</code>
229,7 → 274,7
* <code>Array</code> object the function is applied to.
* @return Array
*/
return function (fCallback, a) {
filter: function (fCallback, a) {
if (a)
{
/* support for old-style calls */
260,7 → 305,7
jsx.throwThis('TypeError');
}
 
var res = new Array();
var res = [];
 
for (var i = 0; i < len; i++)
{
277,8 → 322,7
}
 
return res;
};
}());
},
 
/**
* Returns a function that can be used for filtering an {@link Array}
294,8 → 338,8
* The filter function
* @see jsx.array.filter
*/
jsx.array.createFilter = function (filter, bStrict) {
var keys = jsx.object.getKeys(filter);
createFilter: function (filter, bStrict) {
var keys = _jsx_object.getKeys(filter);
 
/**
* @param {any} element
321,7 → 365,7
}
}
};
};
},
 
/**
* Removes the last element from an array and returns that
328,22 → 372,14
* element. This method changes the length of the array, if
* applied directly to an array object.
*
* @author (C) 2004 Thomas Lahn &lt;js@PointedEars.de&gt;
* @requires jsx.object#isArray()
* @function
*/
jsx.array.pop = (function () {
var _isArray = jsx.object.isArray;
 
/**
* @param {Array} a (optional)
* Array from which the last element should be removed. Is used
* instead of the {@link Array} object the function is
* applied to.
* @return
* @return {any}
* The element removed from the array changed array.
*/
return function (a) {
pop: function (a) {
if (!_isArray(a) && _isArray(this))
{
a = this;
354,7 → 390,7
if (a.length > 0)
{
result = a[a.length - 1];
if (jsx.object.isArray(this))
if (_isArray(this))
{
this.length = this.length - 1;
}
361,54 → 397,15
}
 
return result;
};
}());
},
 
/**
* @author (C) 2004 Thomas Lahn &lt;js@PointedEars.de&gt;
* @requires jsx.object#isArray()
* @function
*/
jsx.array.push = (function () {
var _isArray = jsx.object.isArray;
push: _array_push,
 
/**
* @param {Array} a (optional)
* Array which should be added an element. Is used instead
* of the <code>Array</code> object the function is applied to.
* @param value (optional)
* @return {Array}
* The changed array.
*/
return function (a, value) {
if (!_isArray(a) && _isArray(this))
{
value = a;
a = this;
}
 
for (var i = 0, len = arguments.length; i < len; i++)
{
a[a.length] = arguments[i];
}
 
return a;
};
}());
 
/**
* Takes input array <code>a</code> or the calling <code>Array</code>
* object and returns a new <code>Array</code> object with the
* order of the elements reversed.
*
* @author (C) 2004 Thomas Lahn &lt;js@PointedEars.de&gt;
* @requires jsx.object#isArray()
* @function
*/
jsx.array.reverse = (function () {
var _isArray = jsx.object.isArray;
 
/**
* @param {Array} a (optional)
* <code>Array</code> object which order of elements should be
* reversed. Is used instead of the calling <code>Array</code>
418,35 → 415,26
* object with its elements in reverse order. If <code>a</code>
* has no elements, an empty array is returned.
*/
return function (a) {
reverse: function (a) {
if (!_isArray(a) && _isArray(this))
{
a = this;
}
 
var result = new Array();
var result = [];
 
if (jsx.object.isArray(a))
if (_isArray(a))
{
for (var i = a.length - 1; i > -1; i--) {result[result.length] = a[i];}
}
 
return result;
};
}());
},
 
/**
* Searches an array for a given value and returns
* the corresponding index or vector if successful.
*
* @author (C) 2004 Thomas Lahn &lt;js@PointedEars.de&gt;
* @requires jsx.object#isArray()
* @function
*/
jsx.array.search = (function () {
var _isArray = jsx.object.isArray;
 
/**
* @param needle
* Value to be searched for.
* @param {Array} aHaystack
512,7 → 500,7
* -1
* otherwise.
*/
return function array_search (needle, aHaystack, bStrict, bDeepSearch,
search: function array_search (needle, aHaystack, bStrict, bDeepSearch,
aAncestors, aResultVector, iLevel, index) {
var result = -1;
 
550,7 → 538,7
 
if (!aAncestors)
{
aAncestors = new Array();
aAncestors = [];
}
 
/* avoid dupes */
557,12 → 545,12
if (aAncestors.length == 0
|| aAncestors[aAncestors.length - 1] != aHaystack)
{
array_push(aAncestors, aHaystack);
_array_push(aAncestors, aHaystack);
}
 
if (!aResultVector)
{
aResultVector = new Array();
aResultVector = [];
}
 
if (typeof iLevel == "undefined")
634,12 → 622,9
}
 
return result;
};
}());
},
 
/**
* @author (C) 2004 Thomas Lahn &lt;js@PointedEars.de&gt;
* @requires jsx.object#isArray()
* @param value
* @param {Array} a
* Array which should be searched. Is used instead of the
655,7 → 640,7
* <code>true</code> if <code>value</code> is an element of
* <code>a</code>, <code>false</code> otherwise.
*/
jsx.array.contains = function (value, a, bExactMatch, bDeepSearch) {
contains: function (value, a, bExactMatch, bDeepSearch) {
var result = null;
 
if (bDeepSearch)
668,84 → 653,19
}
 
return result;
};
},
 
/**
* Takes input array <code>a</code> or the <code>Array</code> object
* it is applied to as method and returns a new <code>Array</code>
* object with all string elements (optionally all elements regardless
* of their type) either lowercased or uppercased.
*
* @author (C) 2004, 2008 Thomas Lahn &lt;js@PointedEars.de&gt;
* @requires jsx.object#isArray()
* @function
*/
jsx.array.changeCase = (function () {
var _isArray = jsx.object.isArray;
changeCase: _changeCase,
 
/**
* @param {Array} a (optional)
* Array which elements should be changed. Is used instead
* of the Array object the function is applied to.
* @param {boolean} bUppercase = false
* If <code>false</code> (default), changes the elements to
* be all lowercase. If <code>true</code>, changes them to
* be all uppercase.
* @param {boolean} bConvertNonStrings = false
* If <code>false</code> default, changes only the case of
* string elements. If <code>true</code>, converts non-string
* elements to <code>string</code> and changes their case.
* @return {Array}
* A copy of <code>a</code> or the Array object with its
* elements' value uppercased or lowercased. If <code>a</code>
* has no elements, an empty array is returned.
*/
return function (a, bUppercase, bConvertNonStrings) {
if (!_isArray(a) && _isArray(this))
{
bConvertNonStrings = bUppercase;
bUpperCase = a;
a = this;
}
 
if (_isArray(a))
{
for (var i = 0; i < a.length; i++)
{
if (bConvertNonStrings || typeof a[i] == "string")
{
if (bUppercase)
{
a[i] = String(a[i]).toUpperCase();
}
else
{
a[i] = String(a[i]).toLowerCase();
}
}
}
 
return a;
}
 
return new Array();
};
}());
 
/**
* Takes input array <code>a</code> or the Array object it is
* applied to as method and returns a new Array object with all
* string elements (optionally all elements regardless of their
* type) lowercased.
*
* @author (C) 2004 Thomas Lahn &lt;js@PointedEars.de&gt;
* @requires #array_changeCase()
* @function
*/
jsx.array.toLowerCase = (function () {
var _isArray = jsx.object.isArray;
 
/**
* @param {Array} a (optional)
* Array which elements should be converted. Is used instead
* of the Array object the function is applied to.
758,7 → 678,7
* elements' value lowercased. If <code>a</code> has no
* elements, an empty array is returned.
*/
return function (a, bConvertNonStrings) {
toLowerCase: function (a, bConvertNonStrings) {
if (!_isArray(a) && _isArray(this))
{
bConvertNonStrings = a;
765,9 → 685,8
a = this;
}
 
return jsx.array.changeCase.call(a, a, false, bConvertNonStrings);
};
}());
return _changeCase.call(a, a, false, bConvertNonStrings);
},
 
/**
* Takes input array <code>a</code> or the Array object it is
775,14 → 694,6
* string elements (optionally all elements regardless of their
* type) uppercased.
*
* @author (C) 2004 Thomas Lahn &lt;js@PointedEars.de&gt;
* @requires #array_changeCase()
* @function
*/
jsx.array.toUpperCase = (function () {
var _isArray = jsx.object.isArray;
 
/**
* @param {Array} a (optional)
* Array which elements should be converted. Is used instead
* of the Array object the function is applied to.
795,7 → 706,7
* elements' value uppercased. If <code>a</code> has no
* elements, an empty array is returned.
*/
return function (a, bConvertNonStrings) {
toUpperCase: function (a, bConvertNonStrings) {
if (!_isArray(a) && _isArray(this))
{
bConvertNonStrings = a;
802,19 → 713,19
a = this;
}
 
return jsx.array.changeCase.call(a, a, true, bConvertNonStrings);
};
}());
return _changeCase.call(a, a, true, bConvertNonStrings);
},
 
jsx.array.splice = (function() {
var jsx_object = jsx.object;
 
if (jsx_object.isMethod(jsx.global, "array_splice"))
/**
* @function
*/
splice: (function() {
if (typeof jsx.global.array_splice != "undefined")
{
return array_splice;
return jsx.global.array_splice;
}
else if (typeof Array != "undefined"
&& jsx_object.isMethod(Array, "prototype", "splice"))
&& _jsx_object.isNativeMethod(Array, "prototype", "splice"))
{
return function(a, start, del, ins) {
var proto = Array.prototype;
822,10 → 733,9
return proto.splice.apply(a, [start, del].concat(ins));
};
}
else
{
 
return function(a, start, del, ins) {
var aDeleted = new Array();
var aDeleted = [];
 
for (var i = start + del, len = a.length; i < len; i++)
{
842,12 → 752,10
 
return aDeleted;
};
}
}());
}()),
 
jsx.array.every = (function () {
var _isNativeMethod = jsx.object.isNativeMethod;
var _isArray = jsx.object.isArray;
every: (function () {
var _isNativeMethod = _jsx_object.isNativeMethod;
 
return function (callback, thisObject) {
/* NOTE: null or undefined */
871,12 → 779,9
 
return true;
};
}());
}()),
 
jsx.array.equals = (function () {
var _isArray = jsx.object.isArray;
 
return function (otherObject, thisObject, strict) {
equals: function (otherObject, thisObject, strict) {
/* NOTE: null or undefined */
if (thisObject == null && _isArray(this))
{
907,8 → 812,7
}
 
return true;
};
}());
},
 
/**
* Returns a function that can be used for sorting an {@link Array}.
915,10 → 819,9
*
* @author (C) 2013 Thomas 'PointedEars' Lahn &lt;js@PointedEars.de&gt;
*/
jsx.array.createComparator = (function () {
var _isObject = jsx.object.isObject;
var _isArray = jsx.object.isArray;
var _hasOwnProperty = jsx.object._hasOwnProperty;
createComparator: (function () {
var _isObject = _jsx_object.isObject;
var _hasOwnProperty = _jsx_object._hasOwnProperty;
 
/**
* @param {Array} aKeys
1028,7 → 931,7
* The factory default is non-strict comparison.</td>
* </tr>
* </table>
* @return Function
* @return {Function}
*/
function _createComparator (aKeys, options)
{
1121,32 → 1024,41
}
 
return _createComparator;
}())
};
}());
 
//jsx.array.docURL = jsx.array.path + "array.htm";
 
if (typeof jsx.array.emulate == "undefined")
{
jsx.array.emulate = false;
}
 
if (jsx.array.emulate)
{
jsx.object.extend(Array.prototype, {
contains: jsx.array.contains,
chunk: jsx.array.chunk,
changeCase: jsx.array.changeCase,
contains: jsx.array.contains,
countValues: jsx.array.countValues,
equals: jsx.array.equals,
fill: jsx.array.fill,
createComparator: jsx.array.createComparator,
filter: jsx.array.filter,
pop: jsx.array.pop,
push: jsx.array.push,
reverse: jsx.array.reverse,
search: jsx.array.search,
splice: jsx.array.splice,
toLowerCase: jsx.array.toLowerCase,
toUpperCase: jsx.array.toUpperCase,
 
/* JavaScript 1.6 (1.5 in Gecko 1.8b2 and later) emulation */
every: jsx.array.every,
 
filter: jsx.array.filter,
 
/* TODO */
iterate: function () {
var a = new Array();
var a = [];
 
for (var i = 0, len = this.length; i < len; i++)
{