npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

Iโ€™ve always been into building performant and accessible sites, but lately Iโ€™ve been taking it extremely seriously. So much so that Iโ€™ve been building a tool to help me optimize and monitor the sites that I build to make sure that Iโ€™m making an attempt to offer the best experience to those who visit them. If youโ€™re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, ๐Ÿ‘‹, Iโ€™m Ryan Hefnerย  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If youโ€™re interested in other things Iโ€™m working on, follow me on Twitter or check out the open source projects Iโ€™ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soonโ€“ish.

Open Software & Tools

This site wouldnโ€™t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you ๐Ÿ™

ยฉ 2024 โ€“ย Pkg Stats / Ryan Hefner

@inventistudio/using-js

v1.2.1

Published

Minimal, easy to use chaining lib ๐Ÿš€

Downloads

2

Readme

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()