object.mn
v1.2.6
Published
A package with the aim of facilitating the handling of Objects and Arrays
Downloads
269
Maintainers
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
- Type:
- Value
- Type:
Any
- Required:
true
- Example:
'your value'
- Type:
- Callback
- Type:
Function
- Required:
false
- Example:
(data) => console.log(data)
- Type:
- Path
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
- Type:
- Callback
- Type:
Function
- Required:
false
- Example:
(data) => console.log(data)
- Type:
- Path
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
- Type:
- Callback
- Type:
Function
- Required:
false
- Example:
(data) => console.log(data)
- Type:
- Path
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
- Type:
- Value
- Type:
Object
- Required:
true
- Example:
{ key: 'value' }
- Type:
- Callback
- Type:
Function
- Required:
false
- Example:
(data) => console.log(data)
- Type:
- Path
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
- Type:
- Callback
- Type:
Function
- Required:
false
- Example:
(data) => console.log(data)
- Type:
- Path
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
- Type:
- Value
- Type:
Any
- Required:
true
- Example:
'your value'
- Type:
- Callback
- Type:
Function
- Required:
false
- Example:
(data) => console.log(data)
- Type:
- Path
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
- Type:
- Callback
- Type:
Function
- Required:
false
- Example:
(data) => console.log(data)
- Type:
- Path
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
- Type:
- Callback
- Type:
Function
- Required:
false
- Example:
(data) => console.log(data)
- Type:
- Path
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
- Type:
- Callback
- Type:
Function
- Required:
false
- Example:
(data) => console.log(data)
- Type:
- Path
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
- Type:
- Callback
- Type:
Function
- Required:
false
- Example:
(data) => console.log(data)
- Type:
- Path
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