Skip to content

Commit

Permalink
Relax coupling to global shims.
Browse files Browse the repository at this point in the history
  • Loading branch information
kriskowal committed Sep 11, 2014
1 parent 5574d0b commit 7c674d4
Show file tree
Hide file tree
Showing 37 changed files with 843 additions and 534 deletions.
17 changes: 9 additions & 8 deletions deque.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"use strict";

require("./shim-object");
var GenericCollection = require("./generic-collection");
var GenericOrder = require("./generic-order");
var GenericOrder = require("./generic-order");
var ObservableRange = require("./observable-range");
var ObservableObject = require("./observable-object");
var Iterator = require("./iterator");
var addEach = require("./operators/add-each");
var equalsOperator = require("./operators/equals");

// by Petka Antonov
// https://github.com/petkaantonov/deque/blob/master/js/deque.js
Expand All @@ -27,10 +28,10 @@ function Deque(values, capacity) {
this.addEach(values);
}

Object.addEach(Deque.prototype, GenericCollection.prototype);
Object.addEach(Deque.prototype, GenericOrder.prototype);
Object.addEach(Deque.prototype, ObservableRange.prototype);
Object.addEach(Deque.prototype, ObservableObject.prototype);
addEach(Deque.prototype, GenericCollection.prototype);
addEach(Deque.prototype, GenericOrder.prototype);
addEach(Deque.prototype, ObservableRange.prototype);
addEach(Deque.prototype, ObservableObject.prototype);

Deque.prototype.maxCapacity = (1 << 30) | 0;
Deque.prototype.minCapacity = 16;
Expand Down Expand Up @@ -350,7 +351,7 @@ Deque.prototype.lastIndexOf = function (value, index) {
}

