1
\$\begingroup\$

I partially refactored the code so okFields uses the extracted function checkStringLength.

Now I also want problemFields() to use the same refactored function, but there is a difference between how okFields and problemFields use the function: you see that the problemFields has a little ! in front of validator, and I don't know how to have this vary from the same extracted function. How would you avoid the duplicate code?

function stringLengthValidation( mergedModelAndFormFields ) {
 let fieldsToValidate = mergedModelAndFormFields.okFields
 // extracted function
 function checkStringLength() {
 return _.pickBy( fieldsToValidate, ( value ) => {
 return validator.isLength( value[ 'content' ], {
 'min': value[ 'validation' ].minLength,
 'max': value[ 'validation' ].maxLength
 } )
 } )
 }
 let okFields = checkStringLength()
 // I want to replace this with checkStringLength()
 let problemFields = _.pickBy( fieldsToValidate, ( value ) => {
 // see the '!' here that is the variation
 return !validator.isLength( value[ 'content' ], {
 'min': value[ 'validation' ].minLength,
 'max': value[ 'validation' ].maxLength
 } )
 } )
 return {
 'okFields' : okFields,
 'problemFields': problemFields
 }
}
200_success
145k22 gold badges190 silver badges478 bronze badges
asked Jul 11, 2016 at 13:37
\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

It seems that the ok fields plus the problem fields equal all the fields.

In that case, you should consider deriving problem fields from all fields and ok fields with the Underscore difference method.

So in short:

function stringLengthValidation( mergedModelAndFormFields ) {
 let fieldsToValidate = mergedModelAndFormFields.okFields
 // extracted function
 function checkStringLength() {
 return _.pickBy( fieldsToValidate, ( value ) => {
 return validator.isLength( value[ 'content' ], {
 'min': value[ 'validation' ].minLength,
 'max': value[ 'validation' ].maxLength
 } )
 } )
 }
 let okFields = checkStringLength()
 return {
 'okFields' : okFields,
 'problemFields': _.difference( fieldsToValidate, okFields )
 }
}

Actually, I would probably not even encapsulate what is in essence 1 statement into a separate function, it seems subjectively overkill to me:

function stringLengthValidation( mergedModelAndFormFields ) {
 let fieldsToValidate = mergedModelAndFormFields.okFields 
 let okFields = _.pickBy( fieldsToValidate, ( value ) => {
 return validator.isLength( value[ 'content' ], {
 'min': value[ 'validation' ].minLength,
 'max': value[ 'validation' ].maxLength
 } )
 });
 return {
 'okFields' : okFields,
 'problemFields': _.difference( fieldsToValidate, okFields )
 }
}
answered Jul 11, 2016 at 14:36
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.