Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 142dfcf

Browse filesBrowse files
committed
fromJSON takes a converter function
1 parent d12e46d commit 142dfcf
Copy full SHA for 142dfcf

File tree

Expand file treeCollapse file tree

5 files changed

+135
-14
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+135
-14
lines changed

‎__tests__/Conversion.ts

Copy file name to clipboardExpand all lines: __tests__/Conversion.ts
+35-2Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
jest.autoMockOff();
33
import Immutable = require('../dist/Immutable');
44
import Map = Immutable.Map;
5+
import OrderedMap = Immutable.OrderedMap;
56
import Vector = Immutable.Vector;
67

78
declare function expect(val: any): ExpectWithIs;
@@ -22,6 +23,7 @@ describe('Conversion', () => {
2223
});
2324
});
2425

26+
// Note: order of keys based on Map's hashing order
2527
var js = {
2628
deepList: [
2729
{
@@ -38,6 +40,7 @@ describe('Conversion', () => {
3840
a: "A",
3941
b: "B"
4042
},
43+
point: {x: 10, y: 20},
4144
string: "Hello",
4245
list: [1, 2, 3]
4346
};
@@ -58,6 +61,28 @@ describe('Conversion', () => {
5861
a: "A",
5962
b: "B"
6063
}),
64+
point: Map({x: 10, y: 20}),
65+
string: "Hello",
66+
list: Vector(1, 2, 3)
67+
});
68+
69+
var immutableOrderedData = OrderedMap({
70+
deepList: Vector(
71+
OrderedMap({
72+
position: "first"
73+
}),
74+
OrderedMap({
75+
position: "second"
76+
}),
77+
OrderedMap({
78+
position: "third"
79+
})
80+
),
81+
deepMap: OrderedMap({
82+
a: "A",
83+
b: "B"
84+
}),
85+
point: OrderedMap({x: 10, y: 20}),
6186
string: "Hello",
6287
list: Vector(1, 2, 3)
6388
});
@@ -66,9 +91,17 @@ describe('Conversion', () => {
6691
expect(Immutable.fromJSON(js)).is(immutableData);
6792
});
6893

94+
it('Converts deep JSON with custom conversion', () => {
95+
var seq = Immutable.fromJSON(js, function (key, sequence) {
96+
return Array.isArray(this[key]) ? sequence.toVector() : sequence.toOrderedMap();
97+
});
98+
expect(seq).is(immutableOrderedData);
99+
});
100+
69101
it('Converts deep sequences to JSON', () => {
70-
expect(immutableData.toJSON()).not.is(js); // raw JS is not immutable.
71-
expect(immutableData.toJSON()).toEqual(js); // but should be deep equal.
102+
var json = immutableData.toJSON();
103+
expect(json).not.is(js); // raw JS is not immutable.
104+
expect(json).toEqual(js); // but should be deep equal.
72105
});
73106

74107
it('JSON.stringify() works equivalently on immutable sequences', () => {

‎dist/Immutable.d.ts

Copy file name to clipboardExpand all lines: dist/Immutable.d.ts
+29-1Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,36 @@ export declare function is(first: any, second: any): boolean;
2929
/**
3030
* `Immutable.fromJSON()` deeply converts plain JS objects and arrays to
3131
* Immutable sequences.
32+
*
33+
* If a `converter` is optionally provided, it will be called with every
34+
* sequence (beginning with the most nested sequences and proceeding to the
35+
* original sequence itself), along with the key refering to this Sequence
36+
* and the parent JSON object provided as `this`. For the top level, object,
37+
* the key will be "". This `converter` is expected to return a new Sequence,
38+
* allowing for custom convertions from JSON.
39+
*
40+
* This example converts JSON to Vector and OrderedMap:
41+
*
42+
* Immutable.fromJSON({a: {b: [10, 20, 30]}, c: 40}, function (value, key) {
43+
* var isIndexed = value instanceof IndexedSequence;
44+
* console.log(isIndexed, key, this);
45+
* return isIndexed ? value.toVector() : value.toOrderedMap();
46+
* });
47+
*
48+
* // true, "b", {b: [10, 20, 30]}
49+
* // false, "a", {a: {b: [10, 20, 30]}, c: 40}
50+
* // false, "", {"": {a: {b: [10, 20, 30]}, c: 40}}
51+
*
52+
* If `converter` is not provided, the default behavior will convert Arrays into
53+
* Vectors and Objects into Maps.
54+
*
55+
* Note: `converter` acts similarly to [`reviver`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter)
56+
* in `JSON.parse`.
3257
*/
33-
export declare function fromJSON(json: any): any;
58+
export declare function fromJSON(
59+
json: any,
60+
converter?: (k: any, v: Sequence<any, any>) => any
61+
): any;
3462

3563

3664

‎dist/Immutable.js

Copy file name to clipboardExpand all lines: dist/Immutable.js
+21-5Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,32 @@ function is(first, second) {
2525
return false;
2626
}
2727

28-
function fromJSON(json) {
29-
if (Array.isArray(json)) {
30-
return Sequence(json).map(fromJSON).toVector();
28+
function fromJSON(json, converter) {
29+
if (converter) {
30+
var parentJSON = {'': json};
31+
return fromJSONWith(converter, json, '', parentJSON);
3132
}
32-
if (typeof json === 'object') {
33-
return Sequence(json).map(fromJSON).toMap();
33+
return fromJSONDefault(json);
34+
}
35+
36+
function fromJSONDefault(json) {
37+
if (json) {
38+
if (Array.isArray(json)) {
39+
return Sequence(json).map(fromJSONDefault).toVector();
40+
}
41+
if (json.constructor === Object) {
42+
return Sequence(json).map(fromJSONDefault).toMap();
43+
}
3444
}
3545
return json;
3646
}
3747

48+
function fromJSONWith(converter, json, key, parentJSON) {
49+
if (json && (Array.isArray(json) || json.constructor === Object)) {
50+
return converter.call(parentJSON, key, Sequence(json).map(function(v, k) {return fromJSONWith(converter, v, k, json);}));
51+
}
52+
return json;
53+
}
3854

3955
exports.is = is;
4056
exports.fromJSON = fromJSON;

‎src/Immutable.js

Copy file name to clipboardExpand all lines: src/Immutable.js
+21-5Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,32 @@ function is(first, second) {
2525
return false;
2626
}
2727

28-
function fromJSON(json) {
29-
if (Array.isArray(json)) {
30-
return Sequence(json).map(fromJSON).toVector();
28+
function fromJSON(json, converter) {
29+
if (converter) {
30+
var parentJSON = {'': json};
31+
return fromJSONWith(converter, json, '', parentJSON);
3132
}
32-
if (typeof json === 'object') {
33-
return Sequence(json).map(fromJSON).toMap();
33+
return fromJSONDefault(json);
34+
}
35+
36+
function fromJSONDefault(json) {
37+
if (json) {
38+
if (Array.isArray(json)) {
39+
return Sequence(json).map(fromJSONDefault).toVector();
40+
}
41+
if (json.constructor === Object) {
42+
return Sequence(json).map(fromJSONDefault).toMap();
43+
}
3444
}
3545
return json;
3646
}
3747

48+
function fromJSONWith(converter, json, key, parentJSON) {
49+
if (json && (Array.isArray(json) || json.constructor === Object)) {
50+
return converter.call(parentJSON, key, Sequence(json).map((v, k) => fromJSONWith(converter, v, k, json)));
51+
}
52+
return json;
53+
}
3854

3955
exports.is = is;
4056
exports.fromJSON = fromJSON;

‎type-definitions/Immutable.d.ts

Copy file name to clipboardExpand all lines: type-definitions/Immutable.d.ts
+29-1Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,36 @@ export declare function is(first: any, second: any): boolean;
2929
/**
3030
* `Immutable.fromJSON()` deeply converts plain JS objects and arrays to
3131
* Immutable sequences.
32+
*
33+
* If a `converter` is optionally provided, it will be called with every
34+
* sequence (beginning with the most nested sequences and proceeding to the
35+
* original sequence itself), along with the key refering to this Sequence
36+
* and the parent JSON object provided as `this`. For the top level, object,
37+
* the key will be "". This `converter` is expected to return a new Sequence,
38+
* allowing for custom convertions from JSON.
39+
*
40+
* This example converts JSON to Vector and OrderedMap:
41+
*
42+
* Immutable.fromJSON({a: {b: [10, 20, 30]}, c: 40}, function (value, key) {
43+
* var isIndexed = value instanceof IndexedSequence;
44+
* console.log(isIndexed, key, this);
45+
* return isIndexed ? value.toVector() : value.toOrderedMap();
46+
* });
47+
*
48+
* // true, "b", {b: [10, 20, 30]}
49+
* // false, "a", {a: {b: [10, 20, 30]}, c: 40}
50+
* // false, "", {"": {a: {b: [10, 20, 30]}, c: 40}}
51+
*
52+
* If `converter` is not provided, the default behavior will convert Arrays into
53+
* Vectors and Objects into Maps.
54+
*
55+
* Note: `converter` acts similarly to [`reviver`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse#Example.3A_Using_the_reviver_parameter)
56+
* in `JSON.parse`.
3257
*/
33-
export declare function fromJSON(json: any): any;
58+
export declare function fromJSON(
59+
json: any,
60+
converter?: (k: any, v: Sequence<any, any>) => any
61+
): any;
3462

3563

3664

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.