Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript redux形式的多重规格化器_Javascript_Reactjs_React Native_Redux Form_Redux Form Validators - Fatal编程技术网

Javascript redux形式的多重规格化器

Javascript redux形式的多重规格化器,javascript,reactjs,react-native,redux-form,redux-form-validators,Javascript,Reactjs,React Native,Redux Form,Redux Form Validators,我想为传递多个函数,有没有办法?就像我们在验证道具中传递一个数组 validate={[alphanumeric,maxlength]} 在整个redux表单问题中,我看到了如下方式 normalize={maxlength_alphanumeric(5)} //but here two logic are burried in one function 如何实现类似于 normalize={maxlength(5) , alphanumeric} 我们可以使用闭包实现这一点,因此我们创建

我想为传递多个函数,有没有办法?就像我们在验证道具中传递一个数组

validate={[alphanumeric,maxlength]}
在整个redux表单问题中,我看到了如下方式

normalize={maxlength_alphanumeric(5)} //but here two logic are burried in one function
如何实现类似于

normalize={maxlength(5) , alphanumeric}

我们可以使用闭包实现这一点,因此我们创建了一个接受规范化函数数组的函数

请注意,这与验证不同,因为在验证中,所有函数都不相关,如果其中一个失败,则表示验证失败

在标准化过程中,传递给每个标准化函数的值需要从已经进行了一些标准化的前一个函数传递

因此,我们可以使用下面的
normalizeAll
函数来实现这一点

function normalizeAll(normalizers){
    return function(value , previousValue , allValues , previousAllValues){ //note that these arguments are passed by default from redux form
        var i = 0;
        var normalizersLength = normalizers.length;
        var currentValue = value;
        while(i < normalizersLength)
        {
            var currentNormalizer = normalizers[i];
            if(typeof currentNormalizer == "function")
            {
                currentValue = currentNormalizer(currentValue ,previousValue , allValues , previousAllValues);
            }
            i++;
        }

        return currentValue;
    }
}

//I am using only the `value` argument in normalization functions just for the example, but you can use the rest of them if they are needed
var firstNormalization = function(value , previousValue , allValues , previousAllValues){
    return value+1;
}

var secondNormalization = function(value, previousValue , allValues , previousAllValues){
    return value+2;
}

var thirdNormalization = function(value, previousValue , allValues , previousAllValues){
    return value+3;
}

//To test result: i will execute the function returned from normalizeAll and pass the first argument as 1.
var normalizedValue = normalizeAll([firstNormalization , secondNormalization , thirdNormalization])(1); 
console.log(normalizedValue); // 7 


注意这是假设所有规范化函数都是同步的,我不认为我们通常有规范化函数需要异步的用例,但是解决方案仍然是一样的,但是我们将使用
回调
函数或
承诺

这会成为库中的函数吗?谢谢
normalize={normalizeAll([firstNormalizationFunction , secondNormalizationFunction])}