(function ($){ var plugin = { } ; var checkIfAllArgumentsAreArrays = function (functionArguments){ for (var i = 0; i < _AN_Read_length('length', functionArguments); i++ ){ if (!(functionArguments[i] instanceof Array)) { throw new Error('Every argument must be an array!') } } } ; plugin.distinct = function (array){ if (_AN_Read_length('length', arguments) != 1) throw new Error('There must be exactly 1 array argument!') checkIfAllArgumentsAreArrays(arguments); var result = [] ; for (var i = 0; i < _AN_Read_length('length', array); i++ ){ var item = array[i]; if ($.inArray(item, result) === -1) { result.push(item); } } return result; } ; plugin.union = function (){ if (_AN_Read_length('length', arguments) < 2) throw new Error('There must be minimum 2 array arguments!') checkIfAllArgumentsAreArrays(arguments); var result = this.distinct(arguments[0]); for (var i = 1; i < _AN_Read_length('length', arguments); i++ ){ var arrayArgument = arguments[i]; for (var j = 0; j < _AN_Read_length('length', arrayArgument); j++ ){ var item = arrayArgument[j]; if ($.inArray(item, result) === -1) { result.push(item); } } } return result; } ; plugin.intersect = function (){ if (_AN_Read_length('length', arguments) < 2) throw new Error('There must be minimum 2 array arguments!') checkIfAllArgumentsAreArrays(arguments); var result = [] ; var distinctArray = this.distinct(arguments[0]); if (_AN_Read_length('length', distinctArray) === 0) return [] ; for (var i = 0; i < _AN_Read_length('length', distinctArray); i++ ){ var item = distinctArray[i]; var shouldAddToResult = true ; for (var j = 1; j < _AN_Read_length('length', arguments); j++ ){ var array2 = arguments[j]; if (_AN_Read_length('length', array2) == 0) return [] ; if ($.inArray(item, array2) === -1) { shouldAddToResult = false ; break ; } } if (shouldAddToResult) { result.push(item); } } return result; } ; plugin.except = function (){ if (_AN_Read_length('length', arguments) < 2) throw new Error('There must be minimum 2 array arguments!') checkIfAllArgumentsAreArrays(arguments); var result = [] ; var distinctArray = this.distinct(arguments[0]); var otherArraysConcatenated = [] ; for (var i = 1; i < _AN_Read_length('length', arguments); i++ ){ var otherArray = arguments[i]; otherArraysConcatenated = otherArraysConcatenated.concat(otherArray); } for (var i = 0; i < _AN_Read_length('length', distinctArray); i++ ){ var item = distinctArray[i]; if ($.inArray(item, otherArraysConcatenated) === -1) { result.push(item); } } return result; } ; $.arrayUtilities = plugin; $.distinct = plugin.distinct; $.union = plugin.union; $.intersect = plugin.intersect; $.except = plugin.except; } (jQuery));