Skip to main content
edited tags; edited title
Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Refactoring String length validation predicate

deleted 9 characters in body
Source Link
konijn
  • 33.8k
  • 5
  • 69
  • 264

As you can see in the example below, I partially refactored the code so okFields useokFields uses the extracted function checkStringLength.

Now I also want problemFields()problemFields() to use the same refactored function, but there is differencea difference between how okFields and problemFields use the function: you see that the problemFieldsproblemFields has a little '!'! in front of validatorvalidator, and I don't know how to have this vary from the same extracted function. How would you do it 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
    }
}

As you can see in the example below, I partially refactored the code so okFields use the extracted function checkStringLength.

Now I also want problemFields() to use the same refactored function, but there is 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 do it ?

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
    }
}

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
    }
}
Source Link
Benj
  • 63
  • 4

Refactoring predicate

As you can see in the example below, I partially refactored the code so okFields use the extracted function checkStringLength.

Now I also want problemFields() to use the same refactored function, but there is 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 do it ?

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
    }
}