1.0.0 • Published 1 year ago

object-like-map v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Object Like Map

Use maps as if they were objects, using object iternal methods.

Usage:

npm install object-like-map

And now you can use a Map as if it was an Object, while retaining most of Map's functionality:

import olm from 'object-like-map'

const myOLM = olm({ first: 1, second: 2 })

const third = myOLM.third = 3

const exists = 'first' in myOLM

delete myOLM.second

for (const [key, value] of myOLM) {
  console.log({ key, value })
}

myOLM.clear()

console.log(myOLM.size)

instead of:

const myMap = new Map()
myMap.set('first', 1)
myMap.set('second', 2)

myMap.set('third', 3)
const third = myMap.get('third')

const exists = myMap.has('first')

myMap.delete('second')

for (const [key, value] of myMap) {
  console.log({ key, value })
}

myMap.clear()

console.log(myMap.size)

Notably, if you set a value with a key that's an existing Map property/method you won't be able to read it:

myOLM.clear = 'myvalue!' // works
const myvalue = myOLM.clear // 'function clear() { [native code] }'

const exists = 'clear' in myOLM // works

This package is likely slow as it uses Proxies, but I haven't benchmarked it.

1.0.0

1 year ago