链式调用


function stir(meat: Array<any>): string {
  return meat.join(",");
}

function filling(slurry: ReturnType<typeof stir>): Array<any> {
  return slurry.split(",");
}


function packing(bottles: ReturnType<typeof filling>): Array<object> {
  let a: Array<object> = [];
  bottles.forEach((item) => {
    a.push({
      name: item,
    });
  });
  return a;
}

// packing(filling(stir(["猪肉", "猪肉", "猪肉", "羊肉"])));


function compose(...fns) {
  return function (num) {
    return fns.reduce(function (prevValue, currFn) {
      return prevValue;
    }, num);
  };
}
const pipeline = compose(stir, filling, packing); // 自左向右逐一执行
console.log(pipeline(["猪肉", "猪肉", "猪肉", "羊肉"]));


posted @ 2023-02-16 16:06  7c89  阅读(14)  评论(0)    收藏  举报