lives

A variable lives here

Usage no npm install needed!

<script type="module">
  import lives from 'https://cdn.skypack.dev/lives';
</script>

README

lives

Build Status Coverage Status Minified Size

A variable lives here

Save yourself from messy & unreliable nested object checking.

  • Tiny w/ zero dependencies (500B minified)
  • 100% test coverage
  • Works in Node.js and legacy browser environments
  • Includes useful helper functions for different use cases
npm i --save lives

Usage

Lives(attempt)

const lives = require('lives');

const hello = {
  a: {
    b: {
      c: 'Hello'
    }
  }
};

const missing = {
  a: null
};

if (lives(() => hello.a.b.c)) {
  console.log(hello.a.b.c, 'World!'); // Hello World!
}

Lives.key(target, key)

if (lives.key(hello, 'a.b.c')) {
  console.log(hello.a.b.c, 'World!'); // Hello World!
}

Lives.not(attempt)

if (lives.not(() => missing.a.b.c)) {
  throw new Error('Variable is missing!');
}

Lives.get(attempt)

console.log(lives.get(() => hello.a.b.c), 'World!'); // Hello World!

lives.get(() => missing.a.b.c); // undefined

Lives.or(attempt, fallback)

console.log(lives.or(() => missing.a.b.c, 'Hello'), 'World!'); // Hello World!

Lives.is(attempt, type)

lives.is(() => hello.a.b.c, 'string'); // True

lives.is(() => hello.a.b.c, 'boolean'); // False

lives.is(() => missing.a.b.c, 'string'); // False

lives.is(() => missing.a.b.c, 'undefined'); // True