Skip to content

Commit

Permalink
Update package version and add math utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
0pilatos0 committed Apr 6, 2024
1 parent 8dd11b1 commit ff324e4
Show file tree
Hide file tree
Showing 21 changed files with 403 additions and 2 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

TypeLegend is a versatile and typesafe utility library designed to simplify common tasks in your TypeScript projects. With TypeLegend, you can harness the power of TypeScript's strong typing system to write safer and more maintainable code.

- Total amount of typesafe utility functions: **79**
- Total amount of typesafe utility functions: **87**
- Total amount of typesafe utility classes: **9**

## Features
Expand Down
98 changes: 98 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,13 @@ Here are some of the utility functions and classes available in this library:

- [MathUtils](#mathutils)
- [modulus](#modulus)
- [circumference](#circumference)
- [average](#average)
- [areaTriangle](#areatriangle)
- [factorial](#factorial)
- [median](#median)
- [percentOf](#percentof)
- [areaCircle](#areacircle)
- [randomInt](#randomint)
- [lcm](#lcm)
- [gdc](#gdc)
Expand All @@ -68,10 +71,15 @@ Here are some of the utility functions and classes available in this library:
- [sqrt](#sqrt)
- [subtract](#subtract)
- [add](#add)
- [lerp](#lerp)
- [volumeSphere](#volumesphere)
- [pow](#pow)
- [ratio](#ratio)
- [areaRectangle](#arearectangle)

- [NumberUtils](#numberutils)
- [toRoman](#toroman)
- [padNumber](#padnumber)
- [toBinary](#tobinary)
- [toOrdinal](#toordinal)
- [fromRoman](#fromroman)
Expand Down Expand Up @@ -557,6 +565,16 @@ the following utility functions are available in the `MathUtils` class, they can
```


### circumference
```typescript
/**
* Calculates the circumference of a circle given the radius.
* @param radius - The radius of the circle.
* @returns The circumference of the circle.
*/
```


### average
```typescript
/**
Expand All @@ -567,6 +585,18 @@ the following utility functions are available in the `MathUtils` class, they can
```


### areaTriangle
```typescript
/**
* Calculates the area of a triangle.
*
* @param base - The length of the base of the triangle.
* @param height - The height of the triangle.
* @returns The area of the triangle.
*/
```


### factorial
```typescript
/**
Expand Down Expand Up @@ -601,6 +631,16 @@ the following utility functions are available in the `MathUtils` class, they can
```


### areaCircle
```typescript
/**
* Calculates the area of a circle.
* @param radius - The radius of the circle.
* @returns The area of the circle.
*/
```


### randomInt
```typescript
/**
Expand Down Expand Up @@ -731,6 +771,28 @@ the following utility functions are available in the `MathUtils` class, they can
```


### lerp
```typescript
/**
* Linearly interpolates between two numbers.
* @param from - The starting value.
* @param to - The ending value.
* @param t - The interpolation factor (between 0 and 1).
* @returns The interpolated value.
*/
```


### volumeSphere
```typescript
/**
* Calculates the volume of a sphere given its radius.
* @param radius The radius of the sphere.
* @returns The volume of the sphere.
*/
```


### pow
```typescript
/**
Expand All @@ -742,6 +804,30 @@ the following utility functions are available in the `MathUtils` class, they can
```


### ratio
```typescript
/**
* Calculates the ratio between two numbers.
*
* @param a - The numerator.
* @param b - The denominator.
* @returns The ratio of a to b.
*/
```


### areaRectangle
```typescript
/**
* Calculates the area of a rectangle.
*
* @param length - The length of the rectangle.
* @param width - The width of the rectangle.
* @returns The area of the rectangle.
*/
```



## NumberUtils

Expand All @@ -758,6 +844,18 @@ the following utility functions are available in the `NumberUtils` class, they c
```


### padNumber
```typescript
/**
* Pads a number with leading zeros to a specified length.
*
* @param num - The number to pad.
* @param length - The desired length of the padded number.
* @returns The padded number as a string.
*/
```


### toBinary
```typescript
/**
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "typelegend",
"version": "0.1.17",
"version": "0.1.18",
"module": "dist/index.mjs",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
15 changes: 15 additions & 0 deletions src/math/areaCircle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* Calculates the area of a circle.
* @param radius - The radius of the circle.
* @returns The area of the circle.
*/
export function areaCircle(radius: number): number {
if (radius === 0) {
return 0;
}

if (radius < 0) {
return NaN;
}
return Math.PI * radius ** 2;
}
18 changes: 18 additions & 0 deletions src/math/areaRectangle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* Calculates the area of a rectangle.
*
* @param length - The length of the rectangle.
* @param width - The width of the rectangle.
* @returns The area of the rectangle.
*/
export function areaRectangle(length: number, width: number): number {
if (length === 0 || width === 0) {
return 0;
}

if (length < 0 || width < 0) {
return NaN;
}

return length * width;
}
10 changes: 10 additions & 0 deletions src/math/areaTriangle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Calculates the area of a triangle.
*
* @param base - The length of the base of the triangle.
* @param height - The height of the triangle.
* @returns The area of the triangle.
*/
export function areaTriangle(base: number, height: number): number {
return 0.5 * base * height;
}
8 changes: 8 additions & 0 deletions src/math/circumference.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Calculates the circumference of a circle given the radius.
* @param radius - The radius of the circle.
* @returns The circumference of the circle.
*/
export function circumference(radius: number): number {
return 2 * Math.PI * radius;
}
21 changes: 21 additions & 0 deletions src/math/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@

import { modulus } from "./modulus";
import { circumference } from "./circumference";
import { average } from "./average";
import { areaTriangle } from "./areaTriangle";
import { factorial } from "./factorial";
import { median } from "./median";
import { percentOf } from "./percentOf";
import { areaCircle } from "./areaCircle";
import { randomInt } from "./randomInt";
import { lcm } from "./lcm";
import { gdc } from "./gdc";
Expand All @@ -15,14 +18,21 @@ import { isPrime } from "./isPrime";
import { sqrt } from "./sqrt";
import { subtract } from "./subtract";
import { add } from "./add";
import { lerp } from "./lerp";
import { volumeSphere } from "./volumeSphere";
import { pow } from "./pow";
import { ratio } from "./ratio";
import { areaRectangle } from "./areaRectangle";

export class MathUtils {
static modulus: typeof modulus = modulus;
static circumference: typeof circumference = circumference;
static average: typeof average = average;
static areaTriangle: typeof areaTriangle = areaTriangle;
static factorial: typeof factorial = factorial;
static median: typeof median = median;
static percentOf: typeof percentOf = percentOf;
static areaCircle: typeof areaCircle = areaCircle;
static randomInt: typeof randomInt = randomInt;
static lcm: typeof lcm = lcm;
static gdc: typeof gdc = gdc;
Expand All @@ -34,14 +44,21 @@ export class MathUtils {
static sqrt: typeof sqrt = sqrt;
static subtract: typeof subtract = subtract;
static add: typeof add = add;
static lerp: typeof lerp = lerp;
static volumeSphere: typeof volumeSphere = volumeSphere;
static pow: typeof pow = pow;
static ratio: typeof ratio = ratio;
static areaRectangle: typeof areaRectangle = areaRectangle;
}

export { modulus } from "./modulus";
export { circumference } from "./circumference";
export { average } from "./average";
export { areaTriangle } from "./areaTriangle";
export { factorial } from "./factorial";
export { median } from "./median";
export { percentOf } from "./percentOf";
export { areaCircle } from "./areaCircle";
export { randomInt } from "./randomInt";
export { lcm } from "./lcm";
export { gdc } from "./gdc";
Expand All @@ -53,5 +70,9 @@ export { isPrime } from "./isPrime";
export { sqrt } from "./sqrt";
export { subtract } from "./subtract";
export { add } from "./add";
export { lerp } from "./lerp";
export { volumeSphere } from "./volumeSphere";
export { pow } from "./pow";
export { ratio } from "./ratio";
export { areaRectangle } from "./areaRectangle";

10 changes: 10 additions & 0 deletions src/math/lerp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Linearly interpolates between two numbers.
* @param from - The starting value.
* @param to - The ending value.
* @param t - The interpolation factor (between 0 and 1).
* @returns The interpolated value.
*/
export function lerp(from: number, to: number, t: number): number {
return from + (to - from) * t;
}
10 changes: 10 additions & 0 deletions src/math/ratio.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Calculates the ratio between two numbers.
*
* @param a - The numerator.
* @param b - The denominator.
* @returns The ratio of a to b.
*/
export function ratio(a: number, b: number): number {
return a / b;
}
8 changes: 8 additions & 0 deletions src/math/volumeSphere.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Calculates the volume of a sphere given its radius.
* @param radius The radius of the sphere.
* @returns The volume of the sphere.
*/
export function volumeSphere(radius: number): number {
return (4 / 3) * Math.PI * radius ** 3;
}
3 changes: 3 additions & 0 deletions src/number/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@

import { toRoman } from "./toRoman";
import { padNumber } from "./padNumber";
import { toBinary } from "./toBinary";
import { toOrdinal } from "./toOrdinal";
import { fromRoman } from "./fromRoman";
Expand All @@ -14,6 +15,7 @@ import { fromBinary } from "./fromBinary";

export class NumberUtils {
static toRoman: typeof toRoman = toRoman;
static padNumber: typeof padNumber = padNumber;
static toBinary: typeof toBinary = toBinary;
static toOrdinal: typeof toOrdinal = toOrdinal;
static fromRoman: typeof fromRoman = fromRoman;
Expand All @@ -28,6 +30,7 @@ export class NumberUtils {
}

export { toRoman } from "./toRoman";
export { padNumber } from "./padNumber";
export { toBinary } from "./toBinary";
export { toOrdinal } from "./toOrdinal";
export { fromRoman } from "./fromRoman";
Expand Down
10 changes: 10 additions & 0 deletions src/number/padNumber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/**
* Pads a number with leading zeros to a specified length.
*
* @param num - The number to pad.
* @param length - The desired length of the padded number.
* @returns The padded number as a string.
*/
export function padNumber(num: number, length: number): string {
return num.toString().padStart(length, "0");
}
24 changes: 24 additions & 0 deletions test/math/areaCircle.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { expect, describe, it } from "bun:test";
import { areaCircle } from "../../src/math/areaCircle";

describe("areaCircle", () => {
it("should calculate the area of a circle with a given radius", () => {
const radius = 5;
const expectedArea = Math.PI * radius ** 2;
const calculatedArea = areaCircle(radius);
expect(calculatedArea).toBe(expectedArea);
});

it("should return 0 when the radius is 0", () => {
const radius = 0;
const expectedArea = 0;
const calculatedArea = areaCircle(radius);
expect(calculatedArea).toBe(expectedArea);
});

it("should return NaN when the radius is negative", () => {
const radius = -5;
const calculatedArea = areaCircle(radius);
expect(calculatedArea).toBeNaN();
});
});
Loading

0 comments on commit ff324e4

Please sign in to comment.