Skip to content

proxied

Create a dynamic proxied a object

75 bytes

Usage

Javascript’s Proxy object is powerful but a bit awkward to use. The _.proxied function creates the Proxy for you and handles calling back to your handler when functions on the Proxy are called or properties are accessed.

import * as
import _
_
from 'radashi'
type
type Property = "name" | "size" | "getLocation"
Property
= 'name' | 'size' | 'getLocation'
const
const person: Record<string, "Joe" | 20 | (() => "here")>
person
=
import _
_
.
proxied<Property, "Joe" | 20 | (() => "here")>(handler: (propertyName: Property) => "Joe" | 20 | (() => "here")): Record<string, "Joe" | 20 | (() => "here")>
export proxied

Creates a Proxy object that will dynamically call the handler argument when attributes are accessed.

@seehttps://radashi.js.org/reference/curry/proxied

@example

const proxy = proxied(propertyName => propertyName.toUpperCase())
proxy.foo // => "FOO"

@version12.1.0

proxied
((
prop: Property
prop
:
type Property = "name" | "size" | "getLocation"
Property
) => {
switch (
prop: Property
prop
) {
case 'name':
return 'Joe'
case 'size':
return 20
case 'getLocation'
return () => 'here'
}
})
const person: Record<string, "Joe" | 20 | (() => "here")>
person
.
"Joe" | 20 | (() => "here")
name
// => Joe
const person: Record<string, "Joe" | 20 | (() => "here")>
person
.
"Joe" | 20 | (() => "here")
size
// => 20
const person: Record<string, "Joe" | 20 | (() => "here")>
person
.
"Joe" | 20 | (() => "here")
getLocation
() // => here