1.0.2 • Published 7 years ago

nullable-util v1.0.2

Weekly downloads
2
License
ISC
Repository
github
Last release
7 years ago

nullable-util Build Status

utility functions with Nullable

js-standard-style

usage example

const util = require('nullable-util')

const collection = [1, 2, 3, 4, 5]

util.first(collection)
// => Nullable.of(1)

util.first(collection, x => x > 3)
// => Nullable.of(4)

util.last(collection)
// => Nullable.of(5)

util.last(collection, x => x < 3)
// => Nullable.of(2)


const users = {
  1: {
    name: 'A'
    admin: false
  },
  2: {
    name: 'B',
    admin: true
  }
}

util.first(users)
// => Nullable.of(['1', {name: 'A', admin: false}])
// This returns a [key, value] pair as a 2-element Array.
// Note, Object property names are always strings.

util.first(users, user => user.admin)
// => Nullable.of(['2', {name: 'B', admin: true}])

first and last operate on Arrays and Array-likes (NodeList, etc.), plain Objects, ES6 collections (Set and Map, but not WeakSet or WeakMap which are not iterable).

first can also operate on generators, but not last since generators may be infinite.

See test suite for more examples.

installation

$ npm install nullable-util

running the tests

From package root:

$ npm install
$ npm test

license

ISC. (c) MMXVII jsdnxx. See LICENSE.md