npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

intval

v2.0.0

Published

Returns sensible integer value of a given variable. Unlike parseInt it will never return NaN and you can set a default value to be returned in case the variable is undefined! Hence you can write even cleaner code. So if it's supposed to be an integer, cas

Downloads

19

Readme

Intval

License NPM Package Version GitHub top language Size Last Commit Workflow CI

Intval will always return the right integer value of a given variable. For instance:

intval("1e10") === 10_000_000_000 but parseInt("1e10") === 1

intval("3.125e7") === 31_250_000 but parseInt("3.125e7") === 3

intval(true) === 1 and intval(false) === 0

whereas parseInt would have returned NaN in both cases but as you know:

parseInt(true) !== NaN !== parseInt(false) though it isNaN() ;).

Unlike parseInt, intval will never return NaN. You can provide a default value to be returned, in case the variable is undefined, otherwise it will return 0. Hence intval eliminates the needs to check against NaN and it helps you write even cleaner code.

Syntax:

intval(someValue)

intval(someValue, base)

intval(someValue, base, defaultValue)

This package is inspired by the PHP function intval, but this package is consistent in a javaScript way: for example in PHP

<?php intval("42", 8) === 34; ?> but <?php intval(42, 8) === 42; ?>

whereas this package returns 34 in both cases.

So if it's supposed to be an integer, cast it with intval.

Installation

$ npm i intval

Usage

Require CommonJS (default)
const intval = require("intval");
Import ES-Module (default)
import intval from "intval";
Import ES-Module (named)
import { intval } from "intval";

Use in Code (more examples below)

let intValue = intval(someValue);
With base/radix

intval(someValue, base)

let intValue = intval(someValue, 2);
With default value for undefined variables

intval(someValue, base, defaulValue)

let intValue = intval(someValue, 10, 42);

is the same as

let intValue = typeof someValue != "undefined" ? intval(someValue) : 42;

Even the default value will be type casted but the base/radix has no effect on it i.e. the base is always 10 for the default value. So, the following will return 42:

let myValue = intval(undefinedVariable, 16, "42");

// myValue === 66 --> false
// myValue === "42" --> false
// myValue === 42 --> true

Some examples

intval() === 0; // parseInt would have returned NaN
intval(null) === 0; // parseInt would have returned NaN
intval("") === 0; // parseInt would have returned NaN
intval("1e10") === 10_000_000_000; // parseInt would have returned 1
intval(1e10) === 10_000_000_000;
intval(true) === 1; // parseInt would have returned NaN
intval(false) === 0; // parseInt would have returned NaN
intval(42) === 42;
intval(4.2) === 4;
intval("42") === 42;
intval("+42") === 42;
intval("-42") === -42;
intval(042) === 34;
intval("042") === 42;
intval(0x1a) === 26;
intval("0x1A") === 26;
intval(42000000) === 42000000;
intval(420000000000000000000) === 420000000000000000000;
intval("420000000000000000000") === 420000000000000000000;
intval([]) === 0;
(intval(["22foo", "bar"]) === intval("22foo")) === 22; // same as parseInt, returns intval of the first array element. But in php intval(["22foo", "bar"]) === 1
intval(123_456) === 123456;
intval("123_456") === 123;

Pass in a base/radix as a second argument - just like with parseInt

intval(42, 8) === 34;
intval("42", 8) === 34;

intval(1011, 2) === 11;

intval("1g51", 16) === 1;
intval("1f51", 16) === 8017;

Pass in a default value in case the variable is undefined

As described above the base has no effect on the default value.

let someValue; // undefined
intval(someValue, 10, 42) === 42;
intval(someValue, 8, 42) === 42;
intval(someValue, 8, "42") === 42;

intval("1g51", 16, 42) === 1;
intval(someValue, 16, 42) === 42;

Testing (jest)

npm test

License

See LICENSE.

Copyright

Copyright © 2022. Kossi D. T. Saka.