flow-typer-js
Version:
Declarative static and runtime type checking with Flow
20 lines (16 loc) • 648 B
JavaScript
//
import { validatorError } from '../error.js'
import { isBoolean, isNumber, isString } from '../is.js'
import { EMPTY_VALUE } from '../const.js'
export const literalOf =
(primitive ) => {
function literal (value , _scope = '') {
if (value === EMPTY_VALUE || (value === primitive)) return primitive
throw validatorError(literal, value, _scope)
}
literal.type = () => {
if (isBoolean(primitive)) return `${primitive ? 'true': 'false'}`
else return `"${primitive}"`
}
return literal
}