All Projects → caiogondim → tubo.js

caiogondim / tubo.js

Licence: MIT license
🏄 Your functional (sync/async) pipe | operator

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to tubo.js

Ng Brazil
Commons and utils in angular for brazillian apps ( pipes / validators / directives / masks )
Stars: ✭ 92 (+26.03%)
Mutual labels:  pipe
Pipe
🎷 B3log 分布式社区的 Go 博客端节点系统,欢迎加入下一代社区网络。B3log distributed community blog-end node, welcome to join the next generation community network.
Stars: ✭ 169 (+131.51%)
Mutual labels:  pipe
Termsql
Convert text from a file or from stdin into SQL table and query it instantly. Uses sqlite as backend. The idea is to make SQL into a tool on the command line or in scripts.
Stars: ✭ 230 (+215.07%)
Mutual labels:  pipe
Lessmd
A small markdown viewer/converter for unix terminal.
Stars: ✭ 122 (+67.12%)
Mutual labels:  pipe
Onhold
🔊 Play sounds while and after shell jobs complete
Stars: ✭ 146 (+100%)
Mutual labels:  pipe
Ppipe
pipes values through functions, an alternative to using the proposed pipe operator ( |> ) for ES
Stars: ✭ 192 (+163.01%)
Mutual labels:  pipe
Irccat
cat to IRC
Stars: ✭ 91 (+24.66%)
Mutual labels:  pipe
ngx-timeago
⏰ Live updating timestamps in Angular 6+
Stars: ✭ 70 (-4.11%)
Mutual labels:  pipe
Fluids
Fluid dynamics component of Chemical Engineering Design Library (ChEDL)
Stars: ✭ 154 (+110.96%)
Mutual labels:  pipe
Ngx Order Pipe
▼ Angular 5+ orderBy pipe
Stars: ✭ 224 (+206.85%)
Mutual labels:  pipe
Ngx Filter Pipe
𝗩 Angular 5+ pipeline for array filtering.
Stars: ✭ 129 (+76.71%)
Mutual labels:  pipe
Pipetools
Functional plumbing for Python
Stars: ✭ 143 (+95.89%)
Mutual labels:  pipe
Lightweightrunloop A Reactor Style Nsrunloop
NSRunLoop Reactor Style Implementation: Using BSD kqueue implements iOS/Mac NSRunLoop and RunLoop-Relative Foundation such as perform selector(or delay some times) on other thread , Timer, URLConnection ,LWStream(LWInputStream、LWOutputStream) , LWPort(LWSocketPort) etc.
Stars: ✭ 196 (+168.49%)
Mutual labels:  pipe
Sspipe
Simple Smart Pipe: python productivity-tool for rapid data manipulation
Stars: ✭ 96 (+31.51%)
Mutual labels:  pipe
pipe
Functional Pipeline in Go
Stars: ✭ 30 (-58.9%)
Mutual labels:  pipe
Ok jose
Pipe elixir functions that match ok/error tuples or custom patterns.
Stars: ✭ 91 (+24.66%)
Mutual labels:  pipe
Pipeline.rs
☔️ => ⛅️ => ☀️
Stars: ✭ 188 (+157.53%)
Mutual labels:  pipe
ploot
Plot streaming data from stdin to a tty terminal
Stars: ✭ 54 (-26.03%)
Mutual labels:  pipe
pipeffmpeg
A frontend for ffmpeg using only pipes, not under GPL, but under BSD license.
Stars: ✭ 56 (-23.29%)
Mutual labels:  pipe
Lwrb
Lightweight generic ring buffer manager library
Stars: ✭ 215 (+194.52%)
Mutual labels:  pipe

tubo

 Travis CI codecov

Tubo 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.

Installation

npm install tubo --save

Usage

Sync

function double(x) {
  return x * 2
}

function square(x) {
  return x * x
}

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

Async

try {
  const result = await tubo(
    bookingDetails.userId,
    fetchUserById, // async
    JSON.parse
  )
  console.log(result)
} catch (error) {
  console.error(error)
}

Async with Promise

tubo(
  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.

Examples

Validation (sync and async)

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

// After
function validateEmail(email) {
  return tubo(
    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 tubo(
    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 = tubo(
  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 tubo(
  Object.assign(attrs, { parent_id: parentId, status: 'draft' }),
  Event.create
)

Reference and credits


caiogondim.com  ·  GitHub @caiogondim  ·  Twitter @caio_gondim

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].