Skip to content

Instantly share code, notes, and snippets.

@michsch
Created June 11, 2018 12:37
Show Gist options
  • Save michsch/3f6803ef405c06312282f65f7561c2fa to your computer and use it in GitHub Desktop.
Save michsch/3f6803ef405c06312282f65f7561c2fa to your computer and use it in GitHub Desktop.
deepImmutable
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