Deque.prototype.findValue = function (value, equals, index) {
equals = equals || Object.equals;
equals = equals || equalsOperator;
// Default start index at beginning
if (index == null) {
index = 0;
Expand All @@ -371,7 +372,7 @@ Deque.prototype.findValue = function (value, equals, index) {
};

Deque.prototype.findLastValue = function (value, equals, index) {
equals = equals || Object.equals;
equals = equals || equalsOperator;
// Default start position at the end
if (index == null) {
index = this.length - 1;
Expand All @@ -392,7 +393,7 @@ Deque.prototype.findLastValue = function (value, equals, index) {
};

Deque.prototype.has = function (value, equals) {
equals = equals || Object.equals;
equals = equals || equalsOperator;
// Left to right walk
var mask = this.capacity - 1;
for (var index = 0; index < this.length; index++) {
Expand Down
8 changes: 4 additions & 4 deletions dict.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
"use strict";

var Shim = require("./shim");
var GenericCollection = require("./generic-collection");
var GenericMap = require("./generic-map");
var ObservableObject = require("./observable-object");
var Iterator = require("./iterator");
var addEach = require("./operators/add-each");

// Burgled from https://github.com/domenic/dict

Expand All @@ -30,9 +30,9 @@ function unmangle(mangled) {
return mangled.slice(1);
}

Object.addEach(Dict.prototype, GenericCollection.prototype);
Object.addEach(Dict.prototype, GenericMap.prototype);
Object.addEach(Dict.prototype, ObservableObject.prototype);
addEach(Dict.prototype, GenericCollection.prototype);
addEach(Dict.prototype, GenericMap.prototype);
addEach(Dict.prototype, ObservableObject.prototype);

Dict.prototype.isDict = true;

Expand Down
14 changes: 8 additions & 6 deletions fast-map.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
"use strict";

var Shim = require("./shim");
var Set = require("./fast-set");
var GenericCollection = require("./generic-collection");
var GenericMap = require("./generic-map");
var ObservableObject = require("./observable-object");
var equalsOperator = require("./operators/equals");
var hashOperator = require("./operators/hash");
var addEach = require("./operators/add-each");

module.exports = FastMap;

function FastMap(values, equals, hash, getDefault) {
if (!(this instanceof FastMap)) {
return new FastMap(values, equals, hash, getDefault);
}
equals = equals || Object.equals;
hash = hash || Object.hash;
equals = equals || equalsOperator;
hash = hash || hashOperator;
getDefault = getDefault || this.getDefault;
this.contentEquals = equals;
this.contentHash = hash;
Expand All @@ -33,9 +35,9 @@ function FastMap(values, equals, hash, getDefault) {

FastMap.FastMap = FastMap; // hack so require("fast-map").FastMap will work in MontageJS

Object.addEach(FastMap.prototype, GenericCollection.prototype);
Object.addEach(FastMap.prototype, GenericMap.prototype);
Object.addEach(FastMap.prototype, ObservableObject.prototype);
addEach(FastMap.prototype, GenericCollection.prototype);
addEach(FastMap.prototype, GenericMap.prototype);
addEach(FastMap.prototype, ObservableObject.prototype);

FastMap.prototype.constructClone = function (values) {
return new this.constructor(
Expand Down
17 changes: 10 additions & 7 deletions fast-set.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"use strict";

var Shim = require("./shim");
var Dict = require("./dict");
var List = require("./list");
var GenericCollection = require("./generic-collection");
var GenericSet = require("./generic-set");
var TreeLog = require("./tree-log");
var ObservableObject = require("./observable-object");
var noop = require("./operators/noop");
var hashOperator = require("./operators/hash");
var equalsOperator = require("./operators/equals");
var addEach = require("./operators/add-each");

var object_has = Object.prototype.hasOwnProperty;

Expand All @@ -16,9 +19,9 @@ function FastSet(values, equals, hash, getDefault) {
if (!(this instanceof FastSet)) {
return new FastSet(values, equals, hash, getDefault);
}
equals = equals || Object.equals;
hash = hash || Object.hash;
getDefault = getDefault || Function.noop;
equals = equals || equalsOperator;
hash = hash || hashOperator;
getDefault = getDefault || noop;
this.contentEquals = equals;
this.contentHash = hash;
this.getDefault = getDefault;
Expand All @@ -29,9 +32,9 @@ function FastSet(values, equals, hash, getDefault) {

FastSet.FastSet = FastSet; // hack so require("fast-set").FastSet will work in MontageJS

Object.addEach(FastSet.prototype, GenericCollection.prototype);
Object.addEach(FastSet.prototype, GenericSet.prototype);
Object.addEach(FastSet.prototype, ObservableObject.prototype);
addEach(FastSet.prototype, GenericCollection.prototype);
addEach(FastSet.prototype, GenericSet.prototype);
addEach(FastSet.prototype, ObservableObject.prototype);

FastSet.prototype.Buckets = Dict;
FastSet.prototype.Bucket = List;
Expand Down
25 changes: 14 additions & 11 deletions generic-collection.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
"use strict";

var equalsOperator = require("./operators/equals");
var compareOperator = require("./operators/compare");
var cloneOperator = require("./operators/clone");

module.exports = GenericCollection;
function GenericCollection() {
throw new Error("Can't construct. GenericCollection is a mixin.");
Expand Down Expand Up @@ -64,7 +68,7 @@ GenericCollection.prototype.enumerate = function (start) {
};

GenericCollection.prototype.group = function (callback, thisp, equals) {
equals = equals || Object.equals;
equals = equals || equalsOperator;
var groups = [];
var keys = [];
this.forEach(function (value, key, object) {
Expand Down Expand Up @@ -137,7 +141,7 @@ GenericCollection.prototype.some = function (callback /*, thisp*/) {
};

GenericCollection.prototype.min = function (compare) {
compare = compare || this.contentCompare || Object.compare;
compare = compare || this.contentCompare || compareOperator;
var first = true;
return this.reduce(function (result, value) {
if (first) {
Expand All @@ -150,7 +154,7 @@ GenericCollection.prototype.min = function (compare) {
};

GenericCollection.prototype.max = function (compare) {
compare = compare || this.contentCompare || Object.compare;
compare = compare || this.contentCompare || compareOperator;
var first = true;
return this.reduce(function (result, value) {
if (first) {
Expand Down Expand Up @@ -210,11 +214,11 @@ GenericCollection.prototype.join = function (delimiter) {
};

GenericCollection.prototype.sorted = function (compare, by, order) {
compare = compare || this.contentCompare || Object.compare;
compare = compare || this.contentCompare || compareOperator;
// account for comparators generated by Function.by
if (compare.by) {
by = compare.by;
compare = compare.compare || this.contentCompare || Object.compare;
compare = compare.compare || this.contentCompare || compareOperator;
} else {
by = by || Function.identity;
}
Expand All @@ -238,17 +242,18 @@ GenericCollection.prototype.reversed = function () {
return this.constructClone(this).reverse();
};

GenericCollection.prototype.clone = function (depth, memo) {
GenericCollection.prototype.clone = function (depth, memo, clone) {
if (depth === undefined) {
depth = Infinity;
} else if (depth === 0) {
return this;
}
var clone = this.constructClone();
clone = clone || cloneOperator;
var collection = this.constructClone();
this.forEach(function (value, key) {
clone.add(Object.clone(value, depth - 1, memo), key);
collection.add(clone(value, depth - 1, memo), key);
}, this);
return clone;
return collection;
};

GenericCollection.prototype.only = function () {
Expand All @@ -257,5 +262,3 @@ GenericCollection.prototype.only = function () {
}
};

require("./shim-array");

14 changes: 8 additions & 6 deletions generic-map.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
"use strict";

var Object = require("./shim-object");
var ObservableMap = require("./observable-map");
var ObservableObject = require("./observable-object");
var Iterator = require("./iterator");
var equalsOperator = require("./operators/equals");
var compareOperator = require("./operators/compare");
var addEach = require("./operators/add-each");

module.exports = GenericMap;
function GenericMap() {
throw new Error("Can't construct. GenericMap is a mixin.");
}

Object.addEach(GenericMap.prototype, ObservableMap.prototype);
Object.addEach(GenericMap.prototype, ObservableObject.prototype);
addEach(GenericMap.prototype, ObservableMap.prototype);
addEach(GenericMap.prototype, ObservableObject.prototype);

// all of these methods depend on the constructor providing a `store` set

Expand Down Expand Up @@ -160,7 +162,7 @@ GenericMap.prototype.entries = function () {
};

GenericMap.prototype.equals = function (that, equals) {
equals = equals || Object.equals;
equals = equals || equalsOperator;
if (this === that) {
return true;
} else if (that && typeof that.every === "function") {
Expand All @@ -184,11 +186,11 @@ function Item(key, value) {
}

Item.prototype.equals = function (that) {
return Object.equals(this.key, that.key) && Object.equals(this.value, that.value);
return equalsOperator(this.key, that.key) && equalsOperator(this.value, that.value);
};

Item.prototype.compare = function (that) {
return Object.compare(this.key, that.key);
return compareOperator(this.key, that.key);
};

function GenericMapIterator(map) {
Expand Down
7 changes: 4 additions & 3 deletions generic-order.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@

var Object = require("./shim-object");
var equalsOperator = require("./operators/equals");
var compareOperator = require("./operators/compare");

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;
equals = equals || this.contentEquals || equalsOperator;

if (this === that) {
return true;
Expand All @@ -26,7 +27,7 @@ GenericOrder.prototype.equals = function (that, equals) {
};

GenericOrder.prototype.compare = function (that, compare) {
compare = compare || this.contentCompare || Object.compare;
compare = compare || this.contentCompare || compareOperator;

if (this === that) {
return 0;
Expand Down
17 changes: 10 additions & 7 deletions heap.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
// http://eloquentjavascript.net/appendix2.html

require("./observable-array");
require("./shim");
var GenericCollection = require("./generic-collection");
var ObservableObject = require("./observable-object");
var ObservableRange = require("./observable-range");
var ObservableMap = require("./observable-map");
var equalsOperator = require("./operators/equals");
var compareOperator = require("./operators/compare");
var addEach = require("./operators/add-each");

// Max Heap by default. Comparison can be reversed to produce a Min Heap.

Expand All @@ -17,19 +19,19 @@ function Heap(values, equals, compare) {
if (!(this instanceof Heap)) {
return new Heap(values, equals, compare);
}
this.contentEquals = equals || Object.equals;
this.contentCompare = compare || Object.compare;
this.contentEquals = equals || equalsOperator;
this.contentCompare = compare || compareOperator;
this.content = [];
this.length = 0;
this.addEach(values);
}

Heap.Heap = Heap; // hack so require("heap").Heap will work in MontageJS

Object.addEach(Heap.prototype, GenericCollection.prototype);
Object.addEach(Heap.prototype, ObservableObject.prototype);
Object.addEach(Heap.prototype, ObservableRange.prototype);
Object.addEach(Heap.prototype, ObservableMap.prototype);
addEach(Heap.prototype, GenericCollection.prototype);
addEach(Heap.prototype, ObservableObject.prototype);
addEach(Heap.prototype, ObservableRange.prototype);
addEach(Heap.prototype, ObservableMap.prototype);

Heap.prototype.constructClone = function (values) {
return new this.constructor(
Expand Down Expand Up @@ -235,3 +237,4 @@ Heap.prototype.handleContentMapChange = function (plus, minus, key, type) {
Heap.prototype.handleContentMapWillChange = function (plus, minus, key, type) {
this.dispatchMapWillChange(type, key, plus, minus);
};

2 changes: 1 addition & 1 deletion iterator.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module.exports = Iterator;

var WeakMap = require("./weak-map");
var WeakMap = require("weak-map");
var GenericCollection = require("./generic-collection");

// upgrades an iterable to a Iterator
Expand Down
Loading

0 comments on commit 7c674d4

Please sign in to comment.