ryanve / interfaces

Interface ideas
0 stars 0 forks source link

mapping interface #8

Open ryanve opened 6 years ago

ryanve commented 6 years ago
function mapping(map) {
  const hash = Object.create(null)
  Object.assign(hash, map)
  Object.freeze(hash)
  for (let key in hash) {
    if (typeof hash[key] != "string") throw new Error(hash)
  }

  function get(id) {
    if (id in hash) return hash[id]
    throw new Error("Unknown key:", id)
  }

  get.keys = Object.freeze(Object.keys(hash))
  return Object.freeze(get)
}

module.exports = mapping;