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

@techforge/easy-set

v1.0.0

Published

This project was created to simplify the process of manipulating data structures in JavaScript. It provides a simple API for setting nested object values, as well as a chainable mutator class for performing sequential data transformations.

Downloads

3

Readme

Easy Set

Introduction

This project was created to simplify the process of manipulating data structures in JavaScript. It provides a simple API for setting nested object values, as well as a chainable mutator class for performing sequential data transformations.

Motivation

Working with nested structure containing optional fields can be a pain. It is often necessary to check for the existence of a field before setting it, and this can lead tu a lot of boilerplate code. I've often found myself wanting to write code like this:

data.x.y.z = "value";

But instead, due to the possibility of the intermediate fields being possibly null, I have to write this:

data.x ??= {};
data.x.y ??= {};
data.x.y.z = "value";    

To make life easier for myself, I created a utility function called easySet that allows you to set nested object values without having to check for the existence of intermediate fields.

Installation

To use this library in your project, install it via npm:

npm install easy-set

Features

Easy Set

To make it easy to manipulate object with potentially null field, this library provides the easySet function, which takes an object and a callback function. The callback function is passed a proxy of the object allowing for easy modification but once the callback is complete, the proxy is discarded and only the original object is available.

easySet(data, (o) => o.x.y.z = "value") // data = { x: { y: { z: "value" } } }

Easy Set Proxy

The easySetProxy function takes an object and returns a proxy of that object that will automatically initialize any fields that are accessed. This allows the easy data.x.y.z = "value" syntax to work regardless of whether the intermediate fields are set.

The original, unproxied object can be accessed via the easyOriginal property of the proxy.

A side effect of the proxy is that it becomes impossible to check for missing fields. If you attempt a check like:

if (data.x === undefined) {
  // do something
}

it will never evaluate to true, because the attempt to read x will populate it with an empty object. Because of this, it is likely you will want to stick with the easySet function.

Usage

JavaScript

import { easySet, easySetProxy } from "easy-set"

const data = {}
easySet(data, (o) => o.x.y.z = "value") // data = { x: { y: { z: "value" } } }

// or using a proxy
const data = easySetProxy({})
data.x.y.z = "value" // data = { x: { y: { z: "value" } } }

// get the original unproxied data
const originalData = data.easyOriginal

TypeScript

import { easySet, easySetProxy } from "easy-set"

// working with arbitrary data
const data = {}
easySet(data as any, (o) => o.x.y.z = "value") // data = { x: { y: { z: "value" } } }

// working with typed data
type MyType = {
  x?: {
    y?: {
      z?: string;
    }
  }
}
const myType: MyType = {}
easySet(myType, (o) => o.x.y.z = "value") // data = { x: { y: { z: "value" } } }

// this would generate a typescript error
// easySet(data, (d) => x.foo = "bar"); 

// using a proxy
const proxiedData = easySetProxy<MyType>({})
proxiedData.x.y.z = "value" // proxiedData = { x: { y: { z: "value" } } }

// get the original unproxied data
const originalData = proxiedData.easyOriginal

Immer

If you like immutability, easySet plays well with immer. If you pass an immer proxy to easySet, you can happily set any fields and immer will protect the original object from modification.

type Obj = {
  a?: { b?: { foo?: string } }
}
const original: Obj = {}
const modified = produce(original, (draft) => {
  easySet(draft, (d) => (d.a.b.foo = "bar"))
})