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

safeproxy

v1.0.0

Published

safe access of objects

Downloads

2

Readme

safeproxy

Safe, exception-free access to object properties in Javascript using Proxy objects.

usage

const safe = require('safeproxy');

const obj = safe({
	family: {
		wife: 'whitney',
		kids: [ 'june', 'charlie' ]
	}
});

// normal access
console.log(obj.family.wife);    // prints "whitney"
console.log(obj.family.kids);    // prints "[ 'june', 'charlie' ]"
console.log(obj.family.kids[0]); // prints "june"

// unsafe access made safe
console.log(obj.what.is.going.on.here);                           // prints "{}"
console.log(obj.can[3].even[123][432].handle.arrays[9]);          // prints "{}"
console.log(obj.family.kids[2].my.parents.want.another.grandkid); // prints "{}"

// even works with functions
console.log(obj.i.cant().believe[1][2][3].this.doesnt().throw);   // prints "{}"

how

A specialized Javascript Proxy object is used to override all property access (get) on the "safe" object. If the safe object encounters a property access for which it has no value, instead of returning undefined, it instead returns an empty Proxy object, allowing chained property access to safely continue. It works for object and array access, and even works for function calls.

Be aware that "safe" objects will make use of these property access trap handlers throughout their entire structure. There is minimal overhead imposed on objects utilizing these traps, so take that into account when using them.

why

Safe object access, particularly in testing, can be really useful and save a lot of ugly validation code.

let obj = JSON.parse(someMassiveStringOrNetworkResponse()),
	data;

// without safe access
data = obj && obj.foo && obj.foo.bar && obj.foo.bar.quux && obj.foo.bar.quux.lukasavage;

// or you could try
try {
	data = obj.foo.bar.quux.lukasavage;
} catch (e) {
	// handle exception
}

// or keep it clean with safe access
const safe = require('safeproxy');
data = safe(obj).foo.bar.quux.lukasavage;

And why did I write a module when others exist? I wanted safe access with the ability to have safe access on objects, arrays, and functions but NOT have:

requirements

  • For node.js usage, version 6.0 or higher is required.
  • For browser usage, check Can I Use? for Proxy support. As of the writing of these docs, all major browsers are supporting it (minus IE11).

big thanks