如何解决打字稿中的链接功能
我有一些应该应用于某些字符串的格式化功能:
const limitSize = (limit: number): ((str: string) => string) => {
return (str: string) => str.substring(0,limit)
}
const replaceNewLine = (replaceWith: string): ((str: string) => string) => {
return (str: string) => str.replace(/\n/g,replaceWith)
}
它们都返回可以应用于字符串的函数
如何将它们链接在一起,以便结果还返回可以应用于字符串的函数?
我是否缺少lodash实用程序?
解决方法
我认为您需要Lodash的flow功能或Ramda的pipe
function square(n) {
return n * n;
}
var addSquare = _.flow([_.add,square]);
addSquare(1,2);
// => 9
,
只需创建一个新的函数定义,如下所示:
const limitReplace = (limit: number,replaceWith: string): ((str: string) => string) => {
return str => replaceNewLine(replaceWith)(limitSize(limit)(str));
}
用作:
const input = "Hel\nlo W\norld\n";
const limitSixReplaceSpace = limitReplace(6," ");
const result = limitSixReplaceSpace(input); // Hel lo
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。