Created
July 21, 2015 16:11
-
-
Save jonathantneal/e27c16ba19a3941ac95b to your computer and use it in GitHub Desktop.
PostCSS raw clone method
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
| function clone(node, deep, keepParent) { | |
| if (typeof node !== 'object') return node; | |
| var cloned = new node.constructor(); | |
| Object.keys(node).forEach(function (key) { | |
| var value = node[key]; | |
| if (keepParent && key === 'parent' || key === 'source') cloned[key] = value; | |
| else if (value instanceof Array) cloned[key] = deep ? value.map(function (child) { | |
| return clone(child, true, true); | |
| }) : []; | |
| else cloned[key] = clone(value, deep); | |
| }); | |
| return cloned; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment