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

@mhesus/better-dynamic-properties

v0.2.0

Published

A library that improves upon the default dynamic property.

Downloads

7

Readme

@mhesus/better-dynamic-properties

A library just like dynamic properties, but a little better.

npm | github

If you use a bundler, you can install this via npm:

npm i @mhesus/better-dynamic-properties

Features

  • No type restrictions on values (options for custom serialization/deserialization)
  • Unlimited value size
  • .delete() method (no more .set(PROP, undefined))
  • .exists() method
  • .adjust() method (get and set in one line)
  • Lots of iterators: .ids(), .values(), .entries()

Examples

DynamicProperty.set()

A wide variety of types can be set, since under the hood, everything is serialized into JSON strings.

import { DynamicProperty } from "@mhesus/better-dynamic-properties";

DynamicProperty.set(thing, "example:number", 9001);
DynamicProperty.set(thing, "example:string", "Hello world!");
DynamicProperty.set(thing, "example:boolean", true);
DynamicProperty.set(thing, "example:object", { foo: "bar" });

DynamicProperty.get()

Getting properties back is just as simple.

DynamicProperty.get(thing, "example:string");
// >> Hello world!

DynamicProperty.get(thing, "example:object");
// >> { foo: "bar" }

DynamicProperty.get(thing, "example:doesnt_exist");
// >> undefined

DynamicProperty.exists()

Checking for existance is also simple.

DynamicProperty.exists(world, "example:doesnt_exist");
// >> false

DynamicProperty.exists(world, "example:does_exist");
// >> true

DynamicProperty.delete()

Deleting properties is now more intuitive, however you can still do it the old way if you like.

DynamicProperty.delete(thing, "example:id");

// calls DynamicProperty.delete internally
DynamicProperty.set(thing, "example:id", undefined);

DynamicProperty.adjust()

You can also adjust a property all in one method. Just pass in a function that performs and returns the modification you want.

DynamicProperty.adjust(thing, "example:number", (old) => old + 1);
// >> 9002 (adjust returns the new value)

// calling adjust is basically the same as this
const oldValue = DynamicProperty.get(thing, "example:increment");
const newValue = old + 1;
DynamicProperty.set(thing, "example:increment", newValue);

DynamicProperty Iterators

Iterating over the ids, values, or both of a property is now easy.

for (const id of DynamicProperty.ids(thing))

for (const value of DynamicProperty.values(thing))

for (const [id, value] of DynamicProperty.entries(thing))

You can also specify the namespace of the properties to get.

for (const id of DynamicProperty.ids(thing, { namespace: "example" }))
// >> example:id, example:other_id, ...

DynamicProperty.serialize() & DynamicProperty.deserialize()

Override the underlying serializiation/deserialization system can be done both across the library, or just in a single function.

// global override
DynamicProperty.serialize = (value, id) => /* ... */;
DynamicProperty.deserialize = (value, id) => /* ... */;

// local override
DynamicProperty.set(thing, "example:id", 4000, {
    serialize: (value, id) => /* ... */;
})
DynamicProperty.get(thing, "example:id", {
    deserialize: (value, id) => /* ... */;
})

Unresolved Issues

  • What should happen when trying to get a property that hasnt been created using this library? (doesnt have the format PROPID_CHUNKID)

    Explanation: The library currently doesn't acknowledge properties without the chunked property id format. This needs to change since the ability to fetch properties with simply the id PROPID is intuitive, however what should happen if chunked properties e.g. (PROPID_CHUNKID) and a property (PROPID) with the same property id co-exist? Which should property should be favored? The behaviour for this situation seems undefined so an error could be raised, but this doesn't feel right since nothing has really gone wrong.

    Potential solutions:

    • Raise an error. The expected behaviour is undefined currently, however nothing has really gone wrong so I'm unsatisfied with this solution.
    • Change the chunk naming scheme.
      • One option would be to remove the suffix from the 1st chunk. However, this now brings potential for naming collisions between properties. For example, a property called example_1 and the 2nd chunk of a property called example: example_1. I could prevent you from naming properties matching /.+_\d+/, however this is also an unsatisfying solution.