Skip to content

Commit

Permalink
1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nlundquist committed Apr 13, 2017
1 parent 1e85335 commit c301176
Show file tree
Hide file tree
Showing 8 changed files with 3,110 additions and 0 deletions.
20 changes: 20 additions & 0 deletions dist/amd/src/clause.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*can-set@1.1.0#src/clause*/
define(function (require, exports, module) {
var assign = require('can-util/js/assign');
var each = require('can-util/js/each');
var clause = {};
module.exports = clause;
clause.TYPES = [
'where',
'order',
'paginate',
'id'
];
each(clause.TYPES, function (type) {
var className = type.charAt(0).toUpperCase() + type.substr(1);
clause[className] = function (compare) {
assign(this, compare);
};
clause[className].type = type;
});
});
455 changes: 455 additions & 0 deletions dist/amd/src/compare.js

Large diffs are not rendered by default.

35 changes: 35 additions & 0 deletions dist/amd/src/get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*can-set@1.1.0#src/get*/
define(function (require, exports, module) {
var compare = require('./compare');
var h = require('./helpers');
var each = require('can-util/js/each');
var filterData = function (data, clause, props) {
return h.filter.call(data, function (item) {
var isSubset = compare.subset(item, clause, undefined, undefined, undefined, props, {});
return isSubset;
});
};
module.exports = {
subsetData: function (a, b, bData, algebra) {
var aClauseProps = algebra.getClauseProperties(a);
var bClauseProps = algebra.getClauseProperties(b);
var options = {};
var aData = filterData(bData, aClauseProps.where, algebra.clauses.where);
if (aData.length && (aClauseProps.enabled.order || bClauseProps.enabled.order)) {
options = {};
var propName = h.firstProp(aClauseProps.order), compareOrder = algebra.clauses.order[propName];
aData = aData.sort(function (aItem, bItem) {
return compareOrder(a[propName], aItem, bItem);
});
}
if (aData.length && (aClauseProps.enabled.paginate || bClauseProps.enabled.paginate)) {
options = {};
compare.subset(aClauseProps.paginate, bClauseProps.paginate, undefined, undefined, undefined, algebra.clauses.paginate, options);
each(options.getSubsets, function (filter) {
aData = filter(a, b, aData, algebra, options);
});
}
return aData;
}
};
});
172 changes: 172 additions & 0 deletions dist/amd/src/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*can-set@1.1.0#src/helpers*/
define(function (require, exports, module) {
var assign = require('can-util/js/assign');
var each = require('can-util/js/each');
var last = require('can-util/js/last');
var IgnoreType = function () {
};
var helpers;
module.exports = helpers = {
eachInUnique: function (a, acb, b, bcb, defaultReturn) {
var bCopy = assign({}, b), res;
for (var prop in a) {
res = acb(a[prop], b[prop], a, b, prop);
if (res !== undefined) {
return res;
}
delete bCopy[prop];
}
for (prop in bCopy) {
res = bcb(undefined, b[prop], a, b, prop);
if (res !== undefined) {
return res;
}
}
return defaultReturn;
},
doubleLoop: function (arr, callbacks) {
if (typeof callbacks === 'function') {
callbacks = { iterate: callbacks };
}
var i = 0;
while (i < arr.length) {
if (callbacks.start) {
callbacks.start(arr[i]);
}
var j = i + 1;
while (j < arr.length) {
if (callbacks.iterate(arr[j], j, arr[i], i) === false) {
arr.splice(j, 1);
} else {
j++;
}
}
if (callbacks.end) {
callbacks.end(arr[i]);
}
i++;
}
},
identityMap: function (arr) {
var map = {};
each(arr, function (value) {
map[value] = 1;
});
return map;
},
arrayUnionIntersectionDifference: function (arr1, arr2) {
var map = {};
var intersection = [];
var union = [];
var difference = arr1.slice(0);
each(arr1, function (value) {
map[value] = true;
union.push(value);
});
each(arr2, function (value) {
if (map[value]) {
intersection.push(value);
var index = helpers.indexOf.call(difference, value);
if (index !== -1) {
difference.splice(index, 1);
}
} else {
union.push(value);
}
});
return {
intersection: intersection,
union: union,
difference: difference
};
},
arraySame: function (arr1, arr2) {
if (arr1.length !== arr2.length) {
return false;
}
var map = helpers.identityMap(arr1);
for (var i = 0; i < arr2.length; i++) {
var val = map[arr2[i]];
if (!val) {
return false;
} else if (val > 1) {
return false;
} else {
map[arr2[i]]++;
}
}
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;
},
ignoreType: new IgnoreType(),
firstProp: function (set) {
for (var prop in set) {
return prop;
}
},
index: function (compare, items, props) {
if (!items || !items.length) {
return undefined;
}
if (compare(props, items[0]) === -1) {
return 0;
} else if (compare(props, last(items)) === 1) {
return items.length;
}
var low = 0, high = items.length;
while (low < high) {
var mid = low + high >>> 1, item = items[mid], computed = compare(props, item);
if (computed === -1) {
high = mid;
} else {
low = mid + 1;
}
}
return high;
},
defaultSort: function (sortPropValue, item1, item2) {
var parts = sortPropValue.split(' ');
var sortProp = parts[0];
var item1Value = item1[sortProp];
var item2Value = item2[sortProp];
var temp;
var desc = parts[1] || '';
desc = desc.toLowerCase() === 'desc';
if (desc) {
temp = item1Value;
item1Value = item2Value;
item2Value = temp;
}
if (item1Value < item2Value) {
return -1;
}
if (item1Value > item2Value) {
return 1;
}
return 0;
}
};
});
Loading

0 comments on commit c301176

Please sign in to comment.