Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

can-set

Package Overview
Dependencies
Maintainers
3
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

can-set - npm Package Compare versions

Comparing version 0.3.8-0 to 0.3.9

src/comparator_tests/boolean_test.js

2

package.json
{
"name": "can-set",
"version": "0.3.8-0",
"version": "0.3.9",
"description": "Set logic for CanJS",

@@ -5,0 +5,0 @@ "main": "src/set.js",

@@ -30,4 +30,4 @@ # can-set

> - [count](#setcount)
> - [getUnion](#setgetUnion)
> - [getSubset](#setgetSubset)
> - [getUnion](#setgetunion)
> - [getSubset](#setgetsubset)
> - [Algebra](#setalgebra)

@@ -34,0 +34,0 @@ > - [comparators](#setcomparators)

var QUnit = require("steal-qunit");
var set = require('./set-core'),
comparators = require("./comparators");
QUnit.module("comparators");
test('rangeInclusive set.equal', function(){
ok(
set.equal(
{start: 0, end: 100},
{start: 0, end: 100},
comparators.rangeInclusive("start", "end")),
"they are equal" );
ok(
!set.equal(
{start: 0, end: 100},
{start: 0, end: 101},
comparators.rangeInclusive("start", "end")),
"they are not equal" );
ok(
!set.equal(
{start: 0, end: 100},
{start: 1, end: 100},
comparators.rangeInclusive("start", "end")),
"they are not equal" );
});
test('rangeInclusive set.subset', function(){
ok(
set.subset(
{start: 0, end: 100},
{start: 0, end: 100},
comparators.rangeInclusive("start", "end")),
"self is a subset" );
ok(
set.subset(
{start: 0, end: 100},
{start: 0, end: 101},
comparators.rangeInclusive("start", "end")),
"end extends past subset" );
ok(
set.subset(
{start: 1, end: 100},
{start: 0, end: 100},
comparators.rangeInclusive("start", "end")),
"start extends before subset" );
});
test('rangeInclusive set.difference', function() {
var comparator = comparators.rangeInclusive('start', 'end');
var res = set.difference({ start: 0, end: 99 }, { start: 50, end: 101 }, comparator);
deepEqual(res, { start: 0, end: 49 }, "got a diff");
res = set.difference({}, { start: 0, end: 10 }, comparator);
equal(res, true);
// difference side by side
res = set.difference({ start: 0, end: 49 }, { start: 50, end: 101 }, comparator);
deepEqual(res, { start: 0, end: 49 }, "side by side");
res = set.difference({ start: 0, end: 49 }, { start: 0, end: 20 }, comparator);
deepEqual(res, { start: 21, end: 49 }, "side by side");
res = set.difference({ start: 0, end: 49 }, { start: 20, end: 49 }, comparator);
deepEqual(res, { start: 0, end: 19 }, "side by side");
});
test('rangeInclusive set.union', function() {
var comparator = comparators.rangeInclusive('start', 'end');
var res = set.union({ start: 0, end: 99 }, { start: 50, end: 101 }, comparator);
deepEqual(res, { start: 0, end: 101 }, "got a union");
res = set.union({}, { start: 0, end: 10 }, comparator);
deepEqual(res, {}, "union has everything");
res = set.union({start: 100, end: 199}, {start: 200, end: 299}, comparator);
deepEqual(res, {start:100, end:299}, "default compare works");
res = set.union({start: 200, end: 299}, {start: 100, end: 199}, comparator);
deepEqual(res, {start:100, end:299}, "default compare works");
res = set.union({start: 200, end: 299}, {start: 100, end: 209}, comparator);
deepEqual(res, {start:100, end:299}, "default compare works");
res = set.union({start: 100, end: 299}, {start: 103, end: 209}, comparator);
deepEqual(res, {start:100, end:299}, "default compare works");
res = set.union({start: 100, end: 299}, {start: 100, end: 299}, comparator);
deepEqual(res, {start:100, end:299}, "default compare works");
});
test('rangeInclusive set.count', function(){
var comparator = comparators.rangeInclusive('start', 'end');
var res = set.count({ start: 0, end: 99 }, comparator);
equal(res, 100, "count is right");
});
test('rangeInclusive set.intersection', function(){
var comparator = comparators.rangeInclusive('start', 'end');
var res = set.intersection({ start: 0, end: 99 }, { start: 50, end: 101 }, comparator);
deepEqual(res, { start: 50, end: 99 }, "got a intersection");
});
test('boolean set.difference', function() {
var comparator = comparators.boolean('completed');
var res = set.difference({} , { completed: true }, comparator);
deepEqual(res, { completed: false }, "inverse to false");
res = set.difference({}, { completed: false }, comparator);
deepEqual(res, { completed: true }, "inverse to true");
});
test('boolean set.union', function(){
var comparator = comparators.boolean('completed');
var res = set.union({completed: false} , { completed: true }, comparator);
deepEqual(res, { }, "union has everything");
});
test('boolean set.intersection', function(){
var comparator = comparators.boolean('completed');
var res = set.intersection({foo: "bar"} , { completed: true }, comparator);
equal(res, false, "intersection is false (#4)");
});
test('enum set.intersection', function(){
var comparator = comparators.enum('type',['new','prep','deliver','delivered']);
var res = set.intersection({} , { type: 'new' }, comparator);
deepEqual(res, {type: 'new' }, "all");
res = set.intersection({} , { type: ['new','prep'] }, comparator);
deepEqual(res, {type: ['new','prep'] }, "all v array intersection");
res = set.intersection({type: ['prep'] } , { type: ['new','prep'] }, comparator);
deepEqual(res, {type: 'prep' }, "items v items intersection");
res = set.intersection({type: [] } , { type: ['new','prep'] }, comparator);
deepEqual(res, {type: ['new','prep'] }, "empty v array intersection");
res = set.intersection({ type: 'new' },{}, comparator);
deepEqual(res, {type: 'new' }, "single v all");
});
test('enum set.difference', function(){
var comparator = comparators.enum('type',['new','prep','deliver','delivered']);
var res = set.difference({} , { type: 'new' }, comparator);
deepEqual(res, {type: ['prep','deliver','delivered'] }, "all");
res = set.difference({} , { type: ['new','prep'] }, comparator);
deepEqual(res, {type: ['deliver','delivered'] }, "intersection");
res = set.difference({type: ['prep'] } , { type: ['new','prep'] }, comparator);
deepEqual(res, false, "intersection");
res = set.difference({type: [] } , { type: ['new','prep'] }, comparator);
deepEqual(res, {type: ['deliver','delivered'] }, "intersection");
res = set.difference({ type: 'new' },{}, comparator);
deepEqual(res, false, "all");
});
test('enum set.union', function(){
var comparator = comparators.enum('type',['new','prep','deliver','delivered']);
var res = set.union({} , { type: 'new' }, comparator);
deepEqual(res, {}, "all");
res = set.union({} , { type: ['new','prep'] }, comparator);
deepEqual(res, {}, "intersection");
res = set.union({type: ['prep'] } , { type: ['new','prep'] }, comparator);
deepEqual(res, { type: ['prep','new'] }, "intersection");
res = set.union({type: [] } , { type: ['new','prep'] }, comparator);
deepEqual(res, { }, "intersection");
res = set.union({ type: 'new' },{}, comparator);
deepEqual(res, {}, "all");
res = set.union({type: ['deliver','delivered'] } , { type: ['new','prep'] }, comparator);
deepEqual(res, {}, "intersection");
});
test('enum set.equal', function(){
var comparator = comparators.enum('type',['new','prep','deliver','delivered']);
var res = set.equal({} , { type: 'new' }, comparator);
deepEqual(res, false, "all");
res = set.equal({} , { type: ['new','prep','deliver','delivered'] }, comparator);
deepEqual(res, true, "intersection");
res = set.equal({type: ['prep'] } , { type: ['prep'] }, comparator);
deepEqual(res, true, "intersection");
res = set.equal({type: 'prep'} , { type: 'prep' }, comparator);
deepEqual(res, true, "intersection");
res = set.equal({ type: 'new' },{type: 'prep'}, comparator);
deepEqual(res, false, "all");
});
test('enum set.subset', function(){
var comparator = comparators.enum('type',['new','prep','deliver','delivered']);
var res = set.subset({} , { type: 'new' }, comparator);
deepEqual(res, false, "all");
res = set.subset({ type: 'new' }, {} , comparator);
deepEqual(res, true, "all");
res = set.subset({} , { type: ['new','prep','deliver','delivered'] }, comparator);
deepEqual(res, true, "intersection");
res = set.subset({type: ['prep'] } , { type: ['prep'] }, comparator);
deepEqual(res, true, "intersection");
res = set.subset({type: 'prep'} , { type: 'prep' }, comparator);
deepEqual(res, true, "intersection");
res = set.subset({ type: 'new' },{type: 'prep'}, comparator);
deepEqual(res, false, "all");
res = set.subset({type: 'prep'} , { type: ['new','prep','deliver','delivered'] }, comparator);
deepEqual(res, true, "intersection");
});
require('./comparator_tests/rangeInclusive_test');
require('./comparator_tests/boolean_test');
require('./comparator_tests/enum_test');

@@ -51,11 +51,11 @@ var h = require("./helpers");

};
}
}
// A starts at B but A ends later
else if( sAv1 === sBv1 && sBv2 < sAv2 ) {
return after;
}
}
// A end at B but A starts earlier
else if( sAv2 === sBv2 && sBv1 > sAv1 ) {
return before;
}
}
// B contains A

@@ -88,3 +88,3 @@ else if( within(sAv1, [sBv1, sBv2]) && within(sAv2, [sBv1, sBv2]) ) {

return after;
}
}
// side by side ... nothing intersection

@@ -98,4 +98,4 @@ else if(sAv2 === sBv1-1) {

};
}
}
else if(sBv2 === sAv1 - 1) {

@@ -115,3 +115,3 @@ return {

}
};

