# smart-obj

## [!!!] This repository has been deprecated

[![Build Status](https://travis-ci.org/D-Mobilelab/smart-obj.svg?branch=master&v=2)](https://travis-ci.org/D-Mobilelab/smart-obj)
[![Coverage Status](https://coveralls.io/repos/github/D-Mobilelab/smart-obj/badge.svg?branch=master&v=1)](https://coveralls.io/github/D-Mobilelab/smart-obj?branch=master)
[![npm version](https://badge.fury.io/js/smart-obj.svg)](https://badge.fury.io/js/smart-obj)
[![Bower version](https://badge.fury.io/bo/smart-obj.svg)](https://badge.fury.io/bo/smart-obj)
[![GitHub version](https://badge.fury.io/gh/D-Mobilelab%2Fsmart-obj.svg)](https://badge.fury.io/gh/D-Mobilelab%2Fsmart-obj)

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 <i>node_modules/smart-obj/dist/dist.js</i>

### Bower
```
bower install --save smart-obj
```
You can found the library ready for production on <i>bower_components/smart-obj/dist/dist.js</i>

## Documentation

To read documentation, go to:

[http://d-mobilelab.github.io/smart-obj/1.0.0](http://d-mobilelab.github.io/smart-obj/1.0.0)

Replace <i>1.0.0</i> 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}}
