@lessworkjs/optional

Optionally load object parameters

Stats

StarsIssuesVersionUpdatedCreatedSize
@lessworkjs/optional
711.0.13 years ago3 years agoMinified + gzip package size for @lessworkjs/optional in KB

Readme

Coverage Status Build Status

Optional

Optionally load object parameters. Return the parameter or null.

Idea based off the Laravel optional helper.

Install

npm i @lessworkjs/optional

Example

const optional = require('@lessworkjs/optional');

const users = [{
  name: 'Jane',
}, {
  name: 'John',
  address: {
    street: '101 Main St.',
  },
}];


console.log(optional(users[0].address).street);
// null

console.log(optional(users[1].address).street);
// 101 Main St.

You can also use an optional callback:

const results = optional(users[0], user => ({
  name: user.name,
  address: {
    street: '202 Main St.',
  },
}));

console.log(results)
/**
 * {
      name: 'John',
      address: {
        street: '202 Main St.',
      },
    }
 */

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.