2.0.0 • Published 1 year ago

@zerodep/to_integer v2.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

@zerodep/to_integer

minified size minified+gzipped size tree shaking language types

coverage last commit vulnerabilities

A configurable utility to convert a value (string, boolean, date, or BigInt) to an integer.

tl;dr

A short explanation / quick reference:

import { toInteger } from '@zerodep/to_integer';

// uses the default configuration options
toInteger('3.1415'); // 3
toInteger('3e8'); // 300000000
toInteger(Symbol()); // throws ZeroDepError

Table of Contents

Install

This utility is available from multiple @zerodep packages, enabling developers to select the most appropriately sized package (for both kb and capability) for different use cases. We believe one size does not fit all or most. See @zerodep/app and @zerodep/to.

// all @zerodep features, capabilities and utilities
npm install @zerodep/app

// all @zerodep "to" utilities
npm install @zerodep/to

// only the to_integer package
npm install @zerodep/to_integer

Of course, you may use yarn, pnpm, or the package manager of your choice. Only npm examples are shown for brevity.

This package may also be found on both jsDelivr and unpkg in UMD, ESM and CJS formats.

How to Use

This package exports the following:

  • Functions
    • toInteger - a converter that uses the default configuration options (suitable for most)
    • toIntegerHof - a higher-order function that may be configured and returns a converter function based on the configurations
  • Interface
    • toIntegerOptions - a typescript interface of the options that may be set in the HOF
  • Error types
    • ZeroDepError - the error class all ZeroDep packages extend from, is an instance of the base Error object

Signature

Typescript declarations:

// using default configuration options
declare const toInteger: (value: unknown) => number;

Examples

Using Default Configuration Options

// import from the most appropriate @zerodep package for your needs / specific use case (see the Install section above)
import { toInteger } from '@zerodep/to_integer';

// strings
toInteger(''); // 0
toInteger('42'); // 42
toInteger('a string'); // throws ZeroDepError
toInteger(NaN); // throws ZeroDepError

// integers
toInteger(42); // 42
toInteger(3e8); // 300000000

// floats
toInteger(-273.15); // -273
toInteger(Math.PI); // 3

// number-ish
toInteger(Number.POSITIVE_INFINITY); // throws ZeroDepError
toInteger(NaN); // throws ZeroDepError

// bigints
toInteger(8675309n); // 8675309

// object literals
toInteger({}); // throws ZeroDepError
toInteger({ a: 'one', b: 'two' }); // throws ZeroDepError

// arrays
toInteger([]); // throws ZeroDepError
toInteger([1, 2, 3]); // throws ZeroDepError
toInteger(['a', 'b', 'c']); // throws ZeroDepError

// booleans
toInteger(true); // 1
toInteger(false); // 0

// other
toInteger(Date('2022-04-22T10:30:00.000Z')); // 1650623400000
toInteger(/^$\d{7}/g); // throws ZeroDepError
toInteger(new Set()); // throws ZeroDepError
toInteger(new Set([1, 2, 3])); // throws ZeroDepError
toInteger(new Map()); // // throws ZeroDepError
toInteger(new Map([['a', 1]])); // // throws ZeroDepError
toInteger(new Symbol()); // throws ZeroDepError
toInteger(new Error()); // throws ZeroDepError
toInteger(() => {}); // throws ZeroDepError

// nothing
toInteger(null); // // throws ZeroDepError
toInteger(undefined); // // throws ZeroDepError

Related Packages

The following @zerodep packages may be helpful or more appropriate for your specific case:

Advantages of @zerodep Packages

We help make source code more readable, more secure, faster to craft, less likely to have hidden defects, and easier to maintain.

  • Zero npm dependencies - completely eliminates all risk of supply-chain attacks, decreases node_modules folder 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 and helpful tips
  • 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
  • ESM & CJS - has both ecmascript modules and common javascript exports, both are fully tree-shakable
  • CDN Available - available on fast content delivery networks in UMD, CJS and ESM formats
  • FP Inspired - gently opinionated to encourage functional programming style for cleaner and more maintainable software
  • Predictably Versioned - semantically versioned for peace-of-mind upgrading, this includes changelogs
  • MIT Licensed - permissively licensed for maximum usability

Support

All @zerodep packages are built for the ES2020 specification. Should you need to support older environments you will need to add appropriate polyfills. All packages are tested on the following platforms/browsers:

Browsers

  • Chrome - last 2 major versions
  • Firefox - last 2 major versions
  • Safari - last 2 major versions
  • Edge - last 2 major versions
  • Android - last 2 major versions
  • iOS - last 2 major versions

Node

  • v18.x - Hydrogen LTS
  • v16.x - Gallium LTS

It is likely the package will work on other technologies and version, however development and testing effort is only spent on the above.

Semver

All @zerodep packages, including this one, adhere to Semantic Versioning practices:

  • major versions: correlates with breaking changes to one or more method signatures
  • minor versions: includes addition of new functionality or backwards-compatible software improvements
  • patch versions: are reserved for copy changes, documentation enhancements and bug fixes

The above said, a security best practice is to pin your software packages to specific versions and only upgrade to more recent releases after careful inspection of any CHANGELOG, release notes and all software changes.

Resources

License

MIT

2.0.0

1 year ago