Skip to content

Commit

Permalink
fixes #31 by using sets
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbmeyer committed Nov 30, 2018
1 parent 80c7d68 commit 408ddc5
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 2 deletions.
60 changes: 58 additions & 2 deletions src/types/basic-query.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,26 @@ var KeysAnd = andOrNot.KeysAnd,
// TYPES FOR PAGINATION
var RecordRange = makeRealNumberRangeInclusive(0, Infinity);


// ## makeSort
// Takes:
// - `schemaKeys` - a schema
// - `hydrateAndValue` - Useful to create something like `new GreaterThan( new MaybeDate("10-20-82") )`
//
// Makes a `new Sort(key)` constructor function. This constructor function is used like:
//
// ```
// new Sort("dueDate")
// ```
//
// That constructor function has all the comparison methods (union, intersection, difference)
// built to compare against the `key` value.
//
// Instances of `Sort` have a `compare` method that will
// return a function that can be passed to `Array.prototype.sort`.
//
// That compare function will read the right property and return `-1` or `1`

// WILL MAKE A TYPE FOR SORTING
function makeSort(schemaKeys, hydrateAndValue) {
// Makes gt and lt functions that `helpers.sorter` can use
Expand All @@ -30,21 +50,57 @@ function makeSort(schemaKeys, hydrateAndValue) {
if(valueA == null || valueB == null) {
return helpers.typeCompare.$gt(valueA, valueB);
}
// The following can certainly be done faster
var $gt = hydrateAndValue({
$gt: valueB
}, key, schemaProp,
helpers.valueHydrator);
return $gt[isMemberSymbol](valueA);

var $eq = hydrateAndValue({
$eq: valueA
}, key, schemaProp,
helpers.valueHydrator);

return set.isEqual( set.union($gt, $eq), $gt );
/*
var hydratedIn = hydrateAndValue({
$eq: valueA
}, key, schemaProp,
helpers.valueHydrator);
return $gt[isMemberSymbol](hydratedIn.values[0]);*/
},
$lt: function(valueA, valueB) {
if(valueA == null || valueB == null) {
return helpers.typeCompare.$lt(valueA, valueB);
}


var $lt = hydrateAndValue({
$lt: valueB
}, key, schemaProp,
helpers.valueHydrator);
return $lt[isMemberSymbol](valueA);

var $eq = hydrateAndValue({
$eq: valueA
}, key, schemaProp,
helpers.valueHydrator);

return set.isEqual( set.union($lt, $eq), $lt );
/*
// This doesn't work because it will try to create new SetType(new In([]))
var hydratedValue = hydrateAndValue({
$eq: valueA
}, key, schemaProp,
helpers.valueHydrator);
return $lt[isMemberSymbol](hydratedValue);*/

/*
// This doesn't work because of maybe types.
var hydratedIn = hydrateAndValue({
$eq: valueA
}, key, schemaProp,
helpers.valueHydrator);
return $lt[isMemberSymbol](hydratedIn.values[0]); */
}
};
});
Expand Down
40 changes: 40 additions & 0 deletions test/special-comparison-logic-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,43 @@ QUnit.test("value type", function(){

QUnit.equal(index, 4, "added at the end")
});

QUnit.test("sort a type that is similar to the member values (#31)", function(){
function StringIgnoreCaseSet(value){
this.value = value;
}
StringIgnoreCaseSet.prototype.valueOf = function(){
return this.value.toLowerCase();
};
canReflect.assignSymbols(StringIgnoreCaseSet.prototype,{
"can.serialize": function(){
return this.value;
}
});
var StringIgnoreCase = canReflect.assignSymbols({},{
"can.SetType": StringIgnoreCaseSet,
"can.new": function(value){
return value;
}
});

var queryLogic = new QueryLogic({
keys: {
name: StringIgnoreCase
},
identity: ["id"]
});

var filter = queryLogic.filterMembers(
{sort: "name"},
[{id: 1, name: "grab coffee"},
{id: 2, name: "finish these docs"},
{id: 3, name: "Learn CanJS"}]
);
QUnit.deepEqual([
{id: 2, name: "finish these docs"},
{id: 1, name: "grab coffee"},
{id: 3, name: "Learn CanJS"}
], filter);

});

0 comments on commit 408ddc5

Please sign in to comment.