Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Created July 21, 2015 16:11
Show Gist options
  • Select an option

  • Save jonathantneal/e27c16ba19a3941ac95b to your computer and use it in GitHub Desktop.

Select an option

Save jonathantneal/e27c16ba19a3941ac95b to your computer and use it in GitHub Desktop.
PostCSS raw clone method
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