Skip to content

Instantly share code, notes, and snippets.

@themgoncalves
Last active May 31, 2021 16:06
Show Gist options
  • Save themgoncalves/22f0a15e6833cad518c634138870d635 to your computer and use it in GitHub Desktop.
Save themgoncalves/22f0a15e6833cad518c634138870d635 to your computer and use it in GitHub Desktop.
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