Skip to content

Commit

Permalink
Add asInteger (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
sz-piotr authored Aug 8, 2019
1 parent c4eb986 commit 473df15
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
12 changes: 12 additions & 0 deletions sanitizers/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ yarn add @restless/sanitizers
- [`cast`](#cast)
- [`asString`](#asstring)
- [`asNumber`](#assumber)
- [`asInteger`](#asinteger)
- [`asBoolean`](#asboolean)
- [`asMatching`](#asmatching)
- [`asObject`](#asobject)
Expand Down Expand Up @@ -64,6 +65,17 @@ asNumber('boo', 'path') // Result.error([{expected: 'number', path: 'path'}])
asNumber({}, 'path') // Result.error([{expected: 'number', path: 'path'}])
```

### `asInteger`

Same as [`asNumber`](#asnumber), but does not accept floating point values.

```javascript
asInteger('123', 'path') // Result.ok(123)
asInteger(0.2, 'path') // Result.error([{expected: 'integer', path: 'path'}])
asInteger('boo', 'path') // Result.error([{expected: 'integer', path: 'path'}])
asInteger({}, 'path') // Result.error([{expected: 'integer', path: 'path'}])
```

### `asBoolean`

Accepts any value that is a number or a string that represents a boolean (`"true"` or `"false"`). Returns a number.
Expand Down
2 changes: 1 addition & 1 deletion sanitizers/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@restless/sanitizers",
"version": "0.2.0",
"version": "0.2.1",
"author": "Piotr Szlachciak <[email protected]>",
"description": "Data sanitization in a functional way",
"license": "Unlicense",
Expand Down
10 changes: 10 additions & 0 deletions sanitizers/src/asInteger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { asChecked } from './asChecked'
import { asNumber } from './asNumber'
import { withErrorMessage } from './withErrorMessage'

const isInteger = (num: number) => num === Math.floor(num)

export const asInteger = withErrorMessage(
asChecked(asNumber, isInteger),
'integer'
)
1 change: 1 addition & 0 deletions sanitizers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { asArray } from './asArray'
export { asBoolean } from './asBoolean'
export { asChecked } from './asChecked'
export { asFlatMapped } from './asFlatMapped'
export { asInteger } from './asInteger'
export { asMapped } from './asMapped'
export { asMatching } from './asMatching'
export { asNumber } from './asNumber'
Expand Down
42 changes: 42 additions & 0 deletions sanitizers/test/asInteger.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { expect } from 'chai'
import { asInteger, Result } from '../src'

describe('asInteger', () => {
it('sanitizes strings containing integers', async () => {
const result = asInteger('-123', '')
expect(result).to.deep.equal(Result.ok(-123))
})

it('does not accept non-integer strings', async () => {
const result = asInteger('12abc', 'path')
expect(result).to.deep.equal(
Result.error([{ path: 'path', expected: 'integer' }])
)
})

it('sanitizes integers', async () => {
const result = asInteger(123, '')
expect(result).to.deep.equal(Result.ok(123))
})

it('does not accept floating point values', async () => {
const result = asInteger(123.45, 'path')
expect(result).to.deep.equal(
Result.error([{ path: 'path', expected: 'integer' }])
)
})

it('does not accept NaN', async () => {
const result = asInteger(NaN, 'path')
expect(result).to.deep.equal(
Result.error([{ path: 'path', expected: 'integer' }])
)
})

it('does not accept types other than integer or string', async () => {
const result = asInteger(true, 'path')
expect(result).to.deep.equal(
Result.error([{ path: 'path', expected: 'integer' }])
)
})
})

0 comments on commit 473df15

Please sign in to comment.