@@ -133,3 +133,3 @@

module.exports = {
enum: function(prop, enumData){
"enum": function(prop, enumData){
var compares = {};

@@ -139,3 +139,3 @@ compares[prop] = function(vA, vB, A, B){

vB = cleanUp(vB, enumData);
var data = h.arrayUnionIntersectionDifference(vA, vB);

@@ -146,3 +146,3 @@ // if the difference is empty ... there is no difference

}
// if any of them have everything, return undefined

@@ -158,3 +158,3 @@ h.each(data, function(value, prop){

});
return data;

@@ -168,10 +168,10 @@ };

* [0,20] loads 21 items.
*
*
* @param {String} startIndexProperty
* @param {String} endIndexProperty
*
*
* @body
*
*
* ## Use
*
*
* ```

@@ -199,3 +199,3 @@ * new set.Algebra( extend({}, comparators.rangeInclusive("start","end") ) )

};
compares[startIndexProperty] = function(vA, vB, A, B){

@@ -206,4 +206,4 @@ if(vA === undefined) {

var res = diff(A, B, startIndexProperty, endIndexProperty);
var result = makeResult(res, 0);

@@ -228,5 +228,5 @@ result.getSubset = function(a, b, bItems, algebra, options){

aEndValue = a[endIndexProperty];
var bStartValue = b[startIndexProperty];
if( ! (endIndexProperty in b) || ! (endIndexProperty in a) ) {

@@ -252,3 +252,3 @@ return bItems.slice(aStartValue, aEndValue+1);

};
return res;

@@ -260,3 +260,3 @@ };

* @function
* Makes boolean
* Makes boolean
*/

@@ -263,0 +263,0 @@ "boolean": makeComparator(function(propA, propB) {

@@ -26,33 +26,12 @@ var h = require("./helpers");

}
if(subsetCheck) {
// If the subset side hasn't been determined, set it.
if( !options.subset ) {
options.subset = subsetCheck;
}
// only add the property to the result if it is on the same
// side as the identified subset.
var addProp = options.subset === subsetCheck;
if(addProp) {
if(subsetCheck === "subsetB") {
options.result[prop] = b;
} else {
options.result[prop] = a;
}
// returning undefined allows `eachInUnique` to keep checking other
// properties.
return undefined;
}
return false;
}
// if a and b are the same ... add this to the result if no subset check.
if(a === b) {
if(subsetCheck === "subsetB") {
options.result[prop] = b;
} else {
options.result[prop] = a;
return true;
} else {
return false;
}
return undefined;
};
var addToResult = function(fn, name){
return function(a, b, aParent, bParent, prop, compares, options){

@@ -69,3 +48,3 @@ var res = fn.apply(this, arguments);

};
};

@@ -82,3 +61,3 @@

options["default"] = false;
return loop(a, b, aParent, bParent, prop, compares, options);

@@ -140,3 +119,3 @@ },

}
for (var prop in a) {

@@ -157,3 +136,3 @@ var compare = compares[prop] === undefined ? compares['*'] : compares[prop];

}
},

@@ -169,5 +148,5 @@ subset: function(a, b, aParent, bParent, prop, compares, options) {

options.removeProps = [];
options["default"] = false;
return loop(a, b, aParent, bParent, prop, compares, options);

@@ -178,4 +157,4 @@ },

if(aType === 'object' || aType === 'function') {
return h.eachInUnique(a,
return h.eachInUnique(a,
function(a, b, aParent, bParent, prop){

@@ -186,4 +165,4 @@ var compare = compares[prop] === undefined ? compares['*'] : compares[prop];

}
},
b,
},
b,
function(a, b, aParent, bParent, prop){

@@ -194,6 +173,6 @@ var compare = compares[prop] === undefined ? compares['*'] : compares[prop];

}
},
},
true);
}
},

@@ -212,12 +191,12 @@ /**

} else if(compareResult && typeof compareResult === "object"){
if( compareResult.getSubset ) {
options.removeProps.push(prop);
if(options.getSubsets.indexOf(compareResult.getSubset) === -1) {
if(h.indexOf.call(options.getSubsets, compareResult.getSubset) === -1) {
options.getSubsets.push(compareResult.getSubset);
}
}
// A \ B subset intersects in both directions
// but does not diff from
// but does not diff from
if( ("intersection" in compareResult) && !("difference" in compareResult)) {

@@ -242,3 +221,3 @@ var reverseResult = compares(b, a,bParent, aParent, prop, options);

}
for (var prop in b) {

@@ -264,3 +243,3 @@ var compare = compares[prop] === undefined ? compares['*'] : compares[prop];

}
},

@@ -277,3 +256,3 @@ /**

// A \ B subset intersects in both directions
// but does not diff from
// but does not diff from
if( ("intersection" in compareResult) && !("difference" in compareResult)) {

@@ -303,5 +282,5 @@ var reverseResult = compares(b, a,bParent, aParent, prop, options);

];
options["default"] = true;
var res = loop(a, b, aParent, bParent, prop, compares, options);

@@ -353,3 +332,3 @@ if(res === true && options.performedDifference) {

}
for (var prop in a) {

@@ -384,5 +363,5 @@ var compare = compares[prop] === undefined ? compares['*'] : compares[prop];

options.getUnions = [];
options["default"] = false;
var res = loop(a, b, aParent, bParent, prop, compares, options);

@@ -407,8 +386,8 @@ if(res === true) {

//options.removeProps.push(prop);
if(options.getUnions.indexOf(compareResult.getUnion) === -1) {
if(h.indexOf.call(options.getUnions, compareResult.getUnion) === -1) {
options.getUnions.push(compareResult.getUnion);
}
}
// is there a difference?

@@ -430,3 +409,3 @@ if("union" in compareResult) {

var compare = compares[prop] === undefined ? compares['*'] : compares[prop];
if (! loop(a, b, aParent, bParent, prop, compare, options ) ) {

@@ -446,14 +425,14 @@ var subsetCheck;

}
return false;
}
};
var aType = typeof a;
if(aType === 'object' || aType === 'function') {
return h.eachInUnique(a,
subsetCompare,
b,
subsetCompare,
return h.eachInUnique(a,
subsetCompare,
b,
subsetCompare,
true);

@@ -483,7 +462,7 @@ }

];
options["default"] = false;
loop(a, b, aParent, bParent, prop, compares, options);
if( typeof options.count === "number") {

@@ -502,3 +481,3 @@ return options.count;

if(typeof compareResult.count === "number") {
if(!("count" in options) || compareResult.count === options.count) {

@@ -535,5 +514,5 @@ options.count = compareResult.count;

];
options["default"] = false;
var res = loop(a, b, aParent, bParent, prop, compares, options);

@@ -559,7 +538,6 @@ if(res === true) {

if(compareResult.intersection !== undefined) {
options.performedIntersection++;
return addIntersectedPropertyToResult(compareResult.intersection, compareResult.intersection, aParent, bParent, prop, compares, options);
options.result[prop] = compareResult.intersection;
}
return undefined;
options.performedIntersection++;
return true;
}

@@ -572,3 +550,3 @@ }

var compare = compares[prop] === undefined ? compares['*'] : compares[prop];
// If some property value is not the exact same

@@ -580,10 +558,10 @@ if (! loop(a, b, aParent, bParent, prop, compare, options ) ) {

};
var aType = typeof a;
if(aType === 'object' || aType === 'function') {
return h.eachInUnique(a,
subsetCompare,
b,
subsetCompare,
return h.eachInUnique(a,
subsetCompare,
b,
subsetCompare,
true);

@@ -590,0 +568,0 @@ }

var set = require("./set");
var QUnit = require("steal-qunit");
var comparators = require("./comparators");
var h = require("./helpers");

@@ -22,5 +23,5 @@ QUnit.module("set/src/get");

var res = set.getSubset( {type: 'critical', start: 1, end: 3}, {}, items, comparators.rangeInclusive("start","end") );
deepEqual(res.map(getId), [2,4,6]);
deepEqual(h.map.call(res, getId), [2,4,6]);
});

@@ -30,25 +31,25 @@

var res = set.getSubset(
{type: 'critical', start: 21, end: 23},
{type: 'critical', start: 20, end: 27},
items,
var res = set.getSubset(
{type: 'critical', start: 21, end: 23},
{type: 'critical', start: 20, end: 27},
items,
comparators.rangeInclusive("start","end") );
deepEqual(res.map(getId), [2,4,6]);
deepEqual(h.map.call(res, getId), [2,4,6]);
});
test("getUnion basics", function(){
var union = set.getUnion({},{foo: "bar"},items, items.slice(0,3));
deepEqual(union, items);
});
test("getUnion against ranged sets", function(){
var union = set.getUnion({start: 10, end: 13},{start: 14, end: 17},items.slice(0,4), items.slice(4,8), comparators.rangeInclusive("start","end"));
deepEqual(union, items);
union = set.getUnion({start: 14, end: 17}, {start: 10, end: 13}, items.slice(4,8),items.slice(0,4), comparators.rangeInclusive("start","end"));

@@ -59,28 +60,28 @@ deepEqual(union, items, "disjoint after");

test("getUnion against overlapping ranged sets", function(){
var union = set.getUnion(
{start: 10, end: 14},
{start: 13, end: 17},
items.slice(0,5),
items.slice(3,8),
{start: 13, end: 17},
items.slice(0,5),
items.slice(3,8),
comparators.rangeInclusive("start","end"));
deepEqual(union, items);
union = set.getUnion(
{start: 10, end: 11},
{start: 11, end: 17},
items.slice(0,2),
items.slice(1,8),
{start: 11, end: 17},
items.slice(0,2),
items.slice(1,8),
comparators.rangeInclusive("start","end"));
deepEqual(union, items);
union = set.getUnion(
{start: 11, end: 17},
{start: 11, end: 17},
{start: 10, end: 11},
items.slice(1,8),
items.slice(0,2),
items.slice(1,8),
items.slice(0,2),
comparators.rangeInclusive("start","end"));
deepEqual(union, items);

@@ -87,0 +88,0 @@ });

@@ -7,3 +7,3 @@ var compare = require("./compare");

var defaultGetSubset = function(a, b, bItems, algebra, options){
return bItems.filter(function(item){
return h.filter.call(bItems, function(item){
return set.subset(item, a, algebra);

@@ -17,3 +17,3 @@ });

var options = {};
var isSubset = compare.subset(a, b, undefined, undefined, undefined, algebra, options);

@@ -23,5 +23,5 @@

var aItems = bItems.slice(0);
var aCopy = h.extend({}, a);
// remove properties that are going to do their own filtering.

@@ -35,6 +35,6 @@ h.each(options.removeProps, function(prop){

});
return aItems;
}
},

@@ -48,5 +48,5 @@ getUnion: function(a,b,aItems, bItems, algebra){

}
var isUnion = compare.union(a, b, undefined, undefined, undefined, algebra, options);
if(isUnion) {

@@ -53,0 +53,0 @@ h.each(options.getUnions, function(filter){

@@ -65,3 +65,3 @@ var helpers;

}
var j = i+1;

@@ -90,7 +90,7 @@ while( j < arr.length ) {

var map = {};
var intersection = [];
var union = [];
var difference = arr1.slice(0);
helpers.each(arr1, function(value){

@@ -100,11 +100,11 @@ map[value] = true;

});
helpers.each(arr2, function(value){
if(map[value]) {
intersection.push(value);
var index = difference.indexOf(value);
var index = helpers.indexOf.call(difference, value);
if(index !== -1) {
difference.splice(index, 1);
}
} else {

@@ -114,3 +114,3 @@ union.push(value);

});
return {

@@ -138,3 +138,27 @@ intersection: intersection,

return true;
},
indexOf: Array.prototype.indexOf || function(item) {
for (var i = 0, thisLen = this.length; i < thisLen; i++) {
if (this[i] === item) {
return i;
}
}
return -1;
},
map: Array.prototype.map || function(cb){
var out = [];
for(var i = 0, len = this.length; i < len; i++) {
out.push(cb(this[i], i, this));
}
return out;
},
filter: Array.prototype.filter || function(cb){
var out = [];
for(var i = 0, len = this.length; i < len; i++) {
if(cb(this[i], i, this)) {
out.push(this[i]);
}
}
return out;
}
};
};

@@ -195,3 +195,3 @@ require("steal-qunit");

test('set.intersection', function(){
var res = set.intersection({}, { completed: true });
/*var res = set.intersection({}, { completed: true });
deepEqual(res , { completed: true }, "set / subset");

@@ -206,3 +206,6 @@

res = set.intersection({foo: "bar"},{foo: "zed"});
ok(!res, "values not equal");
ok(!res, "values not equal");*/
var res = set.intersection({foo: 'bar'},{completed: true});
deepEqual(res, {foo: 'bar', completed: true}, 'intersection should combine definitions');
});

@@ -209,0 +212,0 @@

@@ -12,8 +12,11 @@ require("steal-qunit");

set.comparators.boolean('completed'),
set.comparators.enum('type',['new','prep','deliver','delivered'])
set.comparators["enum"]('type',['new','prep','deliver','delivered'])
);
var res = algebra.subset({ type: ['new'] },{type: ['new','prep']});
deepEqual(res, true, "enum");
});

@@ -5,2 +5,2 @@

require('./get_test');
require('./set_test');
require('./set_test');
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc