function parseLineStr(name, str, isIntLine, toNumber, maxLen, vueInstance) { name = name…
- User
function parseLineStr(name, str, isIntLine, toNumber, maxLen, vueInstance) { name = name || '' if (type(str) !== 'string') { vueInstance && vueInstance.message({ type: 'warning', message: name + '请按格式输入' }) return false } str = str.trim() var values = str.split(/[\r\n]+/) var reg = /^\d+/ var newValues = [] for (var i = 0; i < values.length; i++) { var value = values[i] value = value.trim() if (value) { if (isIntLine) { if (reg.test(value)) { newValues.push(toNumber ? Number(value) : value); 2 } else { vueInstance && vueInstance.$message({ type: 'warning', message: name + '每一行必须是由数字组成' }) return false } } else { newValues.push(value) } } }
if (isNum(maxLen) && maxLen > 0 && newValues.length > maxLen) { vueInstance.$message({ type: 'warning', message: name + '不能超过' + maxLen + '个' }) return false }
return newValues }
- Assistant
This function takes in a string input, splits it by line breaks using the regular expression
/[\r\n]+/, and returns an array of values.The
nameparameter is used to give feedback to the user in case of an error message.The
isIntLineparameter indicates whether each line is expected to be an integer or not. IfisIntLineis true, the function uses a regular expression to check that each line consists only of digits. IftoNumberis also true, it converts each line to a number.The
maxLenparameter is used to limit the maximum number of lines, andvueInstanceis optional Vue instance used to display warning messages.If there is any invalid input, the function returns false and displays a warning message to the user. Otherwise, it returns the array of parsed values.
- User
*/ 帮以下代码用jest写ut export function parseLineStr(name, str, isIntLine, toNumber, maxLen, vueInstance) { name = name || '' if (type(str) !== 'string') { vueInstance && vueInstance.message({ type: 'warning', message: name + '请按格式输入' }) return false } str = str.trim() var values = str.split(/[\r\n]+/) var reg = /^\d+/ var newValues = [] for (var i = 0; i < values.length; i++) { var value = values[i] value = value.trim() if (value) { if (isIntLine) { if (reg.test(value)) { newValues.push(toNumber ? Number(value) : value); 2 } else { vueInstance && vueInstance.$message({ type: 'warning', message: name + '每一行必须是由数字组成' }) return false } } else { newValues.push(value) } } }
if (isNum(maxLen) && maxLen > 0 && newValues.length > maxLen) { vueInstance.$message({ type: 'warning', message: name + '不能超过' + maxLen + '个' }) return false }
return newValues }
- Assistant
Here is an example set of unit tests using Jest for the
parseLineStrfunction:describe('parseLineStr', () => { const name = 'Test' const vueInstance = { $message: jest.fn() } it('should return false with a warning message if str input is not a string', () => { const result = parseLineStr(name, null, false, false, null, vueInstance) expect(result).toBe(false) expect(vueInstance.$message).toHaveBeenCalledWith({ type: 'warning', message: name + '请按格式输入' }) }) it('should split str input by line breaks and trim each line', () => { const result = parseLineStr(name, ' a \n b\n \n c\n ', false, false, null, vueInstance) expect(result).toEqual(['a', 'b', 'c']) }) it('should convert integer lines to numbers if toNumber is true', () => { const result = parseLineStr(name, '1\n2\n3', true, true, null, vueInstance) expect(result).toEqual([1, 2, 3]) }) it('should return false with a warning message if a line contains non-numeric characters and isIntLine is true', () => { const result = parseLineStr(name, '1\na\n3', true, true, null, vueInstance) expect(result).toBe(false) expect(vueInstance.$message).toHaveBeenCalledWith({ type: 'warning', message: name + '每一行必须是由数字组成' }) }) it('should return false with a warning message if the number of lines exceeds maxLen', () => { const result = parseLineStr(name, '1\n2\n3', false, false, 2, vueInstance) expect(result).toBe(false) expect(vueInstance.$message).toHaveBeenCalledWith({ type: 'warning', message: name + '不能超过2个' }) }) })