-
Notifications
You must be signed in to change notification settings - Fork 328
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The `enum` is a popular aspect of TypeScript, and can be embedded in interfaces as a concise, but descriptive, shorthand for literal string unions: ```typescript enum Colour { White = '000000', Black = 'ffffff' } ``` This change adds an exported member `enum` to `io-ts`, based on [this suggestion][1] by @noe132 It means that `enum`s can be reused directly in `io-ts`: ```typescript const T = t.enum(Colour) ``` [1]: #216 (comment)
- Loading branch information
Alec Gibson
committed
Sep 17, 2019
1 parent
647e530
commit 068f16a
Showing
6 changed files
with
116 additions
and
13 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,53 @@ | ||
import * as assert from 'assert' | ||
import * as t from '../src/index' | ||
import { assertSuccess, assertFailure } from './helpers' | ||
|
||
describe('enum', () => { | ||
enum A { | ||
Foo = 'foo', | ||
Bar = 'bar' | ||
} | ||
|
||
describe('name', () => { | ||
it('should assign a default name', () => { | ||
const T = t.enum(A) | ||
assert.strictEqual(T.name, 'Enum') | ||
}) | ||
|
||
it('should accept a name', () => { | ||
const T = t.enum(A, 'T') | ||
assert.strictEqual(T.name, 'T') | ||
}) | ||
}) | ||
|
||
describe('is', () => { | ||
it('should check an enum value', () => { | ||
const T = t.enum(A) | ||
assert.strictEqual(T.is(A.Foo), true) | ||
assert.strictEqual(T.is('bar'), true) | ||
assert.strictEqual(T.is('invalid'), false) | ||
assert.strictEqual(T.is(null), false) | ||
assert.strictEqual(T.is(A), false) | ||
}) | ||
}) | ||
|
||
describe('decode', () => { | ||
it('should decode an enum value', () => { | ||
const T = t.enum(A) | ||
assertSuccess(T.decode(A.Foo), A.Foo) | ||
assertSuccess(T.decode('bar'), 'bar') | ||
}) | ||
|
||
it('should fail decoding an invalid value', () => { | ||
const T = t.enum(A) | ||
assertFailure(T, 'invalid', ['Invalid value "invalid" supplied to : Enum']) | ||
}) | ||
}) | ||
|
||
describe('encode', () => { | ||
it('should encode an enum value', () => { | ||
const T = t.enum(A) | ||
assert.deepStrictEqual(T.encode(A.Foo), A.Foo) | ||
}) | ||
}) | ||
}) |