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

jsbuddy

v1.0.4

Published

Javascript immutability helper function

Downloads

8

Readme

jsBuddy

Provides modelUpdate() helper function to easily update an ARRAY or OBJECT without mutating the existing array/object and easily update deep nested objects without having to spread the objects at each level.

npm i jsbuddy -S

modelUpdate

modelUpdate(source, model, variables)

source is the existing object

model is the object with changes you would like to make

variables is the object which contains any variables used in the model

Object Example:

import {modelUpdate} from 'jsbuddy';
const source = {
    name: 'Sum User',
    age: 24,
    hobbies: {
        chess: true,
        sudoku: false,
        badminton: true
    }
};

const updatedSource = modelUpdate(source, {
    name: {
        $set: 'Some User'
    },
    hobbies: {
        $merge: {
            sudoku: true,
            cricket: true
        },
        $unset: ['badminton']
    }
});

Output:

{
    name: 'Some User',
    age: 24,
    hobbies: {
        chess: true,
        sudoku: true,
        cricket: true
    }
}

Array Example:

const source = [
    { id: 2 },
    { id: 3}
];

const updatedSource = modelUpdate(source, {
    $push: [
        { id: 4 },
        { id: 5 }
    ],
    // Don't have to use an array when there is only 1 item
    $unshift: { id: 1 },
    $remove: [1] // Removes 1st item {id: 2}
});

Output:

    [ { id: 1 }, { id: 3 }, { id: 4 }, { id: 5 } ]

Array Operations

| Operation | Values (refer to examples) | | |----------|---| --- | | $set | Any | Replaces current value with passed value in the model | | $push | [items] or item | Adds the passed items or item to the end of an array | | $unshift | same as $push | Adds the passed items or item to the beginning of an array | | $remove | [array indices], {key: value}, {key: function() { }} function must return bool. |Removes the items which | | $splice | [startIndex, endIndex, item1] | Remove items and/or new items to array |

Object Operations

| Operation | Values (refer to examples) | | |----------|---| --- | | $set | Any | Replaces current value with passed value in the model | | $unset | [keys] or "key" | Removes specified keys from the object | | $merge | {newKey: newValue} | Add new properties to the existing object |

Examples


// General Example
const source = {
    store1: {
        products: [{
            id: 'p1'
        }]
    },
    store2: {
        products: [{
            id: 'p4'
        }]
    }
};

modelUpdate(source, {
    store1: {
        products: {
            $push: [{id: 'p2'}, {id: 'p3'}]
        }
    },
    store2: {
        products: {
            'p4': {
                $merge: {
                    name: 'Product 4'
                }
            }
        }
    }
});

// Example Array:
const source = [{ id: 1 }];

// $set examples
modelUpdate(source, {
    0: {
        $set: { id: 'one' }
    }
});

modelUpdate(source, {
    $1: {
        $set: { id: 'one' }
    }
}, {
    $1: function(item, index) {
        return index === 0;
    }
});

// $push or $unshift examples
modelUpdate(source, {
   $push: [{ id: 2 }]
   // or
   $push: { id: 3 }
});

// $remove
modelUpdate(source, {
    $remove: 0,
    // or 
    $remove: [0, 1],
    // or 
    $remove: {       // Removes item with matching object value
        id: 2
    },
    // or
    $remove: function(item, index) {  // Removes Array Item Matching object value
        retrun index === 0;
    },
    // or
    $remove: 'randomId123' // Remove array item object with key id / _id as randomId123
})

// $splice
modelUpdate(source, {
    $splice: [0, 10]    // Removes 10 items from 0
    // or
    $splice: [
        // Removes 1 item and adds 1 item
        [0, 1, {id: 3}], 
        // Removes nothing but adds 2 items
        [1, 0, {id: 4}, {id: 5}] 
    ]
})

// $merge
modelUpdate(source, {
    0: {
        $merge: {
            newId: 'SOME VALUE'
        }
    },
    // or
    '*': { // This will apply to all array items
        $merge: function(item, index) {
            return {
                newId: item.id * 10
            };
        }
    }
});

// $unset
modelUpdate(source, {
    '*': {
        $unset: 'id'
    }
    '*': {
        $unset: function(key, value) {
            return key === 'id';
        }
    }
});