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

objects-manager

v0.4.5

Published

A simple and powerful library for managing arrays of objects in a maintainable way

Downloads

8

Readme

Objects Manager

A tiny and powerful library for managing arrays of objects in a maintainable way

Usage Example

Init Store

import { Store } from 'objects-manager'

const store = new Store({
    uniqueKey: 'id',
    value: [] // value to start with
})

Store options

  • uniqueKey - Represents a unique key that must be on all objects in the store.
  • value - The initial value for the store.
  • deepMergeArrays (optional) - Could be a boolean or an array of strings that represents path`s to arrays inside individual object. Make it true or pass a path to an array inside the object that should be updated instead of replaced when using Store.upsert.

Use Store Value

// You have access to the store items via the store value property
store.value.forEach(item => console.log(item))

NOTE: Store.value cannot be mutated directly.

Subscribe to value

const onValue = value => {/* Do something with the value*/}
const subscription = store.subscribe(onValue)
// It will run on initial and when the value changes.

Unsubscribe from a previous subscription

// Use the unsubscribe method on the subscription object
subscription.unsubscribe()
// Alternatively you can use Store.unsubscribe method instead
// Pass the subscription handler as a parameter
store.unsubscribe(onValue)

Update or Insert items

// Insert new items to the store
// Returns the effected items
store.upsert([
    // example list
    { firstName: 'Delia', lastName: 'Nolan', id: 1 },
    { firstName: 'Clemens', lastName: 'Carter', id: 2 },
    { firstName: 'Kaitlin', lastName: 'Keeling', id: 3 },
    { firstName: 'Benny', lastName: 'Gibson', id: 4 },
    { firstName: 'Harrison', lastName: 'Jones', id: 5 },
])
// You can also put in a single item
store.upsert({ firstName: 'Nathan', lastName: 'Olson', id: 6 })
// Update items example
store.upsert([
    { id: 2, lastName: 'Harvey'},
    { id: 5, firstName: 'Doug' }
])
// output [{ firstName: 'Clemens', lastName: 'Harvey', id: 2 }, { firstName: 'Doug', lastName: 'Jones', id: 5 }]

Delete items from the store

// Pass a callback to determine whether the item should be deleted or not
// Returns the effected items
store.delete(item => item.id === 5)
// output [{ firstName: 'Harrison', lastName: 'Jones', id: 5 }]

Clear all items from the store

// If you do not pass an callback to the delete method all items will be removed
store.delete()

React Usage Example

import { useStore } from 'objects-manager/dist/react'
const Example = ()=> {
    // init store
    const users = useStore(()=> ({
        uniqueKey: 'id',
        value: []
    }))
    // log when value changed
    useEffect(
        ()=> console.log(`Value Changed`),
        [users.value]
    )
    // Insert new items
    // In usual should take a place after some call to an api
    useEffect(
        ()=> {
            users.upsert([
                { name: 'Delia', id: 1 },
                { name: 'Clemens', id: 2 },
                { name: 'Kaitlin', id: 3 },
                { name: 'Benny', id: 4 },
                { name: 'Harrison', id: 5 },
            ])
        },
        []
    )
    return (
        <ul>
            {users.value.map(user => <li key={user.id}>{user.name}</li>)}
        </ul>
    )
}