react-children-utilities
Advanced tools
Comparing version 1.0.1 to 1.0.2
@@ -1,3 +0,3 @@ | ||
'use strict';Object.defineProperty(exports,'__esModule',{value:true});var _extends=Object.assign||function(target){for(var i=1;i<arguments.length;i++){var source=arguments[i];for(var key in source){if(Object.prototype.hasOwnProperty.call(source,key)){target[key]=source[key]}}}return target};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};var _react=require('react');function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true})}else{obj[key]=value}return obj}function _toConsumableArray(arr){if(Array.isArray(arr)){for(var i=0,arr2=Array(arr.length);i<arr.length;i++){arr2[i]=arr[i]}return arr2}else{return Array.from(arr)}}var hasChildren=function hasChildren(child){return Boolean(child&&child.props&&child.props.children)};var hasComplexChildren=function hasComplexChildren(child){return hasChildren(child)&&_typeof(child.props.children)==='object'};exports.default=_extends({},_react.Children,{filter:function filter(children,filterFn){return _react.Children.toArray(children).filter(filterFn)},deepFilter:function deepFilter(children,deepFilterFn){var _this=this;return _react.Children.toArray(children).filter(deepFilterFn).map(function(child){if(hasComplexChildren(child)){return(0,_react.cloneElement)(child,_extends({},child.props,{children:_this.deepFilter(child.props.children,deepFilterFn)}))}return child})},groupByType:function groupByType(children,types,rest){return _react.Children.toArray(children).reduce(function(group,child){var isGrouped=types.includes(child.type);var addChild=isGrouped?child.props.children:child;var key=isGrouped?child.type:rest;return _extends({},group,_defineProperty({},key,[].concat(_toConsumableArray(group[key]||[]),[addChild])))},{})},deepMap:function deepMap(children,deepMapFn){var _this2=this;return _react.Children.map(children,function(child){if(hasComplexChildren(child)){return deepMapFn((0,_react.cloneElement)(child,_extends({},child.props,{children:_this2.deepMap(child.props.children,deepMapFn)})))}return deepMapFn(child)})},deepForEach:function deepForEach(children,deepForEachFn){var _this3=this;_react.Children.forEach(children,function(child){if(hasComplexChildren(child)){_this3.deepForEach(child.props.children,deepForEachFn)}deepForEachFn(child)})},deepFind:function deepFind(children,deepFindFn){var _this4=this;return _react.Children.toArray(children).find(function(child){if(hasComplexChildren(child)){return _this4.deepFind(child.props.children,deepFindFn)}return deepFindFn(child)})},onlyText:function onlyText(children){var _this5=this;return _react.Children.toArray(children).reduce(function(flattened,child){return[].concat(_toConsumableArray(flattened),[hasChildren(child)?_this5.onlyText(child.props.children):child])},[]).join('')}}); | ||
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.default=void 0;var _react=require("react");function _toConsumableArray(arr){return _arrayWithoutHoles(arr)||_iterableToArray(arr)||_nonIterableSpread()}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function _iterableToArray(iter){if(Symbol.iterator in Object(iter)||Object.prototype.toString.call(iter)==="[object Arguments]")return Array.from(iter)}function _arrayWithoutHoles(arr){if(Array.isArray(arr)){for(var i=0,arr2=new Array(arr.length);i<arr.length;i++){arr2[i]=arr[i]}return arr2}}function _objectSpread(target){for(var i=1;i<arguments.length;i++){var source=arguments[i]!=null?arguments[i]:{};var ownKeys=Object.keys(source);if(typeof Object.getOwnPropertySymbols==="function"){ownKeys=ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym){return Object.getOwnPropertyDescriptor(source,sym).enumerable}))}ownKeys.forEach(function(key){_defineProperty(target,key,source[key])})}return target}function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true})}else{obj[key]=value}return obj}function _typeof(obj){if(typeof Symbol==="function"&&typeof Symbol.iterator==="symbol"){_typeof=function _typeof(obj){return typeof obj}}else{_typeof=function _typeof(obj){return obj&&typeof Symbol==="function"&&obj.constructor===Symbol&&obj!==Symbol.prototype?"symbol":typeof obj}}return _typeof(obj)}var hasChildren=function hasChildren(child){return Boolean(child&&child.props&&child.props.children)};var hasComplexChildren=function hasComplexChildren(child){return hasChildren(child)&&_typeof(child.props.children)==="object"};var _default=_objectSpread({},_react.Children,{filter:function filter(children,filterFn){return _react.Children.toArray(children).filter(filterFn)},deepFilter:function deepFilter(children,deepFilterFn){var _this=this;return _react.Children.toArray(children).filter(deepFilterFn).map(function(child){if(hasComplexChildren(child)){return(0,_react.cloneElement)(child,_objectSpread({},child.props,{children:_this.deepFilter(child.props.children,deepFilterFn)}))}return child})},groupByType:function groupByType(children,types,rest){return _react.Children.toArray(children).reduce(function(group,child){var isGrouped=types.includes(child.type);var addChild=isGrouped?child.props.children:child;var key=isGrouped?child.type:rest;return _objectSpread({},group,_defineProperty({},key,_toConsumableArray(group[key]||[]).concat([addChild])))},{})},deepMap:function deepMap(children,deepMapFn){var _this2=this;return _react.Children.map(children,function(child){if(hasComplexChildren(child)){return deepMapFn((0,_react.cloneElement)(child,_objectSpread({},child.props,{children:_this2.deepMap(child.props.children,deepMapFn)})))}return deepMapFn(child)})},deepForEach:function deepForEach(children,deepForEachFn){var _this3=this;_react.Children.forEach(children,function(child){if(hasComplexChildren(child)){_this3.deepForEach(child.props.children,deepForEachFn)}deepForEachFn(child)})},deepFind:function deepFind(children,deepFindFn){var _this4=this;return _react.Children.toArray(children).find(function(child){if(hasComplexChildren(child)){return _this4.deepFind(child.props.children,deepFindFn)}return deepFindFn(child)})},onlyText:function onlyText(children){var _this5=this;return _react.Children.toArray(children).reduce(function(flattened,child){return _toConsumableArray(flattened).concat([hasChildren(child)?_this5.onlyText(child.props.children):child])},[]).join("")}});exports.default=_default; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "react-children-utilities", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Extended utils for React.Children opaque data structure", | ||
@@ -45,33 +45,32 @@ "keywords": [ | ||
"devDependencies": { | ||
"@commitlint/cli": "^6.2.0", | ||
"@commitlint/config-conventional": "^6.1.3", | ||
"babel-cli": "^6.26.0", | ||
"babel-core": "^6.26.3", | ||
"babel-eslint": "^8.2.3", | ||
"babel-jest": "^23.0.0", | ||
"babel-preset-env": "^1.7.0", | ||
"babel-preset-flow": "^6.23.0", | ||
"babel-preset-react": "^6.24.1", | ||
"babel-preset-stage-2": "^6.24.1", | ||
"babel-register": "^6.26.0", | ||
"codecov": "^3.0.2", | ||
"enzyme": "^3.3.0", | ||
"enzyme-adapter-react-16": "^1.1.1", | ||
"eslint": "^4.19.1", | ||
"eslint-config-airbnb": "^16.1.0", | ||
"eslint-plugin-flowtype": "^2.47.1", | ||
"eslint-plugin-import": "^2.12.0", | ||
"eslint-plugin-jsx-a11y": "^6.0.3", | ||
"eslint-plugin-react": "^7.8.2", | ||
"flow-bin": "^0.73.0", | ||
"husky": "^1.0.0-rc.7", | ||
"jest": "^23.0.0", | ||
"jest-enzyme": "^6.0.1", | ||
"jest-junit": "^4.0.0", | ||
"@babel/cli": "^7.1.0", | ||
"@babel/core": "^7.1.0", | ||
"@babel/preset-env": "^7.1.0", | ||
"@babel/preset-flow": "^7.0.0", | ||
"@babel/preset-react": "^7.0.0", | ||
"@commitlint/cli": "^7.1.2", | ||
"@commitlint/config-conventional": "^7.1.2", | ||
"babel-core": "^7.0.0-0", | ||
"babel-eslint": "^10.0.1", | ||
"babel-jest": "^23.6.0", | ||
"codecov": "^3.1.0", | ||
"enzyme": "^3.6.0", | ||
"enzyme-adapter-react-16": "^1.5.0", | ||
"eslint": "^5.6.0", | ||
"eslint-config-airbnb": "^17.1.0", | ||
"eslint-plugin-flowtype": "^2.50.3", | ||
"eslint-plugin-import": "^2.14.0", | ||
"eslint-plugin-jsx-a11y": "^6.1.1", | ||
"eslint-plugin-react": "^7.11.1", | ||
"flow-bin": "^0.81.0", | ||
"husky": "^1.0.1", | ||
"jest": "^23.6.0", | ||
"jest-enzyme": "^6.1.2", | ||
"jest-junit": "^5.1.0", | ||
"prepare-commit-msg-angular": "^0.2.0", | ||
"prop-types": "^15.6.1", | ||
"prop-types": "^15.6.2", | ||
"raf": "^3.4.0", | ||
"react": "^16.4.0", | ||
"react-dom": "^16.4.0", | ||
"react-test-renderer": "^16.4.0" | ||
"react": "^16.5.2", | ||
"react-dom": "^16.5.2", | ||
"react-test-renderer": "^16.5.2" | ||
}, | ||
@@ -78,0 +77,0 @@ "engines": { |
@@ -5,7 +5,8 @@ // @flow | ||
const hasChildren = (child: Element<any>): boolean => | ||
Boolean(child && child.props && child.props.children); | ||
const hasChildren = (child: Element<any>): boolean => Boolean( | ||
child && child.props && child.props.children, | ||
); | ||
const hasComplexChildren = (child: Element<any>): boolean => | ||
hasChildren(child) && typeof child.props.children === 'object'; | ||
const hasComplexChildren = (child: Element<any>): boolean => hasChildren(child) | ||
&& typeof child.props.children === 'object'; | ||
@@ -12,0 +13,0 @@ export default { |
@@ -5,3 +5,3 @@ import React, { cloneElement } from 'react'; | ||
import Adapter from 'enzyme-adapter-react-16'; | ||
import Children from '../src/'; | ||
import Children from '../src'; | ||
@@ -12,5 +12,11 @@ Enzyme.configure({ adapter: new Adapter() }); | ||
it('filter', () => { | ||
const Filtered = props => <div>{ Children.filter(props.children, item => item.type === 'span') }</div>; | ||
const Filtered = ({ children }) => <div>{ Children.filter(children, item => item.type === 'span') }</div>; | ||
Filtered.propTypes = { children: PropTypes.node.isRequired }; | ||
const wrapper = shallow(<Filtered><span>1</span><span>2</span><strong>3</strong></Filtered>); | ||
const wrapper = shallow( | ||
<Filtered> | ||
<span>1</span> | ||
<span>2</span> | ||
<strong>3</strong> | ||
</Filtered>, | ||
); | ||
expect(wrapper.find('span')).toExist(); | ||
@@ -22,3 +28,3 @@ expect(wrapper.find('span')).toHaveLength(2); | ||
it('deep filter', () => { | ||
const DeepFiltered = props => <div>{ Children.deepFilter(props.children, item => item.type === 'span') }</div>; | ||
const DeepFiltered = ({ children }) => <div>{ Children.deepFilter(children, item => item.type === 'span') }</div>; | ||
DeepFiltered.propTypes = { children: PropTypes.node.isRequired }; | ||
@@ -31,3 +37,6 @@ const wrapper = shallow( | ||
<strong>3</strong> | ||
<span><strong>4</strong><span>5</span></span> | ||
<span> | ||
<strong>4</strong> | ||
<span>5</span> | ||
</span> | ||
</span> | ||
@@ -42,7 +51,7 @@ </DeepFiltered>, | ||
it('group by type', () => { | ||
const Grouped = props => ( | ||
const Grouped = ({ children }) => ( | ||
<div> | ||
<div className="spans">{ Children.groupByType(props.children, ['span', 'i'], 'rest').span }</div> | ||
<div className="rest">{ Children.groupByType(props.children, ['span', 'i'], 'rest').rest }</div> | ||
<div className="empty">{ Children.groupByType(props.children, ['span', 'i'], 'rest').i }</div> | ||
<div className="spans">{ Children.groupByType(children, ['span', 'i'], 'rest').span }</div> | ||
<div className="rest">{ Children.groupByType(children, ['span', 'i'], 'rest').rest }</div> | ||
<div className="empty">{ Children.groupByType(children, ['span', 'i'], 'rest').i }</div> | ||
</div> | ||
@@ -52,3 +61,7 @@ ); | ||
const wrapper = shallow( | ||
<Grouped><span><b>1</b></span><span><b>2</b></span><strong>3</strong></Grouped>, | ||
<Grouped> | ||
<span><b>1</b></span> | ||
<span><b>2</b></span> | ||
<strong>3</strong> | ||
</Grouped>, | ||
); | ||
@@ -65,5 +78,5 @@ expect(wrapper.find('.spans b')).toExist(); | ||
it('deep map', () => { | ||
const DeepMapped = props => ( | ||
const DeepMapped = ({ children }) => ( | ||
<div> | ||
{ Children.deepMap(props.children, | ||
{ Children.deepMap(children, | ||
child => child && ( | ||
@@ -101,5 +114,5 @@ child.type === 'b' | ||
const texts = []; | ||
const DeepForEached = props => ( | ||
const DeepForEached = ({ children }) => ( | ||
<div> | ||
{ Children.deepForEach(props.children, (child) => { | ||
{ Children.deepForEach(children, (child) => { | ||
if (child && child.type === 'b') { | ||
@@ -133,3 +146,3 @@ texts.push(child.props.children); | ||
it('deep find', () => { | ||
const DeepFound = props => (<div>{ Children.deepFind(props.children, child => child.type === 'i') }</div>); | ||
const DeepFound = ({ children }) => (<div>{ Children.deepFind(children, child => child.type === 'i') }</div>); | ||
DeepFound.propTypes = { children: PropTypes.node.isRequired }; | ||
@@ -155,6 +168,11 @@ const wrapper = shallow( | ||
it('only text', () => { | ||
const OnlyText = props => (<div>{ Children.onlyText(props.children) }</div>); | ||
const OnlyText = ({ children }) => (<div>{ Children.onlyText(children) }</div>); | ||
OnlyText.propTypes = { children: PropTypes.node.isRequired }; | ||
const wrapper = shallow( | ||
<OnlyText><span>0</span><b>1</b><span><i>2</i></span><i>3</i></OnlyText>, | ||
<OnlyText> | ||
<span>0</span> | ||
<b>1</b> | ||
<span><i>2</i></span> | ||
<i>3</i> | ||
</OnlyText>, | ||
); | ||
@@ -161,0 +179,0 @@ expect(wrapper.find('i')).not.toExist(); |
Sorry, the diff of this file is not supported yet
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
29629
30
332