a-object

A Node.js module for iterating through objects asynchronously and synchronously, updating them recursivley and comparing their keys. Package moved: https://npmjs.org/package/object-analyzr

Stats

stars 🌟issues ⚠️updated 🛠created 🐣size 🏋️‍♀️
00Feb 26, 2015Nov 7, 2014Minified + gzip package size for a-object in KB

Readme

AObject

A Node.js module for iterating through objects asynchronously and synchronously, updating them recursivley and comparing their keys.

License GNU GPL v3.0

Package moved

This package has been renamed to objectAnalyzr and moved to https://npmjs.org/package/object-analyzr. The newer versions will be avalible only there!

Usage

npm install a-object
var AObject = require('a-object');

Functions

AObject()

var aObject = new AObject(object object);

Creates a new instance of AObject with the given object.

Arguments

  • object object - The object to use for instance functions.

Example

var aObject = new AObject({
    key1: 'foo',
    key2: 'bla'
});

AObject#each()

aObject.each(function iterator, function callback);

Calls AObject.each() with the object given in AObject() as object.

Arguments

  • function iterator(string key, mixed value, function callback) - A function to apply to each item in object. iterator is passed a callback(error) which must be called once it has completed. If no error has occurred, the callback should be run without arguments or with an explicit null argument.
  • function callback(error) - A callback which is called when all iterator functions have finished, or an error occurs.

AObject#eachSync()

aObject.eachSync(function iterator);

Calls AObject.eachSync() with the object given in AObject() as object.

Arguments

  • function iterator(string key, mixed value) - A function to apply to each item in object.

AObject#update()

aObject.update(object newObject);

Calls AObject.update() with the object given in AObject() as newObject.

Arguments

  • object newObject - The object to be merged into currentObject.

AObject#compareKeys()

aObject.compareKeys(mixed expectedObject);

Calls AObject.compareKeys() with the object given in AObject() as object.

Arguments

  • mixed object - The object for comparision. It can be an array as well, where only the keys are given, if expectedObject is an array, the comparison isn't recursive.
  • bool strict - Wether all keys of object have to exist in expectedObject as well. Default: false

AObject#getKeys()

aObject.getKeys(array keys)

Calls AObject.getKeys() with the object given in AObject() as object.

Arguments

  • array keys - All keys of object to be returned.

AObject.each()

AObject.each(object object, function iterator, function callback);

Applies the function iterator to each item in object, in parallel. iterator is called with a key and a value from the object, and a callback for when it has finished. If the iterator passes an error to its callback, the main callback (for the each function) is immediately called with the error.

Note, that since this function applies iterator to each item in parallel, there is no guarantee that the iterator functions will complete in order.

Arguments

  • object object - An object to iterate over.
  • function iterator(string key, mixed value, function callback) - A function to apply to each item in object. iterator is passed a callback(error) which must be called once it has completed. If no error has occurred, the callback should be run without arguments or with an explicit null argument.
  • function callback(error) - A callback which is called when all iterator functions have finished, or an error occurs.

Example

// will log 'done' to console if no error occurs in the iterator function
AObject.each(
    {
        key1: 'foo',
        key2: 'bla',
    },
    function(key, value, next) {
        // Do some asynchronous stuff with key and value
        if(err) next(err); // Ooops ... error ...
        next();
    },
    function(error) {
        if(error) throw error;
        console.log('done');
    }
);

AObject.eachSync()

AObject.eachSync(object object, function iterator);

Applies the function iterator to each item in object, serial. iterator is called with a key and a value from the object.

Arguments

  • object object - An object to iterate over.
  • function iterator(string key, mixed value) - A function to apply to each item in object.

Example

// will log 2 messages to console: 'The value of key1 is foo'; 'The value of key2 is bla'
AObject.eachSync(
    {
        key1: 'foo',
        key2: 'bla'
    },
    function(key, value) {
        console.log('The value of ' + key + ' is ' + value);
    }
);

AObject.update()

AObject.update(object currentObject, object newObject);

Adds all keys of newObject and their values recursively to currentObject or overwrites existing ones.

Arguments

  • object currentObject - The object to be updated.
  • object newObject - The object to be merged into currentObject.

Example

// currentObject will be {key1: 'foo', key2: 'bla2', key3: 'foo2'}
AObject.update(
    {
        key1: 'foo',
        key2: 'bla',
    },
    {
        key2: 'bla2',
        key3: 'foo2'
    }
);

AObject.compareKeys()

AObject.compareKeys(mixed expectedObject, object object);

Compares all keys of object with the keys of expectedObject recursively. If the keys of both objects match exactly true will be returned, otherwise false.

Arguments

  • object object - The object to be compared with expectedObject.
  • mixed expectedObject - The object for comparison. It can be an array as well, where only the keys are given, if expectedObject is an array, the comparison isn't recursive.
  • bool strict - Wether all keys of object have to exist in expectedObject as well or not. Default: false

Note, that by default all keys of expectedObject have to exist in object as well to return true, but in object there can be keys not given in expectedObject.

If strict is set to true, all keys of object have to exist in expectedObject as well, so the comparison is bidirectional.

Example

// returns true
AObject.compareKeys(
    {
        key1: 'foo',
        key2: {
            key3: 'bla'
        }
    },
    {
        key1: 'foo2',
        key2: {
            key3: 'bla2'
        }
    }
);
// returns false
AObject.compareKeys(
    [
        'key1',
        'key3'
    ],
    {
        key1: 'foo',
        key2: 'bla'
    }
);

AObject.getKeys()

AObject.getKeys(object object, array keys);

Returns all keys in keys of object.

Arguments

  • object object - The object the wanted values are in.
  • array keys - An array with all keys to be returned.

Example

// returns {first: 'one', third: 'four'}
AObject.getKeys(
    {
        first: 'one',
        second: 'seven',
        third: 'four'
    },
    [
        'first',
        'third'
    ]
);

If you find any bugs or have a feature request, please open an issue on github!

The npm package download data comes from npm's download counts api and package details come from npms.io.