flow-typer-js
Version:
Declarative static and runtime type checking with Flow
33 lines (29 loc) • 956 B
JavaScript
//
import { getType } from '../utils.js'
import { validatorError } from '../error.js'
import { EMPTY_VALUE } from '../const.js'
import { undef } from './primitives.js'
import { object } from './object.js'
import { unionOf } from './union.js'
export const mapOf =
(
keyTypeFn ,
typeFn
) => {
function mapOf (value , _scope = 'Map') {
if (value === EMPTY_VALUE) return {}
const o = object(value, _scope)
const reducer = (acc , key ) =>
Object.assign(
acc,
{
// $FlowFixMe
[keyTypeFn(key, `${_scope}[_]`)]
:typeFn(o[key], `${_scope}.${key}`)
}
)
return Object.keys(o).reduce(reducer, {})
}
mapOf.type = () => `{ [_:${getType(keyTypeFn)}]: ${getType(typeFn)} }`
return mapOf
}