From e7b6a565bc12a21bb10ae0267680a5c9243b2189 Mon Sep 17 00:00:00 2001 From: liberty Date: Tue, 9 May 2023 08:56:02 +0800 Subject: [PATCH] feat: between --- packages/hooks/use-logic/index.js | 11 +++- test/logic/logic.test.js | 105 ++++++++++++++++++++++++++++++ 2 files changed, 113 insertions(+), 3 deletions(-) diff --git a/packages/hooks/use-logic/index.js b/packages/hooks/use-logic/index.js index 1b1c7df..3bbc086 100644 --- a/packages/hooks/use-logic/index.js +++ b/packages/hooks/use-logic/index.js @@ -84,6 +84,10 @@ const lt = (logicValue, value, fieldType) => { const lte = (logicValue, value, fieldType) => { return _.lte(value === undefined ? 0 : value, logicValue) } +const between = (logicValue, value, fieldType) => { + const [min, max] = logicValue + return lte(max, value) && gte(min, value) +} export const validator = (logic, value, field) => { let result = false switch (logic.operator) { @@ -122,14 +126,15 @@ export const validator = (logic, value, field) => { result = lt(logic.value, value, field.type) break case 'less_than_equal': + result = lte(logic.value, value, field.type) + break + case 'between': console.log(logic.value) console.log(`操作符的值:${logic.value} type: ${typeof logic.value}`) console.log(value) console.log(`field的值:${value} type: ${typeof value}`) console.log(field) - result = lte(logic.value, value, field.type) - break - case 'between': + result = between(logic.value, value, field.type) break } return result diff --git a/test/logic/logic.test.js b/test/logic/logic.test.js index 567455e..421b75d 100644 --- a/test/logic/logic.test.js +++ b/test/logic/logic.test.js @@ -600,6 +600,41 @@ describe('validator', () => { }, 1.9, field)).toBeTruthy() + expect( + validator({ + operator: 'between', + value: [1, 2] + }, + 1, + field)).toBeTruthy() + expect( + validator({ + operator: 'between', + value: [1, 2] + }, + 2.0, + field)).toBeTruthy() + expect( + validator({ + operator: 'between', + value: [1, 2] + }, + null, + field)).toBeFalsy() + expect( + validator({ + operator: 'between', + value: [1, 2] + }, + '', + field)).toBeFalsy() + expect( + validator({ + operator: 'between', + value: [1, 2] + }, + undefined, + field)).toBeFalsy() }) test('Radio', () => { const field = { type: 'radio', label: 'Radio', icon: 'radio', key: 'radio_FQWZ5Tf_KQryjLOj_-Oso', id: 'FQWZ5Tf_KQryjLOj_-Oso', options: { dataKey: 'FQWZ5Tf_KQryjLOj_-Oso', displayStyle: 'block', defaultValue: '', labelWidth: 100, isShowLabel: true, disabled: false, required: false }, style: { width: { pc: '100%', mobile: '100%' } } } @@ -1174,6 +1209,41 @@ describe('validator', () => { }, 1.9, field)).toBeTruthy() + expect( + validator({ + operator: 'between', + value: [1, 2] + }, + 1, + field)).toBeTruthy() + expect( + validator({ + operator: 'between', + value: [1, 2] + }, + 2.0, + field)).toBeTruthy() + expect( + validator({ + operator: 'between', + value: [1, 2] + }, + null, + field)).toBeFalsy() + expect( + validator({ + operator: 'between', + value: [1, 2] + }, + '', + field)).toBeFalsy() + expect( + validator({ + operator: 'between', + value: [1, 2] + }, + undefined, + field)).toBeFalsy() }) test('Switch', () => { const field = { type: 'switch', label: 'Switch', icon: 'switch', key: 'switch_XpvOZGZjVMTVdHWZiuehy', id: 'XpvOZGZjVMTVdHWZiuehy', options: { defaultValue: true, labelWidth: 100, isShowLabel: true, disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } } @@ -1313,6 +1383,41 @@ describe('validator', () => { }, 1.9, field)).toBeTruthy() + expect( + validator({ + operator: 'between', + value: [1, 2] + }, + 1, + field)).toBeTruthy() + expect( + validator({ + operator: 'between', + value: [1, 2] + }, + 2.0, + field)).toBeTruthy() + expect( + validator({ + operator: 'between', + value: [1, 2] + }, + null, + field)).toBeFalsy() + expect( + validator({ + operator: 'between', + value: [1, 2] + }, + '', + field)).toBeFalsy() + expect( + validator({ + operator: 'between', + value: [1, 2] + }, + undefined, + field)).toBeFalsy() }) test('Html', () => { const field = { type: 'html', label: 'Html', icon: 'html', key: 'html_cBsbj6NW7wNicW2umHzsE', id: 'cBsbj6NW7wNicW2umHzsE', options: { defaultValue: '

123

', placeholder: 'Please enter', action: 'http://localhost:8001/Everright-api/lowCode/uploads', size: 1, labelWidth: 100, isShowLabel: true, required: false, disabled: false }, style: { width: { pc: '100%', mobile: '100%' } } }