JSFiddle - React, Tailwind, and code Playground

by John-Philip Johansson

HTML

<div id="wru" />

CSS

option[disabled] {
  background-color: red;
}
html {
  font-family: sans-serif;
  font-size: 11pt;
  border: 1px solid #333;
}
div {
  cursor: default;
  padding: 0;
  color: #000;
}
div span, div strong {
  display: block;
  padding: 4px;
  margin: 0;
}
div ul {
  margin: 0;
  padding-bottom: 4px;
}
div.pass {
  background: #90EE90;
}
div.fail {
  background: #FF6347;
}
div.error {
  background: #000;
  color: #FFF;
}

JavaScript

// My component, an intersect method for jQuery to match the existing jQuery.extend()

jQuery.intersect = function (obj1, obj2) {
  var newObj = {};

  $.each(obj2, function (index, newValues) {
    var oldValues = obj1[index];

    if (!oldValues) return;

    var oldArray = oldValues.split(',');
    var newArray = newValues.split(',');

    var result = $.map(newArray, function (newValue, i) {
      return $.inArray(newValue, oldArray) === -1 ? null : newValue;
    });

    if (result.length >= 1) newObj[index] = result.join();
  });

  return newObj;
};

// Tests below

function wru(wru) {


  // enojy your tests!
  wru.test([{
    name: "finding no intersected values",
    test: function () {
      // sync
      wru.assert(1);

      var obj1 = {
        index: "unique1,unique2,unique3"
      };
      var obj2 = {
        index: "unique4,unique5,unique6"
      };

      var obj3 = $.intersect(obj1, obj2);

      wru.assert(obj3.index, !obj3.index);
    }
  }, {
    name: "finding an intersected value",
    test: function () {
      // sync
      wru.assert(1);

      var obj1 = {
        index: "unique1,common,unique2"
      };
      var obj2 = {
        index: "unique3,common,unique4"
      };

      var obj3 = $.intersect(obj1, obj2);

      wru.assert(obj3.index, obj3.index === "common");
    }
  }, {
    name: "finding two intersected values",
    test: function () {
      // sync
      wru.assert(1);

      var obj1 = {
        index: "unique1,common1,unique2,common2"
      };
      var obj2 = {
        index: "common2,unique3,common1,unique4"
      };

      var obj3 = $.intersect(obj1, obj2);

      wru.assert(obj3.index, obj3.index === "common2,common1");
    }
  }, {
    name: "finding no intersected indexes",
    test: function () {
      // sync
      wru.assert(1);

      var obj1 = {
        unique1: "foo"
      };
      var obj2 = {
        unique2: "foo"
      };

      var obj3 = $.intersect(obj1, obj2);

      wru.assert(obj3.unique1,...