react-hooks-combine
Advanced tools
Comparing version 1.0.0 to 1.1.0
@@ -1,2 +0,2 @@ | ||
"use strict";function _interopDefault(t){return t&&"object"==typeof t&&"default"in t?t.default:t}Object.defineProperty(exports,"__esModule",{value:!0});var React=require("react"),React__default=_interopDefault(React);function _typeof(t){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function _defineProperty(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function _objectSpread(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{},n=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(r).filter(function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable}))),n.forEach(function(t){_defineProperty(e,t,r[t])})}return e}function _slicedToArray(t,e){return _arrayWithHoles(t)||_iterableToArrayLimit(t,e)||_nonIterableRest()}function _toConsumableArray(t){return _arrayWithoutHoles(t)||_iterableToArray(t)||_nonIterableSpread()}function _arrayWithoutHoles(t){if(Array.isArray(t)){for(var e=0,r=new Array(t.length);e<t.length;e++)r[e]=t[e];return r}}function _arrayWithHoles(t){if(Array.isArray(t))return t}function _iterableToArray(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}function _iterableToArrayLimit(t,e){var r=[],n=!0,o=!1,i=void 0;try{for(var a,c=t[Symbol.iterator]();!(n=(a=c.next()).done)&&(r.push(a.value),!e||r.length!==e);n=!0);}catch(t){o=!0,i=t}finally{try{n||null==c.return||c.return()}finally{if(o)throw i}}return r}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}var CTOR_PATTERN=/\[object\s(\w+)\]/,baseToString=Object.prototype.toString,getInternalCtor=function(t){return _slicedToArray(baseToString.call(t).match(CTOR_PATTERN),2)[1]},isFunction=function(t){return"Function"===getInternalCtor(t)},isObject=function(t){return"Object"===getInternalCtor(t)},isPromiseLike=function(t){return t&&("Promise"===getInternalCtor(t)||isFunction(t.then)&&isFunction(t.catch))},compose=function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];return e.reduce(function(t,e){return function(){return t(e.apply(void 0,arguments))}})},isCombineConfigMode=function(t){return isObject(t[0])},unwindLoop=function(t,e){var r=Function,n=Object.entries(e).map(function(t){var e=_slicedToArray(t,1)[0];return"box.".concat(e," = useCustomHook(fns.").concat(e,", deps, state, props);")}).join("\n");return new r("useCustomHook","fns","deps","state","props","\n const box = {};\n ".concat(n,"\n return box\n ")).bind(null,t,e)},prop=function(t,e){if(null==t)return t;var r=t,n=e.split("."),o=!0,i=!1,a=void 0;try{for(var c,u=n[Symbol.iterator]();!(o=(c=u.next()).done);o=!0){var f=c.value;if(!(r=r&&r[f]))return}}catch(t){i=!0,a=t}finally{try{o||null==u.return||u.return()}finally{if(i)throw a}}return r},getDeps=function(e,t){return Array.isArray(t)?t.map(function(t){return prop(e,t)}):t},defaultProps=function(e){return function(t){if("object"!==_typeof(e))throw Error("defaultProps expects object, got a ".concat(getInternalCtor(e)));return t.defaultProps=e,t}},identity=function(t){return t},flow=function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];return function(t){return e.reduce(function(t,e){return e(t)},t)}},merge=function(t,e){return _objectSpread({},t,e)},hookBuilder=function(t){var e=t.findIndex(function(t){return!isFunction(t)});if(-1!==e)throw Error("\n Expected function,\n got a: ".concat(_typeof(t[e]),"\n on index: ").concat(e,"\n "));var r=Function,n=t.map(function(t,e){return" \n const result".concat(e," = funcs[").concat(e,"](state").concat(e,", props)\n const state").concat(e+1," = merge(result").concat(e,", state").concat(e,");\n ")}).join("\n");return new r("funcs","merge","props","\n const state0 = {};\n ".concat(n,"\n return state").concat(t.length,";\n ")).bind(null,t,merge)},combineFromConfig=function(t,r){var e=Object.assign({hooks:[],hocs:[],transformProps:identity},t),n=e.hooks,o=e.hocs,i=e.defaultProps,a=e.transformProps,c=hookBuilder(n);i&&defaultProps(i)(r);return flow.apply(void 0,_toConsumableArray(o))(function(t){var e=c(t);return React__default.createElement(r,a(_objectSpread({},e,t)))})},combine=function(){for(var t=arguments.length,r=new Array(t),e=0;e<t;e++)r[e]=arguments[e];return function(t){var e=isCombineConfigMode(r)?combineFromConfig(r[0],t):combineFromConfig({hooks:r},t);return e.displayName="".concat(t.displayName||t.name,"Hooked"),e}},withImperativeHandle=function(n,o,i){return function(t,e){var r=getDeps(_objectSpread({},t,e),i);React.useImperativeHandle(n,function(){return o(t,e)},r)}},withCallbacks=function(t,r){var n=unwindLoop(function(t,e,r,n){var o=isFunction(t)?t:t.func,i=getDeps(_objectSpread({},r,n),t.deps||e);return React.useCallback(o(r,n),i)},t);return function(t,e){return n(r,t,e)}},withCallback=function(n,o,i){return function(t,e){var r=getDeps(_objectSpread({},t,e),i);return _defineProperty({},n,React.useCallback(o(t,e),r))}},withStateHandlers=function(d,y){return function(t,e){for(var r=isFunction(d)?d(t,e):d,n=_slicedToArray(React.useState(r),2),o=n[0],i=n[1],a={},c=function(t){return i(_objectSpread({},o,t))},u=0,f=Object.entries(y);u<f.length;u++){var s=_slicedToArray(f[u],2),l=s[0],p=s[1];a[l]=compose(c,p(o))}return _objectSpread({},o,a)}},withLayoutEffect=function(n,o){return function(t,e){var r=getDeps(_objectSpread({},t,e),o);React.useLayoutEffect(function(){return n(t,e)},r)}},withAsyncEffect=function(t){function e(){a=!1}var a=!1,r=_objectSpread({dataName:"data"},t),c=r.asyncAction,n=r.disposeAction,u=r.dataName,f=r.deps,s=isFunction(n)?function(){e(),n()}:e;return function(r,n){var t,e=_slicedToArray(React.useState((_defineProperty(t={loading:!0},u,null),_defineProperty(t,"error",null),t)),2),o=e[0],i=e[1];return React.useEffect(function(){var t;a&&i((_defineProperty(t={loading:!0},u,o[u]),_defineProperty(t,"error",null),t));a=!0;var e=c(r,n);if(!isPromiseLike(e))throw Error("withAsyncEffect expects Promise, got a: ".concat(getInternalCtor(e)));return e.then(function(t){var e;i((_defineProperty(e={loading:!1},u,t),_defineProperty(e,"error",null),e))},function(t){var e;i((_defineProperty(e={loading:!1},u,o[u]),_defineProperty(e,"error",t),e))}),s},getDeps(_objectSpread({},r,n),f)),o}},withDebugValue=function(n,o){return function(t,e){var r=n(t,e);React.useDebugValue(r,o)}},withMemos=function(t,r){var n=unwindLoop(function(t,e,r,n){var o=isFunction(t)?t:t.func,i=getDeps(_objectSpread({},r,n),t.deps||e);return React.useMemo(function(){return o(r,n)},i)},t);return function(t,e){return n(r,t,e)}},withMemo=function(n,o,i){return function(t,e){var r=React.useMemo(function(){return o(t,e)},getDeps(_objectSpread({},t,e),i));return _defineProperty({},n,r)}},withContext=function(e,r){return function(){var t=React.useContext(r);return _defineProperty({},e,t)}},withReducer=function(t){var e=_objectSpread({dispatchName:"dispatch"},t),o=e.reducer,i=e.stateName,a=e.dispatchName,c=e.initialState,u=e.init;return function(){var t,e=_slicedToArray(React.useReducer(o,c,u),2),r=e[0],n=e[1];return _defineProperty(t={},i,r),_defineProperty(t,a,n),t}},withEffect=function(n,o){return function(t,e){var r=getDeps(_objectSpread({},t,e),o);React.useEffect(function(){return n(t,e)},r)}},withState=function(a,c,r){var u=isFunction(r)?function(t,e){return function(){return r(t,e)}}:function(){return r};return function(t,e){var r,n=_slicedToArray(React.useState(u(t,e)),2),o=n[0],i=n[1];return _defineProperty(r={},a,o),_defineProperty(r,c,i),r}},withRef=function(e,r){return function(){var t=React.useRef(r);return _defineProperty({},e,t)}};exports.combine=combine,exports.defaultProps=defaultProps,exports.flow=flow,exports.withAsyncEffect=withAsyncEffect,exports.withCallback=withCallback,exports.withCallbacks=withCallbacks,exports.withContext=withContext,exports.withDebugValue=withDebugValue,exports.withEffect=withEffect,exports.withImperativeHandle=withImperativeHandle,exports.withLayoutEffect=withLayoutEffect,exports.withMemo=withMemo,exports.withMemos=withMemos,exports.withReducer=withReducer,exports.withRef=withRef,exports.withState=withState,exports.withStateHandlers=withStateHandlers; | ||
"use strict";function _interopDefault(t){return t&&"object"==typeof t&&"default"in t?t.default:t}Object.defineProperty(exports,"__esModule",{value:!0});var React=require("react"),React__default=_interopDefault(React);function _typeof(t){return(_typeof="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t})(t)}function _defineProperty(t,e,r){return e in t?Object.defineProperty(t,e,{value:r,enumerable:!0,configurable:!0,writable:!0}):t[e]=r,t}function _objectSpread(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{},n=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(r).filter(function(t){return Object.getOwnPropertyDescriptor(r,t).enumerable}))),n.forEach(function(t){_defineProperty(e,t,r[t])})}return e}function _slicedToArray(t,e){return _arrayWithHoles(t)||_iterableToArrayLimit(t,e)||_nonIterableRest()}function _toConsumableArray(t){return _arrayWithoutHoles(t)||_iterableToArray(t)||_nonIterableSpread()}function _arrayWithoutHoles(t){if(Array.isArray(t)){for(var e=0,r=new Array(t.length);e<t.length;e++)r[e]=t[e];return r}}function _arrayWithHoles(t){if(Array.isArray(t))return t}function _iterableToArray(t){if(Symbol.iterator in Object(t)||"[object Arguments]"===Object.prototype.toString.call(t))return Array.from(t)}function _iterableToArrayLimit(t,e){var r=[],n=!0,o=!1,i=void 0;try{for(var a,c=t[Symbol.iterator]();!(n=(a=c.next()).done)&&(r.push(a.value),!e||r.length!==e);n=!0);}catch(t){o=!0,i=t}finally{try{n||null==c.return||c.return()}finally{if(o)throw i}}return r}function _nonIterableSpread(){throw new TypeError("Invalid attempt to spread non-iterable instance")}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance")}var CTOR_PATTERN=/\[object\s(\w+)\]/,baseToString=Object.prototype.toString,getInternalCtor=function(t){return _slicedToArray(baseToString.call(t).match(CTOR_PATTERN),2)[1]},isFunction=function(t){return"Function"===getInternalCtor(t)},isObject=function(t){return"Object"===getInternalCtor(t)},isPromiseLike=function(t){return t&&("Promise"===getInternalCtor(t)||isFunction(t.then)&&isFunction(t.catch))},compose=function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];return e.reduce(function(t,e){return function(){return t(e.apply(void 0,arguments))}})},isCombineConfigMode=function(t){return isObject(t[0])},unwindLoop=function(t,e){var r=Function,n=Object.entries(e).map(function(t){var e=_slicedToArray(t,1)[0];return"box.".concat(e," = useCustomHook(fns.").concat(e,", deps, state, props);")}).join("\n");return new r("useCustomHook","fns","deps","state","props","\n const box = {};\n ".concat(n,"\n return box\n ")).bind(null,t,e)},prop=function(t,e){if(null==t)return t;var r=t,n=e.split("."),o=!0,i=!1,a=void 0;try{for(var c,u=n[Symbol.iterator]();!(o=(c=u.next()).done);o=!0){var f=c.value;if(!(r=r&&r[f]))return}}catch(t){i=!0,a=t}finally{try{o||null==u.return||u.return()}finally{if(i)throw a}}return r},getDeps=function(e,t){return Array.isArray(t)?t.map(function(t){return prop(e,t)}):t},defaultProps=function(e){return function(t){if("object"!==_typeof(e))throw Error("defaultProps expects object, got a ".concat(getInternalCtor(e)));return t.defaultProps=e,t}},identity=function(t){return t},flow=function(){for(var t=arguments.length,e=new Array(t),r=0;r<t;r++)e[r]=arguments[r];return function(t){return e.reduce(function(t,e){return e(t)},t)}},merge=function(t,e){return _objectSpread({},t,e)},hookBuilder=function(t){var e=t.findIndex(function(t){return!isFunction(t)});if(-1!==e)throw Error("\n Expected function,\n got a: ".concat(_typeof(t[e]),"\n on index: ").concat(e,"\n "));var r=Function,n=t.map(function(t,e){return" \n const result".concat(e," = funcs[").concat(e,"](state").concat(e,", props)\n const state").concat(e+1," = merge(result").concat(e,", state").concat(e,");\n ")}).join("\n");return new r("funcs","merge","props","\n const state0 = {};\n ".concat(n,"\n return state").concat(t.length,";\n ")).bind(null,t,merge)},combineFromConfig=function(t,n){var e=_objectSpread({hooks:[],hocs:[],transformProps:identity,transformPropsBefore:identity},t),r=e.hooks,o=e.hocs,i=e.defaultProps,a=e.transformProps,c=e.transformPropsBefore,u=hookBuilder(r);i&&defaultProps(i)(n);return flow.apply(void 0,_toConsumableArray(o))(function(t){var e=c(t),r=_objectSpread({},e,u(e));return React__default.createElement(n,a(r))})},combine=function(){for(var t=arguments.length,r=new Array(t),e=0;e<t;e++)r[e]=arguments[e];return function(t){var e=isCombineConfigMode(r)?combineFromConfig(r[0],t):combineFromConfig({hooks:r},t);return e.displayName="".concat(t.displayName||t.name,"Hooked"),e}},withImperativeHandle=function(n,o,i){return function(t,e){var r=getDeps(_objectSpread({},t,e),i);React.useImperativeHandle(n,function(){return o(t,e)},r)}},withCallbacks=function(t,r){var n=unwindLoop(function(t,e,r,n){var o=isFunction(t)?t:t.func,i=getDeps(_objectSpread({},r,n),t.deps||e);return React.useCallback(o(r,n),i)},t);return function(t,e){return n(r,t,e)}},withCallback=function(n,o,i){return function(t,e){var r=getDeps(_objectSpread({},t,e),i);return _defineProperty({},n,React.useCallback(o(t,e),r))}},withStateHandlers=function(d,y){return function(t,e){for(var r=isFunction(d)?d(t,e):d,n=_slicedToArray(React.useState(r),2),o=n[0],i=n[1],a={},c=function(t){return i(_objectSpread({},o,t))},u=0,f=Object.entries(y);u<f.length;u++){var s=_slicedToArray(f[u],2),l=s[0],p=s[1];a[l]=compose(c,p(o))}return _objectSpread({},o,a)}},withLayoutEffect=function(n,o){return function(t,e){var r=getDeps(_objectSpread({},t,e),o);React.useLayoutEffect(function(){return n(t,e)},r)}},withAsyncEffect=function(t){function e(){a=!1}var a=!1,r=_objectSpread({dataName:"data"},t),c=r.asyncAction,n=r.disposeAction,u=r.dataName,f=r.deps,s=isFunction(n)?function(){e(),n()}:e;return function(r,n){var t,e=_slicedToArray(React.useState((_defineProperty(t={loading:!0},u,null),_defineProperty(t,"error",null),t)),2),o=e[0],i=e[1];return React.useEffect(function(){var t;a&&i((_defineProperty(t={loading:!0},u,o[u]),_defineProperty(t,"error",null),t));a=!0;var e=c(r,n);if(!isPromiseLike(e))throw Error("withAsyncEffect expects Promise, got a: ".concat(getInternalCtor(e)));return e.then(function(t){var e;i((_defineProperty(e={loading:!1},u,t),_defineProperty(e,"error",null),e))},function(t){var e;i((_defineProperty(e={loading:!1},u,o[u]),_defineProperty(e,"error",t),e))}),s},getDeps(_objectSpread({},r,n),f)),o}},withDebugValue=function(n,o){return function(t,e){var r=n(t,e);React.useDebugValue(r,o)}},withMemos=function(t,r){var n=unwindLoop(function(t,e,r,n){var o=isFunction(t)?t:t.func,i=getDeps(_objectSpread({},r,n),t.deps||e);return React.useMemo(function(){return o(r,n)},i)},t);return function(t,e){return n(r,t,e)}},withMemo=function(n,o,i){return function(t,e){var r=React.useMemo(function(){return o(t,e)},getDeps(_objectSpread({},t,e),i));return _defineProperty({},n,r)}},withContext=function(e,r){return function(){var t=React.useContext(r);return _defineProperty({},e,t)}},withReducer=function(t){var e=_objectSpread({dispatchName:"dispatch"},t),o=e.reducer,i=e.stateName,a=e.dispatchName,c=e.initialState,u=e.init;return function(){var t,e=_slicedToArray(React.useReducer(o,c,u),2),r=e[0],n=e[1];return _defineProperty(t={},i,r),_defineProperty(t,a,n),t}},withEffect=function(n,o){return function(t,e){var r=getDeps(_objectSpread({},t,e),o);React.useEffect(function(){return n(t,e)},r)}},withState=function(a,c,r){var u=isFunction(r)?function(t,e){return function(){return r(t,e)}}:function(){return r};return function(t,e){var r,n=_slicedToArray(React.useState(u(t,e)),2),o=n[0],i=n[1];return _defineProperty(r={},a,o),_defineProperty(r,c,i),r}},withRef=function(e,r){return function(){var t=React.useRef(r);return _defineProperty({},e,t)}};exports.combine=combine,exports.defaultProps=defaultProps,exports.flow=flow,exports.withAsyncEffect=withAsyncEffect,exports.withCallback=withCallback,exports.withCallbacks=withCallbacks,exports.withContext=withContext,exports.withDebugValue=withDebugValue,exports.withEffect=withEffect,exports.withImperativeHandle=withImperativeHandle,exports.withLayoutEffect=withLayoutEffect,exports.withMemo=withMemo,exports.withMemos=withMemos,exports.withReducer=withReducer,exports.withRef=withRef,exports.withState=withState,exports.withStateHandlers=withStateHandlers; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"version": "1.0.0", | ||
"version": "1.1.0", | ||
"name": "react-hooks-combine", | ||
@@ -77,2 +77,2 @@ "description": "React hooks powered, recompose like utility belt for ladies and gentlemen.", | ||
} | ||
} | ||
} |
@@ -34,2 +34,3 @@ # React hooks combine | ||
```javascript | ||
@@ -39,2 +40,54 @@ // component.jsx | ||
export const UserPageComponent = ({ loading, userData, onSubmit, onCancel }) => ( | ||
<div> | ||
<h2>User Form</h2> | ||
<ContentLoadIndicator loading={loading}> | ||
{ | ||
() => ( | ||
<UserForm | ||
initialValues={userData} | ||
onSubmit={onSubmit} | ||
onCancel={onCancel} /> | ||
) | ||
} | ||
</ContentLoadIndicator> | ||
</div> | ||
) | ||
``` | ||
```javascript | ||
// container.js (withAsyncEffect + withCallbacks) | ||
import { combine, withAsyncEffect, withCallbacks } from 'react-hooks-combine' | ||
import { UserPageComponent } from './component' | ||
export default combine( | ||
withAsyncEffect({ | ||
deps: ['userId'], // will request again if user id is changed | ||
dataName: 'userData', // 'data' by default | ||
asyncAction: (_state, ownProps) => ownProps.userService.load(ownProps.userId), | ||
}), | ||
withCallbacks({ | ||
onSubmit: (state, props) => async (formData) => { | ||
const { userData } = state | ||
const { userService } = props | ||
await userService.save({ ...userData, ...formData}) | ||
... | ||
}, | ||
onCancel: () => () => { | ||
... | ||
} | ||
}, ['userData']) | ||
)(UserPageComponent) | ||
``` | ||
__OR__ | ||
```javascript | ||
// component.jsx | ||
import React from 'react' | ||
export const CounterComponent = ({ count, onPlus, onMinus }) => ( | ||
@@ -74,7 +127,7 @@ <div> | ||
onPlus: ({ counterState, dispatch }, _props) => () => { | ||
dispatchFn({ type: INC }) | ||
dispatch({ type: INC }) | ||
}, | ||
onMinus: ({ counterState, dispatch }, _props) => () => { | ||
dispatchFn({ type: DEC }) | ||
dispatch({ type: DEC }) | ||
} | ||
@@ -81,0 +134,0 @@ }, ['counterState']), // <- deps for useCallback (CHECK API TO LEARN MORE) |
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
32769
158