react-tree-walker
Advanced tools
Comparing version 2.0.0-alpha.2 to 2.0.0-alpha.3
@@ -11,8 +11,29 @@ 'use strict'; | ||
var _pMapSeries = require('p-map-series'); | ||
// Lifted from https://github.com/sindresorhus/p-reduce | ||
// Thanks @sindresorhus! | ||
var pReduce = function pReduce(iterable, reducer, initVal) { | ||
return new Promise(function (resolve, reject) { | ||
var iterator = iterable[Symbol.iterator](); | ||
var i = 0; | ||
var _pMapSeries2 = _interopRequireDefault(_pMapSeries); | ||
var next = function next(total) { | ||
var el = iterator.next(); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
if (el.done) { | ||
resolve(total); | ||
return; | ||
} | ||
Promise.all([total, el.value]).then(function (value) { | ||
// eslint-disable-next-line no-plusplus | ||
next(reducer(value[0], value[1], i++)); | ||
}).catch(reject); | ||
}; | ||
next(initVal); | ||
}); | ||
}; | ||
// Lifted from https://github.com/sindresorhus/p-map-series | ||
// Thanks @sindresorhus! | ||
/* eslint-disable no-console */ | ||
@@ -25,2 +46,14 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
var pMapSeries = function pMapSeries(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; | ||
}); | ||
}; | ||
var isPromise = exports.isPromise = function isPromise(x) { | ||
@@ -55,3 +88,3 @@ return x != null && typeof x.then === 'function'; | ||
// pMapSeries allows us to do depth-first traversal. Thanks @sindresorhus! | ||
(0, _pMapSeries2.default)(_react.Children.map(child, function (cur) { | ||
pMapSeries(_react.Children.map(child, function (cur) { | ||
return cur; | ||
@@ -87,46 +120,42 @@ }), mapper).then(resolve); | ||
if (typeof element.type === 'function') { | ||
(function () { | ||
var Component = element.type; | ||
var props = Object.assign({}, Component.defaultProps, element.props); | ||
var Component = element.type; | ||
var props = Object.assign({}, Component.defaultProps, element.props); | ||
// Is this a class component? (http://bit.ly/2j9Ifk3) | ||
var isReactClassComponent = Component.prototype && (Component.prototype.isReactComponent || Component.prototype.isPureReactComponent); | ||
// Is this a class component? (http://bit.ly/2j9Ifk3) | ||
var isReactClassComponent = Component.prototype && (Component.prototype.isReactComponent || Component.prototype.isPureReactComponent); | ||
if (isReactClassComponent) { | ||
(function () { | ||
// React class component | ||
if (isReactClassComponent) { | ||
// React class component | ||
var instance = new Component(props, context); | ||
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; | ||
// In case the user doesn't pass these to super in the constructor | ||
instance.props = instance.props || props; | ||
instance.context = instance.context || context; | ||
// Make the setState synchronous. | ||
instance.setState = function (newState) { | ||
instance.state = Object.assign({}, instance.state, newState); | ||
}; | ||
// Make the setState synchronous. | ||
instance.setState = function (newState) { | ||
instance.state = Object.assign({}, instance.state, newState); | ||
}; | ||
doVisit(function () { | ||
// Call componentWillMount if it exists. | ||
if (instance.componentWillMount) { | ||
instance.componentWillMount(); | ||
} | ||
doVisit(function () { | ||
// Call componentWillMount if it exists. | ||
if (instance.componentWillMount) { | ||
instance.componentWillMount(); | ||
} | ||
return instance.render(); | ||
}, visitor(element, instance, context), function () { | ||
return ( | ||
// Ensure the child context is initialised if it is available. We will | ||
// need to pass it down the tree. | ||
instance.getChildContext ? Object.assign({}, context, instance.getChildContext()) : context | ||
); | ||
}); | ||
})(); | ||
} else { | ||
// Stateless Functional Component | ||
doVisit(function () { | ||
return Component(props, context); | ||
}, visitor(element, null, context), context); | ||
} | ||
})(); | ||
return instance.render(); | ||
}, visitor(element, instance, context), function () { | ||
return ( | ||
// Ensure the child context is initialised if it is available. We will | ||
// need to pass it down the tree. | ||
instance.getChildContext ? Object.assign({}, context, instance.getChildContext()) : context | ||
); | ||
}); | ||
} else { | ||
// Stateless Functional Component | ||
doVisit(function () { | ||
return Component(props, context); | ||
}, visitor(element, null, context), context); | ||
} | ||
} else { | ||
@@ -133,0 +162,0 @@ // This must be a basic element, such as a string or dom node. |
{ | ||
"name": "react-tree-walker", | ||
"version": "2.0.0-alpha.2", | ||
"version": "2.0.0-alpha.3", | ||
"description": "Walk a React element tree, executing a provided function against each node.", | ||
@@ -59,15 +59,15 @@ "license": "MIT", | ||
"app-root-dir": "1.0.2", | ||
"babel-cli": "6.23.0", | ||
"babel-core": "6.23.1", | ||
"babel-cli": "6.24.0", | ||
"babel-core": "6.24.0", | ||
"babel-eslint": "7.1.1", | ||
"babel-jest": "19.0.0", | ||
"babel-loader": "6.4.0", | ||
"babel-loader": "6.4.1", | ||
"babel-polyfill": "6.23.0", | ||
"babel-preset-env": "1.2.1", | ||
"babel-preset-latest": "6.22.0", | ||
"babel-preset-env": "1.2.2", | ||
"babel-preset-latest": "6.24.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.2.3", | ||
"babel-register": "6.24.0", | ||
"codecov": "2.0.2", | ||
"cross-env": "3.2.4", | ||
"enzyme": "2.7.1", | ||
@@ -86,3 +86,3 @@ "enzyme-to-json": "1.5.0", | ||
"prettier": "0.22.0", | ||
"prettier-eslint": "4.2.1", | ||
"prettier-eslint": "4.3.2", | ||
"prettier-eslint-cli": "3.1.2", | ||
@@ -96,3 +96,3 @@ "pretty-bytes": "4.0.2", | ||
"rimraf": "2.6.1", | ||
"sinon": "1.17.7", | ||
"sinon": "2.0.0", | ||
"webpack": "2.2.1", | ||
@@ -102,5 +102,3 @@ "webpack-dev-middleware": "1.10.1", | ||
}, | ||
"dependencies": { | ||
"p-map-series": "^1.0.0" | ||
} | ||
"dependencies": {} | ||
} |
@@ -18,4 +18,6 @@ # react-tree-walker 🌲 | ||
This is a extract of the implementation within the awesome [`react-apollo`](https://github.com/apollostack/react-apollo) project. I've come to find many use-cases for it in my own projects and want to avoid code duplication. | ||
Originally inspired/lifted from the awesome [`react-apollo`](https://github.com/apollostack/react-apollo) project. | ||
This modified version expands upon the design, making it `Promise` based, and allowing a visit by a visitor to return a `Promise`, which would subsequently delay the tree walking until the `Promise` is resolved. The tree is still walked in a depth-first fashion. | ||
With this you could, for example, perform pre-rendering parses on your React element tree to do things like data prefetching. 🤛 | ||
@@ -22,0 +24,0 @@ |
@@ -10,3 +10,3 @@ (function webpackUniversalModuleDefinition(root, factory) { | ||
root["ReactTreeWalker"] = factory(root["React"]); | ||
})(this, function(__WEBPACK_EXTERNAL_MODULE_1__) { | ||
})(this, function(__WEBPACK_EXTERNAL_MODULE_0__) { | ||
return /******/ (function(modules) { // webpackBootstrap | ||
@@ -77,3 +77,3 @@ /******/ // The module cache | ||
/******/ // Load entry module and return exports | ||
/******/ return __webpack_require__(__webpack_require__.s = 3); | ||
/******/ return __webpack_require__(__webpack_require__.s = 1); | ||
/******/ }) | ||
@@ -83,29 +83,8 @@ /************************************************************************/ | ||
/* 0 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
/***/ (function(module, exports) { | ||
"use strict"; | ||
module.exports = __WEBPACK_EXTERNAL_MODULE_0__; | ||
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) { | ||
module.exports = __WEBPACK_EXTERNAL_MODULE_1__; | ||
/***/ }), | ||
/* 2 */ | ||
/***/ (function(module, exports, __webpack_require__) { | ||
@@ -116,31 +95,2 @@ | ||
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", { | ||
@@ -152,10 +102,31 @@ value: true | ||
var _react = __webpack_require__(1); | ||
var _react = __webpack_require__(0); | ||
var _pMapSeries = __webpack_require__(0); | ||
// Lifted from https://github.com/sindresorhus/p-reduce | ||
// Thanks @sindresorhus! | ||
var pReduce = function pReduce(iterable, reducer, initVal) { | ||
return new Promise(function (resolve, reject) { | ||
var iterator = iterable[Symbol.iterator](); | ||
var i = 0; | ||
var _pMapSeries2 = _interopRequireDefault(_pMapSeries); | ||
var next = function next(total) { | ||
var el = iterator.next(); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
if (el.done) { | ||
resolve(total); | ||
return; | ||
} | ||
Promise.all([total, el.value]).then(function (value) { | ||
// eslint-disable-next-line no-plusplus | ||
next(reducer(value[0], value[1], i++)); | ||
}).catch(reject); | ||
}; | ||
next(initVal); | ||
}); | ||
}; | ||
// Lifted from https://github.com/sindresorhus/p-map-series | ||
// Thanks @sindresorhus! | ||
/* eslint-disable no-console */ | ||
@@ -168,2 +139,14 @@ | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
var pMapSeries = function pMapSeries(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; | ||
}); | ||
}; | ||
var isPromise = exports.isPromise = function isPromise(x) { | ||
@@ -198,3 +181,3 @@ return x != null && typeof x.then === 'function'; | ||
// pMapSeries allows us to do depth-first traversal. Thanks @sindresorhus! | ||
(0, _pMapSeries2.default)(_react.Children.map(child, function (cur) { | ||
pMapSeries(_react.Children.map(child, function (cur) { | ||
return cur; | ||
@@ -230,46 +213,42 @@ }), mapper).then(resolve); | ||
if (typeof element.type === 'function') { | ||
(function () { | ||
var Component = element.type; | ||
var props = Object.assign({}, Component.defaultProps, element.props); | ||
var Component = element.type; | ||
var props = Object.assign({}, Component.defaultProps, element.props); | ||
// Is this a class component? (http://bit.ly/2j9Ifk3) | ||
var isReactClassComponent = Component.prototype && (Component.prototype.isReactComponent || Component.prototype.isPureReactComponent); | ||
// Is this a class component? (http://bit.ly/2j9Ifk3) | ||
var isReactClassComponent = Component.prototype && (Component.prototype.isReactComponent || Component.prototype.isPureReactComponent); | ||
if (isReactClassComponent) { | ||
(function () { | ||
// React class component | ||
if (isReactClassComponent) { | ||
// React class component | ||
var instance = new Component(props, context); | ||
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; | ||
// In case the user doesn't pass these to super in the constructor | ||
instance.props = instance.props || props; | ||
instance.context = instance.context || context; | ||
// Make the setState synchronous. | ||
instance.setState = function (newState) { | ||
instance.state = Object.assign({}, instance.state, newState); | ||
}; | ||
// Make the setState synchronous. | ||
instance.setState = function (newState) { | ||
instance.state = Object.assign({}, instance.state, newState); | ||
}; | ||
doVisit(function () { | ||
// Call componentWillMount if it exists. | ||
if (instance.componentWillMount) { | ||
instance.componentWillMount(); | ||
} | ||
doVisit(function () { | ||
// Call componentWillMount if it exists. | ||
if (instance.componentWillMount) { | ||
instance.componentWillMount(); | ||
} | ||
return instance.render(); | ||
}, visitor(element, instance, context), function () { | ||
return ( | ||
// Ensure the child context is initialised if it is available. We will | ||
// need to pass it down the tree. | ||
instance.getChildContext ? Object.assign({}, context, instance.getChildContext()) : context | ||
); | ||
}); | ||
})(); | ||
} else { | ||
// Stateless Functional Component | ||
doVisit(function () { | ||
return Component(props, context); | ||
}, visitor(element, null, context), context); | ||
} | ||
})(); | ||
return instance.render(); | ||
}, visitor(element, instance, context), function () { | ||
return ( | ||
// Ensure the child context is initialised if it is available. We will | ||
// need to pass it down the tree. | ||
instance.getChildContext ? Object.assign({}, context, instance.getChildContext()) : context | ||
); | ||
}); | ||
} else { | ||
// Stateless Functional Component | ||
doVisit(function () { | ||
return Component(props, context); | ||
}, visitor(element, null, context), context); | ||
} | ||
} else { | ||
@@ -276,0 +255,0 @@ // This must be a basic element, such as a string or dom node. |
@@ -1,1 +0,1 @@ | ||
!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(o){if(n[o])return n[o].exports;var r=n[o]={i:o,l:!1,exports:{}};return t[o].call(r.exports,r,r.exports,e),r.l=!0,r.exports}var n={};return e.m=t,e.c=n,e.i=function(t){return t},e.d=function(t,n,o){e.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:o})},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 o=n(2);t.exports=function(t,e){var n=[];return o(t,function(t,o,r){return Promise.resolve(e(o,r)).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(o,r){var u=t[Symbol.iterator](),i=0,c=function t(n){var c=u.next();return c.done?void o(n):void Promise.all([n,c.value]).then(function(n){t(e(n[0],n[1],i++))}).catch(r)};c(n)})}},function(t,e,n){"use strict";function o(t){return t&&t.__esModule?t:{default:t}}function r(t,e,n){return new Promise(function(o){var i=function(t,n,i,s){var a=function(n){n||o();var f=t(),a="function"==typeof i?i():i;if(null==f)o();else if(s){var l=function(t){return t?r(t,e,a):void 0};(0,c.default)(u.Children.map(f,function(t){return t}),l).then(o)}else r(f,e,a).then(o)};n===!1?o():f(n)?n.then(a).catch(function(t){console.log("Error occurred in Promise based visitor result provided to react-tree-walker."),t&&(console.log(t),t.stack&&console.log(t.stack))}):a(!0)};"function"==typeof t.type?!function(){var o=t.type,r=Object.assign({},o.defaultProps,t.props),u=o.prototype&&(o.prototype.isReactComponent||o.prototype.isPureReactComponent);u?!function(){var u=new o(r,n);u.props=u.props||r,u.context=u.context||n,u.setState=function(t){u.state=Object.assign({},u.state,t)},i(function(){return u.componentWillMount&&u.componentWillMount(),u.render()},e(t,u,n),function(){return u.getChildContext?Object.assign({},n,u.getChildContext()):n})}():i(function(){return o(r,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=r;var u=n(1),i=n(0),c=o(i),f=e.isPromise=function(t){return null!=t&&"function"==typeof t.then}}])}); | ||
!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.ReactTreeWalker=t(require("react")):e.ReactTreeWalker=t(e.React)}(this,function(e){return function(e){function t(o){if(n[o])return n[o].exports;var r=n[o]={i:o,l:!1,exports:{}};return e[o].call(r.exports,r,r.exports,t),r.l=!0,r.exports}var n={};return t.m=e,t.c=n,t.i=function(e){return e},t.d=function(e,n,o){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:o})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=1)}([function(t,n){t.exports=e},function(e,t,n){"use strict";function o(e,t,n){return new Promise(function(i){var f=function(e,n,f,s){var l=function(n){n||i();var c=e(),l="function"==typeof f?f():f;if(null==c)i();else if(s){var a=function(e){return e?o(e,t,l):void 0};u(r.Children.map(c,function(e){return e}),a).then(i)}else o(c,t,l).then(i)};n===!1?i():c(n)?n.then(l).catch(function(e){console.log("Error occurred in Promise based visitor result provided to react-tree-walker."),e&&(console.log(e),e.stack&&console.log(e.stack))}):l(!0)};if("function"==typeof e.type){var s=e.type,l=Object.assign({},s.defaultProps,e.props),a=s.prototype&&(s.prototype.isReactComponent||s.prototype.isPureReactComponent);if(a){var p=new s(l,n);p.props=p.props||l,p.context=p.context||n,p.setState=function(e){p.state=Object.assign({},p.state,e)},f(function(){return p.componentWillMount&&p.componentWillMount(),p.render()},t(e,p,n),function(){return p.getChildContext?Object.assign({},n,p.getChildContext()):n})}else f(function(){return s(l,n)},t(e,null,n),n)}else f(function(){return e.props&&e.props.children?e.props.children:void 0},t(e,null,n),n,!0)})}Object.defineProperty(t,"__esModule",{value:!0}),t.isPromise=void 0,t.default=o;var r=n(0),i=function(e,t,n){return new Promise(function(o,r){var i=e[Symbol.iterator](),u=0,c=function e(n){var c=i.next();return c.done?void o(n):void Promise.all([n,c.value]).then(function(n){e(t(n[0],n[1],u++))}).catch(r)};c(n)})},u=function(e,t){var n=[];return i(e,function(e,o,r){return Promise.resolve(t(o,r)).then(function(e){n.push(e)})}).then(function(){return n})},c=t.isPromise=function(e){return null!=e&&"function"==typeof e.then}}])}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23743
1
351
92
- Removedp-map-series@^1.0.0
- Removedp-map-series@1.0.0(transitive)
- Removedp-reduce@1.0.0(transitive)