Sixtest Wiki:API4/Array

(function API_Array {	"use strict";	var extension;	extension = $API.create.extension('Array');	extension.occurrences = function (str, arr, property) {		arr = arr || [];		var i = 0, len = arr.length, occurrences = [], c;		for (i; i < len; i += 1) {			c = arr[i][property] || arr[i];			if (c === str) {				occurrences[occurrences.length] = arr[i][property] ? arr[i] : i;			}		}		return occurrences;	};	extension.position = function (value, arr, p) {		var i = 0, len = arr.length;		for (i; i < len; i += 1) {			if (arr[i] && arr[i][p] === value[p]) {				return i;			}		}		return -1;	};	extension.remove = function (arr, list) {		var remove, i, len, shift = 0;		remove = function (arr, item) {			var i = 0, len = arr.length;			for (i; i < len; i += 1) {				if (arr[i] === item) {					shift += 1;				} else if (shift) {					arr[i - shift] = arr[i];				}			}			arr.length -= shift;			return arr;		};		if (typeof list !== 'object') { list = [ list ]; }		if (typeof list === 'object' && list.length) { i = 0; len = list.length; for (i; i < len; i += 1) { arr = remove(arr, list[i]); }		}		return arr; };	extension.diff = function (arr1, arr2, p) { var added, removed, isin, results1, data, func; arr1 = arr1.slice(0).sort(function (a, b) { return a[p] > b[p]; }); arr2 = arr2.slice(0).sort(function (a, b) { return a[p] > b[p]; }); function removeFirst(arr1, arr2) { var i = 0, len1 = arr1.length, len2, pos1in2, removed = [], added = [], isin = []; for (i; i < len1; i += 1) { pos1in2 = $API.ext.Array.position(arr1[i], arr2, p); if (pos1in2 === -1) { removed[removed.length] = arr1[i]; arr1[i] = undefined; } else { isin[isin.length] = arr1[i]; arr1[i] = undefined; arr2[pos1in2] = undefined; }			}			$API.ext.Array.remove(arr1, undefined); $API.ext.Array.remove(arr2, undefined); i = 0; len2 = arr2.length; for (i; i < len2; i += 1) { added[added.length] = arr2[i]; }			return { removed: removed, isin: isin, added: added }; }		function iterate(arr, func) { var i = 0, len = arr.length; for (i; i < len; i += 1) { arr[i] = func(arr[i], i); }			return arr; }		results1 = removeFirst(arr1, arr2); func = function (type) { return function (item) { return { "type": type, "data": item };			};		};		added = iterate(results1.added, func('+')); removed = iterate(results1.removed, func('-')); isin = iterate(results1.isin, func('=')); data = added.concat(removed.concat(isin)).sort(function (a, b) { return a.data[p] > b.data[p]; }); return data; };	extension.clone = (function {		var f_obj, f_arr, f_str, which;		f_obj = function (obj) {			var property, robj = {};			for (property in obj) {				if (obj.hasOwnProperty(property)) {					robj[property] = (which(obj[property]));				}			}			return robj;		};		f_arr = function (arr) {			var i = 0, len = arr.length;			for (i; i < len; i += 1) {				arr[i] = (which(arr[i]));			}			return arr;		};		f_str = function (str) {			return str;		};		which = function (item) {			return ((typeof item !== 'object' ? f_str : (item.length !== undefined ? f_arr : f_obj))(item));		};		return which;	}); });