Skip to content

Commit

Permalink
creating unit conversios functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Silva committed Apr 20, 2021
1 parent 4544f0c commit 679a0cd
Show file tree
Hide file tree
Showing 41 changed files with 843 additions and 0 deletions.
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
* //Converting 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);
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)
});
});

0 comments on commit 679a0cd

Please sign in to comment.