Skip to content

Commit

Permalink
New Feature: add Char, closes #12
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Nov 5, 2018
1 parent 0ac7cd0 commit cee71ec
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,15 @@ For the `Prism` type, see the [monocle-ts](https://github.com/gcanti/monocle-ts)

## Builtin refinements

- `Char`
- `Integer`
- `Negative`
- `NegativeInteger`
- `NonNegative`
- `NonNegativeInteger`
- `NonPositive`
- `NonPositiveInteger`
- `NonEmptyString`
- `NonZero`
- `NonZeroInteger`
- `Positive`
Expand Down
9 changes: 9 additions & 0 deletions src/Char.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Prism } from 'monocle-ts'
import { Newtype, prism, URIOf } from './'
import { NonEmptyString } from './NonEmptyString'

export interface Char extends Newtype<URIOf<NonEmptyString> & { readonly Length: 1 }, string> {}

export const isChar = (s: string) => s.length === 1

export const prismChar: Prism<string, Char> = prism<Char>(isChar)
11 changes: 11 additions & 0 deletions test/Char.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import * as assert from 'assert'
import { none, some } from 'fp-ts/lib/Option'
import { prismChar } from '../src/Char'

describe('Char', () => {
it('prismChar', () => {
assert.deepEqual(prismChar.getOption('a'), some('a'))
assert.deepEqual(prismChar.getOption('ab'), none)
assert.deepEqual(prismChar.getOption(''), none)
})
})
1 change: 1 addition & 0 deletions test/NontEmptyString.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { prismNonEmptyString } from '../src/NonEmptyString'
describe('NonEmptyString', () => {
it('prismNonEmptyString', () => {
assert.deepEqual(prismNonEmptyString.getOption('a'), some('a'))
assert.deepEqual(prismNonEmptyString.getOption('ab'), some('ab'))
assert.deepEqual(prismNonEmptyString.getOption(''), none)
})
})

0 comments on commit cee71ec

Please sign in to comment.