From 80cf77cebe5f7a265462cb4e13c159a44b5ef81a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BB=BA=E5=B3=B0?= <645381995@qq.com> Date: Thu, 22 Apr 2021 10:02:54 +0800 Subject: [PATCH 01/11] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20unionRunFunc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/unionRunFunc.ts | 17 +++++++++++++++++ tests/unionRunFunc.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+) create mode 100644 src/unionRunFunc.ts create mode 100644 tests/unionRunFunc.test.js diff --git a/src/unionRunFunc.ts b/src/unionRunFunc.ts new file mode 100644 index 00000000..6c82d735 --- /dev/null +++ b/src/unionRunFunc.ts @@ -0,0 +1,17 @@ +const unionRunFunc = ( + itemProps: Record, + childrenProps: Record, +) => { + const _props = { ...itemProps, ...childrenProps }; + Object.keys(_props).forEach(key => { + if (typeof _props[key] === 'function') { + _props[key] = (...args: any[]) => { + itemProps[key]?.(...args); + childrenProps[key]?.(...args); + }; + } + }); + return _props; +}; + +export default unionRunFunc; diff --git a/tests/unionRunFunc.test.js b/tests/unionRunFunc.test.js new file mode 100644 index 00000000..9ef3def6 --- /dev/null +++ b/tests/unionRunFunc.test.js @@ -0,0 +1,24 @@ +import unionRunFunc from '../src/unionRunFunc'; + +describe('unionRunFunc', () => { + it('unionRunFunc', () => { + const aChange = jest.fn(); + const aBlur = jest.fn(); + const bChange = jest.fn(); + const a = { value: '11', onChange: aChange, onBlur: aBlur }; + const b = { onChange: bChange }; + + const unionRunFuncProps = unionRunFunc(a, b); + unionRunFuncProps.onChange(); + unionRunFuncProps.onBlur(); + expect(aChange).toHaveBeenCalled(); + expect(aBlur).toHaveBeenCalled(); + expect(bChange).toHaveBeenCalled(); + + expect(Object.keys(unionRunFuncProps)).toEqual([ + 'value', + 'onChange', + 'onBlur', + ]); + }); +}); From 8c182a4c4b99514efdcdc5d1651e164a67e6d7aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BB=BA=E5=B3=B0?= <645381995@qq.com> Date: Thu, 22 Apr 2021 10:32:08 +0800 Subject: [PATCH 02/11] fix: review --- src/unionRunFunc.ts | 17 ----------------- tests/unionRunFunc.test.js | 24 ------------------------ 2 files changed, 41 deletions(-) delete mode 100644 src/unionRunFunc.ts delete mode 100644 tests/unionRunFunc.test.js diff --git a/src/unionRunFunc.ts b/src/unionRunFunc.ts deleted file mode 100644 index 6c82d735..00000000 --- a/src/unionRunFunc.ts +++ /dev/null @@ -1,17 +0,0 @@ -const unionRunFunc = ( - itemProps: Record, - childrenProps: Record, -) => { - const _props = { ...itemProps, ...childrenProps }; - Object.keys(_props).forEach(key => { - if (typeof _props[key] === 'function') { - _props[key] = (...args: any[]) => { - itemProps[key]?.(...args); - childrenProps[key]?.(...args); - }; - } - }); - return _props; -}; - -export default unionRunFunc; diff --git a/tests/unionRunFunc.test.js b/tests/unionRunFunc.test.js deleted file mode 100644 index 9ef3def6..00000000 --- a/tests/unionRunFunc.test.js +++ /dev/null @@ -1,24 +0,0 @@ -import unionRunFunc from '../src/unionRunFunc'; - -describe('unionRunFunc', () => { - it('unionRunFunc', () => { - const aChange = jest.fn(); - const aBlur = jest.fn(); - const bChange = jest.fn(); - const a = { value: '11', onChange: aChange, onBlur: aBlur }; - const b = { onChange: bChange }; - - const unionRunFuncProps = unionRunFunc(a, b); - unionRunFuncProps.onChange(); - unionRunFuncProps.onBlur(); - expect(aChange).toHaveBeenCalled(); - expect(aBlur).toHaveBeenCalled(); - expect(bChange).toHaveBeenCalled(); - - expect(Object.keys(unionRunFuncProps)).toEqual([ - 'value', - 'onChange', - 'onBlur', - ]); - }); -}); From 2e7a0c9a87366ef5219da79a4d0aa0217066a6fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BB=BA=E5=B3=B0?= <645381995@qq.com> Date: Thu, 22 Apr 2021 10:33:27 +0800 Subject: [PATCH 03/11] fix: review --- src/composeFuncProps.ts | 17 +++++++++++++++++ tests/composeFuncProps.test.js | 20 ++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/composeFuncProps.ts create mode 100644 tests/composeFuncProps.test.js diff --git a/src/composeFuncProps.ts b/src/composeFuncProps.ts new file mode 100644 index 00000000..6aabff1c --- /dev/null +++ b/src/composeFuncProps.ts @@ -0,0 +1,17 @@ +function composeFuncProps>( + itemProps: T, + childrenProps: Partial, +) { + const composeProps = { ...itemProps, ...childrenProps }; + Object.keys(composeProps).forEach(key => { + if (typeof composeProps[key] === 'function') { + composeProps[key] = (...args: any[]) => { + itemProps[key]?.(...args); + childrenProps[key]?.(...args); + }; + } + }); + return composeProps; +} + +export default composeFuncProps; diff --git a/tests/composeFuncProps.test.js b/tests/composeFuncProps.test.js new file mode 100644 index 00000000..c3021ddb --- /dev/null +++ b/tests/composeFuncProps.test.js @@ -0,0 +1,20 @@ +import composeFuncProps from '../src/composeFuncProps'; + +describe('composeFuncProps', () => { + it('composeFuncProps', () => { + const aChange = jest.fn(); + const aBlur = jest.fn(); + const bChange = jest.fn(); + const a = { value: '11', onChange: aChange, onBlur: aBlur }; + const b = { onChange: bChange }; + + const props = composeFuncProps(a, b); + props.onChange(); + props.onBlur(); + expect(aChange).toHaveBeenCalled(); + expect(aBlur).toHaveBeenCalled(); + expect(bChange).toHaveBeenCalled(); + + expect(Object.keys(props)).toEqual(['value', 'onChange', 'onBlur']); + }); +}); From dd6ff412c6a64d91dfe2c45bb00066c74039c95a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BB=BA=E5=B3=B0?= <645381995@qq.com> Date: Thu, 22 Apr 2021 12:27:12 +0800 Subject: [PATCH 04/11] fix: review --- src/composeFuncProps.ts | 17 +++++++---------- tests/composeFuncProps.test.js | 6 +++--- 2 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/composeFuncProps.ts b/src/composeFuncProps.ts index 6aabff1c..741cd944 100644 --- a/src/composeFuncProps.ts +++ b/src/composeFuncProps.ts @@ -1,17 +1,14 @@ -function composeFuncProps>( - itemProps: T, - childrenProps: Partial, -) { - const composeProps = { ...itemProps, ...childrenProps }; - Object.keys(composeProps).forEach(key => { - if (typeof composeProps[key] === 'function') { +function composeFuncProps(object: Partial, source: T) { + const composeProps = {}; + Object.keys(object).forEach(key => { + if (typeof object[key] === 'function') { composeProps[key] = (...args: any[]) => { - itemProps[key]?.(...args); - childrenProps[key]?.(...args); + object[key]?.(...args); + source[key]?.(...args); }; } }); - return composeProps; + return { ...object, ...source, ...composeProps }; } export default composeFuncProps; diff --git a/tests/composeFuncProps.test.js b/tests/composeFuncProps.test.js index c3021ddb..52f934bb 100644 --- a/tests/composeFuncProps.test.js +++ b/tests/composeFuncProps.test.js @@ -5,10 +5,10 @@ describe('composeFuncProps', () => { const aChange = jest.fn(); const aBlur = jest.fn(); const bChange = jest.fn(); - const a = { value: '11', onChange: aChange, onBlur: aBlur }; - const b = { onChange: bChange }; + const sourceProps = { value: '11', onChange: aChange, onBlur: aBlur }; + const patchProps = { onChange: bChange }; - const props = composeFuncProps(a, b); + const props = composeFuncProps(patchProps, sourceProps); props.onChange(); props.onBlur(); expect(aChange).toHaveBeenCalled(); From 0117a33dfad0f2183fb28f530ef1414c5022c50f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BB=BA=E5=B3=B0?= <645381995@qq.com> Date: Thu, 22 Apr 2021 12:29:06 +0800 Subject: [PATCH 05/11] fix: reviw --- src/composeFuncProps.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/composeFuncProps.ts b/src/composeFuncProps.ts index 741cd944..b7873c35 100644 --- a/src/composeFuncProps.ts +++ b/src/composeFuncProps.ts @@ -1,4 +1,4 @@ -function composeFuncProps(object: Partial, source: T) { +function composeFuncProps(object: Partial, source: Partial) { const composeProps = {}; Object.keys(object).forEach(key => { if (typeof object[key] === 'function') { From ee2c9915106ce36d3ef10b4baa3aa66b6f7296d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BB=BA=E5=B3=B0?= <645381995@qq.com> Date: Thu, 22 Apr 2021 12:31:15 +0800 Subject: [PATCH 06/11] fix: ts --- src/composeFuncProps.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/composeFuncProps.ts b/src/composeFuncProps.ts index b7873c35..77b779d7 100644 --- a/src/composeFuncProps.ts +++ b/src/composeFuncProps.ts @@ -1,4 +1,7 @@ -function composeFuncProps(object: Partial, source: Partial) { +function composeFuncProps( + object: TObject, + source: TSource, +): TObject & TSource { const composeProps = {}; Object.keys(object).forEach(key => { if (typeof object[key] === 'function') { From 90554e572c0caabb51ec39f93ea094925234970c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BB=BA=E5=B3=B0?= <645381995@qq.com> Date: Thu, 22 Apr 2021 12:41:26 +0800 Subject: [PATCH 07/11] fix: test --- tests/composeFuncProps.test.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/tests/composeFuncProps.test.js b/tests/composeFuncProps.test.js index 52f934bb..73776cb6 100644 --- a/tests/composeFuncProps.test.js +++ b/tests/composeFuncProps.test.js @@ -6,15 +6,19 @@ describe('composeFuncProps', () => { const aBlur = jest.fn(); const bChange = jest.fn(); const sourceProps = { value: '11', onChange: aChange, onBlur: aBlur }; - const patchProps = { onChange: bChange }; + const patchProps = { onChange: bChange, placeholder: 'x' }; - const props = composeFuncProps(patchProps, sourceProps); + const props = composeFuncProps(sourceProps, patchProps); props.onChange(); props.onBlur(); expect(aChange).toHaveBeenCalled(); expect(aBlur).toHaveBeenCalled(); expect(bChange).toHaveBeenCalled(); - - expect(Object.keys(props)).toEqual(['value', 'onChange', 'onBlur']); + expect(Object.keys(props)).toEqual([ + 'value', + 'onChange', + 'onBlur', + 'placeholder', + ]); }); }); From 3b8fd1657e321b216bbcb3243fa1ef592626a302 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BB=BA=E5=B3=B0?= <645381995@qq.com> Date: Thu, 22 Apr 2021 21:26:05 +0800 Subject: [PATCH 08/11] feat: review --- src/composeFuncProps.ts | 17 ----------------- tests/composeFuncProps.test.js | 24 ------------------------ 2 files changed, 41 deletions(-) delete mode 100644 src/composeFuncProps.ts delete mode 100644 tests/composeFuncProps.test.js diff --git a/src/composeFuncProps.ts b/src/composeFuncProps.ts deleted file mode 100644 index 77b779d7..00000000 --- a/src/composeFuncProps.ts +++ /dev/null @@ -1,17 +0,0 @@ -function composeFuncProps( - object: TObject, - source: TSource, -): TObject & TSource { - const composeProps = {}; - Object.keys(object).forEach(key => { - if (typeof object[key] === 'function') { - composeProps[key] = (...args: any[]) => { - object[key]?.(...args); - source[key]?.(...args); - }; - } - }); - return { ...object, ...source, ...composeProps }; -} - -export default composeFuncProps; diff --git a/tests/composeFuncProps.test.js b/tests/composeFuncProps.test.js deleted file mode 100644 index 73776cb6..00000000 --- a/tests/composeFuncProps.test.js +++ /dev/null @@ -1,24 +0,0 @@ -import composeFuncProps from '../src/composeFuncProps'; - -describe('composeFuncProps', () => { - it('composeFuncProps', () => { - const aChange = jest.fn(); - const aBlur = jest.fn(); - const bChange = jest.fn(); - const sourceProps = { value: '11', onChange: aChange, onBlur: aBlur }; - const patchProps = { onChange: bChange, placeholder: 'x' }; - - const props = composeFuncProps(sourceProps, patchProps); - props.onChange(); - props.onBlur(); - expect(aChange).toHaveBeenCalled(); - expect(aBlur).toHaveBeenCalled(); - expect(bChange).toHaveBeenCalled(); - expect(Object.keys(props)).toEqual([ - 'value', - 'onChange', - 'onBlur', - 'placeholder', - ]); - }); -}); From cccfb7816accea9cf8e5010e9b7e2e4004ddc6d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BB=BA=E5=B3=B0?= <645381995@qq.com> Date: Thu, 22 Apr 2021 21:26:37 +0800 Subject: [PATCH 09/11] feat: review --- src/composeProps.ts | 23 ++++++++++++++++++++++ tests/composeProps.test.js | 39 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 src/composeProps.ts create mode 100644 tests/composeProps.test.js diff --git a/src/composeProps.ts b/src/composeProps.ts new file mode 100644 index 00000000..6ff7b91e --- /dev/null +++ b/src/composeProps.ts @@ -0,0 +1,23 @@ +function composeProps>( + originProps: T, + patchProps: Partial, + isAll: boolean = true, +) { + const composedProps: Record = { + ...originProps, + ...(isAll ? patchProps : {}), + }; + + Object.keys(patchProps).forEach(key => { + const func = patchProps[key]; + if (typeof func === 'function') { + composedProps[key] = (...args) => { + func(...args); + return originProps[key](...args); + }; + } + }); + return composedProps; +} + +export default composeProps; diff --git a/tests/composeProps.test.js b/tests/composeProps.test.js new file mode 100644 index 00000000..eb3383c6 --- /dev/null +++ b/tests/composeProps.test.js @@ -0,0 +1,39 @@ +import composeProps from '../src/composeProps'; + +describe('composeProps', () => { + it('composeProps all', () => { + const aChange = jest.fn(); + const aBlur = jest.fn(); + const bChange = jest.fn(); + const sourceProps = { value: '11', onChange: aChange, onBlur: aBlur }; + const patchProps = { onChange: bChange, placeholder: 'x' }; + + const props = composeProps(sourceProps, patchProps); + props.onChange(); + props.onBlur(); + expect(aChange).toHaveBeenCalled(); + expect(aBlur).toHaveBeenCalled(); + expect(bChange).toHaveBeenCalled(); + expect(Object.keys(props)).toEqual([ + 'value', + 'onChange', + 'onBlur', + 'placeholder', + ]); + }); + it('composeProps just func', () => { + const aChange = jest.fn(); + const aBlur = jest.fn(); + const bChange = jest.fn(); + const sourceProps = { value: '11', onChange: aChange, onBlur: aBlur }; + const patchProps = { onChange: bChange, placeholder: 'x' }; + + const props = composeProps(sourceProps, patchProps, false); + props.onChange(); + props.onBlur(); + expect(aChange).toHaveBeenCalled(); + expect(aBlur).toHaveBeenCalled(); + expect(bChange).toHaveBeenCalled(); + expect(Object.keys(props)).toEqual(['value', 'onChange', 'onBlur']); + }); +}); From ebfb9cb2aaeb92198e0b13a737b5e990f5925cb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BB=BA=E5=B3=B0?= <645381995@qq.com> Date: Thu, 22 Apr 2021 21:33:33 +0800 Subject: [PATCH 10/11] feat: review --- src/composeProps.ts | 2 +- tests/composeProps.test.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/composeProps.ts b/src/composeProps.ts index 6ff7b91e..4f7a5e89 100644 --- a/src/composeProps.ts +++ b/src/composeProps.ts @@ -1,7 +1,7 @@ function composeProps>( originProps: T, patchProps: Partial, - isAll: boolean = true, + isAll: boolean, ) { const composedProps: Record = { ...originProps, diff --git a/tests/composeProps.test.js b/tests/composeProps.test.js index eb3383c6..c6b0d389 100644 --- a/tests/composeProps.test.js +++ b/tests/composeProps.test.js @@ -8,7 +8,7 @@ describe('composeProps', () => { const sourceProps = { value: '11', onChange: aChange, onBlur: aBlur }; const patchProps = { onChange: bChange, placeholder: 'x' }; - const props = composeProps(sourceProps, patchProps); + const props = composeProps(sourceProps, patchProps, true); props.onChange(); props.onBlur(); expect(aChange).toHaveBeenCalled(); @@ -28,7 +28,7 @@ describe('composeProps', () => { const sourceProps = { value: '11', onChange: aChange, onBlur: aBlur }; const patchProps = { onChange: bChange, placeholder: 'x' }; - const props = composeProps(sourceProps, patchProps, false); + const props = composeProps(sourceProps, patchProps); props.onChange(); props.onBlur(); expect(aChange).toHaveBeenCalled(); From e06a74b9cf0d0ca9b4871e7128a5e7160d9efd84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E5=BB=BA=E5=B3=B0?= <645381995@qq.com> Date: Thu, 22 Apr 2021 21:36:13 +0800 Subject: [PATCH 11/11] feat: review --- src/composeProps.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/composeProps.ts b/src/composeProps.ts index 4f7a5e89..f225dd1e 100644 --- a/src/composeProps.ts +++ b/src/composeProps.ts @@ -1,7 +1,7 @@ function composeProps>( originProps: T, patchProps: Partial, - isAll: boolean, + isAll?: boolean, ) { const composedProps: Record = { ...originProps,