node package manager


Coalesce for JavaScript

koalas NPM version

Coalesce for JavaScript


Install with npm:

npm i koalas --save-dev

Usage Examples

Get the first non-null value

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

Or, 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();

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


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

Get the first valid file path

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

Object with specified property

Find the first object that contains a specific property that is not 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();

Object with method

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


Brian Woodward


Copyright (c) 2014 Brian Woodward, contributors.
Released under the MIT license

This file was generated by verb-cli on September 29, 2014.