Skip to content

Commit 0f38095

Browse files
committed
Fix query sorting schema issue
1 parent 8e54e7c commit 0f38095

File tree

5 files changed

+59
-14
lines changed

5 files changed

+59
-14
lines changed

src/helpers-test.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,35 @@ QUnit.test(".getIndex should not sort unchanged items #33", function() {
8585
QUnit.equal(res3, 2);
8686
QUnit.equal(res4, 3);
8787
});
88+
89+
QUnit.test("Missed schema on helper.getIndex #45", function() {
90+
var items = [
91+
{id: 1, name: "Item 0"},
92+
{id: 2, name: "Item 1"},
93+
{id: 3, name: "Item 2"},
94+
{id: 4, name: "Item 3"},
95+
{id: 5, name: "Item 4"}
96+
];
97+
98+
canReflect.eachIndex(items, function(item) {
99+
canReflect.assignSymbols(item, {
100+
"can.getSchema": function() {
101+
return {
102+
type: "map",
103+
identity: ["id"],
104+
keys: {
105+
id: Number,
106+
name: String
107+
}
108+
};
109+
}
110+
});
111+
});
112+
113+
var compare = helpers.sorter("name", {});
114+
var schema = canReflect.getSchema(items[0]);
115+
116+
QUnit.equal(helpers.getIndex(compare,items, {id: 2, name: "Item 1"}, schema), 1);
117+
118+
119+
});

src/helpers.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,22 @@ var helpers = {
5555
// or in the left direction
5656
// otherwise return the last index
5757
// see getIdentityIndexByDirection
58-
getIdentityIndex: function(compare, items, props, startIndex) {
59-
var identity = canReflect.getIdentity(props),
58+
getIdentityIndex: function(compare, items, props, startIndex, schema) {
59+
var identity = canReflect.getIdentity(props, schema),
6060
starterItem = items[startIndex];
6161
// check if the middle has a match
6262
if (compare(props, starterItem) === 0) {
63-
if (identity === canReflect.getIdentity(starterItem)) {
63+
if (identity === canReflect.getIdentity(starterItem, schema)) {
6464
return startIndex;
6565
}
6666
}
6767

68-
var rightResult = this.getIdentityIndexByDirection(compare, items, props, startIndex+1, 1),
68+
var rightResult = this.getIdentityIndexByDirection(compare, items, props, startIndex+1, 1, schema),
6969
leftResult;
7070
if(rightResult.index) {
7171
return rightResult.index;
7272
} else {
73-
leftResult = this.getIdentityIndexByDirection(compare, items, props, startIndex-1, -1);
73+
leftResult = this.getIdentityIndexByDirection(compare, items, props, startIndex-1, -1, schema);
7474
}
7575
if(leftResult.index !== undefined) {
7676
return leftResult.index;
@@ -82,14 +82,14 @@ var helpers = {
8282
// for a given direction (right or left)
8383
// 1 for right
8484
// -1 for left
85-
getIdentityIndexByDirection: function(compare, items, props, startIndex, direction) {
85+
getIdentityIndexByDirection: function(compare, items, props, startIndex, direction, schema) {
8686
var currentIndex = startIndex;
87-
var identity = canReflect.getIdentity(props);
87+
var identity = canReflect.getIdentity(props, schema);
8888
while(currentIndex >= 0 && currentIndex < items.length) {
8989
var currentItem = items[currentIndex];
9090
var computed = compare(props, currentItem);
9191
if(computed === 0) {
92-
if( identity === canReflect.getIdentity(currentItem)) {
92+
if( identity === canReflect.getIdentity(currentItem, schema)) {
9393
return {index: currentIndex};
9494
}
9595
} else {
@@ -100,7 +100,7 @@ var helpers = {
100100
return {lastIndex: currentIndex - direction};
101101
},
102102
//
103-
getIndex: function(compare, items, props) {
103+
getIndex: function(compare, items, props, schema) {
104104
if (!items || !items.length) {
105105
return undefined;
106106
}
@@ -121,7 +121,7 @@ var helpers = {
121121
item = items[mid],
122122
computed = compare(props, item);
123123
if (computed === 0) {
124-
return this.getIdentityIndex(compare, items, props, mid);
124+
return this.getIdentityIndex(compare, items, props, mid, schema);
125125
} else if (computed === -1) {
126126
high = mid;
127127
} else {

src/serializers/basic-query.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ module.exports = function(schema) {
281281

282282

283283
// Makes a sort type that can make a compare function using the SetType
284-
var Sort = BasicQuery.makeSort(keys, hydrateAndValue);
284+
var Sort = BasicQuery.makeSort(schema, hydrateAndValue);
285285
var serializer = new Serializer(serializeMap);
286286
serializer.add(comparisonsConverter.serializer);
287287

src/types/basic-query-sorting-test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,3 +356,14 @@ QUnit.test("index uses can-reflect", function(){
356356
QUnit.deepEqual([obj1Read, obj2Read, itemKeyRead, itemOwnKeyRead],
357357
[true, true, true, true], "read everything");
358358
});
359+
360+
QUnit.test(".index should work with literal objects", function() {
361+
var query = new BasicQuery({
362+
sort: "name"
363+
});
364+
365+
var items = [{id: 1, name: "Item 0"}, {id: 2, name: "Item 1"}];
366+
var res = query.index({id: 1, name: "Item 1"}, items);
367+
368+
QUnit.equal(res, 1, "Item index at 1");
369+
});

src/types/basic-query.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ var RecordRange = makeRealNumberRangeInclusive(0, Infinity);
3737
// That compare function will read the right property and return `-1` or `1`
3838

3939
// WILL MAKE A TYPE FOR SORTING
40-
function makeSort(schemaKeys, hydrateAndValue) {
40+
function makeSort(schema, hydrateAndValue) {
41+
var schemaKeys = schema.keys;
4142
// Makes gt and lt functions that `helpers.sorter` can use
4243
// to make a `compare` function for `Array.sort(compare)`.`
4344
var sorters = {};
@@ -107,6 +108,7 @@ function makeSort(schemaKeys, hydrateAndValue) {
107108

108109
function Sort(key) {
109110
this.key = key;
111+
this.schema = schema;
110112
this.compare = helpers.sorter(key, sorters);
111113
}
112114

@@ -129,7 +131,7 @@ function makeSort(schemaKeys, hydrateAndValue) {
129131
return Sort;
130132
}
131133

132-
var DefaultSort = makeSort({});
134+
var DefaultSort = makeSort({ keys: {}, identity: ["id"] });
133135

134136

135137
// Define the BasicQuery type
@@ -234,7 +236,7 @@ canReflect.assignMap(BasicQuery.prototype, {
234236
return undefined;
235237
}
236238
// use the passed sort's compare function
237-
return helpers.getIndex(this.sort.compare, items, props);
239+
return helpers.getIndex(this.sort.compare, items, props, this.sort.schema);
238240
},
239241
isMember: function(props) {
240242
// Use the AND type for it's isMember method

0 commit comments

Comments
 (0)