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

obj-tweaks

v0.0.2

Published

Library to manipulate javascript object

Downloads

5

Readme

OBJ-TWEAKS

npm npm bundle size Twitter

Library to manipulate javascript object

Installation

npm i obj-tweaks
import 'obj-tweaks';
require('obj-tweaks');

Commands

  • {Object}.new().{Commands} => Create a deep copy of your object
  • {Object}.find(conditions)
  • {Object}.merge(data)
  • {Object}.update(conditions, data)
  • {Object}.add(position, data)
  • {Object}.delete(conditions)
  • {Object}.swap(conditions, position)
  • {Object}.exist(position)

Examples

// Working Object

const state = {
  data: {
    users: {
      123456: {
        _id: 123456,
        active: true,
        status: true,
        info: {
          name: 'Alessandro',
          age: 24,
          links: {
            blog: 'https://aloisio.work',
          },
        },
      },
    },
    favourites: {
      234567: {
        _id: 234567,
        active: true,
        status: true,
        info: {
          name: 'Alicia',
          age: 24,
        },
      },
    },
  },
  loading: false,
  error: null,
};

1. Find

/**
* Simple find
*/

state.find({ active: true })

// output
[
  {
    _id: 123456,
    active: true,
    ...
  },
  {
    _id: 234567,
    active: true,
    ...
  }
]

/**
* If you want information about users who have 24 years
*/

state.find({ age: 24 })

// output
[
  {
    name: 'Alessandro',
    age: 24,
    links: { blog: 'https://aloisio.work' }
  },
  {
    name: 'Alicia',
    age: 24
  }
]

/**
* But if you want the previous object, just add the parent property
*/

state.find({ 'info.age': 24 })

// output
[
  {
    _id: 123456,
    active: true,
    status: true,
    info: { name: 'Alessandro', age: 24, links: [Object] }
  },
  {
    _id: 234567,
    active: true,
    status: true,
    info: { name: 'Alicia', age: 24 }
  }
]

2. Merge

/**
* Simple merge
*/

state.merge({ 'info.age': 18 })

// output
{
  data: { 
    users: {
      '123456': {
        ...
        info: { age: 18, ... }
      }
    }, 
    favourites: {
      '234567': {
        ...
        info: { age: 18, ... }
      }
    } 
  },
  loading: false,
  error: null
}

2'. Combine

/**
* Combine find and merge
*/

state.find({ _id: 123456 }).merge({ 'info.age': 18 })

// output
[
  {
    _id: 123456,
    active: true,
    status: true,
    info: { name: 'Alessandro', age: 18, links: [Object] }
  }
]

3. Update

/**
* Update the working Object
*/

state.update({ _id: 123456 }, { status: false, 'info.age': 18 })

// output
{
  data: { 
    users: {
      '123456': {
        _id: 123456,
        active: true,
        status: false,
        info: { name: 'Alessandro', age: 18, links: [Object] }
      }
    }, 
    favourites: { '234567': [Object] } },
  loading: false,
  error: null
}

Of course, you can also add more conditions on find option and how many you want of depth for sub properties.

4. Add

/**
* Add an element
*/

state.add('users', {
  987665: {
    _id: 987665,
    active: true,
    status: true,
    info: {
      name: 'Serge',
      age: 40,
    },
  },
})

// output
{
  data: {
    favourites: {
      234567: {
        _id: 234567, active: true, info: { age: 24, links: { blog: 'https://atraversleslivres.be' }, name: 'Alicia' }, status: true,
      },
    },
    users: {
      123456: {
        _id: 123456, active: true, info: { age: 24, links: { blog: 'https://aloisio.work' }, name: 'Alessandro' }, status: true,
      },
      987665: {
        _id: 987665, active: true, info: { age: 40, name: 'Serge' }, status: true,
      },
    },
  },
  error: null,
  loading: false,
}

5. Delete

/**
* Delete an element
*/

state.delete({ _id: 123456 })

// output
{
  data: {
    favourites: {
      234567: {
        _id: 234567, active: true, info: { age: 24, links: { blog: 'https://atraversleslivres.be' }, name: 'Alicia' }, status: true,
      },
    },
    users: {},
  },
  error: null,
  loading: false,
}

6. Swap

/**
* Swap an element
*/

state.swap({ _id: 123456 }, 'favourites')

// output
{
  data: {
    favourites: {
      123456: {
        _id: 123456, active: true, info: { age: 24, links: { blog: 'https://aloisio.work' }, name: 'Alessandro' }, status: true,
      },
      234567: {
        _id: 234567, active: true, info: { age: 24, links: { blog: 'https://atraversleslivres.be' }, name: 'Alicia' }, status: true,
      },
    },
    users: {},
  },
  error: null,
  loading: false,
}

7. Exist

/**
* Determine if an element exist
*/

state.exist('users.123456')
// output
true

state.exist('users.987544')
// output
false

License

MIT © Aloisio Alessandro