不要怂,就是干,撸起袖子干!

Commit 48f39859 by Sascha Depold

Add util function to format the references property

1 parent 10029e1f
Showing with 43 additions and 1 deletions
......@@ -255,7 +255,6 @@ var Utils = module.exports = {
return true;
},
removeNullValuesFromHash: function(hash, omitNull, options) {
var result = hash;
......@@ -392,6 +391,27 @@ var Utils = module.exports = {
validateParameter: function(value, expectation, options) {
return ParameterValidator.check(value, expectation, options);
},
formatReferences: function (obj) {
var references = {};
if (!obj || !_.isPlainObject(obj) || !obj.references) {
return obj;
}
if (_.isPlainObject(obj.references)) {
references = obj.references;
} else {
references.model = obj.references;
}
if (obj.referencesKey) {
references.key = obj.referencesKey;
delete obj.referencesKey;
}
return _.extend(obj, { references: references });
}
};
......
......@@ -189,4 +189,26 @@ describe(Support.getTestDialectTeaser('Utils'), function() {
expect(Utils.singularize('status')).to.equal('status');
});
});
describe('formatReferences', function () {
([
[undefined, undefined],
[false, false],
[null, null],
['a', 'a'],
[{}, {}],
[{a: 1}, {a: 1}],
[{referencesKey: 1}, {referencesKey: 1}],
[{references: 'a'}, {references: {model: 'a'}}],
[{references: 'a', referencesKey: 1}, {references: {model: 'a', key: 1}}],
[{references: {model: 1}}, {references: {model: 1}}]
]).forEach(function (test) {
var input = test[0];
var output = test[1];
it('converts ' + JSON.stringify(input) + ' to ' + JSON.stringify(output), function () {
expect(Utils.formatReferences(input)).to.deep.equal(output);
});
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!