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

creating unit conversions functions #2433

Merged
merged 36 commits into from May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
679a0cd
creating unit conversios functions
Apr 20, 2021
2c56fa6
Fix example
tan75 Apr 23, 2021
2714524
rounding decimal results with Math floor and fixing examples
Apr 28, 2021
97f7e5b
exercising more the tests
Apr 28, 2021
39b5be6
merging
Apr 28, 2021
f3a3e8e
Review PR - minor changes
tan75 May 2, 2021
e10abce
Add minor changes
tan75 May 2, 2021
b4821c7
Add minor changes
tan75 May 2, 2021
44fca47
Add minor changes, fix docs
tan75 May 2, 2021
1b3d5fa
Add minor changes, fix docs
tan75 May 2, 2021
9551598
Add minor changes, doc fix
tan75 May 2, 2021
e4a0cfc
Add minor changes, fix docs
tan75 May 2, 2021
98afdc1
Add minor changes, fix docs
tan75 May 2, 2021
c0384e8
Add minor changes, fix docs
tan75 May 2, 2021
4512e0f
Add minor changes, fix docs
tan75 May 2, 2021
ba00a35
Add minor changes, fix docs
tan75 May 2, 2021
66ff56a
Add minor changes, fix docs
tan75 May 2, 2021
24db4fe
Add minor changes, fix docs
tan75 May 2, 2021
01441b4
Add minor changes, fix docs
tan75 May 2, 2021
31bd40e
Add minor changes, fix docs
tan75 May 2, 2021
1c0bcfa
Add minor changes, fix docs
tan75 May 2, 2021
68cdb7f
Add minor changes, fix docs
tan75 May 2, 2021
03cbd90
Add minor changes, fix docs
tan75 May 2, 2021
b4305a4
Add minor changes, fix docs
tan75 May 2, 2021
b9a7be3
Add minor changes, fix docs
tan75 May 2, 2021
4df6bf4
Merge branch 'master' into 732-unit-conversion-functions
tan75 May 9, 2021
5ac8403
Move constants to separate file, add minor changes
tan75 May 12, 2021
f3d6bd8
Merge branch 'master' into 732-unit-conversion-functions
tan75 May 12, 2021
81aa438
Clean up conversion functions, add doc category
kossnocorp May 14, 2021
7da59cb
Write good test example
kossnocorp May 14, 2021
5b21699
adding floor rounding
May 24, 2021
d111b42
improving tests
May 24, 2021
d4a6472
Fix test
tan75 May 25, 2021
43a49ed
Merge branch 'master' into 732-unit-conversion-functions
tan75 May 25, 2021
27f1bd1
Order constants by name asc, fix docs
tan75 May 25, 2021
dc4b059
Add missing floor to conversion functions
kossnocorp May 28, 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
1 change: 1 addition & 0 deletions deno
Submodule deno added at e5bc4f
1 change: 1 addition & 0 deletions docs/index.js
Expand Up @@ -5,6 +5,7 @@ module.exports = {
'General',
'Types',
'Common Helpers',
'Conversion Helpers',
'Interval Helpers',
'Timestamp Helpers',
'Millisecond Helpers',
Expand Down
99 changes: 94 additions & 5 deletions src/constants/index.ts
@@ -1,13 +1,48 @@
/**
* Maximum allowed time.
* Days in 1 week.
*
* @name maxTime
* @constant
* @type {number}
* @default
* @constant
* @type {number}
* @default
*/
export const daysInWeek = 7

/**
* Maximum allowed time.
*
* @constant
* @type {number}
* @default
*/
export const maxTime = Math.pow(10, 8) * 24 * 60 * 60 * 1000

/**
* Milliseconds in 1 minute
*
* @constant
* @type {number}
* @default
*/
export const millisecondsInMinute = 60000

/**
* Milliseconds in 1 hour
*
* @constant
* @type {number}
* @default
*/
export const millisecondsInHour = 3600000

/**
* Milliseconds in 1 second
*
* @constant
* @type {number}
* @default
*/
export const millisecondsInSecond = 1000

/**
* Minimum allowed time.
*
Expand All @@ -17,3 +52,57 @@ export const maxTime = Math.pow(10, 8) * 24 * 60 * 60 * 1000
* @default
*/
export const minTime = -maxTime

/**
* Minutes in 1 hour
*
* @constant
* @type {number}
* @default
*/
export const minutesInHour = 60

/**
* Months in 1 quarter
*
* @constant
* @type {number}
* @default
*/
export const monthsInQuarter = 3

/**
* Months in 1 year
*
* @constant
* @type {number}
* @default
*/
export const monthsInYear = 12

/**
* Quarters in 1 year
*
* @constant
* @type {number}
* @default
*/
export const quartersInYear = 4

/**
* Seconds in 1 hour
*
* @constant
* @type {number}
* @default
*/
export const secondsInHour = 3600

/**
* Seconds in 1 minute
*
* @constant
* @type {number}
* @default
*/
export const secondsInMinute = 60
31 changes: 31 additions & 0 deletions src/daysToWeeks/index.ts
@@ -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)
}
21 changes: 21 additions & 0 deletions src/daysToWeeks/test.ts
@@ -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)
})
})
25 changes: 25 additions & 0 deletions src/hoursToMilliseconds/index.ts
@@ -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)
}
20 changes: 20 additions & 0 deletions src/hoursToMilliseconds/test.ts
@@ -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)
})
})
25 changes: 25 additions & 0 deletions src/hoursToMinutes/index.ts
@@ -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)
}
20 changes: 20 additions & 0 deletions src/hoursToMinutes/test.ts
@@ -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)
})
})
25 changes: 25 additions & 0 deletions src/hoursToSeconds/index.ts
@@ -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)
}
20 changes: 20 additions & 0 deletions src/hoursToSeconds/test.ts
@@ -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)
})
})
20 changes: 20 additions & 0 deletions src/index.js
Expand Up @@ -207,4 +207,24 @@ export { default as subSeconds } from './subSeconds/index'
export { default as subWeeks } from './subWeeks/index'
export { default as subYears } from './subYears/index'
export { default as toDate } from './toDate/index'
export { default as daysToWeeks } from './daysToWeeks/index'
export { default as hoursToMilliseconds } from './hoursToMilliseconds/index'
export { default as hoursToMinutes } from './hoursToMinutes/index'
export { default as hoursToSeconds } from './hoursToSeconds/index'
export { default as millisecondsToHours } from './millisecondsToHours/index'
export { default as millisecondsToMinutes } from './millisecondsToMinutes/index'
export { default as millisecondsToSeconds } from './millisecondsToSeconds/index'
export { default as minutesToHours } from './minutesToHours/index'
export { default as minutesToMilliseconds } from './minutesToMilliseconds/index'
export { default as minutesToSeconds } from './minutesToSeconds/index'
export { default as monthsToQuarters } from './monthsToQuarters/index'
export { default as monthsToYears } from './monthsToYears/index'
export { default as quartersToMonths } from './quartersToMonths/index'
export { default as quartersToYears } from './quartersToYears/index'
export { default as secondsToHours } from './secondsToHours/index'
export { default as secondsToMilliseconds } from './secondsToMilliseconds/index'
export { default as secondsToMinutes } from './secondsToMinutes/index'
export { default as weeksToDays } from './weeksToDays/index'
export { default as yearsToMonths } from './yearsToMonths/index'
export { default as yearsToQuarters } from './yearsToQuarters/index'
export * from './constants/index'
6 changes: 3 additions & 3 deletions src/milliseconds/index.ts
Expand Up @@ -3,7 +3,7 @@ import { Duration } from '../types'

// Leap year occures every 4 years, except for years that are divisable by 100 and not divisable by 400.
// 1 mean year = (365+1/4-1/100+1/400) days = 365.2425 days
const yearInDays = 365.2425
const daysInYear = 365.2425

/**
* @name milliseconds
Expand Down Expand Up @@ -47,8 +47,8 @@ export default function milliseconds({

let totalDays = 0

if (years) totalDays += years * yearInDays
if (months) totalDays += months * (yearInDays / 12)
if (years) totalDays += years * daysInYear
if (months) totalDays += months * (daysInYear / 12)
if (weeks) totalDays += weeks * 7
if (days) totalDays += days

Expand Down
31 changes: 31 additions & 0 deletions src/millisecondsToHours/index.ts
@@ -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)
}