0

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?

Marcus Junius Brutus
  • 26,087
  • 41
  • 189
  • 331

1 Answers1

3

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.

Ethan Brown
  • 26,892
  • 4
  • 80
  • 92
  • That works. I know it is superfluous but why does `n.should.be.a.Number().and.assert(Number.isSafeInteger(n))` succeeds while `n.should.be.a.Number().and.should.ok(Number.isSafeInteger(n))` fails to complain when 3.2 is given? – Marcus Junius Brutus Nov 08 '16 at 22:05