[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 @   Zhentiw  阅读(9)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2021-08-02 [SAA + SAP] 16. RDS
2021-08-02 [SAA + SAP] 15. ElastiCache
2021-08-02 [SAA + SAP] 14. CloudFront & AWS Global Accelerator
2018-08-02 [Vue @Component] Extend Vue Components in TypeScript
2017-08-02 [React] Style the body element with styled-components and "injectGlobal"
2017-08-02 [React] Style a React component with styled-components
2017-08-02 [D3] Create DOM Elements with D3 v4
点击右上角即可分享
微信分享提示