Permalink
Browse files

Actually it makes more sense to pass in 'for' option to the constructor.

  • Loading branch information...
1 parent 4ab155f commit efa5e4f14ad332c987f412eace9c3f5432ad6161 @Kami Kami committed Sep 19, 2011
Showing with 18 additions and 30 deletions.
  1. +13 −25 lib/serializer.js
  2. +5 −5 tests/test-swiz.js
View
38 lib/serializer.js
@@ -66,7 +66,8 @@ var merge = require('./util').merge;
function Swiz(defs, options) {
var defaultOptions = {
- stripNulls: true
+ 'stripNulls': true,
+ 'for': null
}, i, o;
this._defs = {};
@@ -96,16 +97,14 @@ exports.SERIALIZATION = {
* method.
*
* @param {Object} obj The datastructure to be converted.
- * @param {String} target Current serialization target (used for filtering
- * attributes - filterFrom).
* @param {function} callback A callback fired with (err, result).
*/
-Swiz.prototype.buildObject = function(obj, target, callback) {
- this._buildObject(null, obj, target, callback);
+Swiz.prototype.buildObject = function(obj, callback) {
+ this._buildObject(null, obj, callback);
};
-Swiz.prototype._buildObject = function(parent, obj, target, callback) {
+Swiz.prototype._buildObject = function(parent, obj, callback) {
var self = this;
var stype, def, result, keys;
@@ -115,14 +114,14 @@ Swiz.prototype._buildObject = function(parent, obj, target, callback) {
if (err) {
callback(err);
} else {
- self._buildObject(null, value, target, callback);
+ self._buildObject(null, value, callback);
}
});
}
else if (obj instanceof Array) {
// Recurse onto every element of an array
function iterArr(item, callback) {
- self._buildObject(null, item, target, callback);
+ self._buildObject(null, item, callback);
}
async.map(obj, iterArr, callback);
@@ -148,8 +147,8 @@ Swiz.prototype._buildObject = function(parent, obj, target, callback) {
var dst = field.name;
var src = field.src || dst;
- if (target && (field.filterFrom.length > 0) &&
- (field.filterFrom.indexOf(target) !== -1)) {
+ if (self._options.for && (field.filterFrom.length > 0) &&
+ (field.filterFrom.indexOf(self._options.for) !== -1)) {
callback();
return;
}
@@ -164,7 +163,7 @@ Swiz.prototype._buildObject = function(parent, obj, target, callback) {
}
}
- self._buildObject(obj, obj[src], target, function(err, value) {
+ self._buildObject(obj, obj[src], function(err, value) {
result[dst] = value;
callback(err);
});
@@ -185,7 +184,7 @@ Swiz.prototype._buildObject = function(parent, obj, target, callback) {
return;
}
- self._buildObject(obj, obj[key], target, function(err, value) {
+ self._buildObject(obj, obj[key], function(err, value) {
result[key] = value;
callback(err);
});
@@ -395,24 +394,13 @@ Swiz.prototype._addElement = function(parent, tag, attributes, text) {
*
* @param {enum} mode The mode of serialization.
* @param {number} version The version number.
- * @param {?Object} options Options object.
* @param {Object|Array} obj The object to be serialized.
* @param {function} callback The callback to use.
*/
-Swiz.prototype.serialize = function(mode, version, obj, options, callback) {
+Swiz.prototype.serialize = function(mode, version, obj, callback) {
var self = this, serializeFor;
- if (typeof options === 'function') {
- // Backward compatibility
- callback = options;
- options = {};
- }
- else {
- options = options || {};
- serializeFor = options.for || null;
- }
-
- this.buildObject(obj, serializeFor, function(err, result) {
+ this.buildObject(obj, function(err, result) {
if (err) {
callback(err);
return;
View
10 tests/test-swiz.js
@@ -119,7 +119,7 @@ Node.prototype.getSerializerType = function() {return 'Node';};
exports['test_build_object'] = function(test, assert) {
var blahnode = new Node();
var sw = new swiz.Swiz(def);
- sw.buildObject(blahnode, null, function(err, result) {
+ sw.buildObject(blahnode, function(err, result) {
assert.ifError(err);
assert.deepEqual(result, {
id: 15245,
@@ -174,8 +174,8 @@ exports['test_serial_xml'] = function(test, assert) {
exports['test_serial_xml_filterFrom'] = function(test, assert) {
var blahnode = new Node();
blahnode.active = false;
- var sw = new swiz.Swiz(def, { stripNulls: true });
- sw.serialize(swiz.SERIALIZATION.SERIALIZATION_XML, 1, blahnode, {'for': 'public'},
+ var sw = new swiz.Swiz(def, { stripNulls: true, for: 'public' });
+ sw.serialize(swiz.SERIALIZATION.SERIALIZATION_XML, 1, blahnode,
function(err, results)
{
// need to make an appointemnt with a DOM for this one.
@@ -252,8 +252,8 @@ exports['test_serial_json'] = function(test, assert) {
exports['test_serial_json_filterFrom'] = function(test, assert) {
var blahnode = new Node();
blahnode.active = false;
- var sw = new swiz.Swiz(def, { stripNulls: true });
- sw.serialize(swiz.SERIALIZATION.SERIALIZATION_JSON, 1, blahnode, {'for': 'public'},
+ var sw = new swiz.Swiz(def, { stripNulls: true, for: 'public' });
+ sw.serialize(swiz.SERIALIZATION.SERIALIZATION_JSON, 1, blahnode,
function(err, results)
{
var rep = JSON.parse(results);

0 comments on commit efa5e4f

Please sign in to comment.