map-proxies

1.0.0 • Public • Published

Map proxies

Description

JavaScript proxy objects don't work with maps because they have native properties that are not proxied. This library make them work.

Usage

const { mapHandler } = require('map-proxies')

const handler = {
  get: (target, prop, receiver) => {
    if (prop === 'foo') {
      return 'bar'
    }
    return Reflect.get(target, prop, receiver)
  }
}
const newHandler = mapHandler(handler)

const nativeProxy = new Proxy(new Map(), handler)
const mapProxy = new Proxy(new Map(), newHandler)

console.log(nativeProxy.foo)
// => bar
nativeProxy.forEach(() => {})
// => throws
//    Uncaught TypeError: Method Map.prototype.forEach called on incompatible receiver #<Map>
//        at Proxy.forEach (<anonymous>)

console.log(mapProxy.foo)
// => bar
mapProxy.forEach(() => {})
// okay

API

mapHandler(handler)

The input is a proxy handler.

Returns a new proxy handler which wraps the input in a handler which make proxies work with maps.

Readme

Keywords

none

Package Sidebar

Install

npm i map-proxies

Weekly Downloads

1

Version

1.0.0

License

ISC

Unpacked Size

2.04 kB

Total Files

3

Last publish

Collaborators

  • vincentbailly