Skip to content

Instantly share code, notes, and snippets.

@nicklozon
Last active April 13, 2017 20:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nicklozon/fa260e5cbff28b94ea7963457c12b493 to your computer and use it in GitHub Desktop.
Save nicklozon/fa260e5cbff28b94ea7963457c12b493 to your computer and use it in GitHub Desktop.
var mapSchema = require('./index.js');
// Basic example
var params = {'noise': 'Meow!'};
var schema = {'makeNoise': function(p) { return p.noise; }};
mapSchema(schema, params, function(ret) {
console.log(ret); //{ makeNoise: 'Meow!' }
});
// Nested example
params = {'noiseCat': 'Meow!', 'noiseDog': 'Woof!'};
schema = {
'makeNoises': {
'cat': function(p) { return p.noiseCat; },
'dog': function(p) { return p.noiseDog; }
}
};
mapSchema(schema, params, function(ret) {
console.log(ret); //{ makeNoises: { cat: 'Meow!', dog: 'Woof!' } }
});
// Undefined example
params = {'isCat': true, 'noiseCat': 'Meow!', 'isDog': false, 'noiseDog': 'Woof!'};
schema = {
'makeNoises': {
'cat': function(p) { if(p.isCat) return p.noiseCat; },
'dog': function(p) { if(p.isDog) return p.noiseDog; },
}
};
mapSchema(schema, params, function(ret) {
console.log(ret); //{ makeNoises: { cat: 'Meow!' } }
});
// Static example
params = {'isCat': true, 'noiseCat': 'Meow!'};
schema = {
'makeNoises': {
'cat': function(p) { if(p.isCat) return p.noiseCat; },
'dog': "Meow! I mean, Woof!"
}
};
mapSchema(schema, params, function(ret) {
console.log(ret); //{ makeNoises: { cat: 'Meow!', dog: 'Meow! I mean, Woof!' } }
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment