From 5cf553d94c683848631e311b63c4ba0fb86b3865 Mon Sep 17 00:00:00 2001 From: Thibaud Brault Date: Mon, 31 Jul 2023 15:08:05 +0200 Subject: [PATCH] :sparkles: create new functions --- .changeset/ten-buttons-kiss.md | 5 +++++ README.md | 28 +++++++++++++++++++++++++--- src/index.test.ts | 29 ++++++++++++++++++++++++++++- src/index.ts | 10 ++++++++++ 4 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 .changeset/ten-buttons-kiss.md diff --git a/.changeset/ten-buttons-kiss.md b/.changeset/ten-buttons-kiss.md new file mode 100644 index 0000000..1d90e80 --- /dev/null +++ b/.changeset/ten-buttons-kiss.md @@ -0,0 +1,5 @@ +--- +"unit-convert-fns": minor +--- + +Create milesToKm and kmToMiles functions diff --git a/README.md b/README.md index d5d62e6..e3f3897 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,8 @@ const meter = feetToMeter(feet, 3); - [meterToFeet](#meterToFeet) - [cmToInches](#cmToInches) - [inchesToCm](#inchesToCm) +- [milesToKm](#milesToKm) +- [kmToMiles](#kmToMiles)
@@ -47,8 +49,8 @@ This functions returns two variables: the feet and the inches ```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 ``` @@ -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 ``` + +

milesToKm

+ +```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 +``` + +

kmToMiles

+ +```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 +``` diff --git a/src/index.test.ts b/src/index.test.ts index 4c272a1..d66b6a1 100644 --- a/src/index.test.ts +++ b/src/index.test.ts @@ -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", () => { @@ -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); +}); diff --git a/src/index.ts b/src/index.ts index 3ea44db..7e8e6d2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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)); +};