@zerodep/to-integer
v2.0.12
Published
A utility to convert a value to an integer
Downloads
95
Readme
@zerodep/to-integer
A utility to convert values to an integer. Invalid values will cause a ZeroDepError
to be thrown.
Full documentation is available at the zerodep.app page.
Examples
All @zerodep packages support both ESM and CJS formats, each complete with Typescript typings.
// ESM
import { toNumber } from '@zerodep/to-integer';
// CJS
const { toNumber } = require('@zerodep/to-integer');
// BigInts
toInteger(8675309n); // 8675309
toInteger(42n); // 42
toInteger(0n); // 0
toInteger(-0n); // 0
toInteger(-42n); // -42
toInteger(-8675309n); // -8675309
// Booleans
toInteger(true); // 1
toInteger(false); // 0
// Dates
toInteger(new Date('2022-04-22T10:30:00.000Z')); // 1650623400000
toInteger(new Date('2099-12-31')); // 4102358400000
// Floats
toInteger(3.14); // 3
toInteger(0.0); // 0
toInteger(-0.0); // -0
toInteger(-171.3); // -171
toInteger(Math.E); // 2
toInteger(Math.PI); // 3
toInteger(Number.MIN_VALUE); // 0
// Numbers
toInteger(Number.POSITIVE_INFINITY); // throws ZeroDepError: Cannot convert to integer
toInteger(Number.MAX_SAFE_INTEGER); // 9007199254740991
toInteger(Number.MAX_VALUE); // 1.7976931348623157e+308
toInteger(3e8); // 300000000
toInteger(42); // 42
toInteger(1); // 1
toInteger(0); // 0
toInteger(-0); // -0
toInteger(-1); // -1
toInteger(-42); // -42
toInteger(-3e8); // -300000000
toInteger(Number.MIN_SAFE_INTEGER); // -9007199254740991
toInteger(Number.NEGATIVE_INFINITY); // throws ZeroDepError: Cannot convert to integer
toInteger(Number.NaN); // throws ZeroDepError: Cannot convert to integer
// Strings
toInteger('42'); // 24
toInteger('3e8'); // 300000000
toInteger('8,675,309'); // 8675309 <-- thousand separators are commas
toInteger('8.675.309,123'); // 8675309 <-- thousand separators are decimal points
toInteger(''); // 0
toInteger('a longer string'); // false
toInteger('1000n'); // 1000
toInteger('3e8'); // 300000000
toInteger('42'); // 42
toInteger('3.14'); // 3
toInteger('0'); // 0
toInteger('-0'); // -0
toInteger('-3.14'); // -3
toInteger('-42'); // -42
toInteger('-3e8'); // -300000000
toInteger('-1000n'); // -1000
// Other - anything that is not a number, bigint, string, boolean or Date
toInteger(null); // throws ZeroDepError: Cannot convert to integer <-- CAUTION
toInteger(undefined); // throws ZeroDepError: Cannot convert to integer <-- CAUTION
toInteger('asdf'); // throws ZeroDepError: Cannot convert to integer
toInteger({ not: 'a number' }); // throws ZeroDepError: Cannot convert to integer
ZeroDep Advantages
- Zero npm dependencies - completely eliminates all risk of supply-chain attacks, decreases node_modules folder size
- ESM & CJS - has both ecmascript modules and common javascript exports
- Tree Shakable - built to be fully tree shakable ensuring your packages are the smallest possible size
- Fully typed - typescript definitions are provided for every package for a better developer experience
- Semantically named - package and method names are easy to grok, remember, use, and read
- Documented - actually useful documentation with examples at zerodep.app
- Intelligently Packaged - multiple npm packages of different sizes available allowing a menu or a-la-carte composition of capabilities
- 100% Tested - all methods and packages are fully unit tested
- Predictably Versioned - semantically versioned for peace-of-mind upgrading, this includes changelogs
- MIT Licensed - permissively licensed for maximum usability