8000 Fix: toJS called on entrySeq converts deeply. (#1085) · githubsheng/immutable-js@2bff0cf · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 2bff0cf

Browse files
authored
Fix: toJS called on entrySeq converts deeply. (immutable-js#1085)
As reported in immutable-js#803, entrySeq().toJS() does not convert deeply through the arrays. This fix ensures the key and value in the entry tuple are also converted. Fixes immutable-js#803 Closes immutable-js#804 Closes immutable-js#1057
1 parent 23a68d1 commit 2bff0cf

File tree

5 files changed

+73
-46
lines changed

5 files changed

+73
-46
lines changed

__tests__/Conversion.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,4 +184,10 @@ describe('Conversion', () => {
184184
}).not.toThrow();
185185
});
186186

187+
it('Converts an immutable value of an entry correctly', () => {
188+
var js = [{"key": "a"}];
189+
var result = fromJS(js).entrySeq().toJS();
190+
expect(result).toEqual([[0, {"key": "a"}]]);
191+
});
192+
187193
});

dist/immutable.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2913,10 +2913,6 @@
29132913
this.size = entries.size;
29142914
}
29152915

2916-
FromEntriesSequence.prototype.entrySeq = function() {
2917-
return this._iter.toSeq();
2918-
};
2919-
29202916
FromEntriesSequence.prototype.__iterate = function(fn, reverse) {var this$0 = this;
29212917
return this._iter.__iterate(function(entry ) {
29222918
// Check if entry exists first so array access doesn't throw for holes
@@ -4271,15 +4267,11 @@
42714267
},
42724268

42734269
toJS: function() {
4274-
return this.toSeq().map(
4275-
function(value ) {return value && typeof value.toJS === 'function' ? value.toJS() : value}
4276-
).__toJS();
4270+
return this.toSeq().map(toJS).__toJS();
42774271
},
42784272

42794273
toJSON: function() {
4280-
return this.toSeq().map(
4281-
function(value ) {return value && typeof value.toJSON === 'function' ? value.toJSON() : value}
4282-
).__toJS();
4274+
return this.toSeq().map(toJSON).__toJS();
42834275
},
42844276

42854277
toKeyedSeq: function() {
@@ -4486,6 +4478,16 @@
44864478
}
44874479
var entriesSequence = iterable.toSeq().map(entryMapper).toIndexedSeq();
44884480
entriesSequence.fromEntrySeq = function() {return iterable.toSeq()};
4481+
4482+
// Entries are plain Array, which do not define toJS/toJSON, so it must
4483+
// manually converts keys and values before conversion.
4484+
entriesSequence.toJS = function () {
4485+
return this.map(function(entry ) {return [toJS(entry[0]), toJS(entry[1])]}).__toJS();
4486+
};
4487+
entriesSequence.toJSON = function () {
4488+
return this.map(function(entry ) {return [toJSON(entry[0]), toJSON(entry[1])]}).__toJS();
4489+
};
4490+
44894491
return entriesSequence;
44904492
},
44914493

@@ -4899,6 +4901,14 @@
48994901
return [k, v];
49004902
}
49014903

4904+
function toJS(value) {
4905+
return value && typeof value.toJS === 'function' ? value.toJS() : value;
4906+
}
4907+
4908+
function toJSON(value) {
4909+
return value && typeof value.toJSON === 'function' ? value.toJSON() : value;
4910+
}
4911+
49024912
function not(predicate) {
49034913
return function() {
49044914
return !predicate.apply(this, arguments);

0 commit comments

Comments
 (0)
0