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)
    
    
  • 相关阅读:
    LeetCode522. 最长特殊序列 II
    docker activiti部署到Linux环境,流程图乱码
    linux docker 命令
    linux 安装docker
    JSON,JSONOBJECT,JSONARRAY 互转
    Python和java 的区别笔记(未完成)
    程序员常读书单整理,附下载地址
    javaweb同一个项目打包两次放在同一个tomcat下
    SSM项目集成Redis
    Chrome浏览器崩溃
  • 原文地址:https://www.cnblogs.com/byxxw/p/9584747.html
Copyright © 2011-2022 走看看