Skip to content

Commit

Permalink
Minor refactoring and adding of unit cm
Browse files Browse the repository at this point in the history
  • Loading branch information
rickyplouis committed Feb 18, 2020
1 parent 8ff5311 commit af47c06
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 16 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ distFrom(chicago).to(ny).in('mi');


#### Supported units
* km or kilometers
* m or meters
* mi or miles
* ft or feet
* km || kilometer || kilometers
* m || meter || meters
* cm || centimeter || centimeters
* mi || mile || miles
* ft || feet
* yd || yard || yards

### License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
Expand Down
34 changes: 28 additions & 6 deletions __tests__/distance.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,37 @@ describe('distFrom().from().to().in()', () => {
).toBe(5570);
});

test('gets distance from ny to london in miles', () => {
test('gets distance from ny to london in meters', () => {
expect(
parseInt(
distFrom(ny)
.to(london)
.in('miles'),
.in('m'),
10
)
).toBe(3461);
).toBe(5570315);
});

test('gets distance from ny to london in meters', () => {
test('gets distance from ny to london in centimeters', () => {
expect(
parseInt(
distFrom(ny)
.to(london)
.in('m'),
.in('cm'),
10
)
).toBe(5570315);
).toBe(557031545);
});

test('gets distance from ny to london in miles', () => {
expect(
parseInt(
distFrom(ny)
.to(london)
.in('miles'),
10
)
).toBe(3461);
});

test('gets distance from ny to london in yards', () => {
Expand All @@ -82,6 +93,17 @@ describe('distFrom().from().to().in()', () => {
).toBe(6091752);
});

test('gets distance from ny to london in kilometers', () => {
expect(
parseInt(
distFrom(ny)
.to(london)
.in('inches'),
10
)
).toBe(219303876);
});

test('gets distance from ny to london in feet', () => {
expect(
parseInt(
Expand Down
12 changes: 7 additions & 5 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { Result, Failure, Success} from "amonad"

type Position = [number, number]
type Units = 'km' | 'kilometers' | 'm' | 'meters' | 'meter' | 'metre' | 'mi' | 'mile' | 'miles' | 'feet' | 'ft' | 'yd' | 'yard' | 'yards'
type Units = 'km' | 'kilometer' | 'kilometers' | 'm' | 'meters' | 'meter' | 'metre' | 'cm' | 'centimeter' | 'centimeters' | 'mi' | 'mile' | 'miles' | 'feet' | 'ft' | 'in' | 'inch' | 'inches' | 'yd' | 'yard' | 'yards'

const multiply = (multiplier1: number) => (multiplier2: number) => multiplier1 * multiplier2

const unitList: Units[] = ['km', 'kilometers', 'm', 'meters', 'meter', 'metre', 'mi', 'mile', 'miles', 'feet', 'ft', 'yd', 'yard', 'yards']
const unitList: Units[] = ['km', 'kilometers', 'kilometers', 'm', 'meters', 'meter', 'metre', 'cm', 'centimeter', 'centimeters', 'mi', 'mile', 'miles', 'feet', 'ft', 'in', 'inch', 'inches', 'yd', 'yard', 'yards']

class DistanceFrom {
private distance: Result<number, Error> = Failure(new Error("Destination is not configured, run distFrom.to()."))
Expand Down Expand Up @@ -89,6 +87,10 @@ class DistanceFrom {
return distance * 0.6213712
else if (units === 'm' || units === 'meter' || units === 'meters' || units === 'metre')
return distance * 1000
else if (units === 'cm' || units === 'centimeter' || units === 'centimeters')
return distance * 100000
else if (units === 'in' || units === 'inch' || units === 'inches')
return distance * 39370.1
else if (units === 'ft' || units === 'feet')
return distance * 3280.84
else if (units === 'yd' || units === 'yard' || units === 'yards')
Expand All @@ -104,6 +106,6 @@ class DistanceFrom {
}
}

module.exports = function (val) {
module.exports = function (val: Position) {
return new DistanceFrom(val)
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "distance-from",
"version": "1.1.1",
"version": "1.1.2",
"description": "Calculate distance between two coordinates",
"main": "dist/index.js",
"scripts": {
Expand Down

0 comments on commit af47c06

Please sign in to comment.