输油管.js
管道像管道操作器在功能编程中更为常见 语言,如埃克西尔语.
这个 lib 支持 同步 财务报告和审定财务报表 自动同步 参数.
如果所有参数都是同步函数或字面的,则管道将正常运行 函数返回最后计算值。 如果其中一个论点是 函数返回一个承诺或承诺实例(当时可以使用的对象),a 承诺会在最后归还.
使用量
同步
function double(x) {
return x * 2
}
function square(x) {
return x * x
}
var output = pipeline(
2,
double,
square
)
console.log(output) // => 16
自动同步
pipeline(
bookingDetails.userId,
fetchUserById, // async
JSON.parse
)
.then(function(result) {
console.log(result)
})
.catch(function(error) {
console.log(error)
})
混合
也可以混合同步和同步参数.
只要发现一个能产生承诺或承诺的功能,
lib 切换到 async 模式,然后返回 a Promise。 。 。 .
安装
要使用库,请通过 npm 安装
npm install @caiogondim/pipeline --save
要将其移植到浏览器或任何其他(非 CJS) 环境, 请使用您最喜欢的 CJS bundle. 没有最喜欢的吗? 尝试: 浏览器, (中文(简体) ), 网络制作 或者说 网络软件包。 。 。 .
实例
验证(同步和同步)
// 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)
}
直发案件
// 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
)
}
与 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]
)
功能更新
// 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
)
参考资料和贷项
- Ramda.js 烟斗
- 罗达什.js流
- 烟管.py
- 一些考试从 https://github.com/mindeavor/es-pipeline-operator/wiki/example-use-Cases 互联网档案馆的存檔,存档日期2013-12-21
cariogondim.com 互联网档案馆的存檔,存档日期2013-12-02 • 妇女 GitHub 图像 @caiogondim (英语) • 妇女 微博 @caio gondim (英语)