-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(0.1.3): Add number utility functions
- Loading branch information
0pilatos0
committed
Jan 3, 2024
1 parent
28a05e5
commit 9461b75
Showing
15 changed files
with
260 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |