先写一个栗子 : 将字符串 “NEREV SAY DIE” 转换成 “never-say-die”
// 观察案例题目 解题思路 : 先将字符串用‘ ’分割 -> 转小写 -> 用‘-’组合
const _ = require('lodash');
// _.split(str , sep) 需要两个参数 sep: 分隔符 ; str: 处理的字符串
// 但是组合函数,只能是一个数据流, 所以需要处理_.join
const mySplit = _.curry((sep, str) => _.split(str, sep));
// _.toLower 只需要一个参数
// _.join(array , sep) 也需要两个参数 sep: 合并符 ; str: 处理的字符串 所以需要处理
const myJoin = _.curry((sep, array) => _.join(array, sep));
const formatFn = _.flowRight(myJoin('-'), _.toLower, mySplit(' '));
console.log(formatFn('NEVER SAY DIE'))
// 运行结果 n-e-v-e-r-,-s-a-y-,-d-i-e
// 很明显 上面的运行结果并不是我们想要的,那是哪里的问题?
// 这时候 需要写一个调试函数
// 根据组合函数的特点: mySplist 执行结束之后,会把结果传递给 toLower
// 所以可以在 mySplit 和 toLower之间添加log函数
const logFn = value => {
console.log(value);
return value;
}
const handleFnLog = _.flowRight(myJoin('-') , _.toLower , logFn , mySplit(' '));
console.log(handleFnLog("NEVER SAY DIE"));
// 运行结果
// [ 'NEVER', 'SAY', 'DIE' ]
// n-e-v-e-r-,-s-a-y-,-d-i-e
// 继续调试
const handleFnLog2 = _.flowRight(myJoin('-') , logFn , _.toLower , logFn , mySplit(' '));
console.log(handleFnLog2("NEVER SAY DIE"));
// 运行结果
// [ 'NEVER', 'SAY', 'DIE' ]
// never,say,die
// n-e-v-e-r-,-s-a-y-,-d-i-e
// => 直到找到问题所在 myJoin之前出了问题
const format = _.flowRight(myJoin('-') ,mySplit(','), logFn , _.toLower , logFn , mySplit(' '));
console.log(format("NEVER SAY DIE"));
// 运行结果
// [ 'NEVER', 'SAY', 'DIE' ]
// never,say,die
// never-say-die