Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

react-tree-walker

Package Overview
Dependencies
Maintainers
1
Versions
19
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

react-tree-walker - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0-alpha.1

165

commonjs/index.js

@@ -7,15 +7,19 @@ 'use strict';

exports.isPromise = undefined;
exports.default = reactTreeWalker;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
// Extracted from: https://github.com/apollostack/react-apollo/blob/master/src/server.ts
var _react = require('react');
// eslint-disable-next-line import/no-extraneous-dependencies
var _pMapSeries = require('p-map-series');
var _pMapSeries2 = _interopRequireDefault(_pMapSeries);
exports.default = reactTreeWalker;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _react = require('react');
// Inspired by the awesome work done by the Apollo team.
// See https://github.com/apollostack/react-apollo/blob/master/src/server.ts
// This version has been adapted to be promise based.
// eslint-disable-next-line import/no-extraneous-dependencies
var isPromise = exports.isPromise = function isPromise(x) {
return (typeof x === 'undefined' ? 'undefined' : _typeof(x)) === 'object' && typeof x.then === 'function';
return x != null && typeof x.then === 'function';
};

@@ -26,86 +30,95 @@

// or recurse into its child elements
function reactTreeWalker(element, visitor) {
var context = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
function reactTreeWalker(element, visitor, context) {
return new Promise(function (resolve) {
var handleVisitResult = function handleVisitResult(getChild, visitorResult, childContext, isChildren) {
var tryContinue = function tryContinue() {
// Returned true, indicating a desire to continue traversal immediately.
var child = getChild();
// Is this element a Component?
if (typeof element.type === 'function') {
var Component = element.type;
var props = Object.assign({}, Component.defaultProps, element.props);
var childContext = context;
var child = void 0;
if (child == null) {
resolve();
} else if (isChildren) {
var mapper = function mapper(aChild) {
return aChild ? reactTreeWalker(aChild, visitor, childContext) : undefined;
};
(0, _pMapSeries2.default)(_react.Children.map(child, function (cur) {
return cur;
}), mapper).then(resolve);
} else {
reactTreeWalker(child, visitor, childContext).then(resolve);
}
};
// Is this a class component? (http://bit.ly/2j9Ifk3)
var isReactClassComponent = Component.prototype && (Component.prototype.isReactComponent || Component.prototype.isPureReactComponent);
if (visitorResult === false) {
// Visitor returned false, indicating a desire to not traverse.
resolve();
} else if (isPromise(visitorResult)) {
visitorResult.then(tryContinue);
} else {
tryContinue();
}
};
if (isReactClassComponent) {
var _ret = function () {
var instance = new Component(props, context);
// In case the user doesn't pass these to super in the constructor
instance.props = instance.props || props;
instance.context = instance.context || context;
// Is this element a Component?
if (typeof element.type === 'function') {
(function () {
var Component = element.type;
var props = Object.assign({}, Component.defaultProps, element.props);
var childContext = context;
// Make the setState synchronous.
instance.setState = function (newState) {
instance.state = Object.assign({}, instance.state, newState);
};
// Is this a class component? (http://bit.ly/2j9Ifk3)
var isReactClassComponent = Component.prototype && (Component.prototype.isReactComponent || Component.prototype.isPureReactComponent);
// Call componentWillMount if it exists.
if (instance.componentWillMount) {
instance.componentWillMount();
}
if (isReactClassComponent) {
(function () {
var instanceFactory = function instanceFactory() {
var instance = new Component(props, context);
// In case the user doesn't pass these to super in the constructor
instance.props = instance.props || props;
instance.context = instance.context || context;
// Ensure the child context is initialised if it is available. We will
// need to pass it down the tree.
if (instance.getChildContext) {
childContext = Object.assign({}, context, instance.getChildContext());
}
// Make the setState synchronous.
instance.setState = function (newState) {
instance.state = Object.assign({}, instance.state, newState);
};
// Hit up our visitor!
if (visitor(element, instance, context) === false) {
// Visitor returned false, indicating a desire to not traverse.
return {
v: void 0
};
}
// Call componentWillMount if it exists.
if (instance.componentWillMount) {
instance.componentWillMount();
}
// Get the render output as the child.
child = instance.render();
}();
// Ensure the child context is initialised if it is available. We will
// need to pass it down the tree.
if (instance.getChildContext) {
childContext = Object.assign({}, context, instance.getChildContext());
}
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
} else {
// Stateless Functional Component
return instance;
};
// Hit up our visitor!
if (visitor(element, null, context) === false) {
// Visitor returned false, indicating a desire to not traverse.
return;
}
var instance = instanceFactory();
// Get the output for the function, as the child.
child = Component(props, context);
}
// Hit up our visitor!
handleVisitResult(function () {
return instance.render();
}, visitor(element, instance, context), childContext);
})();
} else {
// Stateless Functional Component
// Only continue walking if a child exists.
if (child) {
reactTreeWalker(child, visitor, childContext);
}
} else {
// This must be a basic element, such as a string or dom node.
// Hit up our visitor!
handleVisitResult(function () {
return Component(props, context);
}, visitor(element, null, context), context);
}
})();
} else {
// This must be a basic element, such as a string or dom node.
// Hit up our visitor!
if (visitor(element, null, context) === false) {
// Visitor returned false, indicating a desire to not traverse.
return;
// Hit up our visitor!
handleVisitResult(function () {
return element.props && element.props.children ? element.props.children : undefined;
}, visitor(element, null, context), context, true);
}
// If the element has children then we will walk them.
if (element.props && element.props.children) {
_react.Children.forEach(element.props.children, function (child) {
if (child) {
reactTreeWalker(child, visitor, context);
}
});
}
}
});
}
{
"name": "react-tree-walker",
"version": "1.0.0",
"version": "2.0.0-alpha.1",
"description": "Walk a React element tree, executing a provided function against each node.",

@@ -29,7 +29,3 @@ "license": "MIT",

"check": "yarn run lint && yarn run test",
"clean": "rimraf ./commonjs && rimraf ./umd && rimraf ./coverage && rimraf ./flow-coverage && rimraf ./umd",
"example:web": "echo 'Make sure to `cd example/web && yarn install`' && cd example/web && yarn run start",
"flow": "babel-node ./tools/scripts/flow",
"flow:coverage": "flow-coverage-report -i 'src/**/*.js' -t html -t json -t text",
"flow:defs": "flow-typed install --overwrite",
"clean": "rimraf ./commonjs && rimraf ./umd && rimraf ./coverage && rimraf ./umd",
"lint": "eslint src",

@@ -46,30 +42,26 @@ "prepublish": "yarn run build",

"app-root-dir": "1.0.2",
"babel-cli": "6.18.0",
"babel-core": "6.21.0",
"babel-cli": "6.23.0",
"babel-core": "6.23.1",
"babel-eslint": "7.1.1",
"babel-jest": "18.0.0",
"babel-loader": "6.2.10",
"babel-polyfill": "6.20.0",
"babel-preset-env": "1.1.8",
"babel-preset-latest": "6.16.0",
"babel-preset-react": "6.16.0",
"babel-preset-stage-3": "6.17.0",
"babel-register": "6.18.0",
"babel-jest": "19.0.0",
"babel-loader": "6.4.0",
"babel-polyfill": "6.23.0",
"babel-preset-env": "1.2.1",
"babel-preset-latest": "6.22.0",
"babel-preset-react": "6.23.0",
"babel-preset-stage-3": "6.22.0",
"babel-register": "6.23.0",
"codecov": "1.0.1",
"cross-env": "3.1.4",
"enzyme": "2.7.0",
"enzyme-to-json": "1.4.5",
"eslint": "3.13.1",
"eslint-config-airbnb": "14.0.0",
"eslint-plugin-flowtype": "2.30.0",
"cross-env": "3.2.3",
"enzyme": "2.7.1",
"enzyme-to-json": "1.5.0",
"eslint": "3.17.1",
"eslint-config-airbnb": "14.1.0",
"eslint-plugin-import": "2.2.0",
"eslint-plugin-jsx-a11y": "3.0.2",
"eslint-plugin-react": "6.9.0",
"flow-bin": "0.37.4",
"flow-coverage-report": "0.2.0",
"flow-typed": "2.0.0",
"eslint-plugin-jsx-a11y": "4.0.0",
"eslint-plugin-react": "6.10.0",
"ghooks": "2.0.0",
"gzip-size": "3.0.0",
"in-publish": "2.0.0",
"jest": "18.1.0",
"jest": "19.0.2",
"pretty-bytes": "4.0.2",

@@ -80,8 +72,8 @@ "ramda": "0.23.0",

"react-dom": "15.4.2",
"readline-sync": "1.4.5",
"rimraf": "2.5.4",
"readline-sync": "1.4.6",
"rimraf": "2.6.1",
"sinon": "1.17.7",
"webpack": "2.2.0-rc.3",
"webpack-dev-middleware": "1.9.0",
"webpack-hot-middleware": "2.15.0"
"webpack": "2.2.1",
"webpack-dev-middleware": "1.10.1",
"webpack-hot-middleware": "2.17.1"
},

@@ -103,3 +95,6 @@ "config": {

]
},
"dependencies": {
"p-map-series": "^1.0.0"
}
}

