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

sweet-virtual-values

v0.1.0

Published

Virtual Values (Value Proxies)

Downloads

3

Readme

Sweet Virtual Values

Sweet Virtual Values is an extension to JavaScript Proxies that uses sweet.js to allow you to trap operations on primitive values. This means that you can make some pretty wild extensions in just a few lines of code.

For example, in a few lines of code you can implement complex numbers with native syntax.

console.log(1 + (1 * i) - (100 + (3 * i)));
// logs: -99 + -2i

Or dynamic units:

var meter = makeUnit("meter");
var second = makeUnit("second");
var g = 9.81 * meter / second / second;

g + 1;
// throws error because units are not compatible

Or dynamic taint tracking.

// taint the string
var username = taint("Robert`); DROP TABLE Students;");
// The `queryUser` function will check for tainted values and
// throw an error if they are tainted. Note that the
// taint propagates through string concatenation.
queryUser("select * from Students where username = '" + username + "'");

Check out the examples in action here. Virtual values are based on ideas from the paper Virtual Values for Language Extension.

Install

Install via npm:

npm install -g sweet-virtual-values

Compile and run in node:

vjs file.js | node --harmony

Or just compile to a file:

vjs -o out.js file.js

Running in node requires the --harmony flag and the harmony-reflect package (get it with npm install harmony-reflect if you are going to run it in your own project).

Using

Sweet virtual values works by extending the Proxy handler API. Proxies can now wrap primitive values and can now understand three new traps: unary for unary operations, left for binary operations where the proxy is on the left, and right for binary operations where the proxy is on the right.

var p = new Proxy(4, {

    // `target` is the wrapped value (`4` in this case)
    // `op` is a string representing the operation ("-", "!", etc.)
    unary: function(target, op) {
        // ...
    },

    // `target` is the wrapped value
    // `op` is a string representing the operation ("*", "+", etc.)
    // `right` is the value on the right-hand side of the operation
    left: function (target, op, right) {
        // ...
    },

    // right is only called if the value on the left is not a proxy
    // `target` is the wrapped value
    // `op` is a string representing the operation ("*", "+", etc.)
    // `left` is the value on the left-hand side of the operation
    right: function(target, op, left) {
        // ...
    }
}, {});

A trap can be omitted in which case the default behavior for each operation is performed.

In addition, there is a third argument to the Proxy constructor which is the key for that virtual value. This key is an object that can be used to retrieve the handler via the unproxy function.

var handler = { /* ... */ };
var key = {};
var p = new Proxy(4, handler, key);

// ...

unproxy(p, key) === handler;

unproxy returns null if the key does not match.

This allow you to create multiple virtual value extensions and to detect if a value is a particular kind of extension based on its key.