object-normalizer
v1.3.1
Published
javascript object normalizer.
Downloads
2
Maintainers
Readme
ObjectNormalizer
object normalizer.
Install
$ npm install object-normalizer
Usage
Normal
const schema = {
'prop1': function (value) {
if (value == undefined)
value = 1;
return value;
},
'prop2': function (value) {
if (typeof value !== 'function')
throw new Error(`'prop2' must be of Function type.`);
return value;
}
};
const defaultPropertyName = 'prop2';
let nor = new ObjectNormalizer(schema, defaultPropertyName);
let r = nor.normalize(function () { });
console.log(r);
// { prop1: 1, prop2: [Function] }
Multiple
const schema = {
parent1: function (value) {
if (value == undefined) value = 'parent1';
return value;
},
parent2: {
schema: {
'prop1': function (value) {
if (value == undefined) value = 1;
return value;
},
'prop2': function (value) {
if (typeof value !== 'function')
throw new Error(`'prop2' must be of Function type.`);
return value;
}
},
defaultProperty: 'prop2'
}
};
const defaultPropertyName = 'parent2';
let nor = new ObjectNormalizer(schema, defaultPropertyName);
let r = nor.normalize(function () { return 'hello!'; });
console.log(r);
// { parent1: 'parent1', parent2: { prop1: 1, prop2: [Function] } }
console.log(r.parent2.prop2());
// hello!
Array
const schema = {
// array
'prop1': [function (value) {
if (value == undefined)
value = 1;
return value;
}],
'prop2': function (value) {
if (typeof value !== 'function')
throw new Error(`'prop2' must be of Function type.`);
return value;
},
// to array, not validate
'prop3': []
};
const defaultPropertyName = 'prop2';
let nor = new ObjectNormalizer(schema, defaultPropertyName);
let r = nor.normalize(function () { });
console.log(r);
// { prop1: [], prop2: [Function], prop3: [] }
Root array
// array
const schema = [{
'prop1': function (value) {
if (value == undefined)
value = 1;
return value;
},
'prop2': function (value) {
if (typeof value !== 'function')
throw new Error(`'prop2' must be of Function type.`);
return value;
}
}];
const defaultPropertyName = 'prop2';
let nor = new ObjectNormalizer(schema, defaultPropertyName);
let r = nor.normalize(function () { });
console.log(r);
// [{ prop1: 1, prop2: [Function] }]