Skip to content

Commit

Permalink
feat: safe-integer/coerce
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Apr 4, 2019
1 parent e52fc4d commit b8549c4
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,22 @@ ensureInteger(null); // Thrown TypeError: null is not a number

---

#### Safe Integer Number

##### `safe-integer/coerce`

Follows [`integer/coerce`](#integercoerce) but returns `null` in place of values which are beyond `Number.MIN_SAFE_INTEGER` and `Number.MAX_SAFE_INTEGER` range.

```javascript
const coerceToSafeInteger = require("type/safe-integer/coerce");

coerceToInteger("12.95"); // 12
coerceToInteger(9007199254740992); // null
coerceToInteger(null); // null
```

---

### Array

The JavaScript _Array_ instance
Expand Down
13 changes: 13 additions & 0 deletions safe-integer/coerce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
"use strict";

var coerceToInteger = require("../integer/coerce");

var MAX_SAFE_INTEGER = 9007199254740991, MIN_SAFE_INTEGER = -9007199254740991;

module.exports = function (value) {
value = coerceToInteger(value);
if (!value) return value;
if (value > MAX_SAFE_INTEGER) return null;
if (value < MIN_SAFE_INTEGER) return null;
return value;
};
6 changes: 6 additions & 0 deletions test/integer/coerce.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ describe("integer/coerce", function () {
it("Should coerce objects", function () {
assert.equal(coerceToInteger({ valueOf: function () { return 23; } }), 23);
});
it("Should coerce number beyond Number.MAX_SAFE_INTEGER", function () {
assert.equal(coerceToInteger(9007199254740992), 9007199254740992);
});
it("Should coerce number beyond Number.MIN_SAFE_INTEGER", function () {
assert.equal(coerceToInteger(-9007199254740992), -9007199254740992);
});

it("Should reject infinite number", function () {
assert.equal(coerceToInteger(Infinity), null);
Expand Down
49 changes: 49 additions & 0 deletions test/safe-integer/coerce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"use strict";

var assert = require("chai").assert
, coerceToSafeInteger = require("../../safe-integer/coerce");

describe("integer/coerce", function () {
it("Should coerce float to integer", function () {
assert.equal(coerceToSafeInteger(123.123), 123);
assert.equal(coerceToSafeInteger(123.823), 123);
assert.equal(coerceToSafeInteger(-123.123), -123);
assert.equal(coerceToSafeInteger(-123.823), -123);
});
it("Should coerce string", function () { assert.equal(coerceToSafeInteger("12.123"), 12); });
it("Should coerce booleans", function () { assert.equal(coerceToSafeInteger(true), 1); });
it("Should coerce number objects", function () {
assert.equal(coerceToSafeInteger(new Number(343)), 343);
});
it("Should coerce objects", function () {
assert.equal(coerceToSafeInteger({ valueOf: function () { return 23; } }), 23);
});
it("Should reject infinite number", function () {
assert.equal(coerceToSafeInteger(Infinity), null);
});
it("Should reject number beyond Number.MAX_SAFE_INTEGER", function () {
assert.equal(coerceToSafeInteger(9007199254740992), null);
});
it("Should reject number beyond Number.MIN_SAFE_INTEGER", function () {
assert.equal(coerceToSafeInteger(-9007199254740992), null);
});

it("Should reject NaN", function () { assert.equal(coerceToSafeInteger(NaN), null); });

if (typeof Object.create === "function") {
it("Should not coerce objects with no number representation", function () {
assert.equal(coerceToSafeInteger(Object.create(null)), null);
});
}

it("Should not coerce null", function () { assert.equal(coerceToSafeInteger(null), null); });
it("Should not coerce undefined", function () {
assert.equal(coerceToSafeInteger(undefined), null);
});

if (typeof Symbol === "function") {
it("Should not coerce symbols", function () {
assert.equal(coerceToSafeInteger(Symbol("foo")), null);
});
}
});

0 comments on commit b8549c4

Please sign in to comment.