Created
August 3, 2014 11:17
-
-
Save dkraczkowski/0d16b91602f33b9eed7f to your computer and use it in GitHub Desktop.
javascript copy object
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
| var util = require('util'); | |
| var isLiteral = require('./isLiteralObject'); | |
| function copyPrimitive(object) { | |
| var copy; | |
| if (typeof object !== 'object') { | |
| return object; | |
| } | |
| if (isLiteral(object)) { | |
| copy = {}; | |
| for (var property in object) { | |
| var result = copyPrimitive(object[property]); | |
| if (result !== undefined) { | |
| copy[property] = result; | |
| } | |
| } | |
| return copy; | |
| } | |
| if (util.isArray(object)) { | |
| copy = []; | |
| for (var i in object) { | |
| var result = copyPrimitive(object[i]); | |
| if (result !== undefined) { | |
| copy[i] = result; | |
| } | |
| } | |
| return copy; | |
| } | |
| if (util.isDate(object)) { | |
| return new Date(object.getTime()); | |
| } | |
| if (util.isRegExp(object)) { | |
| return new RegExp(object.source); | |
| } | |
| if (object === null) { | |
| return null; | |
| } | |
| } | |
| module.exports = copyPrimitive; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment