Skip to content

Commit 408ddc5

Browse files
committed
fixes #31 by using sets
1 parent 80c7d68 commit 408ddc5

File tree

2 files changed

+98
-2
lines changed

2 files changed

+98
-2
lines changed

src/types/basic-query.js

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,26 @@ var KeysAnd = andOrNot.KeysAnd,
1616
// TYPES FOR PAGINATION
1717
var RecordRange = makeRealNumberRangeInclusive(0, Infinity);
1818

19+
20+
// ## makeSort
21+
// Takes:
22+
// - `schemaKeys` - a schema
23+
// - `hydrateAndValue` - Useful to create something like `new GreaterThan( new MaybeDate("10-20-82") )`
24+
//
25+
// Makes a `new Sort(key)` constructor function. This constructor function is used like:
26+
//
27+
// ```
28+
// new Sort("dueDate")
29+
// ```
30+
//
31+
// That constructor function has all the comparison methods (union, intersection, difference)
32+
// built to compare against the `key` value.
33+
//
34+
// Instances of `Sort` have a `compare` method that will
35+
// return a function that can be passed to `Array.prototype.sort`.
36+
//
37+
// That compare function will read the right property and return `-1` or `1`
38+
1939
// WILL MAKE A TYPE FOR SORTING
2040
function makeSort(schemaKeys, hydrateAndValue) {
2141
// Makes gt and lt functions that `helpers.sorter` can use
@@ -30,21 +50,57 @@ function makeSort(schemaKeys, hydrateAndValue) {
3050
if(valueA == null || valueB == null) {
3151
return helpers.typeCompare.$gt(valueA, valueB);
3252
}
53+
// The following can certainly be done faster
3354
var $gt = hydrateAndValue({
3455
$gt: valueB
3556
}, key, schemaProp,
3657
helpers.valueHydrator);
37-
return $gt[isMemberSymbol](valueA);
58+
59+
var $eq = hydrateAndValue({
60+
$eq: valueA
61+
}, key, schemaProp,
62+
helpers.valueHydrator);
63+
64+
return set.isEqual( set.union($gt, $eq), $gt );
65+
/*
66+
var hydratedIn = hydrateAndValue({
67+
$eq: valueA
68+
}, key, schemaProp,
69+
helpers.valueHydrator);
70+
return $gt[isMemberSymbol](hydratedIn.values[0]);*/
3871
},
3972
$lt: function(valueA, valueB) {
4073
if(valueA == null || valueB == null) {
4174
return helpers.typeCompare.$lt(valueA, valueB);
4275
}
76+
77+
4378
var $lt = hydrateAndValue({
4479
$lt: valueB
4580
}, key, schemaProp,
4681
helpers.valueHydrator);
47-
return $lt[isMemberSymbol](valueA);
82+
83+
var $eq = hydrateAndValue({
84+
$eq: valueA
85+
}, key, schemaProp,
86+
helpers.valueHydrator);
87+
88+
return set.isEqual( set.union($lt, $eq), $lt );
89+
/*
90+
// This doesn't work because it will try to create new SetType(new In([]))
91+
var hydratedValue = hydrateAndValue({
92+
$eq: valueA
93+
}, key, schemaProp,
94+
helpers.valueHydrator);
95+
return $lt[isMemberSymbol](hydratedValue);*/
96+
97+
/*
98+
// This doesn't work because of maybe types.
99+
var hydratedIn = hydrateAndValue({
100+
$eq: valueA
101+
}, key, schemaProp,
102+
helpers.valueHydrator);
103+
return $lt[isMemberSymbol](hydratedIn.values[0]); */
48104
}
49105
};
50106
});

test/special-comparison-logic-test.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,3 +263,43 @@ QUnit.test("value type", function(){
263263

264264
QUnit.equal(index, 4, "added at the end")
265265
});
266+
267+
QUnit.test("sort a type that is similar to the member values (#31)", function(){
268+
function StringIgnoreCaseSet(value){
269+
this.value = value;
270+
}
271+
StringIgnoreCaseSet.prototype.valueOf = function(){
272+
return this.value.toLowerCase();
273+
};
274+
canReflect.assignSymbols(StringIgnoreCaseSet.prototype,{
275+
"can.serialize": function(){
276+
return this.value;
277+
}
278+
});
279+
var StringIgnoreCase = canReflect.assignSymbols({},{
280+
"can.SetType": StringIgnoreCaseSet,
281+
"can.new": function(value){
282+
return value;
283+
}
284+
});
285+
286+
var queryLogic = new QueryLogic({
287+
keys: {
288+
name: StringIgnoreCase
289+
},
290+
identity: ["id"]
291+
});
292+
293+
var filter = queryLogic.filterMembers(
294+
{sort: "name"},
295+
[{id: 1, name: "grab coffee"},
296+
{id: 2, name: "finish these docs"},
297+
{id: 3, name: "Learn CanJS"}]
298+
);
299+
QUnit.deepEqual([
300+
{id: 2, name: "finish these docs"},
301+
{id: 1, name: "grab coffee"},
302+
{id: 3, name: "Learn CanJS"}
303+
], filter);
304+
305+
});

0 commit comments

Comments
 (0)