[Typescript] Pipe function

function pipe<A, B>(fn: (a: A) => B) {
  function run(a: A) {
    return fn(a)
  }

  run.pipe = <C, >(fn2: (b:  B) => C) => pipe((a: A) => fn2(fn(a)))

  return run;
}

const stringToDateAndTime = pipe(Date.parse)
  .pipe(n => new Date())
  .pipe(d => d.toISOString())
  .pipe(s => s.split("T"))
  .pipe(a => ({date: a[0], time: a[1]}))

const result = stringToDateAndTime("Jan 1, 2024")
console.log(result)

/*
{
  "date": "2024-08-02",
  "time": "06:50:47.398Z"
} 
*/

 

posted @ 2024-08-02 14:51  Zhentiw  阅读(6)  评论(0编辑  收藏  举报