Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

converting -0 to 0 when it appears #2571

Merged
merged 27 commits into from Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
ae83ea7
converting -0 to 0 when it appears
Aug 1, 2021
6802d22
refactoring created function
Aug 2, 2021
0841dfd
Add minor changes
tan75 Aug 4, 2021
215cf8a
Add attempt to pass a rounding method as a parameter - WIP to discuss
tan75 Aug 12, 2021
2d6b15e
Fix differenceInXXX functions
tan75 Aug 20, 2021
5da07c4
Fix differenceInXX functions, migrate differenceInMinutes to TS
tan75 Aug 20, 2021
4c88250
Fix differenceInXXX functions
tan75 Aug 20, 2021
60c4bde
Delete getRoundedValue
tan75 Aug 20, 2021
09b3182
Refactor rounding methods
tan75 Sep 4, 2021
14003e7
Fix test
tan75 Sep 5, 2021
0e189c9
Update src/_lib/roundingMethods/index.ts
tan75 Sep 8, 2021
fab44f9
Update src/_lib/roundingMethods/index.ts
tan75 Sep 8, 2021
88bc1e4
Fix rounding method
tan75 Sep 8, 2021
53a75d3
Add negative cases with default trunc
tan75 Sep 10, 2021
b4cf986
Fix doc
tan75 Sep 10, 2021
c8a4072
Update src/differenceInWeeks/index.ts
kossnocorp Sep 16, 2021
5fee309
Update src/differenceInSeconds/index.ts
kossnocorp Sep 16, 2021
6d3f46a
Update src/differenceInQuarters/index.ts
kossnocorp Sep 16, 2021
85e8754
Update src/differenceInMinutes/index.ts
kossnocorp Sep 16, 2021
7c8fe6f
Update src/differenceInHours/index.ts
kossnocorp Sep 16, 2021
31eb7ef
Update src/differenceInQuarters/index.ts
kossnocorp Sep 16, 2021
721039f
Update src/differenceInMinutes/index.ts
kossnocorp Sep 16, 2021
88ec338
Update src/differenceInSeconds/index.ts
kossnocorp Sep 16, 2021
d890bf3
Update src/differenceInWeeks/index.ts
kossnocorp Sep 16, 2021
7508689
Update src/differenceInHours/index.ts
kossnocorp Sep 16, 2021
728479f
Move rounding method into options object to accomodate fp approach
tan75 Sep 16, 2021
30ea4e7
Clean up JSDoc and getRoundingMethod usage
kossnocorp Sep 17, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/_lib/roundingMethods/index.ts
@@ -0,0 +1,18 @@
import type { RoundingMethod } from '../../types'

type RoundingFn = typeof Math.round

type RoundingFnsMap = { [method in RoundingMethod]: RoundingFn }

const RoundingMap: RoundingFnsMap = {
ceil: Math.ceil,
round: Math.round,
floor: Math.floor,
trunc: (value: number) => (value < 0 ? Math.ceil(value) : Math.floor(value)), // Math.trunc is not supported by IE
}

export function getRoundingMethod(method: RoundingMethod) {
return RoundingMap[method]
}

export const defaultRoundingMethod: RoundingMethod = 'trunc'
23 changes: 16 additions & 7 deletions src/differenceInHours/index.ts
@@ -1,7 +1,11 @@
import type { RoundingMethod } from '../../src/types'
kossnocorp marked this conversation as resolved.
Show resolved Hide resolved
kossnocorp marked this conversation as resolved.
Show resolved Hide resolved
import { millisecondsInHour } from '../constants/index'
import differenceInMilliseconds from '../differenceInMilliseconds/index'
import requiredArgs from '../_lib/requiredArgs/index'

const MILLISECONDS_IN_HOUR = 3600000
import {
defaultRoundingMethod,
getRoundingMethod,
} from '../_lib/roundingMethods/index'

/**
* @name differenceInHours
Expand All @@ -17,22 +21,27 @@ const MILLISECONDS_IN_HOUR = 3600000
*
* @param {Date|Number} dateLeft - the later date
* @param {Date|Number} dateRight - the earlier date
* @param {String} roundingMethod - the rounding method (`ceil`, `floor`, `round` or `trunc`) with `trunc` by default.
* @returns {Number} the number of hours
* @throws {TypeError} 2 arguments required
*
* @example
* // How many hours are between 2 July 2014 06:50:00 and 2 July 2014 19:00:00?
* var result = differenceInHours(
* const result = differenceInHours(
* new Date(2014, 6, 2, 19, 0),
* new Date(2014, 6, 2, 6, 50)
* )
* //=> 12
*/
export default function differenceInHours(dirtyDateLeft: Date | number, dirtyDateRight: Date | number): number {
export default function differenceInHours(
dateLeft: Date | number,
dateRight: Date | number,
roundingMethod: RoundingMethod = defaultRoundingMethod
): number {
requiredArgs(2, arguments)

const diff =
differenceInMilliseconds(dirtyDateLeft, dirtyDateRight) /
MILLISECONDS_IN_HOUR
return diff > 0 ? Math.floor(diff) : Math.ceil(diff)
differenceInMilliseconds(dateLeft, dateRight) / millisecondsInHour
const roundingFn = getRoundingMethod(roundingMethod)
return roundingFn(diff)
}
79 changes: 65 additions & 14 deletions src/differenceInHours/test.ts
@@ -1,60 +1,111 @@
// @flow
/* eslint-env mocha */

import assert from 'assert'
import differenceInHours from '.'

describe('differenceInHours', function() {
it('returns the number of hours between the given dates', function() {
describe('differenceInHours', function () {
it('returns the number of hours between the given dates with `trunc` as a default rounding method', function () {
const result = differenceInHours(
new Date(2014, 6 /* Jul */, 2, 6, 0, 29),
new Date(2014, 6 /* Jul */, 2, 20, 0, 28.973)
)
assert(result === -13)
})

it('returns the number of hours between the given dates', function () {
const result = differenceInHours(
new Date(2014, 6 /* Jul */, 2, 20, 0),
new Date(2014, 6 /* Jul */, 2, 6, 0)
)
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 () {
const result = differenceInHours(
new Date(2014, 6 /* Jul */, 2, 6, 0),
new Date(2014, 6 /* Jul */, 2, 20, 0)
)
assert(result === -14)
})

it('accepts timestamps', function() {
it('returns a 0, not a negative 0 - issue #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(result === 0)
})

it('returns 2 with a rounding method of `ceil`, not a negative 0 - issue #2555 ', function () {
const result = differenceInHours(
new Date(2021, 6 /* Jul */, 22, 7, 1, 29, 976),
new Date(2021, 6 /* Jul */, 22, 6, 1, 28, 173),
'ceil'
)
assert(result === 2)
})

it('returns 1 with a rounding method of `floor`, not a negative 0 - issue #2555 ', function () {
const result = differenceInHours(
new Date(2021, 6 /* Jul */, 22, 7, 1, 29, 976),
new Date(2021, 6 /* Jul */, 22, 6, 1, 28, 173),
'floor'
)
assert(result === 1)
})

it('returns 1 with a rounding method of `round`, not a negative 0 - issue #2555 ', function () {
const result = differenceInHours(
new Date(2021, 6 /* Jul */, 22, 7, 1, 29, 976),
new Date(2021, 6 /* Jul */, 22, 6, 1, 28, 173),
'round'
)
assert(result === 1)
})

it('returns 1 with a rounding method of `trunc`, not a negative 0 - issue #2555 ', function () {
const result = differenceInHours(
new Date(2021, 6 /* Jul */, 22, 7, 1, 29, 976),
new Date(2021, 6 /* Jul */, 22, 6, 1, 28, 173),
'trunc'
)
assert(result === 1)
})

it('accepts timestamps', function () {
const result = differenceInHours(
new Date(2014, 8 /* Sep */, 5, 18, 0).getTime(),
new Date(2014, 8 /* Sep */, 5, 6, 0).getTime()
)
assert(result === 12)
})

describe('edge cases', function() {
it('the difference is less than an hour, but the given dates are in different calendar hours', function() {
describe('edge cases', function () {
it('the difference is less than an hour, but the given dates are in different calendar hours', function () {
const result = differenceInHours(
new Date(2014, 8 /* Sep */, 5, 12),
new Date(2014, 8 /* Sep */, 5, 11, 59)
)
assert(result === 0)
})

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

it('the difference is an integral number of hours', function() {
it('the difference is an integral number of hours', function () {
const result = differenceInHours(
new Date(2014, 8 /* Sep */, 5, 13, 0),
new Date(2014, 8 /* Sep */, 5, 12, 0)
)
assert(result === 1)
})

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

it('returns NaN if the first date is `Invalid Date`', function() {
it('returns NaN if the first date is `Invalid Date`', function () {
const result = differenceInHours(
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 () {
const result = differenceInHours(
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 () {
const result = differenceInHours(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 () {
//@ts-expect-error
assert.throws(differenceInHours.bind(null), TypeError)
//@ts-expect-error
Expand Down
8 changes: 3 additions & 5 deletions src/differenceInMilliseconds/index.ts
Expand Up @@ -28,12 +28,10 @@ import requiredArgs from '../_lib/requiredArgs/index'
* //=> 1100
*/
export default function differenceInMilliseconds(
dirtyDateLeft: Date | number,
dirtyDateRight: Date | number
dateLeft: Date | number,
dateRight: Date | number
): number {
requiredArgs(2, arguments)

const dateLeft = toDate(dirtyDateLeft)
const dateRight = toDate(dirtyDateRight)
return dateLeft.getTime() - dateRight.getTime()
return toDate(dateLeft).getTime() - toDate(dateRight).getTime()
}
5 changes: 3 additions & 2 deletions src/differenceInMilliseconds/test.ts
@@ -1,6 +1,6 @@
/* eslint-env mocha */

import assert from 'power-assert'
import assert from 'assert'
import differenceInMilliseconds from '.'

describe('differenceInMilliseconds', function () {
Expand Down Expand Up @@ -32,7 +32,6 @@ describe('differenceInMilliseconds', function () {
function isNegativeZero(x: number): boolean {
return x === 0 && 1 / x < 0
}

const result = differenceInMilliseconds(
new Date(2014, 8 /* Sep */, 5, 0, 0),
new Date(2014, 8 /* Sep */, 5, 0, 0)
Expand Down Expand Up @@ -64,7 +63,9 @@ describe('differenceInMilliseconds', function () {
})

it('throws TypeError exception if passed less than 2 arguments', function () {
// @ts-expect-error
assert.throws(differenceInMilliseconds.bind(null), TypeError)
// @ts-expect-error
assert.throws(differenceInMilliseconds.bind(null, 1), TypeError)
})
})
@@ -1,7 +1,11 @@
import type { RoundingMethod } from '../types'
import { millisecondsInMinute } from '../constants/index'
import differenceInMilliseconds from '../differenceInMilliseconds/index'
import requiredArgs from '../_lib/requiredArgs/index'

var MILLISECONDS_IN_MINUTE = 60000
import {
defaultRoundingMethod,
getRoundingMethod,
} from '../_lib/roundingMethods/index'

/**
* @name differenceInMinutes
Expand All @@ -17,30 +21,35 @@ var MILLISECONDS_IN_MINUTE = 60000
*
* @param {Date|Number} dateLeft - the later date
* @param {Date|Number} dateRight - the earlier date
* @param {String} roundingMethod - the rounding method (`ceil`, `floor`, `round` or `trunc`) with `trunc` by default.
* @returns {Number} the number of minutes
* @throws {TypeError} 2 arguments required
*
* @example
* // How many minutes are between 2 July 2014 12:07:59 and 2 July 2014 12:20:00?
* var result = differenceInMinutes(
* const result = differenceInMinutes(
* new Date(2014, 6, 2, 12, 20, 0),
* new Date(2014, 6, 2, 12, 7, 59)
* )
* //=> 12
*
* @example
* // How many minutes are from 10:01:59 to 10:00:00
* var result = differenceInMinutes(
* // How many minutes are between 10:01:59 and 10:00:00
* const result = differenceInMinutes(
* new Date(2000, 0, 1, 10, 0, 0),
* new Date(2000, 0, 1, 10, 1, 59)
* )
* //=> -1
*/
export default function differenceInMinutes(dirtyDateLeft, dirtyDateRight) {
export default function differenceInMinutes(
dateLeft: Date | number,
dateRight: Date | number,
roundingMethod: RoundingMethod = defaultRoundingMethod
): number {
requiredArgs(2, arguments)

var diff =
differenceInMilliseconds(dirtyDateLeft, dirtyDateRight) /
MILLISECONDS_IN_MINUTE
return diff > 0 ? Math.floor(diff) : Math.ceil(diff)
const roundingFn = getRoundingMethod(roundingMethod)
const diff =
differenceInMilliseconds(dateLeft, dateRight) / millisecondsInMinute
return roundingFn(diff)
}