Created
June 11, 2018 12:37
-
-
Save michsch/3f6803ef405c06312282f65f7561c2fa to your computer and use it in GitHub Desktop.
deepImmutable
This file contains hidden or 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
import {List, Map} from 'immutable' | |
/** | |
* Creates a deep immutable map and/or list. | |
* | |
* @public | |
* @param {boolean|number|string} data | |
* @returns {boolean|number|string|Immutable.List|Immutable.Map} | |
*/ | |
const deepImmutable = (data) => { | |
let newData | |
if (Array.isArray(data)) { | |
newData = data.map(value => { | |
return deepImmutable(value) | |
}) | |
return new List(newData) | |
} else if (typeof data === 'object') { | |
newData = {} | |
Object.keys(data).forEach(key => { | |
newData[key] = deepImmutable(data[key]) | |
}) | |
return new Map(newData) | |
} else { | |
return data | |
} | |
} | |
export default deepImmutable |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment