immutable-sorted
immutable-sorted copied to clipboard
fromJS with SortedMap
trafficstars
I would like to request a function that works like fromJS but insert SortedMap instances instead of Map when objects are encountered.
An example is to execute this code and get the described result.
console.log(fromJSsorted(fromJS({
z: {
c: 1,
b: 2,
a: 3,
},
y: 5,
x: 6,
})).toJS());
// Would print { x: 6, y: 5, z: { a: 3, b: 2, c: 1 } }
I have written this code that can perform this feature as far as is needed in the project I am currently working on. You can have it for reference.
import {
Map,
List,
fromJS,
} from "immutable"
import {
SortedMap,
} from "immutable-sorted"
function fromJSsorted(object) {
function sort(obj) {
if(obj instanceof List) {
return obj.map(sort);
}
else if(obj instanceof Map) {
return SortedMap(obj).map(sort);
}
else {
return obj;
}
}
return sort(fromJS(object));
}
Hi, thank you for your suggestion, would you mind to submit a PR?