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

defaultdict2

v1.1.1

Published

Dictionary-like object with a handler for missing keys

Downloads

10

Readme

defaultdict2

Returns a new dictionary-like object

Behaves exactly like Python's defaultdict.

npm install --save defaultdict2

defaultdict(default_factory, initial_dict)

default_factory: value or function to be used for missing keys initial_dict: (optional) dictionary to start with

See this live demo to check whether your browser implements Proxies (Edge, FF, Chrome).

Getting started

The first argument is the value for missing keys - it can be a value or function like in Python.

var defaultdict = require('defaultdict2');
var d = defaultdict(0);
d.a++; 
d.b++;
console.log(d);
// { a: 1, b: 1 }

The same example using a function:

var d = defaultdict(()=>0);
d.a++; 
d.b++;
console.log(d);
// { a: 1, b: 1 }

The function will be called with two arguments target_dict,name.

Initial dictionary

You can add an initial dictionary as second argument.

var d = defaultdict(0, {a: 1});
d.b++;
console.log(d);
// { a: 1, b: 1 }

Nested dictionaries

There is no limit to your creativity!

var d = defaultdict(()=>defaultdict(0));
d.a.b++;
console.log(d);
// { a: { b: 1 } }

Using in a browser

Unfortunately ES6 proxies aren't fully adopted in browsers, so this might not work in every browser.

However Edge, FF and Chrome (>49) support it.

Embedding in your client script

A) Traditional include

<script src="https://cdn.rawgit.com/greenify/defaultdict2/master/src/index.js"></script>

B) With webpack or browserify

If you use webpack or browserify, please require the browser-part of defaultdict:

var defaultdict = require('defaultdict/browser');

C) Bower

bower install --save defaultdict2

And then the script to your page

<script src=".../bower_components/defaultdict2/src/index.js"></script>

Gotchas

  1. Javascript doesn't allow string keys on dictionaries -> h[20] will create the key '20'
var p = defaultdict(0, {1: 1});
console.log(1 in p);
// false
console.log('1' in p);
// true
  1. Be careful when checking for existence. Either use in or set the d.default_factory to undefined
var p = defaultdict(0, {a: 1});
console.log('b' in p);
// false
p.default_factory = undefined;
console.log(p.b);
// undefined

Requirements

This depends on ES6 proxies. They should be part of your node version.

License

The MIT License