Skip to content

fromJS with SortedMap #10

Description

@sutbult

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));
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions