Ready to take your JavaScript development to the next level? Meet npm Enterprise - the ultimate in enterprise JavaScript. Learn more »

model-detect

1.0.1 • Public • Published

model-detect

Detect Model Version

npm version dependencies Build Status Pledge To Arupex!

Install

npm install model-detect --save

Usage

    var modelRules = {
        v1 : { properties : [ 'name.userName', 'name.lastName' ] },
        v2 : { properties : [ 'name.user_name', 'name.last_name' ] }
    };

    var ModelDetector = require('model-detect');
    var detector = new ModelDetector(modelRules);

    var impls = {
        vNull : function(obj){
            return 'no user-name';
        },
        v1 : function(obj){
            return obj.name.userName;
        } ,
        v2 : function(obj){
            return obj.name.user_name;
        }
    };

    function doSomething(obj){
        return impls[detector(obj)](obj);
    }

    var userNames = [];

    var objects = [
        {
            name : {
                'user-name' : 'vNull User',
                'last-name' : 'vNull'
            }
        },
        {
            name : {
                'userName': 'v1 user',
                'lastName': 'v1 user'
            }
        },
        {
            name : {
                'user_name': 'v2 user',
                'last_name': 'v2 user'
            }
        }
    ];


    objects.forEach(function(obj){
        userNames.push(doSomething(obj));
    });

Result :

    userNames = [
        'no user-name',
        'v1 user',
        'v2 user'
    ];

install

npm i model-detect

Downloadsweekly downloads

5

version

1.0.1

license

UNLICENSED

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability