Skip to content

Commit

Permalink
feat: integer/coerce
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Apr 3, 2019
1 parent 51e4174 commit 89dea2e
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ ensureFinite(12); // "12"
ensureFinite(null); // Thrown TypeError: null is not a number
```

#### Integer Number

##### `integer/coerce`

Follows [`finite/coerce`](#finitecoerce) additionally stripping decimal part from the number

```javascript
const coerceToInteger = require("type/integer/coerce");

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

#### Object

_Object_ is assumed to be any non-primitive JavaScript value
Expand Down
11 changes: 11 additions & 0 deletions integer/coerce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
"use strict";

var coerceToFinite = require("../finite/coerce");

var abs = Math.abs, floor = Math.floor;

module.exports = function (value) {
value = coerceToFinite(value);
if (!value) return value;
return (value > 0 ? 1 : -1) * floor(abs(value));
};
43 changes: 43 additions & 0 deletions test/integer/coerce.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"use strict";

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

describe("integer/coerce", function () {
it("Should coerce float to integer", function () {
assert.equal(coerceToInteger(123.123), 123);
assert.equal(coerceToInteger(123.823), 123);
assert.equal(coerceToInteger(-123.123), -123);
assert.equal(coerceToInteger(-123.823), -123);
});
it("Should coerce string", function () { assert.equal(coerceToInteger("12.123"), 12); });
it("Should coerce booleans", function () { assert.equal(coerceToInteger(true), 1); });
it("Should coerce number objects", function () {
assert.equal(coerceToInteger(new Number(343)), 343);
});
it("Should coerce objects", function () {
assert.equal(coerceToInteger({ valueOf: function () { return 23; } }), 23);
});

it("Should reject infinite number", function () {
assert.equal(coerceToInteger(Infinity), null);
});
it("Should reject NaN", function () { assert.equal(coerceToInteger(NaN), null); });

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

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

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

0 comments on commit 89dea2e

Please sign in to comment.