model-detect
v1.0.1
Published
Detect Model Versions based on properties within the models
Downloads
3
Maintainers
Readme
model-detect
Detect Model Version
#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'
];