Skip to content

Commit

Permalink
feat(0.1.3): Add number utility functions
Browse files Browse the repository at this point in the history
  • Loading branch information
0pilatos0 committed Jan 3, 2024
1 parent 28a05e5 commit 9461b75
Show file tree
Hide file tree
Showing 15 changed files with 260 additions and 1 deletion.
68 changes: 68 additions & 0 deletions USAGE.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ Here are some of the utility functions and classes available in this library:

- [NumberUtils](#numberutils)
- [toRoman](#toroman)
- [toBinary](#tobinary)
- [fromRoman](#fromroman)
- [randomNumber](#randomnumber)
- [fromHex](#fromhex)
- [toHex](#tohex)
- [isOdd](#isodd)
- [isEven](#iseven)
- [formatCurrency](#formatcurrency)
- [fromBinary](#frombinary)

- [ObjectUtils](#objectutils)
- [deepClone](#deepclone)
Expand Down Expand Up @@ -470,6 +476,16 @@ the following utility functions are available in the `NumberUtils` class, they c
```


### toBinary
```typescript
/**
* Converts a number to its binary representation.
* @param value - The number to convert.
* @returns The binary representation of the number.
*/
```


### fromRoman
```typescript
/**
Expand All @@ -492,6 +508,47 @@ the following utility functions are available in the `NumberUtils` class, they c
```


### fromHex
```typescript
/**
* Converts a hexadecimal string to a number.
*
* @param hexString - The hexadecimal string to convert.
* @returns The converted number.
*/
```


### toHex
```typescript
/**
* Converts a number to its hexadecimal representation.
* @param value - The number to convert.
* @returns The hexadecimal representation of the number.
*/
```


### isOdd
```typescript
/**
* Checks if a number is odd.
* @param n - The number to check.
* @returns True if the number is odd, false otherwise.
*/
```


### isEven
```typescript
/**
* Checks if a number is even.
* @param n - The number to check.
* @returns True if the number is even, false otherwise.
*/
```


### formatCurrency
```typescript
/**
Expand All @@ -504,6 +561,17 @@ the following utility functions are available in the `NumberUtils` class, they c
```


### fromBinary
```typescript
/**
* Converts a binary string to a number.
*
* @param binaryString The binary string to convert.
* @returns The converted number.
*/
```



## ObjectUtils

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.2",
"version": "0.1.3",
"module": "dist/index.mjs",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
9 changes: 9 additions & 0 deletions src/number/fromBinary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Converts a binary string to a number.
*
* @param binaryString The binary string to convert.
* @returns The converted number.
*/
export function fromBinary(binaryString: string): number {
return parseInt(binaryString, 2);
}
9 changes: 9 additions & 0 deletions src/number/fromHex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Converts a hexadecimal string to a number.
*
* @param hexString - The hexadecimal string to convert.
* @returns The converted number.
*/
export function fromHex(hexString: string): number {
return parseInt(hexString, 16);
}
18 changes: 18 additions & 0 deletions src/number/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,36 @@

import { toRoman } from "./toRoman";
import { toBinary } from "./toBinary";
import { fromRoman } from "./fromRoman";
import { randomNumber } from "./randomNumber";
import { fromHex } from "./fromHex";
import { toHex } from "./toHex";
import { isOdd } from "./isOdd";
import { isEven } from "./isEven";
import { formatCurrency } from "./formatCurrency";
import { fromBinary } from "./fromBinary";

export class NumberUtils {
static toRoman: typeof toRoman = toRoman;
static toBinary: typeof toBinary = toBinary;
static fromRoman: typeof fromRoman = fromRoman;
static randomNumber: typeof randomNumber = randomNumber;
static fromHex: typeof fromHex = fromHex;
static toHex: typeof toHex = toHex;
static isOdd: typeof isOdd = isOdd;
static isEven: typeof isEven = isEven;
static formatCurrency: typeof formatCurrency = formatCurrency;
static fromBinary: typeof fromBinary = fromBinary;
}

export { toRoman } from "./toRoman";
export { toBinary } from "./toBinary";
export { fromRoman } from "./fromRoman";
export { randomNumber } from "./randomNumber";
export { fromHex } from "./fromHex";
export { toHex } from "./toHex";
export { isOdd } from "./isOdd";
export { isEven } from "./isEven";
export { formatCurrency } from "./formatCurrency";
export { fromBinary } from "./fromBinary";

8 changes: 8 additions & 0 deletions src/number/isEven.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Checks if a number is even.
* @param n - The number to check.
* @returns True if the number is even, false otherwise.
*/
export function isEven(n: number): boolean {
return n % 2 === 0;
}
8 changes: 8 additions & 0 deletions src/number/isOdd.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Checks if a number is odd.
* @param n - The number to check.
* @returns True if the number is odd, false otherwise.
*/
export function isOdd(n: number): boolean {
return n % 2 !== 0;
}
8 changes: 8 additions & 0 deletions src/number/toBinary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Converts a number to its binary representation.
* @param value - The number to convert.
* @returns The binary representation of the number.
*/
export function toBinary(value: number): string {
return value.toString(2);
}
8 changes: 8 additions & 0 deletions src/number/toHex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Converts a number to its hexadecimal representation.
* @param value - The number to convert.
* @returns The hexadecimal representation of the number.
*/
export function toHex(value: number): string {
return value.toString(16);
}
13 changes: 13 additions & 0 deletions test/number/fromBinary.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { expect, describe, it } from "bun:test";
import { fromBinary } from "../../src/number/fromBinary";

describe("fromBinary", () => {
it("should convert a binary string to a number", () => {
const binaryString = "101010";
const expectedNumber = 42;

const result = fromBinary(binaryString);

expect(result).toEqual(expectedNumber);
});
});
10 changes: 10 additions & 0 deletions test/number/fromHex.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { expect, describe, it } from "bun:test";
import { fromHex } from "../../src/number/fromHex";

describe("fromHex", () => {
it("should convert a hex string to a number", () => {
const hexString = "FF";
const result = fromHex(hexString);
expect(result).toEqual(255);
});
});
20 changes: 20 additions & 0 deletions test/number/isEven.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, describe, it } from "bun:test";
import { isEven } from "../../src/number/isEven";

describe("isEven", () => {
it("should return true for even numbers", () => {
expect(isEven(2)).toBe(true);
expect(isEven(4)).toBe(true);
expect(isEven(6)).toBe(true);
expect(isEven(8)).toBe(true);
expect(isEven(10)).toBe(true);
});

it("should return false for odd numbers", () => {
expect(isEven(1)).toBe(false);
expect(isEven(3)).toBe(false);
expect(isEven(5)).toBe(false);
expect(isEven(7)).toBe(false);
expect(isEven(9)).toBe(false);
});
});
20 changes: 20 additions & 0 deletions test/number/isOdd.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { expect, describe, it } from "bun:test";
import { isOdd } from "../../src/number/isOdd";

describe("isOdd", () => {
it("should return true for odd numbers", () => {
expect(isOdd(1)).toBe(true);
expect(isOdd(3)).toBe(true);
expect(isOdd(5)).toBe(true);
expect(isOdd(7)).toBe(true);
expect(isOdd(9)).toBe(true);
});

it("should return false for even numbers", () => {
expect(isOdd(2)).toBe(false);
expect(isOdd(4)).toBe(false);
expect(isOdd(6)).toBe(false);
expect(isOdd(8)).toBe(false);
expect(isOdd(10)).toBe(false);
});
});
30 changes: 30 additions & 0 deletions test/number/toBinary.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect, describe, it } from "bun:test";
import { toBinary } from "../../src/number/toBinary";

describe("toBinary", () => {
it("should convert decimal numbers to binary", () => {
// Test case 1: Convert 0 to binary
const result1 = toBinary(0);
expect(result1).toEqual("0");

// Test case 2: Convert positive numbers to binary
const result2 = toBinary(5);
expect(result2).toEqual("101");

const result3 = toBinary(10);
expect(result3).toEqual("1010");

const result4 = toBinary(15);
expect(result4).toEqual("1111");

// Test case 3: Convert negative numbers to binary
const result5 = toBinary(-5);
expect(result5).toEqual("-101");

const result6 = toBinary(-10);
expect(result6).toEqual("-1010");

const result7 = toBinary(-15);
expect(result7).toEqual("-1111");
});
});
30 changes: 30 additions & 0 deletions test/number/toHex.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { expect, describe, it } from "bun:test";
import { toHex } from "../../src/number/toHex";

describe("toHex", () => {
it("should convert a number to its hexadecimal representation", () => {
// Test case 1
const result1 = toHex(10);
expect(result1).toEqual("a");

// Test case 2
const result2 = toHex(255);
expect(result2).toEqual("ff");

// Test case 3
const result3 = toHex(16);
expect(result3).toEqual("10");

// Test case 4
const result4 = toHex(0);
expect(result4).toEqual("0");

// Test case 5
const result5 = toHex(123456789);
expect(result5).toEqual("75bcd15");

// Test case 6
const result6 = toHex(987654321);
expect(result6).toEqual("3ade68b1");
});
});

0 comments on commit 9461b75

Please sign in to comment.