generated from norskeld/serpent
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parsers/letter): add
letter
and letters
parsers
- Loading branch information
Showing
4 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters