This package has been deprecated

Author message:

Migrated to @jf/object

jf-object

2.0.0 • Public • Published

jfObject stable

Class to easily manipulate objects. Can be used as base class for event-oriented libraries.

Usage

npm install jfObject

Objects as parameters

You can pass several objects and will be merged recursively.

const obj = new jfObject().assign(
    {
        a : {
            b : 0
        }
    },
    {
        a : {
            b : 1,
            c : 2
        }
    },
    {
        a : {
            c : 3
        }
    }
);
console.log(obj.toJSON()); // { a : { b : 1, c : 3 }, d : { e : { f : 5 } } }

Dot notation

You can use dot notation in order to assign/retrieve/test nested objects.

const obj = new jfObject();
obj.set('d.e.f', 5);
console.log(obj.toJSON()); // { e : { f : 5 } }
console.log(obj.get('d.e')); // { f : 5 }
console.log(obj.has('d.e.f')); // true

Merging objects

Objects can be merged in 2 ways:

  • Recursively: Use obj.assign(...).
  • Overwrite existing keys: Use Object.assign(obj, ...)

Using obj.assign will convert all objects into jfObject instances.

const obj = new jfObject(
    {
        a : {
            b : 0
        }
    }
);
// Recursive merge
obj.assign(
    {
        a : {
            c : 1
        }
    }
);
console.log(obj.toJSON()); // { a : { b : 0, c : 1 } }
// Overwrite merge
Object.assign(
    obj,
    {
        a : {
            c : 2
        }
    }
);
console.log(obj.toJSON()); // { a : { c : 2 } }

Iterating over properties

You can use new ES6 loop for..of:

const obj = new jfObject().assign(
    {
        a : 1,
        c : 2,
        f : 3,
        h : 4
    }
);
for (let prop of obj)
{
    console.log('%s = %s', prop, obj[prop]);    
}
// a = 1
// c = 2
// f = 3
// h = 4

Keys & values

If you need to split object into keys and values, you can use split or toArray methods.

const obj = new jfObject().assign(
    {
        a : 1,
        c : 2,
        f : 3,
        h : 4
    }
);
console.log(obj.split()); // { keys: [ 'a', 'c', 'f', 'h' ], values: [ 1, 2, 3, 4 ] }
console.log(obj.toArray());  // [ [ 'a', 1 ], [ 'c', 2 ], [ 'f', 3 ], [ 'h', 4 ] ]

Events

jfObject instances can emit/listen events because they extend node Events class:

const obj = new jfObject();
obj.on(data => console.log(data));
obj.emit('test-event', { a : 1 });

Singleton

If you need to share the same instance between differents external modules, you can use static method i() in order to retrieve the same instance anywhere in the app.

class User extends jfObject {
    // User class methods
}
// Initialize the User instance on ajax response using external library:
function onAjaxResponse(data)
{
    //...
    User.i().assign(data);
    //...
}
// In UI, using external framework
<user-info data="{{User.i()}}" />

Package Sidebar

Install

npm i jf-object

Weekly Downloads

3

Version

2.0.0

License

MIT

Unpacked Size

26.1 kB

Total Files

17

Last publish

Collaborators

  • joaquinfq