diff --git a/readme.md b/readme.md index 72ce502..c1a0845 100644 --- a/readme.md +++ b/readme.md @@ -40,17 +40,19 @@ const f5 = f4.div(f1).toString() // -> "6" (3 / 0.5) ## API -### `fraq(...args: [Fraq] | [number, number])` +```typescript +type Fraq = Fraction | [number, number] | number | string +``` + +### `fraq(val: Fraq)` Helper function to create fraction from various inputs. ```typescript -type Fraq = Fraction | [number, number] | number | string - -fraq(1, 2).toPair() // -> [1, 2] fraq(2).toPair() // -> [2, 1] fraq([2, 1]).toPair() // -> [2, 1] fraq(0.5).toPair() // -> [1, 2] +fraq("1/2").toPair() // -> [1, 2] ``` ### `new Fraction(n: number, d = 1, reduce = false)` @@ -142,14 +144,14 @@ fraq(1, 2).eq([1, 3]) // -> false ```typescript fraq(0.5).toUnicode() // -> "1/2" -fraq(1, 64).toUnicode() // -> "0" -fraq(1, 64).toUnicode(64) // -> "1/64" +fraq([1, 64]).toUnicode() // -> "0" +fraq([1, 64]).toUnicode(64) // -> "1/64" ``` ### `.toUnicode()` ```typescript fraq(0.5).toUnicode() // -> "½" -fraq(1, 64).toUnicode() // -> "0" -fraq(1, 64).toUnicode(64) // -> "¹⁄₆₄" +fraq([1, 64]).toUnicode() // -> "0" +fraq([1, 64]).toUnicode(64) // -> "¹⁄₆₄" ``` diff --git a/src/main.test.ts b/src/main.test.ts index bd1c82c..55a0a05 100644 --- a/src/main.test.ts +++ b/src/main.test.ts @@ -40,7 +40,6 @@ test("should create fraction", () => { throws(() => new Fraction(3.1415, 1), /TypeError/i) equal(fraq([1, 2]).toPair(), [1, 2]) - equal(fraq(1, 2).toPair(), [1, 2]) equal(fraq(2).toPair(), [2, 1]) equal(fraq(new Fraction(2)).toPair(), [2, 1]) diff --git a/src/main.ts b/src/main.ts index b53a99c..5fbd95e 100644 --- a/src/main.ts +++ b/src/main.ts @@ -78,12 +78,12 @@ export const gcd = (a: number, b: number): number => { return a } -export const fraq = (...args: [Fraq] | [number, number]): Fraction => { - if (args.length === 2) return new Fraction(...args) - - let val = args[0] +export const fraq = (val: Fraq): Fraction => { if (val instanceof Fraction) return val - if (Array.isArray(val)) return new Fraction(...val) + if (Array.isArray(val)) { + if (val.length !== 2) throw new Error("ValueError") + return new Fraction(...val) + } val = val.toString().trim()