Skip to content

Commit

Permalink
BREAKING: disallow fraq(1, 2) syntax, use fraq([1, 2]) instead
Browse files Browse the repository at this point in the history
  • Loading branch information
vladkens committed Jun 1, 2023
1 parent 9b6a9db commit 39629ca
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
18 changes: 10 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)`
Expand Down Expand Up @@ -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) // -> "¹⁄₆₄"
```
1 change: 0 additions & 1 deletion src/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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])

Expand Down
10 changes: 5 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down

0 comments on commit 39629ca

Please sign in to comment.