can-query-logic
Advanced tools
Comparing version 0.8.3 to 0.8.4
{ | ||
"name": "can-query-logic", | ||
"version": "0.8.3", | ||
"version": "0.8.4", | ||
"description": "query data", | ||
@@ -5,0 +5,0 @@ "homepage": "", |
@@ -0,1 +1,10 @@ | ||
var defaultCompare = { | ||
$gt: function(valueA, valueB) { | ||
return valueA > valueB; | ||
}, | ||
$lt: function(valueA, valueB) { | ||
return valueA < valueB; | ||
} | ||
}; | ||
var helpers = { | ||
@@ -52,4 +61,11 @@ // given two arrays of items, combines and only returns the unique ones | ||
}, | ||
sorter: function (sortPropValue) { | ||
defaultCompare: defaultCompare, | ||
sorter: function (sortPropValue, sorters) { | ||
var data = helpers.sortData(sortPropValue); | ||
var compare; | ||
if(sorters && sorters[data.prop]) { | ||
compare = sorters[data.prop]; | ||
} else { | ||
compare = defaultCompare; | ||
} | ||
return function(item1, item2){ | ||
@@ -60,3 +76,2 @@ var item1Value = item1[data.prop]; | ||
if(data.desc) { | ||
@@ -68,7 +83,7 @@ temp = item1Value; | ||
if(item1Value < item2Value) { | ||
if( compare.$lt( item1Value, item2Value) ) { | ||
return -1; | ||
} | ||
if(item1Value > item2Value) { | ||
if( compare.$gt( item1Value, item2Value)) { | ||
return 1; | ||
@@ -75,0 +90,0 @@ } |
@@ -259,4 +259,4 @@ var canSymbol = require("can-symbol"); | ||
if(basicQuery.sort !== id) { | ||
res.sort = basicQuery.sort; | ||
if(basicQuery.sort.key !== id) { | ||
res.sort = basicQuery.sort.key; | ||
} | ||
@@ -270,4 +270,4 @@ return res; | ||
// Makes a sort type that can make a compare function using the SetType | ||
var Sort = BasicQuery.makeSort(keys, hydrateAndValue); | ||
var serializer = new Serializer(serializeMap); | ||
@@ -310,5 +310,5 @@ serializer.add(comparisonsConverter.serializer); | ||
if(data.sort) { | ||
query.sort = data.sort; | ||
query.sort = new Sort(data.sort); | ||
} else { | ||
query.sort = id; | ||
query.sort = new Sort(id); | ||
} | ||
@@ -315,0 +315,0 @@ return new BasicQuery(query); |
@@ -9,2 +9,3 @@ var set = require("../set"); | ||
// TYPES FOR FILTERING | ||
var KeysAnd = andOrNot.KeysAnd, | ||
@@ -14,7 +15,50 @@ Or = andOrNot.ValuesOr, | ||
// TYPES FOR PAGINATION | ||
var RecordRange = makeRealNumberRangeInclusive(0, Infinity); | ||
// Wire up the sub-types to be able to compare to each other | ||
// WILL MAKE A TYPE FOR SORTING | ||
function makeSort(schemaKeys, hydrateAndValue){ | ||
// Makes gt and lt functions that `helpers.sorter` can use | ||
// to make a `compare` function for `Array.sort(compare)`.` | ||
var sorters = {}; | ||
canReflect.eachKey(schemaKeys, function(schemaProp, key){ | ||
sorters[key] = { | ||
// valueA is GT valueB | ||
$gt: function(valueA, valueB) { | ||
var $gt = hydrateAndValue({$gt: valueB}, key, schemaProp); | ||
return $gt.isMember(valueA); | ||
}, | ||
$lt: function( valueA, valueB ){ | ||
var $lt = hydrateAndValue({$lt: valueB}, key, schemaProp); | ||
return $lt.isMember(valueA); | ||
} | ||
}; | ||
}); | ||
function Sort(key) { | ||
this.key = key; | ||
this.compare = helpers.sorter(key, sorters); | ||
} | ||
function identityIntersection(v1, v2) { | ||
return v1.key === v2.key ? v1 : set.EMPTY; | ||
} | ||
function identityDifference(v1, v2) { | ||
return v1.key === v2.key ? set.EMPTY : v1; | ||
} | ||
function identityUnion(v1, v2) { | ||
return v1.key === v2.key ? v1 : set.UNDEFINABLE; | ||
} | ||
set.defineComparison(Sort, Sort, { | ||
intersection: identityIntersection, | ||
difference: identityDifference, | ||
union: identityUnion | ||
}); | ||
return Sort; | ||
} | ||
var DefaultSort = makeSort({}); | ||
// Define the BasicQuery type | ||
@@ -32,2 +76,5 @@ function BasicQuery(query) { | ||
} | ||
if(typeof this.sort === "string") { | ||
this.sort = new DefaultSort(this.sort); | ||
} | ||
} | ||
@@ -40,2 +87,3 @@ | ||
BasicQuery.RecordRange = RecordRange; | ||
BasicQuery.makeSort = makeSort; | ||
@@ -50,4 +98,3 @@ // BasicQuery's prototype methods. | ||
sortData: function(data) { | ||
var sort = helpers.sorter(this.sort); | ||
return data.slice(0).sort(sort); | ||
return data.slice(0).sort(this.sort.compare); | ||
}, | ||
@@ -71,3 +118,3 @@ filterMembersAndGetCount: function(bData, parentQuery) { | ||
// sort the data if needed | ||
if (count && (this.sort !== parentQuery.sort)) { | ||
if (count && (this.sort.key !== parentQuery.sort.key)) { | ||
aData = this.sortData(aData); | ||
@@ -93,3 +140,3 @@ } | ||
// everything but range is equal | ||
else if (this.sort === parentQuery.sort && set.isEqual(parentQuery.filter, this.filter)) { | ||
else if (this.sort.key === parentQuery.sort.key && set.isEqual(parentQuery.filter, this.filter)) { | ||
return { | ||
@@ -116,14 +163,11 @@ data: aData.slice(this.page.start - parentQuery.page.start, this.page.end - parentQuery.page.start + 1), | ||
} | ||
// basically if there's pagination, we might not be able to do this | ||
}, | ||
index: function(props, items) { | ||
var data = helpers.sortData(this.sort); | ||
// make sure we have the property | ||
var data = helpers.sortData(this.sort.key); | ||
if (!Object.prototype.hasOwnProperty.call(props, data.prop)) { | ||
return undefined; | ||
} | ||
var sort = helpers.sorter(this.sort); | ||
return helpers.getIndex(sort, items, props); | ||
// use the passed sort's compare function | ||
return helpers.getIndex(this.sort.compare, items, props); | ||
}, | ||
@@ -140,3 +184,3 @@ isMember: function(props) { | ||
// Helpers used for the `set` comparators | ||
var CLAUSE_TYPES = ["filter", "page", "sort"]; | ||
var CLAUSE_TYPES = ["filter", "page","sort"]; | ||
@@ -189,3 +233,3 @@ function getDifferentClauseTypes(queryA, queryB) { | ||
"sortIsEqual": function() { | ||
return this.queryA.sort === this.queryB.sort; | ||
return this.queryA.sort.key === this.queryB.sort.key; | ||
}, | ||
@@ -227,24 +271,21 @@ "aFilterIsSubset": function() { | ||
var pageIsEqual = set.isEqual(queryA.page, queryB.page); | ||
var pagesAreUniversal = pageIsEqual && set.isEqual(queryA.page, set.UNIVERSAL); | ||
var meta = metaInformation(queryA, queryB); | ||
var filterUnion = set.union(queryA.filter, queryB.filter); | ||
var sortIsEqual = set.isEqual(queryA.sort, queryB.sort); | ||
if (pagesAreUniversal) { | ||
if (meta.pagesAreUniversal) { | ||
// We ignore the sort. | ||
return new BasicQuery({ | ||
filter: filterUnion, | ||
sort: sortIsEqual ? queryA.sort : undefined | ||
sort: meta.sortIsEqual ? queryA.sort.key : undefined | ||
}); | ||
} | ||
var filterIsEqual = set.isEqual(queryA.filter, queryB.filter); | ||
if (filterIsEqual) { | ||
if (sortIsEqual) { | ||
if (meta.filterIsEqual) { | ||
if (meta.sortIsEqual) { | ||
return new BasicQuery({ | ||
filter: queryA.filter, | ||
sort: queryA.sort, | ||
sort: queryA.sort.key, | ||
page: set.union(queryA.page, queryB.page) | ||
@@ -286,3 +327,3 @@ }); | ||
filter: filterResult, | ||
sort: meta.sortIsEqual ? queryA.sort : undefined | ||
sort: meta.sortIsEqual ? queryA.sort.key : undefined | ||
}); | ||
@@ -306,3 +347,3 @@ | ||
filter: queryA.filter, | ||
sort: queryA.sort, | ||
sort: queryA.sort.key, | ||
page: set.intersection(queryA.page, queryB.page) | ||
@@ -344,3 +385,3 @@ }); | ||
filter: set.difference(queryA.filter, queryB.filter), | ||
sort: queryA.sort | ||
sort: queryA.sort.key | ||
}); | ||
@@ -386,3 +427,3 @@ } | ||
page: queryA.page, | ||
sort: queryA.sort | ||
sort: queryA.sort.key | ||
}; | ||
@@ -389,0 +430,0 @@ query[clause] = result; |
@@ -235,2 +235,26 @@ var QueryLogic = require("../can-query-logic"); | ||
var result = queryLogic.filterMembers({ | ||
sort: "date" | ||
},[ | ||
{id: 2, date: new Date(1982,9,20).toString()}, | ||
{id: 1, date: new Date(1981,9,20).toString()}, | ||
{id: 4, date: new Date(1984,9,20).toString()}, | ||
{id: 3, date: new Date(1983,9,20).toString()} | ||
]); | ||
var ids = result.map(function(item){ return item.id}); | ||
QUnit.deepEqual(ids,[1,2,3,4], "sorted correctly"); | ||
var index = queryLogic.index({ | ||
sort: "date" | ||
}, | ||
[ | ||
{id: 1, date: new Date(2018,4,20).toString()}, // M | ||
{id: 2, date: new Date(2018,4,21).toString()}, // Tu | ||
{id: 3, date: new Date(2018,4,22).toString()}, // We | ||
{id: 4, date: new Date(2018,4,23).toString()} // Thurs | ||
], | ||
{id: 4, date: new Date(2018,4,24).toString()}); //F | ||
QUnit.equal(index, 4, "added at the end") | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
369844
9961