Skip to content

Commit

Permalink
refactor: change the output type from to number for number parsers
Browse files Browse the repository at this point in the history
- Now the following parsers return a `number` instead of a `string`: `integer`, `float`, `whole`, `hex`, `octal`, and `binary`.
- Changed tests accordingly.
- Changed docs accordingly.
- Changed tsdocs accordingly.

BREAKING CHANGE: The output type of number parsers has changed from `string` to `number`, i.e. these parsers now have the type of `Parser<number>` instead of `Parser<string>`.
  • Loading branch information
norskeld committed Oct 2, 2022
1 parent 782a6f9 commit 24406ab
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 51 deletions.
12 changes: 8 additions & 4 deletions docs/content/parsers/binary.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
---
title: 'binary'
kind: 'composite'
description: "binary parses a binary number prefixed with '0b' or '0B', e.g. '0b10', '0B10'. Returns parsed number as a string."
description: "binary parses a binary number prefixed with '0b' or '0B', e.g. '0b10', '0B10'. Returns a decimal number obtained using parseInt with radix of 2."
---

```typescript {{ withLineNumbers: false }}
function binary(): Parser<string>
function binary(): Parser<number>
```

## Description

`binary` parses a binary number prefixed with `0b` or `0B`, e.g. `0b10`, `0B10`. Returns parsed number **as a string**.
`binary` parses a binary number prefixed with `0b` or `0B`, e.g. `0b10`, `0B10`. Returns **a decimal number** obtained using [parseInt] with radix of 2.

## Usage

Expand All @@ -29,7 +29,7 @@ const Parser = binary()
{
isOk: true,
pos: 4,
value: '0b10'
value: 2
}
```

Expand All @@ -45,3 +45,7 @@ const Parser = binary()
}
```
</details>

<!-- Links. -->

[parseInt]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
12 changes: 8 additions & 4 deletions docs/content/parsers/float.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
---
title: 'float'
kind: 'composite'
description: "float parses a float number with an optional minus sign, e.g. '0.25', '-7.90', '4.20'. Returns parsed number as a string."
description: "float parses a float number with an optional minus sign, e.g. '0.25', '-7.90', '4.20'. Returns a decimal number obtained using parseInt with radix of 8."
---

```typescript {{ withLineNumbers: false }}
function float(): Parser<string>
function float(): Parser<number>
```

## Description

> Note: It doesn't handle floats with exponent parts.

`float` parses a float number with an optional minus sign, e.g. `0.25`, `-7.90`, `4.20`. Returns parsed number **as a string**.
`float` parses a float number with an optional minus sign, e.g. `0.25`, `-7.90`, `4.20`. Returns **a decimal number** obtained using [parseFloat].

## Usage

Expand All @@ -31,7 +31,7 @@ const Parser = float()
{
isOk: true,
pos: 5,
value: '-42.0'
value: -42
}
```

Expand All @@ -47,3 +47,7 @@ const Parser = float()
}
```
</details>

<!-- Links. -->

[parseFloat]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat
12 changes: 8 additions & 4 deletions docs/content/parsers/hex.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
---
title: 'hex'
kind: 'composite'
description: "hexadecimal parses a hexadecimal number prefixed with '0x' or '0X', e.g. '0xFF', '0XFF', '0xff'. Returns parsed number as a string."
description: "hexadecimal parses a hexadecimal number prefixed with '0x' or '0X', e.g. '0xFF', '0XFF', '0xff'. Returns a decimal number obtained using parseInt with radix of 16."
---

```typescript {{ withLineNumbers: false }}
function hex(): Parser<string>
function hex(): Parser<number>
```

## Description

`hex` parses a hexadecimal number prefixed with `0x` or `0X`, e.g. `0xFF`, `0XFF`, `0xff`. Returns parsed number **as a string**.
`hex` parses a hexadecimal number prefixed with `0x` or `0X`, e.g. `0xFF`, `0XFF`, `0xff`. Returns **a decimal number** obtained using [parseInt] with radix of 16.

## Usage

Expand All @@ -29,7 +29,7 @@ const Parser = hex()
{
isOk: true,
pos: 4,
value: '0xFF'
value: 255
}
```

Expand All @@ -45,3 +45,7 @@ const Parser = hex()
}
```
</details>

<!-- Links. -->

[parseInt]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
12 changes: 8 additions & 4 deletions docs/content/parsers/integer.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
---
title: 'integer'
kind: 'composite'
description: "integer parses an integer number with an optional minus sign, e.g. '0', '-7', '420'. Returns parsed number as a string."
description: "integer parses an integer number with an optional minus sign, e.g. '0', '-7', '420'. Returns a decimal number obtained using parseInt with radix of 10."
---

```typescript {{ withLineNumbers: false }}
function integer(): Parser<string>
function integer(): Parser<number>
```

## Description

`integer` parses an integer number with an optional minus sign, e.g. `0`, `-7`, `420`. Returns parsed number **as a string**.
`integer` parses an integer number with an optional minus sign, e.g. `0`, `-7`, `420`. Returns **a decimal number** obtained using [parseInt] with radix of 10.

## Usage

Expand All @@ -29,7 +29,7 @@ const Parser = integer()
{
isOk: true,
pos: 3,
value: '-42'
value: -42
}
```

Expand All @@ -45,3 +45,7 @@ const Parser = integer()
}
```
</details>

<!-- Links. -->

