Advertisement
Double_X

Set operations for Array

Oct 18th, 2017
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Set operations for Array: https://pastebin.com/yMY0Q4AE
  3.  * This snippet adds the following functions to Array.prototype:
  4.  * 1. isProperSubsetOf(Array) -> Boolean
  5.  *    - Checks whether this Array is a proper subset of the passed one
  6.  * 2. isProperSupersetOf(Array) -> Boolean
  7.  *    - Checks whether this Array is a proper superset of the passed one
  8.  * 3. isSameSetAs(Array) -> Boolean
  9.  *    - Checks whether this Array is the same set of the passed one
  10.  * 4. isSupersetOf(Array) -> Boolean
  11.  *    - Checks whether this Array is a superset of the passed one
  12.  * 5. isSubsetOf(Array) -> Boolean
  13.  *    - Checks whether this Array is a subset of the passed one
  14.  * 6. isEmptySet() -> Boolean
  15.  *    - Checks whether this Array is an empty set
  16.  * 7. symmetricDifference(Array) -> Array
  17.  *    - Returns the symmetric difference from this Array and the passed one
  18.  * 8. union(Array) -> Array
  19.  *    - Returns the union from this Array and the passed one
  20.  * 9. difference(Array) -> Array
  21.  *    - Returns the difference of this Array with respect to the passed one
  22.  * 10. intersection(Array) -> Array
  23.  *     - Returns the intersection from this Array and the passed one
  24.  * 11. excludes(arrayElement) -> Boolean
  25.  *    - Checks whether this Array doesn't have element arrayElement
  26.  * 12. includes(arrayElement) -> Boolean
  27.  *    - Checks whether this Array has element arrayElement
  28.  * If any of the above function are already defined before running this snippet,
  29.  * this snippet won't define those already defined ones
  30.  */
  31.  
  32. (function() {
  33.  
  34.     "use strict";
  35.  
  36.     var $ = Array.prototype;
  37.  
  38.     /**
  39.      * Pure function
  40.      * @author DoubleX
  41.      * @interface
  42.      * @param {Array} array - The array to be checked against
  43.      * @returns {Boolean} The check result
  44.      * @since v1.00a
  45.      * @version v1.00a
  46.      */
  47.     $.isProperSubsetOf = $.isProperSubsetOf || function(array) {
  48.         return this.isSubsetOf(array) && !array.isSubsetOf(this);
  49.     }; // $.isProperSubsetOf
  50.  
  51.     /**
  52.      * Pure function
  53.      * @author DoubleX
  54.      * @interface
  55.      * @param {Array} array - The array to be checked against
  56.      * @returns {Boolean} The check result
  57.      * @since v1.00a
  58.      * @version v1.00a
  59.      */
  60.     $.isProperSupersetOf = $.isProperSupersetOf || function(array) {
  61.         return this.isSupersetOf(array) && !array.isSupersetOf(this);
  62.     }; // $.isProperSupersetOf
  63.  
  64.     /**
  65.      * Pure function
  66.      * @author DoubleX
  67.      * @interface
  68.      * @param {Array} array - The array to be checked against
  69.      * @returns {Boolean} The check result
  70.      * @since v1.00a
  71.      * @version v1.00a
  72.      */
  73.     $.isSameSetAs = $.isSameSetAs || function(array) {
  74.         return this.isSubsetOf(array) && this.isSupersetOf(array);
  75.     }; // $.isSameSetAs
  76.  
  77.     /**
  78.      * Pure function
  79.      * @author DoubleX
  80.      * @interface
  81.      * @param {Array} array - The array to be checked against
  82.      * @returns {Boolean} The check result
  83.      * @since v1.00a
  84.      * @version v1.00a
  85.      */
  86.     $.isSupersetOf = $.isSupersetOf || function(array) {
  87.         return array.isSubsetOf(this);
  88.     }; // $.isSupersetOf
  89.  
  90.     /**
  91.      * Pure function
  92.      * @author DoubleX
  93.      * @interface
  94.      * @param {Array} array - The array to be checked against
  95.      * @returns {Boolean} The check result
  96.      * @since v1.00a
  97.      * @version v1.00a
  98.      */
  99.     $.isSubsetOf = $.isSubsetOf || function(array) {
  100.         return this.difference(array).isEmptySet();
  101.     }; // $.isSubsetOf
  102.  
  103.     /**
  104.      * Pure function
  105.      * @author DoubleX
  106.      * @interface
  107.      * @returns {Boolean} The check result
  108.      * @since v1.00a
  109.      * @version v1.00a
  110.      */
  111.     $.isEmptySet = $.isEmptySet || function() { return this.length <= 0; };
  112.  
  113.     /**
  114.      * Pure function
  115.      * @author DoubleX
  116.      * @interface
  117.      * @param {Array} array - The array to have symmetric difference with
  118.      * @returns {Array} The requested symmetric difference
  119.      * @since v1.00a
  120.      * @version v1.00a
  121.      */
  122.     $.symmetricDifference = $.symmetricDifference || function(array) {
  123.         return this.difference(array).union(array.difference(this));
  124.     }; // $.symmetricDifference
  125.  
  126.     /**
  127.      * Pure function
  128.      * @author DoubleX
  129.      * @interface
  130.      * @param {Array} array - The array to have union with this array
  131.      * @returns {Array} The requested union
  132.      * @since v1.00a
  133.      * @version v1.00a
  134.      */
  135.     $.union = $.union || function(array) {
  136.         return this.concat(array.difference(this));
  137.     }; // $.union
  138.  
  139.     /**
  140.      * Pure function
  141.      * @author DoubleX
  142.      * @interface
  143.      * @param {Array} array - The array to have difference with this array
  144.      * @returns {Array} The requested difference
  145.      * @since v1.00a
  146.      * @version v1.00a
  147.      */
  148.     $.difference = $.difference || function(array) {
  149.         return this.filter(this.excludes, array);
  150.     }; // $.difference
  151.  
  152.     /**
  153.      * Pure function
  154.      * @author DoubleX
  155.      * @interface
  156.      * @param {Array} array - The array to have intersection with this array
  157.      * @returns {Array} The requested intersection
  158.      * @since v1.00a
  159.      * @version v1.00a
  160.      */
  161.     $.intersection = $.intersection || function(array) {
  162.         return this.filter(this.includes, array);
  163.     }; // $.intersection
  164.  
  165.     /**
  166.      * Pure function
  167.      * @author DoubleX
  168.      * @interface
  169.      * @param {} element - The element to be checked against
  170.      * @returns {Boolean} The check result
  171.      * @since v1.00a
  172.      * @version v1.00a
  173.      */
  174.     $.excludes = $.excludes || function(element) {
  175.         return !this.includes(element);
  176.     }; // $.excludes
  177.  
  178.     /**
  179.      * Pure function
  180.      * @author DoubleX
  181.      * @interface
  182.      * @param {} element - The element to be checked against
  183.      * @returns {Boolean} The check result
  184.      * @since v1.00a
  185.      * @version v1.00a
  186.      */
  187.     $.includes = $.includes || function(element) {
  188.         return this.indexOf(element) >= 0;
  189.     }; // $.includes
  190.  
  191. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement