Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

D-Mobilelab/smart-obj

Repository files navigation

smart-obj

[!!!] This repository has been deprecated

Build Status Coverage Status npm version Bower version GitHub version

SmartObj is a custom object that lets you define default values and value checkers.

Installation

NPM

npm install --save smart-obj

You can found the library ready for production on node_modules/smart-obj/dist/dist.js

Bower

bower install --save smart-obj

You can found the library ready for production on bower_components/smart-obj/dist/dist.js

Documentation

To read documentation, go to:

http://d-mobilelab.github.io/smart-obj/1.0.0

Replace 1.0.0 with the version of the documentation you want to read.

Default getters

var obj = new SmartObj(function(key){
    var myDefaultValues = {
        bananas: 3,
        apples: 2
    };

    if (myDefaultValues[key] == undefined) {
        return 0;
    }
    return myDefaultValues[key];
});

obj.get('unknownKey'); // returns 1
obj.get('apples'); // returns 2
obj.get('bananas'); // return 3

Constants as default getters

var obj = new SmartObj(42);

obj.get('answer'); // return 42

Value checkers

var obj = new SmartObj(undefined, function(value){
    if (value < 0) 
        throw 'Illegal value';
    else 
        return true;
});

obj.set('test1', 1);
obj.get('test1'); // returns 1

obj.set('test2', -1); // throws 'Illegal value'

asPOJO

var obj = new SmartObj(0);

obj.get(1);
obj.get(2);

obj.asPOJO(); // returns {1: 0, 2: 0}

Nested default objects

var returnNestedSmartObj = function(){
    return new SmartObj(returnNestedSmartObj);
}

var obj = new SmartObj(return returnNestedSmartObj);

obj.get(1).get(2).set(3, 4);

obj.asPOJO(); // return {1: 2: {3: 4}}

About

SmartObj is a custom object that lets you define default values and value checkers.

Resources

License

Stars

Watchers

Forks

Packages

No packages published