@@ -82,3 +82,3 @@ # react-tree-walker 🌲

reactTreeWalker(tree, visitor);
reactTreeWalker(app, visitor);

@@ -85,0 +85,0 @@ console.log(values); // [1, 2, 4, 5, 3];

@@ -7,6 +7,6 @@ (function webpackUniversalModuleDefinition(root, factory) {

else if(typeof exports === 'object')
exports["react-tree-walker"] = factory(require("react"));
exports["ReactTreeWalker"] = factory(require("react"));
else
root["react-tree-walker"] = factory(root["React"]);
})(this, function(__WEBPACK_EXTERNAL_MODULE_0__) {
root["ReactTreeWalker"] = factory(root["React"]);
})(this, function(__WEBPACK_EXTERNAL_MODULE_1__) {
return /******/ (function(modules) { // webpackBootstrap

@@ -77,3 +77,3 @@ /******/ // The module cache

/******/ // Load entry module and return exports
/******/ return __webpack_require__(__webpack_require__.s = 1);
/******/ return __webpack_require__(__webpack_require__.s = 3);
/******/ })

@@ -83,13 +83,63 @@ /************************************************************************/

/* 0 */
/***/ function(module, exports) {
/***/ (function(module, exports, __webpack_require__) {
module.exports = __WEBPACK_EXTERNAL_MODULE_0__;
"use strict";
/***/ },
var pReduce = __webpack_require__(2);
module.exports = function (iterable, iterator) {
var ret = [];
return pReduce(iterable, function (a, b, i) {
return Promise.resolve(iterator(b, i)).then(function (val) {
ret.push(val);
});
}).then(function () {
return ret;
});
};
/***/ }),
/* 1 */
/***/ function(module, exports, __webpack_require__) {
/***/ (function(module, exports) {
module.exports = __WEBPACK_EXTERNAL_MODULE_1__;
/***/ }),
/* 2 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
module.exports = function (iterable, reducer, initVal) {
return new Promise(function (resolve, reject) {
var iterator = iterable[Symbol.iterator]();
var i = 0;
var next = function next(total) {
var el = iterator.next();
if (el.done) {
resolve(total);
return;
}
Promise.all([total, el.value]).then(function (value) {
next(reducer(value[0], value[1], i++));
}).catch(reject);
};
next(initVal);
});
};
/***/ }),
/* 3 */
/***/ (function(module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {

@@ -99,15 +149,19 @@ value: true

exports.isPromise = undefined;
exports.default = reactTreeWalker;
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
// Extracted from: https://github.com/apollostack/react-apollo/blob/master/src/server.ts
var _react = __webpack_require__(1);
// eslint-disable-next-line import/no-extraneous-dependencies
var _pMapSeries = __webpack_require__(0);
var _pMapSeries2 = _interopRequireDefault(_pMapSeries);
exports.default = reactTreeWalker;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
var _react = __webpack_require__(0);
// Inspired by the awesome work done by the Apollo team.
// See https://github.com/apollostack/react-apollo/blob/master/src/server.ts
// This version has been adapted to be promise based.
// eslint-disable-next-line import/no-extraneous-dependencies
var isPromise = exports.isPromise = function isPromise(x) {
return (typeof x === 'undefined' ? 'undefined' : _typeof(x)) === 'object' && typeof x.then === 'function';
return x != null && typeof x.then === 'function';
};

@@ -118,90 +172,99 @@

// or recurse into its child elements
function reactTreeWalker(element, visitor) {
var context = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
function reactTreeWalker(element, visitor, context) {
return new Promise(function (resolve) {
var handleVisitResult = function handleVisitResult(getChild, visitorResult, childContext, isChildren) {
var tryContinue = function tryContinue() {
// Returned true, indicating a desire to continue traversal immediately.
var child = getChild();
// Is this element a Component?
if (typeof element.type === 'function') {
var Component = element.type;
var props = Object.assign({}, Component.defaultProps, element.props);
var childContext = context;
var child = void 0;
if (child == null) {
resolve();
} else if (isChildren) {
var mapper = function mapper(aChild) {
return aChild ? reactTreeWalker(aChild, visitor, childContext) : undefined;
};
(0, _pMapSeries2.default)(_react.Children.map(child, function (cur) {
return cur;
}), mapper).then(resolve);
} else {
reactTreeWalker(child, visitor, childContext).then(resolve);
}
};
// Is this a class component? (http://bit.ly/2j9Ifk3)
var isReactClassComponent = Component.prototype && (Component.prototype.isReactComponent || Component.prototype.isPureReactComponent);
if (visitorResult === false) {
// Visitor returned false, indicating a desire to not traverse.
resolve();
} else if (isPromise(visitorResult)) {
visitorResult.then(tryContinue);
} else {
tryContinue();
}
};
if (isReactClassComponent) {
var _ret = function () {
var instance = new Component(props, context);
// In case the user doesn't pass these to super in the constructor
instance.props = instance.props || props;
instance.context = instance.context || context;
// Is this element a Component?
if (typeof element.type === 'function') {
(function () {
var Component = element.type;
var props = Object.assign({}, Component.defaultProps, element.props);
var childContext = context;
// Make the setState synchronous.
instance.setState = function (newState) {
instance.state = Object.assign({}, instance.state, newState);
};
// Is this a class component? (http://bit.ly/2j9Ifk3)
var isReactClassComponent = Component.prototype && (Component.prototype.isReactComponent || Component.prototype.isPureReactComponent);
// Call componentWillMount if it exists.
if (instance.componentWillMount) {
instance.componentWillMount();
}
if (isReactClassComponent) {
(function () {
var instanceFactory = function instanceFactory() {
var instance = new Component(props, context);
// In case the user doesn't pass these to super in the constructor
instance.props = instance.props || props;
instance.context = instance.context || context;
// Ensure the child context is initialised if it is available. We will
// need to pass it down the tree.
if (instance.getChildContext) {
childContext = Object.assign({}, context, instance.getChildContext());
}
// Make the setState synchronous.
instance.setState = function (newState) {
instance.state = Object.assign({}, instance.state, newState);
};
// Hit up our visitor!
if (visitor(element, instance, context) === false) {
// Visitor returned false, indicating a desire to not traverse.
return {
v: void 0
};
}
// Call componentWillMount if it exists.
if (instance.componentWillMount) {
instance.componentWillMount();
}
// Get the render output as the child.
child = instance.render();
}();
// Ensure the child context is initialised if it is available. We will
// need to pass it down the tree.
if (instance.getChildContext) {
childContext = Object.assign({}, context, instance.getChildContext());
}
if ((typeof _ret === 'undefined' ? 'undefined' : _typeof(_ret)) === "object") return _ret.v;
} else {
// Stateless Functional Component
return instance;
};
// Hit up our visitor!
if (visitor(element, null, context) === false) {
// Visitor returned false, indicating a desire to not traverse.
return;
}
var instance = instanceFactory();
// Get the output for the function, as the child.
child = Component(props, context);
}
// Hit up our visitor!
handleVisitResult(function () {
return instance.render();
}, visitor(element, instance, context), childContext);
})();
} else {
// Stateless Functional Component
// Only continue walking if a child exists.
if (child) {
reactTreeWalker(child, visitor, childContext);
}
} else {
// This must be a basic element, such as a string or dom node.
// Hit up our visitor!
handleVisitResult(function () {
return Component(props, context);
}, visitor(element, null, context), context);
}
})();
} else {
// This must be a basic element, such as a string or dom node.
// Hit up our visitor!
if (visitor(element, null, context) === false) {
// Visitor returned false, indicating a desire to not traverse.
return;
// Hit up our visitor!
handleVisitResult(function () {
return element.props && element.props.children ? element.props.children : undefined;
}, visitor(element, null, context), context, true);
}
// If the element has children then we will walk them.
if (element.props && element.props.children) {
_react.Children.forEach(element.props.children, function (child) {
if (child) {
reactTreeWalker(child, visitor, context);
}
});
}
}
});
}
/***/ }
/***/ })
/******/ ]);
});

