diff --git a/src/core/fresh-resume.js b/src/core/fresh-resume.js index 310fa9d..66ef99a 100644 --- a/src/core/fresh-resume.js +++ b/src/core/fresh-resume.js @@ -62,6 +62,7 @@ Definition of the FRESHResume class. consistent format. Then sort each section by startDate descending. */ FreshResume.prototype.parseJSON = function( rep, opts ) { + // Convert JSON Resume to FRESH if necessary if( rep.basics ) { rep = CONVERTER.toFRESH( rep ); @@ -161,45 +162,9 @@ Definition of the FRESHResume class. a transformation function (such as a Markdown filter or XML encoder). */ FreshResume.prototype.transformStrings = function( filt, transformer ) { - - var that = this; var ret = this.dupe(); - - // TODO: refactor recursion - function transformStringsInObject( obj, filters ) { - - if( !obj ) return; - if( moment.isMoment( obj ) ) return; - - if( _.isArray( obj ) ) { - obj.forEach( function(elem, idx, ar) { - if( typeof elem === 'string' || elem instanceof String ) - ar[idx] = transformer( null, elem ); - else if (_.isObject(elem)) - transformStringsInObject( elem, filters ); - }); - } - else if (_.isObject( obj )) { - Object.keys( obj ).forEach(function(k) { - if( filters.length && _.contains(filters, k) ) - return; - var sub = obj[k]; - if( typeof sub === 'string' || sub instanceof String ) { - obj[k] = transformer( k, sub ); - } - else if (_.isObject( sub )) - transformStringsInObject( sub, filters ); - }); - } - - } - - Object.keys( ret ).forEach(function(member){ - if( !filt || !filt.length || !_.contains(filt, member) ) - transformStringsInObject( ret[ member ], filt || [] ); - }); - - return ret; + var trx = require('../utils/string-transformer'); + return trx( ret, filt, transformer ); }; diff --git a/src/utils/string-transformer.js b/src/utils/string-transformer.js new file mode 100644 index 0000000..71f4bee --- /dev/null +++ b/src/utils/string-transformer.js @@ -0,0 +1,65 @@ +/** +Object string transformation. +@module string-transformer.js +@license MIT. See LICENSE.md for details. +*/ + + + +(function() { + + + + var _ = require('underscore'); + var moment = require('moment'); + + + + /** + Create a copy of this object in which all string fields have been run through + a transformation function (such as a Markdown filter or XML encoder). + */ + module.exports = function( ret, filt, transformer ) { + + var that = this; + + // TODO: refactor recursion + function transformStringsInObject( obj, filters ) { + + if( !obj ) return; + if( moment.isMoment( obj ) ) return; + + if( _.isArray( obj ) ) { + obj.forEach( function(elem, idx, ar) { + if( typeof elem === 'string' || elem instanceof String ) + ar[idx] = transformer( null, elem ); + else if (_.isObject(elem)) + transformStringsInObject( elem, filters ); + }); + } + else if (_.isObject( obj )) { + Object.keys( obj ).forEach(function(k) { + if( filters.length && _.contains(filters, k) ) + return; + var sub = obj[k]; + if( typeof sub === 'string' || sub instanceof String ) { + obj[k] = transformer( k, sub ); + } + else if (_.isObject( sub )) + transformStringsInObject( sub, filters ); + }); + } + + } + + Object.keys( ret ).forEach(function(member){ + if( !filt || !filt.length || !_.contains(filt, member) ) + transformStringsInObject( ret[ member ], filt || [] ); + }); + + return ret; + }; + + + +}());