Skip to content

Commit ad0d583

Browse files
committed
fix the helpers.index
1 parent 7ef8dbe commit ad0d583

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

src/helpers.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,37 @@ var helpers = {
6060
} else if (compare(props, items[items.length - 1]) === 1) {
6161
return items.length;
6262
}
63+
6364
var low = 0,
64-
high = items.length;
65+
high = items.length,
66+
range = [];
6567

6668
// From lodash lodash 4.6.1 <https://lodash.com/>
6769
// Copyright 2012-2016 The Dojo Foundation <http://dojofoundation.org/>
6870
while (low < high) {
6971
var mid = (low + high) >>> 1,
7072
item = items[mid],
7173
computed = compare(props, item);
72-
if (computed === -1) {
74+
75+
if (computed === 0) {
76+
range.push(item);
77+
low++;
78+
} else if (computed === -1) {
7379
high = mid;
7480
} else {
7581
low = mid + 1;
7682
}
7783
}
84+
if (range.length > 0) {
85+
for (var i = 0; i < range.length; i++) {
86+
var itemInRange = range[i],
87+
id = canReflect.getSchema(itemInRange).identity;
88+
if (canReflect.hasOwnKey(props, id) && props[id] === itemInRange[id]) {
89+
high = items.indexOf(itemInRange);
90+
break;
91+
}
92+
}
93+
}
7894
return high;
7995
// bisect by calling sortFunc
8096
},

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

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -359,19 +359,49 @@ QUnit.test("index uses can-reflect", function(){
359359

360360

361361
QUnit.test("index should not sort unchanged items #33", function(assert) {
362+
canReflect.assignSymbols({},{
363+
"can.getSchema": function() {
364+
return {
365+
type: "map",
366+
identity: ["id"],
367+
keys: {
368+
id: Number,
369+
name: String
370+
}
371+
};
372+
}
373+
});
374+
362375
var items = [
363-
{id: 1, name: "Item 1"},
376+
{id: 1, name: "Item 0"},
364377
{id: 2, name: "Item 1"},
365378
{id: 3, name: "Item 1"},
366379
{id: 4, name: "Item 1"},
367-
{id: 5, name: "Item 1"}
380+
{id: 5, name: "Item 2"}
368381
];
369382

383+
canReflect.eachIndex(items, function(item, i) {
384+
canReflect.assignSymbols(item, {
385+
"can.getSchema": function() {
386+
return {
387+
type: "map",
388+
identity: ["id"],
389+
keys: {
390+
id: Number,
391+
name: String
392+
}
393+
};
394+
}
395+
});
396+
});
397+
398+
399+
370400
var query = new BasicQuery({
371401
sort: "name"
372402
});
373403

374404
var res = query.index({id:4, name: "Item 1"}, items);
375405

376-
QUnit.equal(res, 4);
406+
QUnit.equal(res, 3);
377407
});

0 commit comments

Comments
 (0)