8000 Generalize `is` and make Cursor construction generic · designtestcode/immutable-js@2ced269 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2ced269

Browse files
committed
Generalize is and make Cursor construction generic
1 parent 7fbd8fb commit 2ced269

File tree

6 files changed

+80
-69
lines changed

6 files changed

+80
-69
lines changed

dist/Immutable.js

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,18 @@ $traceurRuntime.createClass = createClass;
4141
$traceurRuntime.superCall = superCall;
4242
$traceurRuntime.defaultSuperCall = defaultSuperCall;
4343
"use strict";
44+
function is(first, second) {
45+
if (first === second) {
46+
return first !== 0 || second !== 0 || 1 / first === 1 / second;
47+
}
48+
if (first !== first) {
49+
return second !== second;
50+
}
51+
if (first && typeof first.equals === 'function') {
52+
return first.equals(second);
53+
}
54+
return false;
55+
}
4456
var DELETE = 'delete';
4557
var SHIFT = 5;
4658
var SIZE = 1 << SHIFT;
@@ -1514,14 +1526,16 @@ function interposeFactory(sequence, separator) {
15141526
};
15151527
return interposedSequence;
15161528
}
1517-
var Cursor = function Cursor(rootData, keyPath, onChange, value) {
1518-
value = value ? value : rootData.getIn(keyPath);
1519-
this.length = value instanceof Sequence ? value.length : null;
1529+
var Cursor = function Cursor(rootData, keyPath, onChange, length) {
1530+
this.length = length;
15201531
this._rootData = rootData;
15211532
this._keyPath = keyPath;
15221533
this._onChange = onChange;
15231534
};
15241535
($traceurRuntime.createClass)(Cursor, {
1536+
equals: function(second) {
1537+
return is(this.deref(), second && (typeof second.deref === 'function' ? second.deref() : second));
1538+
},
15251539
deref: function(notSetValue) {
15261540
return this._rootData.getIn(this._keyPath, notSetValue);
15271541
},
@@ -1588,35 +1602,24 @@ var Cursor = function Cursor(rootData, keyPath, onChange, value) {
15881602
}, {}, Sequence);
15891603
Cursor.prototype[DELETE] = Cursor.prototype.remove;
15901604
Cursor.prototype.getIn = Cursor.prototype.get;
1605+
function makeCursor(rootData, keyPath, onChange, value) {
1606+
if (arguments.length < 4) {
1607+
value = rootData.getIn(keyPath);
1608+
}
1609+
var length = value instanceof Sequence ? value.length : null;
1610+
return new Cursor(rootData, keyPath, onChange, length);
1611+
}
15911612
function wrappedValue(cursor, key, value) {
15921613
return value instanceof Sequence ? subCursor(cursor, key, value) : value;
15931614
}
15941615
function subCursor(cursor, key, value) {
1595-
return new Cursor(cursor._rootData, cursor._keyPath.concat(key), cursor._onChange, value);
1616+
return makeCursor(cursor._rootData, cursor._keyPath.concat(key), cursor._onChange, value);
15961617
}
15971618
function updateCursor(cursor, changeFn, changeKey) {
15981619
var newRootData = cursor._rootData.updateIn(cursor._keyPath, changeKey ? Map.empty() : undefined, changeFn);
15991620
var keyPath = cursor._keyPath || [];
16001621
cursor._onChange && cursor._onChange.call(undefined, newRootData, cursor._rootData, changeKey ? keyPath.concat(changeKey) : keyPath);
1601-
return new Cursor(newRootData, cursor._keyPath, cursor._onChange);
1602-
}
1603-
function is(first, second) {
1604-
if (first instanceof Cursor) {
1605-
first = first.deref();
1606-
}
1607-
if (second instanceof Cursor) {
1608-
second = second.deref();
1609-
}
1610-
if (first === second) {
1611-
return first !== 0 || second !== 0 || 1 / first === 1 / second;
1612-
}
1613-
if (first !== first) {
1614-
return second !== second;
1615-
}
1616-
if (first instanceof Sequence) {
1617-
return first.equals(second);
1618-
}
1619-
return false;
1622+
return makeCursor(newRootData, cursor._keyPath, cursor._onChange);
16201623
}
16211624
var Map = function Map(sequence) {
16221625
var map = $Map.empty();
@@ -1686,7 +1689,7 @@ var $Map = Map;
16861689
} else if (!Array.isArray(keyPath)) {
16871690
keyPath = [keyPath];
16881691
}
1689-
return new Cursor(this, keyPath, onChange);
1692+
return makeCursor(this, keyPath, onChange);
16901693
},
16911694
withMutations: function(fn) {
16921695
var mutable = this.asMutable();

0 commit comments

Comments
 (0)
0