Socket
Socket
Sign inDemoInstall

flat-options

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    flat-options

One-level options with default values and validation


Version published
Weekly downloads
586
increased by28.51%
Maintainers
1
Install size
7.60 kB
Created
Weekly downloads
 

Readme

Source

flat-options

Build Status npm license

One-level options with default values and validation

Utility function to merge simple one-level options with default values and perform validation.

Comparison to Object.assign

Benefits over Object.assign are:

  • exclude of undefined values (useful for conditional options):

    const defaults = {foo: 'bar'};
    const options = {foo: undefined};
    
    Object.assign({}, defaults, options); // -> {foo: undefined}
    // vs
    flatOptions(options, defaults); // -> {foo: 'bar'}
    
  • validation of options keys:

    const defaults = {foo: 'bar'};
    const options = {unknown: 'baz'};
    
    Object.assign({}, defaults, options); // -> {foo: 'bar', unknown: 'baz'}
    // vs
    flatOptions(options, defaults); // -> throws error "Unknown option"!
    

Comparison to other packages

Benefits over existing defaults, lodash.defaults and object.defaults are:

  • auto-validation of options keys
  • zero dependencies

Note that this package is only for one-level options, for nested ones please use alternative packages

Installation

npm install --save flat-options

Usage

import flatOptions from 'flat-options';

const defaults = {
  a: 1,
  b: false
};

class Foo {
  constructor(options) {
    this._options = flatOptions(options, defaults);
  }
}

const foo = new Foo({a: 2}); // foo._options will be {a: 2, b: false} 

License

MIT @ Vitaliy Potapov

Keywords

FAQs

Last updated on 11 Oct 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc