Skip to content

Commit

Permalink
feat(parsers/letter): add letter and letters parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
norskeld committed Nov 28, 2021
1 parent 1a82c76 commit 2540b17
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/internal/parsers/letter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Parser } from '../state'

import { regexp } from './regexp'

const LETTER_RE = /\p{Letter}/gu
const LETTERS_RE = /\p{Letter}+/gu

export function letter(): Parser<string> {
return regexp(LETTER_RE, 'letter')
}

export function letters(): Parser<string> {
return regexp(LETTERS_RE, 'letters')
}
1 change: 1 addition & 0 deletions src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './internal/parsers/eof'
export * from './internal/parsers/float'
export * from './internal/parsers/integer'
export * from './internal/parsers/lazy'
export * from './internal/parsers/letter'
export * from './internal/parsers/nothing'
export * from './internal/parsers/regexp'
export * from './internal/parsers/rest'
Expand Down
65 changes: 65 additions & 0 deletions tests/internal/parsers/letter.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { letter, letters } from '@lib/internal/parsers/letter'

import { run, result, should } from '@tests/@helpers'

describe(letter, () => {
it('should succeed with an ASCII letter', () => {
const actual = run(letter(), 'A')
const expected = result('success', 'A')

should.matchState(actual, expected)
})

it('should succeed with a Unicode letter', () => {
const actual = run(letter(), 'Â')
const expected = result('success', 'Â')

should.matchState(actual, expected)
})

it('should fail if given something other than a letter', () => {
;['1', '+', '~', '`', ':', `'`].forEach((tcase) => {
const actual = run(letter(), tcase)
const expected = result('failure', 'letter')

should.matchState(actual, expected)
})
})
})

describe(letters, () => {
it('should succeed with an ASCII letter if given input with a single ASCII letter', () => {
const actual = run(letters(), 'A')
const expected = result('success', 'A')

should.matchState(actual, expected)
})

it('should succeed with a Unicode letter if given input with a single Unicode letter', () => {
const actual = run(letters(), 'Â')
const expected = result('success', 'Â')

should.matchState(actual, expected)
})

it('should succeed with letters if given input with letters', () => {
const actual = run(letters(), 'Âne')
const expected = result('success', 'Âne')

should.matchState(actual, expected)
})

it('should succeed with letters if given input with letters and other symbols', () => {
const actual = run(letters(), 'Âne+9000')
const expected = result('success', 'Âne')

should.matchState(actual, expected)
})

it('should fail if given something other than letters', () => {
const actual = run(letters(), '9000+Âne')
const expected = result('failure', 'letters')

should.matchState(actual, expected)
})
})
2 changes: 2 additions & 0 deletions tests/parsers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ it('should expose parsers', () => {
'integer',
'int',
'lazy',
'letter',
'letters',
'nothing',
'nil',
'regexp',
Expand Down

0 comments on commit 2540b17

Please sign in to comment.