javascriptshould.js

assert that an object is an integer number


Using should.js, I am currently doing:

ordinal.should.be.a.Number().and.equal(Math.floor(ordinal)); 

… is there a more concise way to require that a given object be an integer?


Solution

  • You can use Number.isSafeInteger (to test for 64-bit integers) and Number.isInteger. To use with Should.js:

    should.ok(Number.isSafeInteger(ordinal));
    

    I generally recommend using Number.isSafeInteger...unless you know you're dealling with potentially really large integer values that have to be stored as floating-point.