-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
89d1537
commit e26971f
Showing
7 changed files
with
219 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; | ||
} | ||
}; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.