Skip to content

Commit

Permalink
Merge pull request #3 from thibaudbrault/dev
Browse files Browse the repository at this point in the history
📝 create documentation
  • Loading branch information
thibaudbrault committed Jul 29, 2023
2 parents 9af4742 + 813c31e commit d757896
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/four-queens-smoke.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"unit-convert-fns": minor
---

Release documentation
75 changes: 74 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,76 @@
# `unit-converter`

Package with multiple functions to convert units of measurements
Package with multiple functions to convert units of measurements
Every function accepts 2 arguments (unless stated otherwise):

- `unit` which can be a number or a string (if it's a string it can have a comma instead of a dot separate the decimals) and is the number that will be converted.
- `fixed` which is the number that will be passed in the `toFixed()` at the end of every function. This argument is optional and if not passed it will default to 2

The result will be of type number

### Install

```bash
npm install unit-convert-fns
```

### Using the functions

```typescript
import { feetToMeter } from "unit-convert-fns";
const feet = 8.56;
const fixed = 3;
const meter = feetToMeter(feet, 3);
```

## List of functions

- [feetToMeter](#feetToMeter)
- [meterToFeet](#meterToFeet)
- [cmToInches](#cmToInches)
- [inchesToCm](#inchesToCm)

<hr />

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

```typescript
import { feetToMeter } from "unit-convert-fns";
const feet = 5.9;
const meter = feetToMeter(feet);
console.log(meter);
// meter will be equal to 5.9
```

<h3 id="meterToFeet"><code>meterToFeet</code></h3>
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);
console.log(feet, inches);
// feet will be equal to 5 and inches will be equal to 10.08
```

In this function the `toFixed()` is applied only on the inches. The feet has a `Math.floor()` applied.

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

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

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

```typescript
import { inchesToCm } from "unit-convert-fns";
const inches = "8.45;
const cm = inchesToCm(inches);
console.log(cm);
// cm will be equal to 21.46
```
22 changes: 11 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
const setup = (unit: number | string) => {
let formattedUnit = unit;
if (typeof formattedUnit === "string") {
formattedUnit = formattedUnit.includes(",")
? formattedUnit.replace(",", ".")
: formattedUnit;
formattedUnit = parseFloat(formattedUnit);
}
return formattedUnit;
};

export const feetToMeter = (unit: number | string, fixed: number = 2) => {
let formattedUnit = unit;
if (typeof formattedUnit === "string" && formattedUnit.includes(",")) {
Expand All @@ -17,17 +28,6 @@ export const feetToMeter = (unit: number | string, fixed: number = 2) => {
return Number(result.toFixed(fixed));
};

const setup = (unit: number | string) => {
let formattedUnit = unit;
if (typeof formattedUnit === "string") {
formattedUnit = formattedUnit.includes(",")
? formattedUnit.replace(",", ".")
: formattedUnit;
formattedUnit = parseFloat(formattedUnit);
}
return formattedUnit;
};

export const meterToFeet = (unit: number | string, fixed: number = 2) => {
const formattedUnit = setup(unit);
const length = formattedUnit / 0.0254;
Expand Down

0 comments on commit d757896

Please sign in to comment.