Last active
May 31, 2021 16:06
-
-
Save themgoncalves/22f0a15e6833cad518c634138870d635 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
import typeOf from './type-of'; | |
/** | |
* Shallow copy | |
* @description Create a copy of a original collection with same structure. | |
* @param value | |
*/ | |
const shallowCopy = (value) => { | |
let copy; | |
if (typeOf(value) === 'array') { | |
copy = [...value]; | |
} else if (typeOf(value) === 'object') { | |
copy = { ...value }; | |
} else if (typeOf(value) === 'date') { | |
copy = new Date(value); | |
} else if (typeOf(value) === 'map') { | |
copy = new Map(value); | |
} else if (typeOf(value) === 'set') { | |
copy = new Set(value); | |
} else { | |
copy = value; | |
} | |
return copy; | |
}; | |
export default shallowCopy; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment