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

easy-copy

v1.0.1

Published

Copy some properties of an object easily

Downloads

11

Readme

easy-copy

Build Status Coverage Status

Copy some properties of an object easily 中文文档

Install

By npm

$ npm install easy-copy

By bower

$ bower install easy-copy

Usage

easyCopy(src [, filter [, opt]])

  • src: The source object, where all of the props comes from
  • filter: The filter condition, it could be a String, Array, or an Object, and they could be nested deeply
  • opt: The options for copy operation

Get Start

If you often write some code like this:

const data = {
    name: body.name,
    age: body.age,
    major: body.major,
    email: body.email
}

You must fell tired very much. How can we do if we use easy-copy?

// Import easy-copy module
const easycopy = require('easy-copy');

const data = easycopy(body, ['name', 'age', 'major', 'email']);
// Yes! Just write an array :)

Deeply copy

If an object is nested like this:

const foo = {
  id: 1,
  children: [
    { id: 2, children: [] },
    {
      id: 3,
      children: [
        { id: 4, children: [] },
        { id: 5, children: [] }
      ]
    },
    { id: 6, children: [] }
  ]
}

We can process this object easily using easy-copy. For example, if we just want to keep the second property of the children property, we can do like this:

const bar = easycopy(foo, {children: 1})

console.log(bar);
// {
//   children: [
//     {
//       id: 3,
//       children: [
//         { id: 4: children: [] },
//         { id: 5: children: [] }
//       ]
//     }
//   ]
// }

OK, but if I want copy the 2 properties which id is 4 and 5, how can I do? You can write like this:

const baz = easycopy(foo, {children: [{1: {children: 0}}, 2]});

console.log(baz);
// {
//   children: [
//     {
//       children: [
//         { id: 4, children: [] }
//       ]
//     },
//     { id: 6, children: [] }
//   ]
// }

Yeah! It's so easy!

Options

The third argument of easycopy() is options. At present, the v1.0 version is support only one option: undefined. it could transform the non existed prop of src object set to undefined automatically. The default value is true, in other words, it relay on the filter argument to set the target properties.

Here are some example:

const foo = {
  a: 1,
  b: 2,
  c: {
    d: 1,
    e: 2
  }
}

const bar = easycopy(foo, [{c: ['d', 'z']}, {f: 'h'}]);
// {
//   c: {
//     d: 1,
//     z: undefined
//   },
//   f: {
//     h: unfined
//   }
// }

const baz = easycopy(foo, [{c: ['d', 'z']}, {f: 'h'}], {undefined: false});
// {
//   c: {
//     d: 1,
//   },
// }

So you can decide how to filter and copy an object by your self, and you will not meet with some error like Cannot read property 'xxx' of undefined, it is so nasty, isn't it?

Licence

MIT