-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c9f2f1
commit 3e64629
Showing
2 changed files
with
84 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,39 @@ | ||
import Spec from './spec' | ||
import { getName } from '../util' | ||
import { invalid } from '../symbols' | ||
|
||
export class Enum extends Spec { | ||
conform(value) { | ||
if (this.options.values.indexOf(value) !== -1) { | ||
return value | ||
} | ||
return invalid | ||
} | ||
|
||
toString() { | ||
return this.name || `Enum(${this.options.values.map(p => getName(p)).join(", ")})` | ||
} | ||
|
||
explain(path, via, value) { | ||
if (this.options.values.indexOf(value) === -1) { | ||
return [{ | ||
path, | ||
via: [...via, getName(this)], | ||
value, | ||
predicate: function contains(x) { | ||
return this.options.values.indexOf(x) !== -1 | ||
} | ||
}] | ||
} | ||
return null | ||
} | ||
} | ||
|
||
export default function oneOf(...values) { | ||
if (values.length === 0) { | ||
throw new Error(`Cannot use Enum spec without values`) | ||
} | ||
return new Enum({ | ||
values | ||
}) | ||
} |
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,45 @@ | ||
import { expect } from 'chai' | ||
import oneOf from '../../lib/spec/enum' | ||
import * as p from '../../lib/predicates' | ||
import { invalid } from '../../lib/symbols' | ||
import { explainData } from '../../lib/util' | ||
|
||
describe("enum", () => { | ||
describe("conform", () => { | ||
it("works with numbers", () => { | ||
const e = oneOf(1, 2, 3) | ||
expect(e.conform(1)).to.equal(1) | ||
expect(e.conform(2)).to.equal(2) | ||
expect(e.conform(3)).to.equal(3) | ||
expect(e.conform("3")).to.equal(invalid) | ||
expect(e.conform(4)).to.equal(invalid) | ||
}) | ||
|
||
it("works with symbols", () => { | ||
const s1 = Symbol() | ||
const s2 = Symbol() | ||
const e = oneOf(s1, s2) | ||
|
||
expect(e.conform(s1)).to.equal(s1) | ||
expect(e.conform(s2)).to.equal(s2) | ||
expect(e.conform(Symbol())).to.equal(invalid) | ||
}) | ||
}) | ||
|
||
describe("explain", () => { | ||
it("outputs expected data", () => { | ||
const e = oneOf(1, 2, 3) | ||
const problems = explainData(e, 4) | ||
|
||
expect(problems).to.be.an("array").and.have.length(1) | ||
expect(problems).to.have.deep.property("[0].via") | ||
.that.deep.equals(["Enum(1, 2, 3)"]) | ||
expect(problems).to.have.deep.property("[0].path") | ||
.that.deep.equals([]) | ||
expect(problems).to.have.deep.property("[0].predicate") | ||
.that.is.a("function") | ||
expect(problems).to.have.deep.property("[0].value") | ||
.that.deep.equals(4) | ||
}) | ||
}) | ||
}) |