Skip to content

Commit

Permalink
converting -0 to 0 when it appears
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Silva committed Aug 1, 2021
1 parent a3e7e6e commit 1b3a13a
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 19 deletions.
3 changes: 2 additions & 1 deletion src/differenceInHours/index.ts
@@ -1,5 +1,6 @@
import differenceInMilliseconds from '../differenceInMilliseconds/index'
import requiredArgs from '../_lib/requiredArgs/index'
import removesNegativeZeroIfPresent from '../utils/removesNegativeZeroIfPresent'

const MILLISECONDS_IN_HOUR = 3600000

Expand Down Expand Up @@ -34,5 +35,5 @@ export default function differenceInHours(dirtyDateLeft: Date | number, dirtyDat
const diff =
differenceInMilliseconds(dirtyDateLeft, dirtyDateRight) /
MILLISECONDS_IN_HOUR
return diff > 0 ? Math.floor(diff) : Math.ceil(diff)
return removesNegativeZeroIfPresent(diff > 0 ? Math.floor(diff) : Math.ceil(diff))
}
8 changes: 8 additions & 0 deletions src/differenceInHours/test.ts
Expand Up @@ -21,6 +21,14 @@ describe('differenceInHours', function() {
assert(result === -14)
})

it("returns a 0, not a negative 0 #2555 ", function () {
const result = differenceInHours(
new Date(2021, 6 /* Jul */, 22, 6, 1, 28.973),
new Date(2021, 6 /* Jul */, 22, 6, 1, 28.976),
)
assert(Object.is(result, 0))
})

it('accepts timestamps', function() {
const result = differenceInHours(
new Date(2014, 8 /* Sep */, 5, 18, 0).getTime(),
Expand Down
5 changes: 4 additions & 1 deletion src/differenceInMinutes/index.js
@@ -1,5 +1,6 @@
import differenceInMilliseconds from '../differenceInMilliseconds/index'
import requiredArgs from '../_lib/requiredArgs/index'
import removesNegativeZeroIfPresent from '../utils/removesNegativeZeroIfPresent'

var MILLISECONDS_IN_MINUTE = 60000

Expand Down Expand Up @@ -42,5 +43,7 @@ export default function differenceInMinutes(dirtyDateLeft, dirtyDateRight) {
var diff =
differenceInMilliseconds(dirtyDateLeft, dirtyDateRight) /
MILLISECONDS_IN_MINUTE
return diff > 0 ? Math.floor(diff) : Math.ceil(diff)
return removesNegativeZeroIfPresent(
diff > 0 ? Math.floor(diff) : Math.ceil(diff)
)
}
34 changes: 21 additions & 13 deletions src/differenceInMinutes/test.js
Expand Up @@ -4,57 +4,65 @@
import assert from 'power-assert'
import differenceInMinutes from '.'

describe('differenceInMinutes', function() {
it('returns the number of minutes between the given dates', function() {
describe('differenceInMinutes', function () {
it('returns the number of minutes between the given dates', function () {
var result = differenceInMinutes(
new Date(2014, 6 /* Jul */, 2, 12, 20),
new Date(2014, 6 /* Jul */, 2, 12, 6)
)
assert(result === 14)
})

it('returns a negative number if the time value of the first date is smaller', function() {
it('returns a negative number if the time value of the first date is smaller', function () {
var result = differenceInMinutes(
new Date(2014, 6 /* Jul */, 2, 12, 6),
new Date(2014, 6 /* Jul */, 2, 12, 20)
)
assert(result === -14)
})

it('accepts timestamps', function() {
it('returns a 0, not a negative 0 #2555 ', function () {
const result = differenceInMinutes(
new Date(2021, 6 /* Jul */, 22, 6, 1, 28.973),
new Date(2021, 6 /* Jul */, 22, 6, 1, 28.976)
)
assert(Object.is(result, 0))
})

it('accepts timestamps', function () {
var result = differenceInMinutes(
new Date(2014, 8 /* Sep */, 5, 18, 45).getTime(),
new Date(2014, 8 /* Sep */, 5, 18, 15).getTime()
)
assert(result === 30)
})

describe('edge cases', function() {
it('the difference is less than a minute, but the given dates are in different calendar minutes', function() {
describe('edge cases', function () {
it('the difference is less than a minute, but the given dates are in different calendar minutes', function () {
var result = differenceInMinutes(
new Date(2014, 8 /* Sep */, 5, 12, 12),
new Date(2014, 8 /* Sep */, 5, 12, 11, 59)
)
assert(result === 0)
})

it('the same for the swapped dates', function() {
it('the same for the swapped dates', function () {
var result = differenceInMinutes(
new Date(2014, 8 /* Sep */, 5, 12, 11, 59),
new Date(2014, 8 /* Sep */, 5, 12, 12)
)
assert(result === 0)
})

it('the difference is an integral number of minutes', function() {
it('the difference is an integral number of minutes', function () {
var result = differenceInMinutes(
new Date(2014, 8 /* Sep */, 5, 12, 25),
new Date(2014, 8 /* Sep */, 5, 12, 15)
)
assert(result === 10)
})

it('the given dates are the same', function() {
it('the given dates are the same', function () {
var result = differenceInMinutes(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
Expand All @@ -77,28 +85,28 @@ describe('differenceInMinutes', function() {
})
})

it('returns NaN if the first date is `Invalid Date`', function() {
it('returns NaN if the first date is `Invalid Date`', function () {
var result = differenceInMinutes(
new Date(NaN),
new Date(2017, 0 /* Jan */, 1)
)
assert(isNaN(result))
})

it('returns NaN if the second date is `Invalid Date`', function() {
it('returns NaN if the second date is `Invalid Date`', function () {
var result = differenceInMinutes(
new Date(2017, 0 /* Jan */, 1),
new Date(NaN)
)
assert(isNaN(result))
})

it('returns NaN if the both dates are `Invalid Date`', function() {
it('returns NaN if the both dates are `Invalid Date`', function () {
var result = differenceInMinutes(new Date(NaN), new Date(NaN))
assert(isNaN(result))
})

it('throws TypeError exception if passed less than 2 arguments', function() {
it('throws TypeError exception if passed less than 2 arguments', function () {
assert.throws(differenceInMinutes.bind(null), TypeError)
assert.throws(differenceInMinutes.bind(null, 1), TypeError)
})
Expand Down
3 changes: 2 additions & 1 deletion src/differenceInQuarters/index.ts
@@ -1,5 +1,6 @@
import differenceInMonths from '../differenceInMonths/index'
import requiredArgs from '../_lib/requiredArgs/index'
import removesNegativeZeroIfPresent from '../utils/removesNegativeZeroIfPresent'

/**
* @name differenceInQuarters
Expand Down Expand Up @@ -30,5 +31,5 @@ export default function differenceInQuarters(
requiredArgs(2, arguments)

const diff = differenceInMonths(dirtyDateLeft, dirtyDateRight) / 3
return diff > 0 ? Math.floor(diff) : Math.ceil(diff)
return removesNegativeZeroIfPresent(diff > 0 ? Math.floor(diff) : Math.ceil(diff))
}
8 changes: 8 additions & 0 deletions src/differenceInQuarters/test.ts
Expand Up @@ -20,6 +20,14 @@ describe('differenceInQuarters', function () {
assert(result === -4)
})

it("returns a 0, not a negative 0 #2555 ", function () {
const result = differenceInQuarters(
new Date(2021, 6 /* Jul */, 22, 6, 1, 28.973),
new Date(2021, 6 /* Jul */, 22, 6, 1, 28.976),
)
assert(Object.is(result, 0))
})

it('accepts timestamps', function () {
const result = differenceInQuarters(
new Date(2014, 9 /* Oct */, 2).getTime(),
Expand Down
5 changes: 3 additions & 2 deletions src/differenceInSeconds/index.ts
@@ -1,5 +1,6 @@
import differenceInMilliseconds from '../differenceInMilliseconds/index'
import requiredArgs from '../_lib/requiredArgs/index'
import removesNegativeZeroIfPresent from '../utils/removesNegativeZeroIfPresent'

/**
* @name differenceInSeconds
Expand Down Expand Up @@ -34,5 +35,5 @@ export default function differenceInSeconds(
requiredArgs(2, arguments)

const diff = differenceInMilliseconds(dirtyDateLeft, dirtyDateRight) / 1000
return diff > 0 ? Math.floor(diff) : Math.ceil(diff)
}
return removesNegativeZeroIfPresent(diff > 0 ? Math.floor(diff) : Math.ceil(diff))
}
8 changes: 8 additions & 0 deletions src/differenceInSeconds/test.ts
Expand Up @@ -20,6 +20,14 @@ describe('differenceInSeconds', function () {
assert(result === -14)
})

it("returns a 0, not a negative 0 #2555 ", function () {
const result = differenceInSeconds(
new Date(2021, 6 /* Jul */, 22, 6, 1, 28.973),
new Date(2021, 6 /* Jul */, 22, 6, 1, 28.976),
)
assert(Object.is(result, 0))
})

it('accepts timestamps', function () {
const result = differenceInSeconds(
new Date(2014, 8 /* Sep */, 5, 18, 30, 45).getTime(),
Expand Down
3 changes: 2 additions & 1 deletion src/differenceInWeeks/index.ts
@@ -1,5 +1,6 @@
import differenceInDays from '../differenceInDays/index'
import requiredArgs from '../_lib/requiredArgs/index'
import removesNegativeZeroIfPresent from '../utils/removesNegativeZeroIfPresent'

/**
* @name differenceInWeeks
Expand Down Expand Up @@ -51,5 +52,5 @@ export default function differenceInWeeks(
requiredArgs(2, arguments)

const diff = differenceInDays(dirtyDateLeft, dirtyDateRight) / 7
return diff > 0 ? Math.floor(diff) : Math.ceil(diff)
return removesNegativeZeroIfPresent(diff > 0 ? Math.floor(diff) : Math.ceil(diff))
}
8 changes: 8 additions & 0 deletions src/differenceInWeeks/test.ts
Expand Up @@ -20,6 +20,14 @@ describe('differenceInWeeks', function () {
assert(result === -1)
})

it("returns a 0, not a negative 0 #2555 ", function () {
const result = differenceInWeeks(
new Date(2021, 6 /* Jul */, 22, 6, 1, 28.973),
new Date(2021, 6 /* Jul */, 22, 6, 1, 28.976),
)
assert(Object.is(result, 0))
})

it('accepts timestamps', function () {
const result = differenceInWeeks(
new Date(2014, 6 /* Jul */, 12).getTime(),
Expand Down
3 changes: 3 additions & 0 deletions src/utils/removesNegativeZeroIfPresent/index.ts
@@ -0,0 +1,3 @@
export default function removesNegativeZeroIfPnumberent(number:number) : number {
return Object.is(number, -0) ? 0 : number
}
23 changes: 23 additions & 0 deletions src/utils/removesNegativeZeroIfPresent/test.ts
@@ -0,0 +1,23 @@
// @flow
/* eslint-env mocha */

import assert from 'power-assert'
import removesNegativeZeroIfPresent from '.'

describe('removesNegativeZeroIfPresent', () => {
it("returns the original number if it isn't -0", () => {
const result = removesNegativeZeroIfPresent(2)
assert(Object.is(result, 2))
})

it("returns 0 if the argument is 0", () => {
const result = removesNegativeZeroIfPresent(0)
assert(Object.is(result, 0))
})

it("returns 0 if the argument is -0", () => {
const result = removesNegativeZeroIfPresent(-0)
assert(Object.is(result, 0))
})

})

0 comments on commit 1b3a13a

Please sign in to comment.