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 2 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
30 changes: 30 additions & 0 deletions src/daysToWeeks/index.ts
@@ -0,0 +1,30 @@
import requiredArgs from '../_lib/requiredArgs/index'
/**
* @name daysToWeeks
* @category Common Helpers
* @summary Convert days to weeks.
*
* @description
* Convert days number to weeks numbers.
*
* @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
*/

export default function daysToWeeks(days: number): number {
requiredArgs(1, arguments)

const weeks = days / 7
const weeksRounded = Number(
Math.round(Number(weeks + 'e' + 2)) + 'e' + 2 * -1
) //Precision of 2 in decimals

return weeksRounded
}
12 changes: 12 additions & 0 deletions src/daysToWeeks/test.ts
@@ -0,0 +1,12 @@
// @flow
/* eslint-env mocha */

import assert from 'assert'
import daysToWeeks from '.'

describe('daysToWeeks', function() {
it('converts days to weeks', function() {
const result = daysToWeeks(14);
LucasHFS marked this conversation as resolved.
Show resolved Hide resolved
assert.deepStrictEqual(result, 2)
});
});
31 changes: 31 additions & 0 deletions src/hoursToMilliseconds/index.ts
@@ -0,0 +1,31 @@
import requiredArgs from '../_lib/requiredArgs/index'
/**
* @name hoursToMilliseconds
* @category Common Helpers
* @summary Convert hours to milliseconds.
*
* @description
* Convert hours number to milliseconds numbers.
*
* @param { number } hours - number of hours to be converted.
*
* @returns {number} the number of hours converted in milliseconds
* @throws {TypeError} 1 argument required
*
* @example
* //Converting 2 hours to milliseconds
* const result = hoursToMilliseconds(2) => 7,200,000
*/

export default function hoursToMilliseconds(
hours: number,

): number {
requiredArgs(1, arguments);

const MILLISECONDS_IN_1HOUR = 3600000;

const milliseconds = hours * MILLISECONDS_IN_1HOUR;

return milliseconds;
}
12 changes: 12 additions & 0 deletions src/hoursToMilliseconds/test.ts
@@ -0,0 +1,12 @@
// @flow
/* eslint-env mocha */

import assert from 'assert'
import hoursToMilliseconds from '.'

describe('hoursToMilliseconds', function() {
it('converts hours to milliseconds', function() {
const result = hoursToMilliseconds(2);
assert.deepStrictEqual(result, 7200000)
});
});
27 changes: 27 additions & 0 deletions src/hoursToMinutes/index.ts
@@ -0,0 +1,27 @@
import requiredArgs from '../_lib/requiredArgs/index'
/**
* @name hoursToMinutes
* @category Common Helpers
* @summary Convert hours to minutes.
*
* @description
* Convert hours number to minutes numbers.
*
* @param { number } hours - number of hours to be converted.
*
* @returns {number} the number of hours converted in minutes
* @throws {TypeError} 1 argument required
*
* @example
* //Converting 2 hours to minutes
* const result = hoursToMinutes(2) => 120
*/

export default function hoursToMinutes(
hours: number,

): number {
requiredArgs(1, arguments);

return hours*60;
}
12 changes: 12 additions & 0 deletions src/hoursToMinutes/test.ts
@@ -0,0 +1,12 @@
// @flow
/* eslint-env mocha */

import assert from 'assert'
import hoursToMinutes from '.'

describe('hoursToMinutes', function() {
it('converts hours to minutes', function() {
const result = hoursToMinutes(2);
assert.deepStrictEqual(result, 120)
});
});
27 changes: 27 additions & 0 deletions src/hoursToSeconds/index.ts
@@ -0,0 +1,27 @@
import requiredArgs from '../_lib/requiredArgs/index'
/**
* @name hoursToSeconds
* @category Common Helpers
* @summary Convert hours to seconds.
*
* @description
* Convert hours number to seconds numbers.
*
* @param { number } hours - number of hours to be converted.
*
* @returns {number} the number of hours converted in seconds
* @throws {TypeError} 1 argument required
*
* @example
* //Converting 2 hours to seconds
* const result = hoursToSeconds(2) => 7200
*/

export default function hoursToSeconds(
hours: number,

): number {
requiredArgs(1, arguments);

return hours * 3600;
}
12 changes: 12 additions & 0 deletions src/hoursToSeconds/test.ts
@@ -0,0 +1,12 @@
// @flow
/* eslint-env mocha */

import assert from 'assert'
import hoursToSeconds from '.'

describe('hoursToSeconds', function() {
it('converts hours to minutes', function() {
const result = hoursToSeconds(2);
assert.deepStrictEqual(result, 7200)
});
});
20 changes: 20 additions & 0 deletions src/index.js
Expand Up @@ -206,4 +206,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'
32 changes: 32 additions & 0 deletions src/millisecondsToHours/index.ts
@@ -0,0 +1,32 @@
import requiredArgs from '../_lib/requiredArgs/index'
/**
* @name millisecondsToHours
* @category Common Helpers
* @summary Convert milliseconds to hours.
*
* @description
* Convert milliseconds number to hours numbers.
*
* @param { number } milliseconds - number of milliseconds to be converted.
*
* @returns {number} the number of milliseconds converted in hours
* @throws {TypeError} 1 argument required
*
* @example
* //Converting 720000 milliseconds to hours
* const result = millisecondsToHours(7200000) => 2
*/

