Skip to content

Instantly share code, notes, and snippets.

@tolmasky
Last active August 29, 2015 14:14
Show Gist options
  • Save tolmasky/8a91af5ac1e0a949b423 to your computer and use it in GitHub Desktop.
Save tolmasky/8a91af5ac1e0a949b423 to your computer and use it in GitHub Desktop.
function fromJS(anObject)
{
var objectsToReferences = new Map();
return fromJS(anObject);
function fromJS(anObject)
{
if (objectsToReferences.has(anObject))
return objectsToReferences.get(anObject);
if (isPlainObj(anObject) || Array.isArray(anObject))
{
if (Array.isArray(anObject))
{
result = I.List().withMutations(function(aList)
{
objectsToReferences.set(anObject, aList);
R.forEach(function(aKey)
{
if (!isNaN(aKey) && parseInt(aKey) > 0)
aList.set(aKey, fromJS(anObject[aKey]));
}, Object.keys(anObject));
});
}
else
{
result = I.Map().withMutations(function(aMap)
{
objectsToReferences.set(anObject, aMap);
R.forEach(function(aKey)
{
aMap.set(aKey, fromJS(anObject[aKey]));
}, Object.keys(anObject));
});
}
return result;
}
return anObject;
}
function isPlainObj(value)
{
return value && (value.constructor === Object || value.constructor === undefined);
}
}
@NV
Copy link

NV commented Feb 23, 2015

What is R? I’m getting undefined.

Update: answering to my own question, it looks like Ramba.

@NV
Copy link

NV commented Feb 23, 2015

https://gist.github.com/tolmasky/8a91af5ac1e0a949b423#file-gistfile1-js-L16
Missing var for result.

Otherwise, works as expected.

var obj = {}
obj.self = obj
var iObj = fromJS(obj)
iObj === iObj.get('self') // true

Immutable.fromJS(obj) // RangeError: Maximum call stack size exceeded

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