Skip to content

Commit

Permalink
Replaced preprocessor framework
Browse files Browse the repository at this point in the history
  • Loading branch information
notheotherben committed Dec 3, 2013
1 parent 89d1537 commit e26971f
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 99 deletions.
57 changes: 57 additions & 0 deletions lib/preprocessors/conversion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
var preprocessor = require('./preprocessor');
var _ = require('lodash');

(require.modules || {}).conversion = module.exports = Conversion;

function Conversion(functions) {
/// <summary>Creates a new rename preprocessor with the given mappings</summary>
/// <param name="functions" type="Object">Mapping of conversion functions to their respective properties</param>

if(!(this instanceof Conversion)) return new Conversion(functions);

preprocessor.call(this);

Object.defineProperty(this, 'functions', {
get: function() { return functions; },
enumerable: false
});
}

Conversion.prototype.__proto__ = preprocessor.prototype;

Conversion.prototype.toSource = function(object) {
/// <summary>Transforms the given object into the source format</summary>
/// <param name="object" type="Object">The object to transform</summary>

transform(this.functions, '$up', object);
};

Conversion.prototype.toLocal = Conversion.prototype.fromSource = function(object) {
/// <summary>Transforms the given object into the destination format</summary>
/// <param name="object" type="Object">The object to transform</summary>

transform(this.functions, '$down', object);
};

function transform(transforms, method, properties) {
/// <summary>Transforms properties according to a number of transformation functions</summary>
/// <param name="transforms" type="Object">Mapped transformation functions to apply</param>
/// <param name="method" type="String">The transformation method to apply to the properties</param>
/// <param name="properties" type="Object">The object on which to apply the transformations</param>

if (!_.isPlainObject(properties)) return; // Cannot transform strange objects

for (var k in transforms) {
if(k[0] === '$') break; // Transform methods, not properties to transform

if (transforms[k] && transforms[k][method]) {
// Have a direct transform to apply
var newValue = transforms[k][method](properties[k]);
if(newValue === undefined) delete properties[k];
else properties[k] = newValue;
} else if (transforms[k]) {
// Have a nested transformation to apply
transform(transforms[k], method, properties[k]);
}
}
}
4 changes: 4 additions & 0 deletions lib/preprocessors/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(require.modules || {}).preprocessors = module.exports = {
Rename: require('./rename'),
Convert: require('./conversion')
};
16 changes: 16 additions & 0 deletions lib/preprocessors/preprocessor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
(require.modules || {}).preprocessor = module.exports = PreprocessorBase;

function PreprocessorBase() {
/// <summary>Base structure for a preprocessor</summary>

}

PreprocessorBase.prototype.toSource = function(object) {
/// <summary>Transforms the given object into the source format</summary>
/// <param name="object" type="Object">The object to transform</summary>
};

PreprocessorBase.prototype.toLocal = PreprocessorBase.prototype.fromSource = function(object) {
/// <summary>Transforms the given object into the destination format</summary>
/// <param name="object" type="Object">The object to transform</summary>
};
41 changes: 41 additions & 0 deletions lib/preprocessors/rename.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
var preprocessor = require('./preprocessor');

(require.modules || {}).rename = module.exports = Rename;

function Rename(mapping) {
/// <summary>Creates a new rename preprocessor with the given mappings</summary>
/// <param name="mapping" type="Object">The map of source properties to destination properties</param>

if(!(this instanceof Rename)) return new Rename(mapping);

preprocessor.call(this);

Object.defineProperty(this, 'mapping', {
get: function() { return mapping; },
enumerable: false
});
}

Rename.prototype.__proto__ = preprocessor.prototype;

Rename.prototype.toSource = function(object) {
/// <summary>Transforms the given object into the source format</summary>
/// <param name="object" type="Object">The object to transform</summary>

for(var k in this.mapping) {
if(!object.hasOwnProperty(this.mapping[k])) continue;
object[k] = object[this.mapping[k]];
delete object[this.mapping[k]];
}
};

Rename.prototype.toLocal = Rename.prototype.fromSource = function(object) {
/// <summary>Transforms the given object into the destination format</summary>
/// <param name="object" type="Object">The object to transform</summary>

for(var k in this.mapping) {
if(!object.hasOwnProperty(k)) continue;
object[this.mapping[k]] = object[k];
delete object[k];
}
};
43 changes: 0 additions & 43 deletions lib/utils/transforms.js

This file was deleted.

101 changes: 101 additions & 0 deletions test/preprocessors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
describe('preprocessors', function() {
describe('rename', function() {
var rename = require('../lib/preprocessors/rename.js');
it('should correctly transform properties', function() {
var processor = new rename({
from: 'to'
});

var obj = {
p1: 'v1',
from: 'expected'
};

processor.toLocal(obj);
obj.should.have.ownProperty('p1', 'v1');
obj.should.have.ownProperty('to', 'expected');

processor.toSource(obj);
obj.should.have.ownProperty('p1', 'v1');
obj.should.have.ownProperty('from', 'expected');
});
});

describe('conversion', function() {
var conversion = require('../lib/preprocessors/conversion.js');
it('should correctly transform properties', function() {
var processor = new conversion({
toUpper: {
$up: function(obj) {
return obj.toLowerCase();
},
$down: function(obj) {
return obj.toUpperCase();
}
}
});

var obj = {
toUpper: 'test string',
pass: 'Something else'
};

processor.toLocal(obj);
obj.should.have.ownProperty('toUpper', 'TEST STRING');
obj.should.have.ownProperty('pass', 'Something else');

processor.toSource(obj);
obj.should.have.ownProperty('toUpper', 'test string');
obj.should.have.ownProperty('pass', 'Something else');
});

it('should allow the transformation of an object\'s properties', function () {
var original = {
a: 'a',
b: 1,
c: 'c',
d: 'd'
};

var expected = {
a: 'aa',
b: 1,
c: 'c',
d: 'd'
};

var trans = {
a: { $up: function (value) { return value + value; } },
b: false,
c: { $up: false }
};

var processor = new conversion(trans);

processor.toSource(original);

original.should.eql(expected);
});

it('should remove properties where the transform returns undefined', function () {
var original = {
a: 1
};

var expected = {
b: '1'
};

var trans = {
a: { $up: function (value) { return undefined; } },
b: { $up: function (value) { return '1'; } }
};

var processor = new conversion(trans);

processor.toSource(original);

original.should.eql(expected);
});
});
});
56 changes: 0 additions & 56 deletions test/transforms.js

This file was deleted.

0 comments on commit e26971f

Please sign in to comment.