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

defaultdict-proxy

v1.0.1

Published

This is a port of [python's defaultdict](https://docs.python.org/2/library/collections.html#defaultdict-objects) to JavaScript. It uses proxies to create objects that return values other than ```undefined``` for attributes that haven't yet been set.

Downloads

963

Readme

JavaScript defaultdict

This is a port of python's defaultdict to JavaScript. It uses proxies to create objects that return values other than undefined for attributes that haven't yet been set.

This package needs native Proxy support to work.

Install

yarn add defaultdict-proxy

Usage

If called with undefined, null, a boolean, number, string or symbol, defaultdict returns a proxy object that returns this value for unset attributes. If called with an array or object, accessing unset attributes returns a shallow copy of the array or object. If defaultdict is given a function, it returns a proxy object that has unset attributes initialized to the result of executing the function.

import defaultdict from 'defaultdict-proxy';

// This defaultdict returns fresh empty arrays for unset attributes
const dd1 = defaultdict([]);

// defaultdicts act like normal objects
dd1.a = { b: 0 };
dd1.a.b === 0; // true

// We can use attributes without setting them first
dd1.b.push(42);
dd1.b; // [42]
dd1.c; // []
dd1.d === dd1.e; // false, every attribute gets its own copy

// A defaultdict that initializes unset attributes with the time of their
// first access
const dd2 = defaultdict(() => new Date());

// The attribute initialization function is given the name of the attribute
// as a parameter. This defaultdict initializes unset attributes to their
// name
const dd3 = defaultdict(attr => attr);
dd3.blah // 'blah'

// To create defaultdicts that initialize unset attributes with functions,
// we have to pass defaultdict a higher order function. In this example,
// unset attribute accesses return the identity function. 
const dd4 = defaultdict(() => x => x);

License

MIT