首页 > 解决方案 > 使用这些 [input] 和 [output] 函数有什么意义

问题描述

我在 GitHub 上的 react-native excel 导出演示中找到了这个,我不明白为什么这两个函数是必要input output ,为什么不直接传递wbout writeFile,这是一些函数式编程类型的东西吗?

const input = res => res;
const output = str => str;

const exportFile =()=> {
        //...some code 
        writeFile(file, output(wbout), 'ascii').then((res) =>{
             console.log("exportFile success", "Exported to " + file);
        }).catch((err) => { 
             console.log("exportFile Error", "Error " + err.message); 
        });
};
const importFile=()=> {
        readFile(DDP + "sheetjs.xlsx", 'ascii').then((res) => {
            /* parse file */
            const wb = XLSX.read(input(res), {type:'binary'});
             //...rest of coe
        })
}

演示:https ://github.com/SheetJS/sheetjs/blob/9866dfc010338394e4cfcd33a9fbc15dae017ee5/demos/react/react-native.js

标签: javascriptfunctional-programming

解决方案


查看注释块中的标题:

// react-native-fetch-blob
/*
import RNFetchBlob from 'react-native-fetch-blob';
const { writeFile, readFile, dirs:{ DocumentDir } } = RNFetchBlob.fs;
const DDP = DocumentDir + "/";
const input = res => res.map(x => String.fromCharCode(x)).join("");
const output = str => str.split("").map(x => x.charCodeAt(0));
*/

在您的情况下inputoutput没有必要,但是当您删除react-native-fetch-blob.

此处的功能是为两个演示提供一个代码。


推荐阅读