Skip to content

Commit

Permalink
Merge pull request #16 from pranavacharya/development
Browse files Browse the repository at this point in the history
Added support for milliseconds and metre
  • Loading branch information
pranavacharya authored Aug 13, 2020
2 parents 3f687e7 + 25082f6 commit 5c80d29
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 40 deletions.
57 changes: 24 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ A npm package to calculate pace using distance and time in various units.
![GitHub](https://img.shields.io/github/license/pranavacharya/pacecal)
![npm](https://img.shields.io/npm/v/pacecal)




## Installation


### npm

```sh
npm install pacecal
```


### Browser

#### CDN
Expand All @@ -29,21 +24,16 @@ npm install pacecal

#### [download package](https://unpkg.com/pacecal@latest/dist/pacecal.umd.js)



## Usage


#### Import the package

```javascript
const { Pace } = require('pacecal');
```



#### Creating Pace object


##### Class parameters

1. distance - default unit: km
Expand All @@ -67,23 +57,24 @@ const pace = new Pace(6.2, 60, { distanceUnit: 'mi', timeUnit: 'min' }); //dista
const pace = new pacecal.Pace(10, 60, { distanceUnit: 'km', timeUnit: 'min' });
```


##### Available methods
* `.getPace();`
```javascript
// returns pace in s/km
pace.getPace();
```
* `.getPaceTimeString();`
```javascript
// returns time taken for 1 km in hh:mm:ss format
pace.getPaceTimeString();
```
* `.format('mi', 'min')`

- `.getPace();`
```javascript
// returns pace in s/km
pace.getPace();
```
- `.getPaceTimeString();`
```javascript
// returns time taken for 1 km in hh:mm:ss format
pace.getPaceTimeString();
```
- `.format('mi', 'min')`

```javascript
// returns pace in min/mi
pace.format('mi', 'min').getPace();

// returns time taken for 1 mile in hh:mm:ss format
pace.format('mi', 'min').getPaceTimeString();
```
Expand All @@ -110,20 +101,22 @@ pace.format('km', 'min').getPace(); // 6
pace.format('km', 'min').getPaceTimeString(); // 00:06:00
```


## Units supported

##### Distance
* km - kilometers
* mi - miles

- m - metre
- km - kilometers
- mi - miles

##### Time
* s - seconds
* min - minutes
* hr - hours

- ms - milliseconds
- s - seconds
- min - minutes
- hr - hours

### ***Deprecated functions***
### **_Deprecated functions_**

```javascript
const pacecal = require('pacecal');
Expand All @@ -143,8 +136,6 @@ pacecal.getAreragePace([360, 366, 390]); // 372
pacecal.getAreragePaceinTime([360, 366, 390]);
```



## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details
4 changes: 4 additions & 0 deletions src/lib/distance.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const units = {
m: {
ratio: 0.001,
name: 'metre'
},
mi: {
ratio: 1.60934,
name: 'miles'
Expand Down
6 changes: 4 additions & 2 deletions src/lib/pace.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export default class Pace {
* @param {Object} [optional] - optional params
* @param {String} [optional.distanceUnit] - unit of distance
* - default unit: km
* - available units: "km", "mi"
* - available units: "m", "km", "mi"
* @param {String} [optional.timeUnit] - unit of time
* - default unit: s
* - available units: "s", "min" , "h"
* - available units: "ms", "s", "min" , "h"
*/
constructor(
distance = throwParamRequiredError('distance'),
Expand Down Expand Up @@ -71,10 +71,12 @@ export default class Pace {
* - default unit - s/km
* @param {string} distanceUnit
* Available units
* - m
* - km
* - mi
* @param {string} timeUnit
* Available units
* - ms
* - s
* - min
* - h
Expand Down
4 changes: 4 additions & 0 deletions src/lib/time.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
const units = {
ms: {
ratio: 0.001,
name: 'miliseconds'
},
s: {
ratio: 1,
name: 'seconds'
Expand Down
8 changes: 6 additions & 2 deletions tests/distance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import { listUnit, convertUnit } from '../src/lib/distance';
describe('Distance', () => {
test('list units', () => {
expect(typeof listUnit()).toBe('object');
expect(listUnit()).toEqual(['mi', 'km']);
expect(listUnit()).toEqual(['m', 'mi', 'km']);
});

test('convertUnit', () => {
test('convertUnit - km to mi', () => {
expect(convertUnit(10, 'km', 'mi')).toBeCloseTo(6.2, 1);
});

test('convertUnit - km to m', () => {
expect(convertUnit(10, 'km', 'm')).toBe(10000);
});
});
2 changes: 1 addition & 1 deletion tests/pacecal.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe('Pace Class', () => {
expect(() => {
// invalid distanceUnit
// eslint-disable-next-line no-unused-vars
const pace = new Pace(6, 3600, { distanceUnit: 'm', timeUnit: 'min' });
const pace = new Pace(6, 3600, { distanceUnit: 'yard', timeUnit: 'min' });
}).toThrow(TypeError);
});

Expand Down
8 changes: 6 additions & 2 deletions tests/time.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ import { listUnit, convertUnit } from '../src/lib/time';
describe('Time', () => {
test('list units', () => {
expect(typeof listUnit()).toBe('object');
expect(listUnit()).toEqual(['s', 'min', 'h']);
expect(listUnit()).toEqual(['ms', 's', 'min', 'h']);
});

test('convertUnit', () => {
test('convertUnit - min to s', () => {
expect(convertUnit(60, 'min', 's')).toBe(3600);
});

test('convertUnit - min to ms', () => {
expect(convertUnit(60, 'min', 'ms')).toBe(3600000);
});
});

0 comments on commit 5c80d29

Please sign in to comment.