es-abstract-to-integer

0.1.2 • Public • Published

ToInteger

A method to convert the argument to an integral numeric value. This method follows ECMAScript's specification for the 'ToInteger' abstract operation.

Currently, this module only supports the ES2017 (ES8) specification.

Installation Using npm

npm install es-abstract-to-integer

Example Usage

var toInteger = require('es-abstract-to-integer')
 
console.log(toInteger(-3.14))  // =>   -3
console.log(toInteger(3.14))   // =>    3
console.log(toInteger(42))     // =>   42
console.log(toInteger("9001")) // => 9001
console.log(toInteger(null))   // =>    0
console.log(toInteger(false))  // =>    0
console.log(toInteger(true))   // =>    1
 
console.log(toInteger("foo"))  // =>    0
console.log(toInteger({}))     // =>    0
console.log(toInteger([]))     // =>    0
 
var d = new Date(2015, 3, 14, 0, 0, 0, 0)
console.log(toInteger(d)) // => 1428984000000

Documentation

API

ToInteger ( argument )

The abstract operation ToInteger converts argument to an integral numeric value.

A Number value is returned.

A TypeError exception may be thrown for arguments that are Symbols or Objects that lack a valueOf method or a toString method capable of returning a non-object value.

argument

Type: *

The value to convert.

Related Projects

  • es-abstract: a single library for multiple ECMAScript abstract operations.

/es-abstract-to-integer/

    Package Sidebar

    Install

    npm i es-abstract-to-integer

    Weekly Downloads

    6

    Version

    0.1.2

    License

    BSD-3-Clause

    Last publish

    Collaborators

    • npetruzzelli