-
Notifications
You must be signed in to change notification settings - Fork 183
/
generic-order.js
58 lines (49 loc) · 1.32 KB
/
generic-order.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
var Object = require("./shim-object");
module.exports = GenericOrder;
function GenericOrder() {
throw new Error("Can't construct. GenericOrder is a mixin.");
}
GenericOrder.prototype.equals = function (that, equals) {
equals = equals || this.contentEquals || Object.equals;
if (this === that) {
return true;
}
if (!that) {
return false;
}
var self = this;
return (
this.length === that.length &&
this.zip(that).every(function (pair) {
return equals(pair[0], pair[1]);
})
);
};
GenericOrder.prototype.compare = function (that, compare) {
compare = compare || this.contentCompare || Object.compare;
if (this === that) {
return 0;
}
if (!that) {
return 1;
}
var length = Math.min(this.length, that.length);
var comparison = this.zip(that).reduce(function (comparison, pair, index) {
if (comparison === 0) {
if (index >= length) {
return comparison;
} else {
return compare(pair[0], pair[1]);
}
} else {
return comparison;
}
}, 0);
if (comparison === 0) {
return this.length - that.length;
}
return comparison;
};
GenericOrder.prototype.toJSON = function () {
return this.toArray();
};