@caiogondim/pipeline

中文文档
1.0.6发布于 2016/10/18MITCommonJS零依赖近 30 天下载 1830

Your functional (sync/async) pipe | operator

此软件包已被标记为弃用

This package was renamed to *tubo*

原始使用文档
文档目录11 节
  1. Usage
  2. Sync
  3. Async
  4. Mixed
  5. Installation
  6. Examples
  7. Validation (sync and async)
  8. Straight-forward Cases
  9. Usage with const
  10. Functional Updates
  11. Reference and credits

pipeline.js

Travis CI codecov

Pipeline works like the pipe operator more common in functional programming languages, like Elixir.

This lib supports sync and async arguments.

If all arguments are sync functions or literal, the pipeline will run as a normal function returning the last value computed. In case one of the arguments is a function that returns a promise or a promise instance (then-able object), a promise will be returned at the end.

Usage

Sync

function double(x) {
  return x * 2
}

function square(x) {
  return x * x
}

var output = pipeline(
  2,
  double,
  square
)
console.log(output) // => 16

Async

pipeline(
  bookingDetails.userId,
  fetchUserById, // async
  JSON.parse
)
  .then(function(result) {
    console.log(result)
  })
  .catch(function(error) {
    console.log(error)
  })

Mixed

It is also possible to mix sync and async arguments. Whenever a function that generates promises or a promise intance is found, the lib switchs to async mode and will return a Promise.

Installation

To use the library, install it through npm

npm install @caiogondim/pipeline --save

To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: Browserify, Webmake or Webpack.

Examples

Validation (sync and async)

// Before
function validateEmail(email) {
  email = trim(email)
  validateEmailTld(email)
  validateEmailFormat(email)
}

// After
function validateEmail(email) {
  return pipeline(
    email,
    trim,
    validateEmailTld,
    validateEmailFormat
  )
}

try {
  validateEmail('[email protected]')
} catch (error) {
  console.log(error)
}

Straight-forward Cases

// Before
if (cache && localStorage.getItem(endpoint)) {
  return m.prop( JSON.parse( localStorage.get(endpoint) ) )
}

// After
if (cache && localStorage.getItem(endpoint)) {
  return pipeline(
    localStorage.get(endpoint),
    JSON.parse,
    m.prop
  )
}

Usage with const

// Before
let items = base64ToJSON(response.data.content)
items = Array.isArray(items) ? items : [items]

// After
const items = pipeline(
  base64ToJSON(response.data.content),
  x => Array.isArray(x) ? x : [x]
)

Functional Updates

// Before
return Event.create(
  Object.assign(attrs, { parent_id: parentId, status: 'draft' })
)

// After
return pipeline(
  Object.assign(attrs, { parent_id: parentId, status: 'draft' }),
  Event.create
)

Reference and credits


caiogondim.com  ·  GitHub @caiogondim  ·  Twitter @caio_gondim

版本与发布记录

@caiogondim/pipeline 共发布 7 个版本,首次发布于 2016/09/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.6latest2016/10/1810 年前
1.0.52016/10/1810 年前
1.0.42016/09/2510 年前
1.0.32016/09/2510 年前
1.0.22016/09/2510 年前
1.0.12016/09/2310 年前

关于 @caiogondim/pipeline 的常见问题

@caiogondim/pipeline 是什么?

Your functional (sync/async) pipe | operator注意:该包已被作者标记为弃用——This package was renamed to *tubo*

如何安装 @caiogondim/pipeline?

在项目目录运行 npm install @caiogondim/pipeline,或使用 pnpm add / yarn add / bun add。

@caiogondim/pipeline 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/caiogondim__pipeline 是否存在。

@caiogondim/pipeline 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@caiogondim/pipeline 可以商用吗?

@caiogondim/pipeline 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@caiogondim/pipeline 的下载量有多少?

根据 npm 官方下载统计,@caiogondim/pipeline 在 2026/10/09 – 2026/11/07 这 30 天内被下载 1,830 次,日均约 61 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@caiogondim/pipeline 还在维护吗?

最新版本 1.0.6 发布于 2016/10/18(10 年前);至今共发布 7 个版本,其中过去 12 个月发布了 0 个;首次发布于 2016/09/23。作者已将其标记为弃用,新项目建议寻找替代方案。