Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Set operations for Array: https://pastebin.com/yMY0Q4AE
- * This snippet adds the following functions to Array.prototype:
- * 1. isProperSubsetOf(Array) -> Boolean
- * - Checks whether this Array is a proper subset of the passed one
- * 2. isProperSupersetOf(Array) -> Boolean
- * - Checks whether this Array is a proper superset of the passed one
- * 3. isSameSetAs(Array) -> Boolean
- * - Checks whether this Array is the same set of the passed one
- * 4. isSupersetOf(Array) -> Boolean
- * - Checks whether this Array is a superset of the passed one
- * 5. isSubsetOf(Array) -> Boolean
- * - Checks whether this Array is a subset of the passed one
- * 6. isEmptySet() -> Boolean
- * - Checks whether this Array is an empty set
- * 7. symmetricDifference(Array) -> Array
- * - Returns the symmetric difference from this Array and the passed one
- * 8. union(Array) -> Array
- * - Returns the union from this Array and the passed one
- * 9. difference(Array) -> Array
- * - Returns the difference of this Array with respect to the passed one
- * 10. intersection(Array) -> Array
- * - Returns the intersection from this Array and the passed one
- * 11. excludes(arrayElement) -> Boolean
- * - Checks whether this Array doesn't have element arrayElement
- * 12. includes(arrayElement) -> Boolean
- * - Checks whether this Array has element arrayElement
- * If any of the above function are already defined before running this snippet,
- * this snippet won't define those already defined ones
- */
- (function() {
- "use strict";
- var $ = Array.prototype;
- /**
- * Pure function
- * @author DoubleX
- * @interface
- * @param {Array} array - The array to be checked against
- * @returns {Boolean} The check result
- * @since v1.00a
- * @version v1.00a
- */
- $.isProperSubsetOf = $.isProperSubsetOf || function(array) {
- return this.isSubsetOf(array) && !array.isSubsetOf(this);
- }; // $.isProperSubsetOf
- /**
- * Pure function
- * @author DoubleX
- * @interface
- * @param {Array} array - The array to be checked against
- * @returns {Boolean} The check result
- * @since v1.00a
- * @version v1.00a
- */
- $.isProperSupersetOf = $.isProperSupersetOf || function(array) {
- return this.isSupersetOf(array) && !array.isSupersetOf(this);
- }; // $.isProperSupersetOf
- /**
- * Pure function
- * @author DoubleX
- * @interface
- * @param {Array} array - The array to be checked against
- * @returns {Boolean} The check result
- * @since v1.00a
- * @version v1.00a
- */
- $.isSameSetAs = $.isSameSetAs || function(array) {
- return this.isSubsetOf(array) && this.isSupersetOf(array);
- }; // $.isSameSetAs
- /**
- * Pure function
- * @author DoubleX
- * @interface
- * @param {Array} array - The array to be checked against
- * @returns {Boolean} The check result
- * @since v1.00a
- * @version v1.00a
- */
- $.isSupersetOf = $.isSupersetOf || function(array) {
- return array.isSubsetOf(this);
- }; // $.isSupersetOf
- /**
- * Pure function
- * @author DoubleX
- * @interface
- * @param {Array} array - The array to be checked against
- * @returns {Boolean} The check result
- * @since v1.00a
- * @version v1.00a
- */
- $.isSubsetOf = $.isSubsetOf || function(array) {
- return this.difference(array).isEmptySet();
- }; // $.isSubsetOf
- /**
- * Pure function
- * @author DoubleX
- * @interface
- * @returns {Boolean} The check result
- * @since v1.00a
- * @version v1.00a
- */
- $.isEmptySet = $.isEmptySet || function() { return this.length <= 0; };
- /**
- * Pure function
- * @author DoubleX
- * @interface
- * @param {Array} array - The array to have symmetric difference with
- * @returns {Array} The requested symmetric difference
- * @since v1.00a
- * @version v1.00a
- */
- $.symmetricDifference = $.symmetricDifference || function(array) {
- return this.difference(array).union(array.difference(this));
- }; // $.symmetricDifference
- /**
- * Pure function
- * @author DoubleX
- * @interface
- * @param {Array} array - The array to have union with this array
- * @returns {Array} The requested union
- * @since v1.00a
- * @version v1.00a
- */
- $.union = $.union || function(array) {
- return this.concat(array.difference(this));
- }; // $.union
- /**
- * Pure function
- * @author DoubleX
- * @interface
- * @param {Array} array - The array to have difference with this array
- * @returns {Array} The requested difference
- * @since v1.00a
- * @version v1.00a
- */
- $.difference = $.difference || function(array) {
- return this.filter(this.excludes, array);
- }; // $.difference
- /**
- * Pure function
- * @author DoubleX
- * @interface
- * @param {Array} array - The array to have intersection with this array
- * @returns {Array} The requested intersection
- * @since v1.00a
- * @version v1.00a
- */
- $.intersection = $.intersection || function(array) {
- return this.filter(this.includes, array);
- }; // $.intersection
- /**
- * Pure function
- * @author DoubleX
- * @interface
- * @param {} element - The element to be checked against
- * @returns {Boolean} The check result
- * @since v1.00a
- * @version v1.00a
- */
- $.excludes = $.excludes || function(element) {
- return !this.includes(element);
- }; // $.excludes
- /**
- * Pure function
- * @author DoubleX
- * @interface
- * @param {} element - The element to be checked against
- * @returns {Boolean} The check result
- * @since v1.00a
- * @version v1.00a
- */
- $.includes = $.includes || function(element) {
- return this.indexOf(element) >= 0;
- }; // $.includes
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement