zoukankan      html  css  js  c++  java
  • es6 nodejs compose

    const compose = (...fns) => {
      let len = fns.length;
      let fn_index = len - 1;
      let fn_result;
    
      function invoke(...args) {
        fn_result = len ? fns[fn_index].apply(this, args) : args[0]
        if(fn_index <= 0){
          fn_index = len - 1;
          return fn_result;
        }else{
          fn_index--;
          return invoke.call(null, fn_result);
        }
      }
    
      return invoke;
    }
    
    const compose2 = (...fns) => {
      const length = fns.length;
      let index = length;
      while(index--){
        if(typeof(fns[index]) !== 'function'){
          throw new TypeError(`fns ${index} not a function.`)
        }
      }
    
      return (...args) => {
        let index = 0;
        let result = length ? fns[index].apply(this, args) : args[0]
        while(++index < length) {
          result = fns[index].call(this, result)
        }
        return result;
      }
    }
    
    const compose2Right = (...fns) => {
      return compose2.apply(null, fns.reverse())
    }
    
    const upperName = compose(str => str.toUpperCase(), (firstName, lastName) => firstName + ',' + lastName)
    const name = upperName('flex', 'react')
    console.log(name)
    
    const upperName2 = compose2((firstName, lastName) => firstName + ',' + lastName, str => str.toUpperCase())
    const name2 = upperName2('flex', 'react')
    console.log(name2)
    
    const upperName3 = compose2Right(str => str.toUpperCase(), (firstName, lastName) => firstName + ',' + lastName)
    const name3 = upperName3('flex', 'react')
    console.log(name3)
    
    const upperName4 = compose2()
    const name4 = upperName4('a','b')
    console.log(name4)
    
    const upperName5 = compose()
    const name5 = upperName5('a','b')
    console.log(name5)
    
    
  • 相关阅读:
    UNIX时间戳/日期转换
    慎用date获取未来时间
    lnmp集成开发环境安装pdo_dblib扩展
    elementary OS下netbeans中文乱码的问题
    一个轻量级javascript框架的设计模式
    debian清空消息通知
    一道数组方面的笔试题
    模拟post提交
    P2970 [USACO09DEC]自私的放牧Selfish Grazing
    P1063 能量项链
  • 原文地址:https://www.cnblogs.com/byxxw/p/9584747.html
Copyright © 2011-2022 走看看