Giter VIP home page Giter VIP logo

using-js's Introduction

UsingJS by InventiStudio

Minimal, easy to use chaining lib ๐Ÿš€

Getting started

Install:

  • Npm:

    npm install @inventistudio/using-js
  • Yarn:

    yarn add @inventistudio/using-js

Import:

  • ES5:

    var using = require('@inventistudio/using-js')
  • Yarn:

    import using from '@inventistudio/using-js'

Example

You can easily use it with your own methods or any functional library (e.g. RamdaJS)

import R from 'ramda'
import using from '@inventistudio/using-js'

// Data
const response = [
  { name: 'Zulu', age: 12,   role: 'admin' },
  { name: 'John', age: 20,   role: 'user'  },
  { name: 'Don',  age: null, role: 'owner' },
]

// Params
const onlyAdults = false
const sortBy     = 'role'

// ๐Ÿš€ Chain ๐Ÿš€
const persons = using(response)
  // Get only with age
  .do(R.filter(person => person.age))
  // Get only adluts if it's required (boolean condition example)
  .doIf(onlyAdults, R.filter(person => person.age >= 18))
  // Throw error unless persons array is not empty (functional condition example)
  .doUnless(persons => persons.length, () => { throw new Error('Empty array') })
  // Alow only sorting by age and name (default)
  .switch(sortBy, {
    age:     R.sortBy(R.prop('age')),
    default: R.sortBy(R.prop('name'))
  })
  // Get value
  .value()

console.log(persons)
// >
/*
[
  { name: 'John', age: 20, role: 'user' },
  { name: 'Zulu', age: 12, role: 'admin' },
]
*/

API

using(data : Any) : wrapper

It wraps data and returns object which contains below methods

using([1,2,4])
using({ name: 'Mike' })
using(10)
...you get it.

wrapper.value() : Any

Returns value.

using(1).value() // 1

wrapper.do(func : Function (data : Any -> wrapper))

It invokes func with data passed

// vanilla js
using([1,2,4])
  .do((arr) => arr.length)
  .value() // 3

// with ramda
using([1,2,4])
  .do(R.filter(n => n%2===0))
  .value() // [2,4]
...

wrapper.doIf(condition : Any, func : Function (data : Any -> Any)) : wrapper

It invokes func with data passed if condition is "truthy". Otherwise returns wrapper. If condition is function, it'll be invoked with data passed

// boolean condition
const onlyEven = true
using([1,2,4])
  .doIf(onlyEven, R.filter(n => n%2===0))
  .value() // [2,4] or [1,2,4] if onlyEven = false

// functional condition
using([1,2,4])
  .doIf((arr) => arr.length, R.filter(n => n%2===0))
  .value() // [2,4]
...

wrapper.doUnless(condition : Any, func : Function (data : Any -> data : Any)) : wrapper

It invokes func with data passed if condition is "falsy". Otherwise returns wrapper. If condition is function, it'll be invoked with data passed

// boolean condition
const withOdds = true
using([1,2,4])
  .doUnless(withOdds, R.filter(n => n%2===0))
  .value() // [1,2,4] or [1,2] if withOdds = false

wrapper.doIfElse(condition : Any, funcTruthy : Function (data : Any -> Any), funcFalsy : Function (data : Any -> Any)) : wrapper

It invokes funcTruthy with data passed if condition is "truthy". Otherwise it invokes funcFalsy. If condition is function, it'll be invoked with data passed

using(user)
  .doIfElse(hasPermission, fetchData, askForPermission)
  .value()
...

wrapper.switch([functionName : Any], Object<Any, Function (data : Any -> data : Any)>) : wrapper

It invokes object[functionName] with data passed. If it not exists, it tries to invoke object['default']. If function name is skipped, it uses data as functionName. It returns wrapper if there is no default case.

// boolean condition
const withOdds = true
using([1,2,4])
  .switch((arr) => `has${arr.length}Elements`, {
    has1Elements() {
      return 'Single'
    },
    has2Elemenets() {
      return 'Pair'
    }
    default() {
      return 'Group'
    }
  })
  .value() // 'Group'

wrapper.debug(logFunction : Function (Any) : Void) : wrapper

It calls passed function without mutating data

// with ramda
using([1,2,4])
  .do(R.filter(n => n%2===0))
  .debug(console.log)
  .value() // [2,4]

Asynchronous functions

using.async(value) allows you to use functions that returns promises. Speaking more specifically, if value is a promise, function will be invoked as data.then(func) instead of func(data).

const post = { title: 'Lorem ipsum...', content: '   test   ' }

const newPost = await using.async(post)
  .do(sanitizeContent) // result = sanitizeContent(post)
  .do(Post.create)     // result = Post.create(result) -- function returns Promise โš ๏ธ
  .do(mapWithAuthor)   // result = result.then(mapWithAuthor) โค๏ธ
  .value()

using-js's People

Contributors

piotrekfracek avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

rpdmkr

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.