A wrap/unwrap Proxy utility as answer to these weird limitations.
This module allows any primitive to complex value to be proxied in a way that any handler can understand once the unwrap(target) operation is performed among actions:
callbacks remain callbacks and arrays remain arrays unless proxy-target/array is used
primitives and null, undefined or object generic types are preserved, among any other primitive, and return {type: actualType, value: actualValue} once wrapped
Type / Value -> Wrap
type
value
wrap
"array"
[1, 2]
[1, 2] or [[1,2]] w/array
"bigint"
1n
{type: "bigint", value: 1n}
"boolean"
false
{type: "boolean", value: false}
"function"
(a, b) => {}
(a, b) => {} or Ctx.bind(fn) w/all
"null"
null
{type: "null", value: null}
"number"
1.2
{type: "number", value: 1.2}
Example / API
import { bound, // return a function that returns its bound context unbound,// if function, invokes it to return the context target, // create a `{type, value}` pair to proxy as target wrap, // returns array, function, or a pair unwrap // returns the wrapped value} from 'proxy-target';let proxied;proxied = wrap([1, 2, 3]);// remains [1, 2, 3]unwrap(proxied);// still [1, 2, 3]// both wrap and unwrap accept an optional callback// the returned value will the one returned by wrapproxied = wrap({a: 1}, (type, value) => target(type, value));// {type: "object", value: {a: 1}}
"object"
{a: 0}
{type: "object", value: {a: 0}}
"string"
""
{type: "string", value: ""}
"symbol"
Symbol()
{type: "symbol", value: Symbol()}
"undefined"
void 0
{type: "undefined", value: undefined}
"custom"
notArrOrFunc
{type: "custom", value: notArrOrFunc}
unwrap
(proxied, (type, value)
=>
value);
// {a: 1}
proxied = wrap(i => i + 123);
// remains i => i + 123
unwrap(proxied);
// i => i + 123
// bound / unbound
const callbacks = [
a => a + 1,
b => b + 2
];
proxied = wrap(
callbacks[1],
(type, value) => bound(
target(type, callbacks.indexOf(value))
)
);
// function () { return {type: "function", value: 1} );
unwrap(unbound(proxied), (type, value) => {
return type === "function" ? callbacks[value] : value;