export default function millisecondsToHours(
milliseconds: number,

): number {
requiredArgs(1, arguments);

const MILLISECONDS_IN_1HOUR = 3600000;

const hours = milliseconds/MILLISECONDS_IN_1HOUR;
const hoursRounded = Number(Math.round(Number(hours + "e" + 3)) + "e" + 3 * -1); //Precision of 3 in decimals

return hoursRounded;
}
12 changes: 12 additions & 0 deletions src/millisecondsToHours/test.ts
@@ -0,0 +1,12 @@
// @flow
/* eslint-env mocha */

import assert from 'assert'
import millisecondsToHours from '.'

describe('millisecondsToHours', function() {
it('converts milliseconds to hours', function() {
const result = millisecondsToHours(7200000);
assert.deepStrictEqual(result, 2)
});
});
32 changes: 32 additions & 0 deletions src/millisecondsToMinutes/index.ts
@@ -0,0 +1,32 @@
import requiredArgs from '../_lib/requiredArgs/index'
/**
* @name millisecondsToMinutes
* @category Common Helpers
* @summary Convert milliseconds to minutes.
*
* @description
* //Converting milliseconds number to minutes numbers.
*
* @param { number } milliseconds - number of milliseconds to be converted.
*
* @returns {number} the number of milliseconds converted in minutes
* @throws {TypeError} 1 argument required
*
* @example
* //Converting 60000 milliseconds to minutes
* const result = millisecondsToMinutes(60000) => 1
*/

export default function millisecondsToMinutes(
milliseconds: number,

): number {
requiredArgs(1, arguments);

const MILLISECONDS_IN_1MINUTE = 60000;

const minutes = milliseconds/MILLISECONDS_IN_1MINUTE;
const minutesRounded = Number(Math.round(Number(minutes + "e" + 3)) + "e" + 3 * -1); //Precision of 3 in decimals

return minutesRounded;
}
12 changes: 12 additions & 0 deletions src/millisecondsToMinutes/test.ts
@@ -0,0 +1,12 @@
// @flow
/* eslint-env mocha */

import assert from 'assert'
import millisecondsToMinutes from '.'

describe('millisecondsToMinutes', function() {
it('converts milliseconds to minutes', function() {
const result = millisecondsToMinutes(120000);
assert.deepStrictEqual(result, 2)
});
});
33 changes: 33 additions & 0 deletions src/millisecondsToSeconds/index.ts
@@ -0,0 +1,33 @@
import requiredArgs from '../_lib/requiredArgs/index'
/**
* @name millisecondsToSeconds
* @category Common Helpers
* @summary Convert milliseconds to seconds.
*
* @description
* Convert milliseconds number to seconds numbers.
*
* @param { number } milliseconds - number of milliseconds to be converted.
*
* @returns {number} the number of milliseconds converted in seconds
* @throws {TypeError} 1 argument required
*
* @example
* //Converting 1000 miliseconds to seconds
* const result = millisecondsToSeconds(1000) => 1
*/

export default function millisecondsToSeconds(
milliseconds: number,

): number {
requiredArgs(1, arguments);

const MILLISECONDS_IN_1SECOND = 1000;

const seconds = milliseconds/MILLISECONDS_IN_1SECOND;

const secondsRounded = Number(Math.round(Number(seconds + "e" + 3)) + "e" + 3 * -1); //Precision of 3 in decimals

return secondsRounded;
}
12 changes: 12 additions & 0 deletions src/millisecondsToSeconds/test.ts
@@ -0,0 +1,12 @@
// @flow
/* eslint-env mocha */

import assert from 'assert'
import millisecondsToSeconds from '.'

describe('millisecondsToSeconds', function() {
it('converts milliseconds to seconds', function() {
const result = millisecondsToSeconds(3000);
assert.deepStrictEqual(result, 3)
});
});
30 changes: 30 additions & 0 deletions src/minutesToHours/index.ts
@@ -0,0 +1,30 @@
import requiredArgs from '../_lib/requiredArgs/index'
/**
* @name minutesToHours
* @category Common Helpers
* @summary Convert minutes to hours.
*
* @description
* Convert minutes number to hours numbers.
*
* @param { number } minutes - number of minutes to be converted.
*
* @returns {number} the number of minutes converted in hours
* @throws {TypeError} 1 argument required
*
* @example
* //Converting 140 minutes to hours
* const result = minutesToHours(140) => 2.3
*/

export default function minutesToHours(
minutes: number,

): number {
requiredArgs(1, arguments);

const hours = minutes/60;
const hoursRounded = Number(Math.round(Number(hours + "e" + 2)) + "e" + 2 * -1); //Precision of 2 in decimals

return hoursRounded;
}
12 changes: 12 additions & 0 deletions src/minutesToHours/test.ts
@@ -0,0 +1,12 @@
// @flow
/* eslint-env mocha */

import assert from 'assert'
import minuteToHours from '.'

describe('minuteToHours', function() {
it('converts minutes to hours', function() {
const result = minuteToHours(140);
assert.deepStrictEqual(result, 2.33)
});
});