Joshua Comeau
Joshua Comeau

Reputation: 2743

Distinguishing between ImmutableJS and native JS data structures?

I'm writing a React component that I intend to make public, and I'd like to have it play nice either with JS arrays/objects, or immutable.js equivalents (Map/List).

What's the best way to identify an Immutable.js Map or List? I don't want to simply use Array.isArray, because I do want to enforce that it is either an Array or a List, for example.

I could just check for some of the Immutable.js properties, like _origin or __ownerID, but I don't want to depend on internal APIs that are subject to change in minor versions.

Upvotes: 2

Views: 170

Answers (1)

hazardous
hazardous

Reputation: 10837

I would very much recommend the suggestions given by @robg and @joseph-the-dreamer. However, just for the sake of answering your exact need, for every Immutable.Type there is a Immutable.Type.isType() static function which you can use to determine if a given object is of that type.

E.g. Map docs -

var im = require("immutable");

if (im.Map.isMap(someObjectWhichMayBeMap)){
    ...
}

Upvotes: 1

Related Questions