Last active
August 29, 2015 14:14
-
-
Save tolmasky/8a91af5ac1e0a949b423 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
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
What is
R
? I’m gettingundefined
.Update: answering to my own question, it looks like Ramba.