Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit conversion functions (#2433)
Added: - Added 18 new conversion functions: - `daysToWeeks` - `hoursToMilliseconds` - `hoursToMinutes` - `hoursToSeconds` - `millisecondsToHours` - `millisecondsToMinutes` - `millisecondsToSeconds` - `minutesToHours` - `minutesToMilliseconds` - `minutesToSeconds` - `monthsToQuarters` - `monthsToYears` - `quartersToMonths` - `quartersToYears` - `secondsToHours` - `secondsToMilliseconds` - `secondsToMinutes` - `weeksToDays` - `yearsToMonths` - `yearsToQuarters`
- Loading branch information
1 parent
c6d080f
commit 5f35b3e
Showing
45 changed files
with
1,090 additions
and
8 deletions.
There are no files selected for viewing
Submodule deno
added at
e5bc4f
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,31 @@ | ||
import requiredArgs from '../_lib/requiredArgs/index' | ||
import { daysInWeek } from '../constants/index' | ||
|
||
/** | ||
* @name daysToWeeks | ||
* @category Conversion Helpers | ||
* @summary Convert days to weeks. | ||
* | ||
* @description | ||
* Convert a number of days to a full number of weeks. | ||
* | ||
* @param {number} days - number of days to be converted | ||
* | ||
* @returns {number} the number of days converted in weeks | ||
* @throws {TypeError} 1 argument required | ||
* | ||
* @example | ||
* // Convert 14 days to weeks: | ||
* const result = daysToWeeks(14) | ||
* //=> 2 | ||
* | ||
* @example | ||
* // It uses floor rounding: | ||
* const result = daysToWeeks(13) | ||
* //=> 1 | ||
*/ | ||
export default function daysToWeeks(days: number): number { | ||
requiredArgs(1, arguments) | ||
const weeks = days / daysInWeek | ||
return Math.floor(weeks) | ||
} |
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,21 @@ | ||
/* eslint-env mocha */ | ||
|
||
import assert from 'assert' | ||
import daysToWeeks from '.' | ||
|
||
describe('daysToWeeks', () => { | ||
it('converts days to weeks', function () { | ||
assert(daysToWeeks(7) === 1) | ||
assert(daysToWeeks(14) === 2) | ||
}) | ||
|
||
it('uses floor rounding', () => { | ||
assert(daysToWeeks(8) === 1) | ||
assert(daysToWeeks(6) === 0) | ||
}) | ||
|
||
it('handles border values', () => { | ||
assert(daysToWeeks(7.5) === 1) | ||
assert(daysToWeeks(0) === 0) | ||
}) | ||
}) |
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,25 @@ | ||
import requiredArgs from '../_lib/requiredArgs/index' | ||
import { millisecondsInHour } from '../constants/index' | ||
|
||
/** | ||
* @name hoursToMilliseconds | ||
* @category Conversion Helpers | ||
* @summary Convert hours to milliseconds. | ||
* | ||
* @description | ||
* Convert a number of hours to a full number of milliseconds. | ||
* | ||
* @param {number} hours - number of hours to be converted | ||
* | ||
* @returns {number} the number of hours converted to milliseconds | ||
* @throws {TypeError} 1 argument required | ||
* | ||
* @example | ||
* // Convert 2 hours to milliseconds: | ||
* const result = hoursToMilliseconds(2) | ||
* //=> 7200000 | ||
*/ | ||
export default function hoursToMilliseconds(hours: number): number { | ||
requiredArgs(1, arguments) | ||
return Math.floor(hours * millisecondsInHour) | ||
} |
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,20 @@ | ||
/* eslint-env mocha */ | ||
|
||
import assert from 'assert' | ||
import hoursToMilliseconds from '.' | ||
|
||
describe('hoursToMilliseconds', function () { | ||
it('converts hours to milliseconds', function () { | ||
assert(hoursToMilliseconds(1) === 3600000) | ||
assert(hoursToMilliseconds(2) === 7200000) | ||
}) | ||
|
||
it('uses floor rounding', () => { | ||
assert(hoursToMilliseconds(0.123456) === 444441) | ||
}) | ||
|
||
it('handles border values', () => { | ||
assert(hoursToMilliseconds(1.5) === 5400000) | ||
assert(hoursToMilliseconds(0) === 0) | ||
}) | ||
}) |
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,25 @@ | ||
import requiredArgs from '../_lib/requiredArgs/index' | ||
import { minutesInHour } from '../constants/index' | ||
|
||
/** | ||
* @name hoursToMinutes | ||
* @category Conversion Helpers | ||
* @summary Convert hours to minutes. | ||
* | ||
* @description | ||
* Convert a number of hours to a full number of minutes. | ||
* | ||
* @param {number} hours - number of hours to be converted | ||
* | ||
* @returns {number} the number of hours converted in minutes | ||
* @throws {TypeError} 1 argument required | ||
* | ||
* @example | ||
* // Convert 2 hours to minutes: | ||
* const result = hoursToMinutes(2) | ||
* //=> 120 | ||
*/ | ||
export default function hoursToMinutes(hours: number): number { | ||
requiredArgs(1, arguments) | ||
return Math.floor(hours * minutesInHour) | ||
} |
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,20 @@ | ||
/* eslint-env mocha */ | ||
|
||
import assert from 'assert' | ||
import hoursToMinutes from '.' | ||
|
||
describe('hoursToMinutes', function () { | ||
it('converts hours to minutes', function () { | ||
assert(hoursToMinutes(1) === 60) | ||
assert(hoursToMinutes(2) === 120) | ||
}) | ||
|
||
it('uses floor rounding', () => { | ||
assert(hoursToMinutes(0.123) === 7) | ||
}) | ||
|
||
it('handles border values', () => { | ||
assert(hoursToMinutes(1.5) === 90) | ||
assert(hoursToMinutes(0) === 0) | ||
}) | ||
}) |
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,25 @@ | ||
import requiredArgs from '../_lib/requiredArgs/index' | ||
import { secondsInHour } from '../constants/index' | ||
|
||
/** | ||
* @name hoursToSeconds | ||
* @category Conversion Helpers | ||
* @summary Convert hours to seconds. | ||
* | ||
* @description | ||
* Convert a number of hours to a full number of seconds. | ||
* | ||
* @param {number} hours - number of hours to be converted | ||
* | ||
* @returns {number} the number of hours converted in seconds | ||
* @throws {TypeError} 1 argument required | ||
* | ||
* @example | ||
* // Convert 2 hours to seconds: | ||
* const result = hoursToSeconds(2) | ||
* //=> 7200 | ||
*/ | ||
export default function hoursToSeconds(hours: number): number { | ||
requiredArgs(1, arguments) | ||
return Math.floor(hours * secondsInHour) | ||
} |
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,20 @@ | ||
/* eslint-env mocha */ | ||
|
||
import assert from 'assert' | ||
import hoursToSeconds from '.' | ||
|
||
describe('hoursToSeconds', function () { | ||
it('converts hours to seconds', function () { | ||
assert(hoursToSeconds(1) === 3600) | ||
assert(hoursToSeconds(2) === 7200) | ||
}) | ||
|
||
it('uses floor rounding', () => { | ||
assert(hoursToSeconds(0.123) === 442) | ||
}) | ||
|
||
it('handles border values', () => { | ||
assert(hoursToSeconds(1.5) === 5400) | ||
assert(hoursToSeconds(0) === 0) | ||
}) | ||
}) |
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,31 @@ | ||
import requiredArgs from '../_lib/requiredArgs/index' | ||
import { millisecondsInHour } from '../constants/index' | ||
|
||
/** | ||
* @name millisecondsToHours | ||
* @category Conversion Helpers | ||
* @summary Convert milliseconds to hours. | ||
* | ||
* @description | ||
* Convert a number of milliseconds to a full number of hours. | ||
* | ||
* @param {number} milliseconds - number of milliseconds to be converted | ||
* | ||
* @returns {number} the number of milliseconds converted in hours | ||
* @throws {TypeError} 1 argument required | ||
* | ||
* @example | ||
* // Convert 7200000 milliseconds to hours: | ||
* const result = millisecondsToHours(7200000) | ||
* //=> 2 | ||
* | ||
* @example | ||
* // It uses floor rounding: | ||
* const result = millisecondsToHours(7199999) | ||
* //=> 1 | ||
*/ | ||
export default function millisecondsToHours(milliseconds: number): number { | ||
requiredArgs(1, arguments) | ||
const hours = milliseconds / millisecondsInHour | ||
return Math.floor(hours) | ||
} |
Oops, something went wrong.