[parseInt]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
12 changes: 8 additions & 4 deletions docs/content/parsers/octal.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
---
title: 'octal'
kind: 'composite'
description: "octal parses an octal number prefixed with '0o' or '0O', e.g. '0o42', '0O42'. Returns parsed number as a string."
description: "octal parses an octal number prefixed with '0o' or '0O', e.g. '0o42', '0O42'. Returns a decimal number obtained using parseInt with radix of 8."
---

```typescript {{ withLineNumbers: false }}
function octal(): Parser<string>
function octal(): Parser<number>
```

## Description

`octal` parses an octal number prefixed with `0o` or `0O`, e.g. `0o42`, `0O42`. Returns parsed number **as a string**.
`octal` parses an octal number prefixed with `0o` or `0O`, e.g. `0o42`, `0O42`. Returns **a decimal number** obtained using [parseInt] with radix of 8.

## Usage

Expand All @@ -29,7 +29,7 @@ const Parser = octal()
{
isOk: true,
pos: 4,
value: '0o42'
value: 34
}
```

Expand All @@ -45,3 +45,7 @@ const Parser = octal()
}
```
</details>

<!-- Links. -->

[parseInt]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
12 changes: 8 additions & 4 deletions docs/content/parsers/whole.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
---
title: 'whole'
kind: 'composite'
description: "whole parses a positive whole number without leading zeros, e.g. '0', '7', '420'. Returns parsed number as a string."
description: "whole parses a positive whole number without leading zeros, e.g. '0', '7', '420'. Returns a decimal number obtained using parseInt with radix of 10."
---

```typescript {{ withLineNumbers: false }}
function whole(): Parser<string>
function whole(): Parser<number>
```

## Description

`whole` parses a positive whole number without leading zeros, e.g. `0`, `7`, `420`. Returns parsed number **as a string**.
`whole` parses a positive whole number without leading zeros, e.g. `0`, `7`, `420`. Returns **a decimal number** obtained using [parseInt] with radix of 10.

## Usage

Expand All @@ -29,7 +29,7 @@ const Parser = whole()
{
isOk: true,
pos: 2,
value: '42'
value: 42
}
```

Expand All @@ -45,3 +45,7 @@ const Parser = whole()
}
```
</details>

<!-- Links. -->

[parseInt]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt
32 changes: 23 additions & 9 deletions src/__tests__/parsers/numbers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@ import { describe, testFailure, testSuccess } from '../@helpers'

describe('hex', (it) => {
it('should succeed if given a hexadecimal number', () => {
const tcases = ['0x1F', '0X1F', '0x1f', '0X1f']
tcases.forEach((tcase) => testSuccess(tcase, tcase, hex()))
const tcases = [
['0x1F', 31],
['0X1F', 31],
['0x1f', 31],
['0X1f', 31]
] as const

tcases.forEach(([tcase, tresult]) => testSuccess(tcase, tresult, hex()))
})

it('should fail if given a non-hexadecimal number', () => {
Expand All @@ -15,8 +21,12 @@ describe('hex', (it) => {

describe('binary', (it) => {
it('should succeed if given a binary number', () => {
const tcases = ['0b10', '0B10']
tcases.forEach((tcase) => testSuccess(tcase, tcase, binary()))
const tcases = [
['0b10', 2],
['0B10', 2]
] as const

tcases.forEach(([tcase, tresult]) => testSuccess(tcase, tresult, binary()))
})

it('should fail if given a non-binary number', () => {
Expand All @@ -27,8 +37,12 @@ describe('binary', (it) => {

describe('octal', (it) => {
it('should succeed if given an octal number', () => {
const tcases = ['0o42', '0O42']
tcases.forEach((tcase) => testSuccess(tcase, tcase, octal()))
const tcases = [
['0o42', 34],
['0O42', 34]
] as const

tcases.forEach(([tcase, tresult]) => testSuccess(tcase, tresult, octal()))
})

it('should fail if given a non-octal number', () => {
Expand All @@ -40,7 +54,7 @@ describe('octal', (it) => {
describe('whole', (it) => {
it('should succeed if given a whole number', () => {
const tcases = ['0', '1', '42', '1000']
tcases.forEach((tcase) => testSuccess(tcase, tcase, whole()))
tcases.forEach((tcase) => testSuccess(tcase, parseInt(tcase, 10), whole()))
})

it('should fail if given a non-whole number', () => {
Expand All @@ -52,7 +66,7 @@ describe('whole', (it) => {
describe('integer', (it) => {
it('should succeed if given an integer number', () => {
const tcases = ['0', '1', '-1', '42', '-42']
tcases.forEach((tcase) => testSuccess(tcase, tcase, integer()))
tcases.forEach((tcase) => testSuccess(tcase, parseInt(tcase, 10), integer()))
})

it('should fail if given a non-integer number', () => {
Expand All @@ -64,7 +78,7 @@ describe('integer', (it) => {
describe('float', (it) => {
it('should succeed if given an float number', () => {
const tcases = ['0.25', '4.20', '-42.0']
tcases.forEach((tcase) => testSuccess(tcase, tcase, float()))
tcases.forEach((tcase) => testSuccess(tcase, parseFloat(tcase), float()))
})

it('should fail if given a non-float number', () => {
Expand Down
Loading

1 comment on commit 24406ab

@vercel
Copy link

@vercel vercel bot commented on 24406ab Oct 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.