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

spreadem

v1.0.6

Published

A simple package that uses spread operator to return a new copy of an object using ES6 spread operator. It is mostly compatible with older browsers.

Downloads

5

Readme

Spread'em

spread'em is a really small, relatively useless package that will simply take an object and return a new shallow copy using the spread operator.

Usage

To run the example:

npm install --save spreadem
npm run example

The example will log out 2 objects - the original object and a new copy of the object that has been mutated. Note that the original object has not been altered.

Q: Why would you use this package?

A: You probably shouldn't. It's dumb and you could easily whip up this functionality yourself.

Q: Then why did you make this package in the first place?

A: The purpose of this package is simply to explore publishing a package to the mighty npm Registry. spreadem is not intended for production, use it entirely at your own risk. If you want to make a PR to this repo to make it better, go for it.

Reason for creating this package

While using React, I have often seen people create what they believe to be a new 'copy' of state called newState, manipulate newState, then run this.setstate(newState);. In React, this is a big no-no, you are supposed to call setState();.

Little do they realise that they have now just mutated state directly, because by using let newState = this.state; we are actually creating a new reference to the original object, not creating a new object. This is a nuance of JavaScript, more useful info can be found here.

Incorrect Example:

let newState = this.state; // newState is not brand new, it is just a reference to this.state.
newState.someProperty = "new value"; // Changing this.state changes newState and vice-versa. They are one and the same thing!
this.setState(newState); // pointless - you have already manipulated state.
// Do not pass Go, do not collect $200.

Naughty naughty! Doing the above is the same as the following:

this.state.someProperty = "new value"; // mutating state directly - oh the shame!

Remember, we are not supposed to mutate state directly ourselves. Instead we should pass an updated copy of state/key-value to setState(). From there, React queues up the state changes and asynchronously updates state in the most efficient way. Correct example:

let newState = {...this.state}; // make a new 'copy'
newState.someProperty = "new value"; // change the copy
this.setState(newState);

Using spread'em:

let newState = spreadem(this.state);
newState.someProperty = "new value";
this.setState(newState);

Under the hood, here's a simplified version of how spread'em works:

export const spreadem = (object) => {
    return {...object}; // yes it's really that simple
}

Future functionality

I would like to increase the scope of spread'em to work with arrays as well:

export const spreadem = (array) => {
    return [...array];
}

Todo list

  • Upgrade to work with arrays.