Security News
New Proposed CISA Mandate Would Require Critical Infrastructure to Report Ransom Payments Within 24 Hours
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
defaultdict-proxy
Advanced tools
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.
Weekly downloads
Readme
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.
yarn add defaultdict-proxy
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);
MIT
FAQs
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.
The npm package defaultdict-proxy receives a total of 444 weekly downloads. As such, defaultdict-proxy popularity was classified as not popular.
We found that defaultdict-proxy demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
CISA has proposed a set of new rules that would require critical infrastructure to report cyber incidents and ransom payments.
Security News
Redis is no longer OSS, breaking its explicit commitment to remain under the BSD 3-Clause License forever. This has angered contributors who are now working to fork the software.
Product
Socket AI now enables 'AI detected potential malware' alerts by default, ensuring users benefit from AI-powered state-of-the-art malware detection without needing to opt-in.