@@ -1,1 +0,1 @@

!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):"object"==typeof exports?exports["react-tree-walker"]=t(require("react")):e["react-tree-walker"]=t(e.React)}(this,function(e){return function(e){function t(n){if(o[n])return o[n].exports;var r=o[n]={i:n,l:!1,exports:{}};return e[n].call(r.exports,r,r.exports,t),r.l=!0,r.exports}var o={};return t.m=e,t.c=o,t.i=function(e){return e},t.d=function(e,o,n){t.o(e,o)||Object.defineProperty(e,o,{configurable:!1,enumerable:!0,get:n})},t.n=function(e){var o=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(o,"a",o),o},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=1)}([function(t,o){t.exports=e},function(e,t,o){"use strict";function n(e,t){var o=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};if("function"==typeof e.type){var u=e.type,c=Object.assign({},u.defaultProps,e.props),f=o,p=void 0,s=u.prototype&&(u.prototype.isReactComponent||u.prototype.isPureReactComponent);if(s){var l=function(){var n=new u(c,o);return n.props=n.props||c,n.context=n.context||o,n.setState=function(e){n.state=Object.assign({},n.state,e)},n.componentWillMount&&n.componentWillMount(),n.getChildContext&&(f=Object.assign({},o,n.getChildContext())),t(e,n,o)===!1?{v:void 0}:void(p=n.render())}();if("object"===("undefined"==typeof l?"undefined":r(l)))return l.v}else{if(t(e,null,o)===!1)return;p=u(c,o)}p&&n(p,t,f)}else{if(t(e,null,o)===!1)return;e.props&&e.props.children&&i.Children.forEach(e.props.children,function(e){e&&n(e,t,o)})}}Object.defineProperty(t,"__esModule",{value:!0}),t.isPromise=void 0;var r="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(e){return typeof e}:function(e){return e&&"function"==typeof Symbol&&e.constructor===Symbol&&e!==Symbol.prototype?"symbol":typeof e};t.default=n;var i=o(0);t.isPromise=function(e){return"object"===("undefined"==typeof e?"undefined":r(e))&&"function"==typeof e.then}}])});
!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("react")):"function"==typeof define&&define.amd?define(["react"],e):"object"==typeof exports?exports.ReactTreeWalker=e(require("react")):t.ReactTreeWalker=e(t.React)}(this,function(t){return function(t){function e(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return t[r].call(o.exports,o,o.exports,e),o.l=!0,o.exports}var n={};return e.m=t,e.c=n,e.i=function(t){return t},e.d=function(t,n,r){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:r})},e.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return e.d(n,"a",n),n},e.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},e.p="",e(e.s=3)}([function(t,e,n){"use strict";var r=n(2);t.exports=function(t,e){var n=[];return r(t,function(t,r,o){return Promise.resolve(e(r,o)).then(function(t){n.push(t)})}).then(function(){return n})}},function(e,n){e.exports=t},function(t,e,n){"use strict";t.exports=function(t,e,n){return new Promise(function(r,o){var u=t[Symbol.iterator](),i=0,c=function t(n){var c=u.next();return c.done?void r(n):void Promise.all([n,c.value]).then(function(n){t(e(n[0],n[1],i++))}).catch(o)};c(n)})}},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{default:t}}function o(t,e,n){return new Promise(function(r){var i=function(t,n,i,s){var p=function(){var n=t();if(null==n)r();else if(s){var f=function(t){return t?o(t,e,i):void 0};(0,c.default)(u.Children.map(n,function(t){return t}),f).then(r)}else o(n,e,i).then(r)};n===!1?r():f(n)?n.then(p):p()};"function"==typeof t.type?!function(){var r=t.type,o=Object.assign({},r.defaultProps,t.props),u=n,c=r.prototype&&(r.prototype.isReactComponent||r.prototype.isPureReactComponent);c?!function(){var c=function(){var t=new r(o,n);return t.props=t.props||o,t.context=t.context||n,t.setState=function(e){t.state=Object.assign({},t.state,e)},t.componentWillMount&&t.componentWillMount(),t.getChildContext&&(u=Object.assign({},n,t.getChildContext())),t},f=c();i(function(){return f.render()},e(t,f,n),u)}():i(function(){return r(o,n)},e(t,null,n),n)}():i(function(){return t.props&&t.props.children?t.props.children:void 0},e(t,null,n),n,!0)})}Object.defineProperty(e,"__esModule",{value:!0}),e.isPromise=void 0,e.default=o;var u=n(1),i=n(0),c=r(i),f=e.isPromise=function(t){return null!=t&&"function"==typeof t.then}}])});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc