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

object.mn

v1.2.6

Published

A package with the aim of facilitating the handling of Objects and Arrays

Downloads

269

Readme

Table of contents

Installation

Your Node.js must be version 16.6.0 or higher!

# Using npm:
npm install object.mn --save

# Using yarn:
yarn add object.mn

# Using pnpm
pnpm add object.mn

Introduction

// Supports ES6 and CommonJS
import ObjectManager from 'object.mn';

const myObject = {};
/**
 * You can use these parameters below to configure the manager:
 * 
 * @param {Object} [object] Source object to save the information.
 * @param {?String} [split] Query path separator.
 */
const myObjectManager = new ObjectManager(myObject, /* '/' */);
  
console.log(myObjectManager);

Functions

Set

Use this function to set values inside the object.

  • Params

    • Path
      • Type: String
      • Required: true
      • Example: path/to/value
    • Value
      • Type: Any
      • Required: true
      • Example: 'your value'
    • Callback
      • Type: Function
      • Required: false
      • Example: (data) => console.log(data)
  • If you want to see the code, click here!


Example

import ObjectManager from 'object.mn';

let myObject = {},
  myObjectManager = new ObjectManager(myObject);
  
// Without using callback:
console.log(myObjectManager.set('path/to/value', true)); // output: { value: true }
console.log(myObject); // output: { path: { to: { value: true } } }

// Using callback:
myObjectManager.set('path/to/value', true, (data) => {
  console.log(data); // output: { value: true }
  console.log(myObject); // output: { path: { to: { value: true } } }
});

Get

Use this function to get values inside the object.

  • Params

    • Path
      • Type: String
      • Required: true
      • Example: path/to/value
    • Callback
      • Type: Function
      • Required: false
      • Example: (data) => console.log(data)
  • If you want to see the code, click here!


Example

import ObjectManager from 'object.mn';

let
  myObject = {
    path: {
      to: { value: 'myValue' }
    }
  },
  myObjectManager = new ObjectManager(myObject);
  
// Without using callback:
let data = myObjectManager.get('path/to/value');
console.log(data); // output: 'myValue'

// Using callback:
myObjectManager.get('path/to/value', console.log); // output: 'myValue'

Delete

Use this function to delete a specific value in the object.

  • Params

    • Path
      • Type: String
      • Required: true
      • Example: path/to/value
    • Callback
      • Type: Function
      • Required: false
      • Example: (data) => console.log(data)
  • If you want to see the code, click here!


Example

import ObjectManager from 'object.mn';

let
  myObject = {
    path: {
      to: { value: true }
    }
  },
  myObjectManager = new ObjectManager(myObject);
  
// Without using callback:
console.log(myObjectManager.delete('path/to/value')); // output: { path: { to: {} } }
console.log(myObject); // output: { path: { to: {} } }

// Using callback:
myObjectManager.delete('path/to/value', (data) => {
  console.log(data); // output: { path: { to: {} } }
  console.log(myObject); // output: { path: { to: {} } }
});

Update

Use this function to update object elements..

  • Params

    • Path
      • Type: String
      • Required: true
      • Example: path/to/value
    • Value
      • Type: Object
      • Required: true
      • Example: { key: 'value' }
    • Callback
      • Type: Function
      • Required: false
      • Example: (data) => console.log(data)
  • If you want to see the code, click here!


Example

import ObjectManager from 'object.mn';

let
  myObject = {
    path: {
      to: { value: true }
    }
  },
  myObjectManager = new ObjectManager(myObject);
  
// Without using callback:
console.log(myObjectManager.update('path/to', {
  value: false,
  key: 'value'
})); // output: { path: { to: { value: false, key: 'value' } } }
console.log(myObject); // output: { path: { to: { value: false, key: 'value' } }

// Using callback:
myObjectManager.update('path/to', {
  value: false,
  key: 'value'
}, (data) => {
  console.log(data); // output: { path: { to: { value: false, key: 'value' } }
  console.log(myObject); // output: { path: { to: { value: false, key: 'value' } }
});

Has

Use this function to check the existence of a value in the object.

  • Params

    • Path
      • Type: String
      • Required: true
      • Example: path/to/value
    • Callback
      • Type: Function
      • Required: false
      • Example: (data) => console.log(data)
  • If you want to see the code, click here!


Example

import ObjectManager from 'object.mn';

let myObject = { key: 'value' },
  myObjectManager = new ObjectManager(myObject);
  
// Without using callback:
console.log(myObjectManager.has('key')); // output: true
console.log(myObjectManager.has('value')); // output: false

// Using callback:
myObjectManager.had('key', console.log); // output: true
myObjectManager.had('value', console.log); // output: false

Push

Use this function to insert/create an Array in the object.

  • Params

    • Path
      • Type: String
      • Required: true
      • Example: path/to/value
    • Value
      • Type: Any
      • Required: true
      • Example: 'your value'
    • Callback
      • Type: Function
      • Required: false
      • Example: (data) => console.log(data)
  • If you want to see the code, click here!


Example

import ObjectManager from 'object.mn';

let myObject = {},
  myObjectManager = new ObjectManager(myObject);
  
// Without using callback:
console.log(myObjectManager.push('github/profiles', [
  'isBucky',
  '7Silva',
  '7Johnsz'
])); // output: { github: { profiles: ['isBucky', '7Silva', '7Johnsz'] } }
console.log(myObject); // output: { github: { profiles: ['isBucky', '7Silva', '7Johnsz'] } }

// Using callback:
myObjectManager.set('github/profiles', [
  'isBucky',
  '7Silva',
  '7Johnsz'
], (data) => {
  console.log(data); // output: { github: { profiles: ['isBucky', '7Silva', '7Johnsz'] } }
  console.log(myObject); // output: { github: { profiles: ['isBucky', '7Silva', '7Johnsz'] } }
});

Keys

Use this function to return all the keys of the object.

  • Params

    • Path
      • Type: String
      • Required: true
      • Example: path/to/value
    • Callback
      • Type: Function
      • Required: false
      • Example: (data) => console.log(data)
  • If you want to see the code, click here!


Example

import ObjectManager from 'object.mn';

let
  myObject = {
    user: {
      name: 'Bucky',
      age: 17
    }
  },
  myObjectManager = new ObjectManager(myObject);
  
// Without using callback:
console.log(myObjectManager.keys('user')); // output: ['name', 'age']
console.log(myObjectManager.keys('/')); // output: ['user']

// Using callback:
myObjectManager.keys('user', console.log); // output: ['name', 'age']
myObjectManager.keys('/', console.log); // output: ['user']

Values

Use this function to return all the Values of the object.

  • Params

    • Path
      • Type: String
      • Required: true
      • Example: path/to/value
    • Callback
      • Type: Function
      • Required: false
      • Example: (data) => console.log(data)
  • If you want to see the code, click here!


Example

import ObjectManager from 'object.mn';

let
  myObject = {
    user: {
      name: 'Bucky',
      age: 17
    }
  },
  myObjectManager = new ObjectManager(myObject);
  
// Without using callback:
console.log(myObjectManager.values('user')); // output: ['Bucky', '17']
console.log(myObjectManager.values('/')); // output: [{ name: 'Bucky', age: 17 }]

// Using callback:
myObjectManager.values('user', console.log); // output: ['Bucky', '17']
myObjectManager.valued('/', console.log); // output: [{ name: 'Bucky', age: 17 }]

Entries

Use this function to return all the Entries of the object.

  • Params

    • Path
      • Type: String
      • Required: true
      • Example: path/to/value
    • Callback
      • Type: Function
      • Required: false
      • Example: (data) => console.log(data)
  • If you want to see the code, click here!


Example

import ObjectManager from 'object.mn';

let
  myObject = {
    user: {
      name: 'Bucky',
      age: 17
    }
  },
  myObjectManager = new ObjectManager(myObject);
  
// Without using callback:
console.log(myObjectManager.entries('user')); // output: [ [ 'name', 'Bucky' ], [ 'age', 17 ] ]
console.log(myObjectManager.entries('/')); // output: [ [ 'user', { name: 'Bucky', age: 17 } ] ]

// Using callback:
myObjectManager.entries('uset', console.log); // output: [ [ 'name', 'Bucky' ], [ 'age', 17 ] ]
myObjectManager.entries('/', console.log); // output: [ [ 'user', { name: 'Bucky', age: 17 } ] ]

ToJSON

Use this function to transform an object into a string.

  • Params

    • Path
      • Type: String
      • Required: true
      • Example: path/to/value
    • Callback
      • Type: Function
      • Required: false
      • Example: (data) => console.log(data)
  • If you want to see the code, click here!


Example

import ObjectManager from 'object.mn';

let
  myObject = {
    user: {
      name: 'Bucky',
      age: 17
    }
  },
  myObjectManager = new ObjectManager(myObject);
  
// Without using callback:
console.log(myObjectManager.toJSON('user')); // output: "{"name":"Bucky","age":17}"
console.log(myObjectManager.toJSON('/')); // output: "{"user":{"name":"Bucky","age":17}}"

// Using callback:
myObjectManager.toJSON('uset', console.log); // output: "{"name":"Bucky","age":17}"
myObjectManager.toJSON('/', console.log); // output: "{"user":{"name":"Bucky","age":17}}"

Arrays Management (Examples)

Just like functions manage Objects like set, update, etc. We will do this in Arrays in a simplified and unified way, for you to change an element within an Array or access it, you must pass the Index (position of the element in relation to the Array) in the function path, so you can do what you want with him.

Push in Array

object.push('path/to/value', [1, 2, 3]); // Output: [ 1, 2, 3 ]

Set in Array

object.push('path/to/value', [1, 2, 3]); // Output: [ 1, 2, 3 ]

object.set('path/to/value/0', 5) // Output: 5
object.get('path/to/value') // Output: [ 5, 2, 3 ]

Get in Array

object.push('path/to/value', [1, 2, 3]); // Output: [ 1, 2, 3 ]

object.get('path/to/value') // Output: [ 5, 2, 3 ]
object.get('path/to/value/0') // Output: 5
object.get('path/to/value/1') // Output: 2
object.get('path/to/value/2') // Output: 3

Delete in Array

object.push('path/to/value', [1, 2, 3]); // Output: [ 1, 2, 3 ]

object.delete('path/to/value/0'); // Output: true

object.get('path/to/value'); // Output: [ 2, 3 ]

Update in Array

object.push('path/to/value', [1, 2, 3]); // Output: [ 1, 2, 3 ]

object.update('path/to/value/2', 10); // Output: true

object.get('path/to/value'); // Output: [ 1, 2, 10 ]

Footer

To learn more how to use npm functions go to Table of contents