Socket
Socket
Sign inDemoInstall

plusone

Package Overview
Dependencies
0
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    plusone

A function to increment a value that treats undefined as 0, useful for hash tables of counts.


Version published
Weekly downloads
7
increased by133.33%
Maintainers
1
Created
Weekly downloads
 

Readme

Source

plusone

plusone

plusone is a simple function that will return the increment of a value. What makes it special? It will increment undefined to be 1 (null already increments to 1 just fine).

Why would you need that? Because of this:

var obj = {
    bar: 'foo'
};

// this results in undefined + 1 === NaN
obj.foo = obj.foo + 1;

// this doesn't work right if obj.bar is a truthy string
obj.bar = obj.bar ? obj.bar + 1 : 1;

// this works as expected, but is long and not that readable
obj.baz = typeof obj.baz !== 'undefined' ? obj.baz + 1 : 1;

// this works as we expect, and reads pretty nicely, see? :)
obj.count = plusone( obj.count );

Installation

npm install plusone --save

Usage

var plusone = require( 'plusone' );

var i = 0;
i = plusone( i );

var obj = {};
obj.count = plusone( obj.count );

Contributing

Pull requests are very welcome! Just make sure your code:

  1. Passes jshint given the included .jshintrc

  2. Is beautified using jsbeautifier and the included .jsbeautifyrc

  3. Has tests and passes them.

CHANGELOG

0.0.3

  • REAME example fixed

0.0.2

  • Documentation updates
  • A couple of more tests

0.0.1

  • Initial release.

Keywords

FAQs

Last updated on 16 Sep 2015

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