npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

proxy-target

v3.0.2

Published

A wrap/unwrap Proxy utility

Downloads

39,939

Readme

proxy-target

build status Coverage Status

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} | | "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} |

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 wrap
proxied = wrap({a: 1}, (type, value) => target(type, value));
// {type: "object", value: {a: 1}}
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;
});
// b => b + 2


proxied = wrap(null);
// {type: "null", value: null}
unwrap(proxied);
// null

proxied = wrap(1);
// {type: "number", value: 1}
unwrap(proxied);
// 1

proxied = wrap(false);
// {type: "boolean", value: false}
unwrap(proxied);
// false

proxied = wrap(Symbol());
// {type: "symbol", value: thatSymbol}
unwrap(proxied);
// thatSymbol

// ... and so on ...