forked from date-fns/date-fns
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…ate-fns#767)(date-fns#765) - Convert number arguments into integer number using a custom `toInteger` implementation - Change null/undefined/true/false handling strategy - Review handling of arguments of a wrong type - Review usage of === undefined
- Loading branch information
1 parent
eae7d39
commit daddf06
Showing
100 changed files
with
451 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export default function toInteger (dirtyNumber) { | ||
if (dirtyNumber === null || dirtyNumber === true || dirtyNumber === false) { | ||
return NaN | ||
} | ||
|
||
var number = Number(dirtyNumber) | ||
|
||
if (isNaN(number)) { | ||
return number | ||
} | ||
|
||
return number < 0 ? Math.ceil(number) : Math.floor(number) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
// @flow | ||
/* eslint-env mocha */ | ||
|
||
import assert from 'power-assert' | ||
import toInteger from '.' | ||
|
||
describe('toInteger', function () { | ||
it('truncates positive numbers', function () { | ||
var result = toInteger(10.99) | ||
assert(result === 10) | ||
}) | ||
|
||
it('truncates negative numbers', function () { | ||
var result = toInteger(-5.5) | ||
assert(result === -5) | ||
}) | ||
|
||
it('converts convertable strings', function () { | ||
var result = toInteger('-10.75') | ||
assert(result === -10) | ||
}) | ||
|
||
it('returns NaN for non-convertable strings', function () { | ||
var result = toInteger('Foobar') | ||
assert(typeof result === 'number' && isNaN(result)) | ||
}) | ||
|
||
it('returns NaN for false', function () { | ||
var result = toInteger(false) | ||
assert(typeof result === 'number' && isNaN(result)) | ||
}) | ||
|
||
it('returns NaN for true', function () { | ||
var result = toInteger(true) | ||
assert(typeof result === 'number' && isNaN(result)) | ||
}) | ||
|
||
it('returns NaN for null', function () { | ||
var result = toInteger(null) | ||
assert(typeof result === 'number' && isNaN(result)) | ||
}) | ||
|
||
it('returns NaN for undefined', function () { | ||
var result = toInteger(undefined) | ||
assert(typeof result === 'number' && isNaN(result)) | ||
}) | ||
|
||
it('returns NaN for NaN', function () { | ||
var result = toInteger(NaN) | ||
assert(typeof result === 'number' && isNaN(result)) | ||
}) | ||
|
||
it('converts convertable objects', function () { | ||
// eslint-disable-next-line no-new-wrappers | ||
var result = toInteger(new Number(123)) | ||
assert(result === 123) | ||
}) | ||
|
||
it('returns NaN for non-convertable objects', function () { | ||
// eslint-disable-next-line no-new-wrappers | ||
var result = toInteger({}) | ||
assert(typeof result === 'number' && isNaN(result)) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.