Skip to content

Commit

Permalink
Merge pull request #6 from thibaudbrault/dev
Browse files Browse the repository at this point in the history
✨ create new functions
  • Loading branch information
thibaudbrault committed Jul 31, 2023
2 parents 5d844a9 + 5cf553d commit 6acf599
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-buttons-kiss.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"unit-convert-fns": minor
---

Create milesToKm and kmToMiles functions
28 changes: 25 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const meter = feetToMeter(feet, 3);
- [meterToFeet](#meterToFeet)
- [cmToInches](#cmToInches)
- [inchesToCm](#inchesToCm)
- [milesToKm](#milesToKm)
- [kmToMiles](#kmToMiles)

<hr />

Expand All @@ -47,8 +49,8 @@ This functions returns two variables: the <b>feet</b> and the <b>inches</b>

```typescript
import { meterToFeet } from "unit-convert-fns";
const meter = "1,78;
const {feet, inches} = meterToFeet(meter);
const meter = "1,78";
const { feet, inches } = meterToFeet(meter);
console.log(feet, inches);
// feet will be equal to 5 and inches will be equal to 10.08
```
Expand All @@ -69,8 +71,28 @@ console.log(inches);

```typescript
import { inchesToCm } from "unit-convert-fns";
const inches = "8.45;
const inches = "8.45";
const cm = inchesToCm(inches);
console.log(cm);
// cm will be equal to 21.46
```

<h3 id="milesToKm"><code>milesToKm</code></h3>

```typescript
import { milesToKm } from "unit-convert-fns";
const miles = 4.23;
const km = milesToKm(miles);
console.log(km);
// km will be equal to 6.81
```

<h3 id="kmToMiles"><code>kmToMiles</code></h3>

```typescript
import { kmToMiles } from "unit-convert-fns";
const km = 12.56;
const miles = kmToMiles(km);
console.log(miles);
// miles will be equal to 7.80
```
29 changes: 28 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { cmToInches, feetToMeter, inchesToCm, meterToFeet } from ".";
import {
cmToInches,
feetToMeter,
inchesToCm,
kmToMiles,
meterToFeet,
milesToKm,
} from ".";
import { expect, test } from "vitest";

test("Convert feet to meter with a number", () => {
Expand Down Expand Up @@ -50,3 +57,23 @@ test("Convert inches to centimeters with a string with a comma", () => {
const result = inchesToCm("8,45");
expect(result).toEqual(21.46);
});

test("Convert miles to kilometers wih a number", () => {
const result = milesToKm(4.23);
expect(result).toEqual(6.81);
});

test("Convert miles to kilometers with a string with a comma", () => {
const result = milesToKm("24,75");
expect(result).toEqual(39.83);
});

test("Convert kilometers to miles wih a number", () => {
const result = kmToMiles(12.56);
expect(result).toEqual(7.8);
});

test("Convert kilometers to miles with a string with a comma", () => {
const result = kmToMiles("4,22");
expect(result).toEqual(2.62);
});
10 changes: 10 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,13 @@ export const inchesToCm = (unit: number | string, fixed: number = 2) => {
const formattedUnit = setup(unit);
return Number((formattedUnit * 2.54).toFixed(fixed));
};

export const milesToKm = (unit: number | string, fixed: number = 2) => {
const formattedUnit = setup(unit);
return Number((formattedUnit * 1.609344).toFixed(fixed));
};

export const kmToMiles = (unit: number | string, fixed: number = 2) => {
const formattedUnit = setup(unit);
return Number((formattedUnit / 1.609344).toFixed(fixed));
};

0 comments on commit 6acf599

Please sign in to comment.