aboutsummaryrefslogtreecommitdiff
path: root/node_modules/espurify/lib/clone-ast.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2017-05-28 00:38:50 +0200
committerFlorian Dold <florian.dold@gmail.com>2017-05-28 00:40:43 +0200
commit7fff4499fd915bcea3fa93b1aa8b35f4fe7a6027 (patch)
tree6de9a1aebd150a23b7f8c273ec657a5d0a18fe3e /node_modules/espurify/lib/clone-ast.js
parent963b7a41feb29cc4be090a2446bdfe0c1f1bcd81 (diff)
add linting (and some initial fixes)
Diffstat (limited to 'node_modules/espurify/lib/clone-ast.js')
-rw-r--r--node_modules/espurify/lib/clone-ast.js69
1 files changed, 69 insertions, 0 deletions
diff --git a/node_modules/espurify/lib/clone-ast.js b/node_modules/espurify/lib/clone-ast.js
new file mode 100644
index 000000000..f748b885d
--- /dev/null
+++ b/node_modules/espurify/lib/clone-ast.js
@@ -0,0 +1,69 @@
+'use strict';
+
+var isArray = require('core-js/library/fn/array/is-array');
+var objectKeys = require('core-js/library/fn/object/keys');
+var indexOf = require('core-js/library/fn/array/index-of');
+var reduce = require('core-js/library/fn/array/reduce');
+
+module.exports = function cloneWithWhitelist (astWhiteList) {
+ var whitelist = reduce(objectKeys(astWhiteList), function (props, key) {
+ var propNames = astWhiteList[key];
+ var prepend = (indexOf(propNames, 'type') === -1) ? ['type'] : [];
+ props[key] = prepend.concat(propNames);
+ return props;
+ }, {});
+
+ function cloneNodeOrObject (obj) {
+ var props = obj.type ? whitelist[obj.type] : null;
+ if (props) {
+ return cloneNode(obj, props);
+ } else {
+ return cloneObject(obj);
+ }
+ }
+
+ function cloneArray (ary) {
+ var i = ary.length, clone = [];
+ while (i--) {
+ clone[i] = cloneOf(ary[i]);
+ }
+ return clone;
+ }
+
+ function cloneNode (node, props) {
+ var i, len, key, clone = {};
+ for (i = 0, len = props.length; i < len; i += 1) {
+ key = props[i];
+ if (node.hasOwnProperty(key)) {
+ clone[key] = cloneOf(node[key]);
+ }
+ }
+ return clone;
+ }
+
+ function cloneObject (obj) {
+ var props = objectKeys(obj);
+ var i, len, key, clone = {};
+ for (i = 0, len = props.length; i < len; i += 1) {
+ key = props[i];
+ clone[key] = cloneOf(obj[key]);
+ }
+ return clone;
+ }
+
+ function cloneOf (val) {
+ if (typeof val === 'object' && val !== null) {
+ if (val instanceof RegExp) {
+ return new RegExp(val);
+ } else if (isArray(val)) {
+ return cloneArray(val);
+ } else {
+ return cloneNodeOrObject(val);
+ }
+ } else {
+ return val;
+ }
+ }
+
+ return cloneNodeOrObject;
+};