From 408ddc506c3afd618859998c5a892e7917836882 Mon Sep 17 00:00:00 2001 From: Justin Meyer Date: Thu, 29 Nov 2018 21:07:26 -0600 Subject: [PATCH] fixes #31 by using sets --- src/types/basic-query.js | 60 ++++++++++++++++++++++++++- test/special-comparison-logic-test.js | 40 ++++++++++++++++++ 2 files changed, 98 insertions(+), 2 deletions(-) diff --git a/src/types/basic-query.js b/src/types/basic-query.js index ec05fe4..d45cc32 100644 --- a/src/types/basic-query.js +++ b/src/types/basic-query.js @@ -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 @@ -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]); */ } }; }); diff --git a/test/special-comparison-logic-test.js b/test/special-comparison-logic-test.js index 7b15709..0a2715c 100644 --- a/test/special-comparison-logic-test.js +++ b/test/special-comparison-logic-test.js @@ -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); + +});