koalas

Koalas is a library for doing a coalesce in JavaScript

npm install koalas
1 downloads in the last day
15 downloads in the last week
41 downloads in the last month

koalas NPM version

Koalas is a library for doing a coalesce in JavaScript

Getting Started

To install the module, run the following in the command line:

npm i koalas --save

Use within your application with the following line of JavaScript:

var koalas = require('koalas');

Usage Examples

Simple Example

Given a list of values, find the first one that isn't null or undefined.

var value = koalas.coalesce('foo', 'bar', 'baz', null, undefined);

Simple Example 2

Use something similar to lodash by passing your arguments directly into koalas and calling value() to get the first non null/undefined value.

var value = koalas(null, undefined, 'foo', 'bar').value();

Simple Example 3 - Array Argument

Sometimes you want to use an array instead of passing all the arguments in.

var pages = ['index.html', 'index.htm', 'default.html', 'default.htm'];

var defaultPage = koalas.apply(null, pages).value();

Use Method Example

With the use method, pass in functions that will evaluate can evaluate each argument until a non null/undefined value is returned.

var filter = function (value) {
  if (value.indexOf('Brian') !== -1) {
    return value;
  }
  return null;
};

var foo = koalas('Jon Schlinkert', 'Brian Woodward', 'Joe Developer').use(filter).value();

Find Valid Path Example

This example returns the first path that exists.

var fs = require('fs');
var exists = function (file) {
  if (fs.existsSync(file)) {
    return file;
  }
};

var pages = ['index.html', 'index.htm', 'default.html', 'default.htm'];
var defaultPage = koalas.apply(null, pages).use(exists).value();

Complex Example 1

Find the first object that contains a specific property that is non null/undefined.

var prop = function (key) {
  return function (person) {
    if (person && person[key])
      return person;
  };
};

var people = [
  { first: 'Jon', last: 'Schlinkert' },
  { first: 'Brian', middle: 'G', last: 'Woodward' },
  { first: 'Joe', middle: 'S', last: 'Developer' }
];

var firstMiddle = koalas.apply(null, people).use(prop('middle')).value();

Complex Example 2

Find the first object that contains a method that returns a valid non null/undefined value.

var func = function (key) {
  return function (person) {
    if (person && person[key] && person[key]())
      return person;
  };
};

var people = [
  { age: function () { return this.dbo; }, first: 'Jon', last: 'Schlinkert' },
  { age: function () { return this.dbo; }, first: 'Brian', middle: 'G', last: 'Woodward', dob: new Date('1979 SEP 07') },
  { age: function () { return this.dbo; }, first: 'Joe', middle: 'S', last: 'Developer' }
];

var hasDOB = koalas.apply(null, people).use(func('age')).value();

Contributing

Find a bug? Have a feature request? Please create an Issue.

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality, and run docs in the command line to build the docs with Verb.

Pull requests are also encouraged, and if you find this project useful please consider "starring" it to show your support! Thanks!

Author

Brian Woodward

License

Copyright (c) 2014 Brian Woodward, contributors.


This file was generated by grunt-verb on March 26, 2014.

npm loves you