1.0.2 • Published 4 years ago

object-helper-js v1.0.2

Weekly downloads
3
License
GPL-3.0
Repository
github
Last release
4 years ago

Object-Helper-JS

Control object like array in JavaScript.

Korean|한국어

const ObjectHelperJS = require('object-helper-js');

// Test Object
const Obj = {
    'a': [1, 2, 3],
    'b': [2, 3, 4],
    'c': [3, 4, 5]
}

// forEach
Obj.forEach((value, key) => {
    console.log('Object.prototype.forEach: ', `[${key}] => `, value);
});
Object.prototype.forEach:  [a] => [ 1, 2, 3 ]
Object.prototype.forEach:  [b] => [ 2, 3, 4 ]
Object.prototype.forEach:  [c] => [ 3, 4, 5 ]

✨ Requirements

  • Node.js: This package is a Node.js module. Before using it, You should install Node.js version 12 or higher.

🎯 Installation

$ npm install object-helper-js

🎲 Methods

Accessor methods

Object.prototype.concat

Object.concat([value1[, value2[, ...[, valueN]]]]);

Parameters

  • (Object) valueN: Objects to concatenate into a new object. If all valueN parameters are omitted, concat returns a shallow copy of the existing object on which it is called.

Return value

A new Object instance.

Object.prototype.filter

Object.filter(callback(value[, key[, object]])[, thisarg]);

Parameters

  • (Callback) callback: A function to test for each element, taking three arguments:
    • (Variable) value: The current value being processed in the object.
    • [(String) key]: The current key being processed in the object.
    • [(Object) object]: The object that filter was called on.
  • [(Object) thisarg]: A object to use as this when executing callback.

Return value

A new object with the elements that pass the test. If no elements pass the test, an empty object will be returned.

Object.prototype.includes

Object.includes(valueToFind);

Parameters

  • (Variable) valueToFind: The value to search for.

Return value

A Boolean which is true if the value valueToFind is found within the object.

Object.prototype.length

Object.length();

Return value

A Number which is length of object.

Iteration methods

Object.prototype.every

Object.every(callback(value[, key[, object]])[, thisarg]);

Parameters

  • (Callback) callback: A function to test for each element, taking three arguments:
    • (Variable) value: The current value being processed in the object.
    • [(String) key]: The current key being processed in the object.
    • [(Object) object]: The object that every was called on.
  • [(Object) thisarg]: A object to use as this when executing callback.

Return value

true if the callback function returns a truthy value for every elements in object. Otherwise, false.

Object.prototype.find

Object.find(callback(value[, key[, object]])[, thisarg]);

Parameters

  • (Callback) callback: A function to test for each element, taking three arguments:
    • (Variable) value: The current value being processed in the object.
    • [(String) key]: The current key being processed in the object.
    • [(Object) object]: The object that find was called on.
  • [(Object) thisarg]: A object to use as this when executing callback.

Return value

The elements of the first element in the object that satisfies the provided testing function. Otherwise, undefined is returned.

Object.prototype.forEach

Object.forEach(callback(value[, key[, object]])[, thisarg]);

Parameters

  • (Callback) callback: Function to execute on each element. It accepts between one and three arguments:
    • (Variable) value: The current value being processed in the object.
    • [(String) key]: The current key being processed in the object.
    • [(Object) object]: The object that forEach was called on.
  • [(Object) thisarg]: A object to use as this when executing callback.

Return value

undefined.

Object.prototype.keys

Object.keys([thisarg]);

Return value

A new Array iterator object.

Object.prototype.map

Object.map(callback(value[, key[, object]])[, thisarg]);

Parameters

  • (Callback) callback: Function that is called for every element of object. Each time callback executes, the returned object is added to new_object. The callback function accepts the following arguments:
    • (Variable) value: The current value being processed in the object.
    • [(String) key]: The current key being processed in the object.
    • [(Object) object]: The object that map was called on.
  • [(Object) thisarg]: A object to use as this when executing callback.

Return value

A new object with each element being the result of the callback function.

Object.prototype.reduce

Object.reduce(callback(accumulator, currentValue[, index[, object]])[, initialValue]);

Parameters

  • (Callback) callback: Function that is called for every element of object. Each time callback executes, the returned object is added to new_object. The callback function accepts the following arguments:
    • (Number) accumulator: The accumulator accumulates callback's return values. It is the accumulated value previously returned in the last invocation of the callback—or initialValue, if it was supplied.
    • (Variable) currentValue: The current element being processed in the object.
    • [(String) key]: The current key being processed in the object.
    • [(Object) object]: The object that reduce was called on.
  • [(Number) initialValue]: A value to use as the first argument to the first call of the callback. If no initialValue is supplied, the first element in the array will be used and skipped. Calling reduce on an empty object without an initialValue will throw a 0.

Return value

The single value that results from the reduction.

Object.prototype.some

Object.some(callback(value[, key[, object]])[, thisarg]);

Parameters

  • (Callback) callback: A function to test for each element, taking three arguments:
    • (Variable) value: The current value being processed in the object.
    • [(String) key]: The current key being processed in the object.
    • [(Object) object]: The object that some was called on.
  • [(Object) thisarg]: A object to use as this when executing callback.

Return value

true if the callback function returns a truthy value for at least one element in the array. Otherwise, false.

Object.prototype.values

Object.values([thisarg]);

Return value

A new Array iterator object.

Coding pleasure

Object.prototype.readSafely

Object.readSafely(...args);

Parameters

  • (String | Number) ...args: Name of indices.

Return value

It returns a object which was processed with ...args. If the data not exists, It returns undefined.

Object.prototype.readRecursively

Object.readRecursively(...args);

Parameters

  • (String | Number) ...args: Name of indices.

Return value

It returns a object which was processed with ...args. If the data not exists, It returns a empty object.

Object.prototype.writeRecursively

Object.writeRecursively(...args, value);

Parameters

  • (String | Number) ...args: Name of indices.
  • (Any) value: A value to write.

Return value

It returns a object which was processed with ...args and value. But, It is not required to rewrite it self like obj = obj.writeRecursively('test', 'app', 10);. It saves a result of processing to origin object.

🚩 Author

Dong-Hoon Yoo Developer

  • Email: yoodonghoon01@gmail.com
  • Blog: blog.donghoonyoo.com
  • Roles: Development, Writing document in English and Korean.
1.0.0

4 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

4 years ago

1.0.1

6 years ago