setdefault

1.0.2 • Public • Published

setdefault

Small utility function, which I got used to in Python and miss in JS. The name is a little misleading, but that's the name in Python. I think of it as "get (after setting, if necessary)".

This is useful for that pattern where if a key hasn't been used yet, you have to first set it to a default value.

In typical JS, you end up either doing the lookup twice, or having extra variables floating around.

Replace this:

 const results = {}
 for (let p of items) {
    if (!results[p.bucket]) results[p.bucket] = []     // ewwww
    results[p.bucket].push(p)
 }

with this:

 const setdefault = require('setdefault')
 // ..
 const results = {}
 for (let p of items) {
    setdefault(results, p.bucket, []).push(p)   // nicer!
 }

Works for Object or Maps.

lazy mode

If the default value is going to be expensive to create, or have a side effect, there's a variation:

setdefault.lazy(obj, prop, f)
 
function f () {
  // return some big default value
}

We also have pre-defined variations for some common cases, just to make them a tiny bit more efficient (no extra object to garbage collect), maybe.

  • setdefault.array == setdefault.lazy( , , () => [])
  • setdefault.object == setdefault.lazy( , , () => ({}))
  • setdefault.map == setdefault.lazy( , , () => new Map())

Readme

Keywords

Package Sidebar

Install

npm i setdefault

Weekly Downloads

1

Version

1.0.2

License

MIT

Unpacked Size

7.29 kB

Total Files

5

Last publish

Collaborators

  • sandhawke