@webreflection/utils

Coverage Status

Social media photo by Benjamin Lehman on Unsplash

A curated, TypeScript-friendly collection of utilities:


Background

I’ve written too many micro-utilities. When I realized I couldn’t even remember their names or where to find them, I decided to create this module. The philosophy behind it is pretty simple:

That’s it. If you keep solving or rewriting the same patterns, take a look here — and the dedicated docs page goes deeper.

I’ll gradually deprecate, archive, and abandon the older micro-utilities that landed here. For now, I just want one place I can trust and use as needed.


Map put convention

Every utility that subclasses Map or WeakMapmap, cache, registry, and weakmap — adds a put(key, value) method. It stores the entry like set, but returns the stored value instead of the map reference.

That replaces the awkward get-or-insert dance where set returns the map, not the value, and the initializer cannot be deferred:

// before: value is always computed; set returns the map, not the value
const value = map.get(key) || (map.set(key, expensive()), map.get(key));

// after: expensive() runs only when the key is absent
const value = map.get(key) ?? map.put(key, expensive());

Use set when map chaining is needed; use put when the stored value should flow into the next expression.

json-storage is not a Map subclass, but its Map-like API follows the same put contract.

Set put convention

Every utility that subclasses Set or WeakSetset and weakset — adds a put(value) method. It stores the entry like add, but returns the value instead of the set reference.

That replaces the awkward membership dance where add returns the set, not the value, and a separate reference is needed to keep using the entry:

// before: add returns the set, not the value
set.has(value) ? value : (set.add(doSomethingWith(value)), value);

// after: put returns the value, not the set
set.has(value) ? value : set.put(doSomethingWith(value));

Use add when set chaining is needed; use put when the stored value should flow into the next expression.

MIT-style license.