Skip to content

Commit

Permalink
Refactoring themes
Browse files Browse the repository at this point in the history
  • Loading branch information
igorshubovych committed Mar 14, 2016
1 parent 824048a commit 9740281
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 21 deletions.
36 changes: 15 additions & 21 deletions lib/theme.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
var get = require('lodash.get');
var isEmpty = require('lodash.isempty');
var identity = require('lodash.identity');
var partial = require('lodash.partial');
var memoize = require('lodash.memoize');
var chalk = require('chalk');

// Translates strings like 'red, underline, bold'
// into function chalk.red.underline.bold(text)
function buildStylingFunction(styles) {
if (isEmpty(styles)) {
return identity;
Expand All @@ -15,32 +19,22 @@ function buildStylingFunction(styles) {
var Theme = function(options) {
var theme = options;

function renderPart(partName, text) {
var renderPart = function(partName, text) {
var styles = theme[partName];
var stylingFunction = buildStylingFunction(styles);
return stylingFunction(text);
}

return {
renderName: function(text) {
return renderPart('name', text);
},

renderDescription: function(text) {
return renderPart('description', text);
},

renderExampleDescription: function(text) {
return renderPart('exampleDescription', text);
},
};

renderExampleCode: function(text) {
return renderPart('exampleCode', text);
},
var renderPartMemoized = function(partName) {
return memoize(partial(renderPart, partName));
};

renderExampleArgument: function(text) {
return renderPart('exampleArgument', text);
}
return {
renderName: renderPartMemoized('name'),
renderDescription: renderPartMemoized('description'),
renderExampleDescription: renderPartMemoized('exampleDescription'),
renderExampleCode: renderPartMemoized('exampleCode'),
renderExampleArgument: renderPartMemoized('exampleArgument')
};
};

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@
"lodash.isempty": "~4.1.3",
"lodash.last": "~3.0.0",
"lodash.map": "~4.2.0",
"lodash.memoize": "~4.0.1",
"lodash.partial": "~4.1.2",
"lodash.sample": "~4.1.0",
"lodash.unescape": "~4.0.0",
"marked": "~0.3.5",
Expand Down

0 comments on commit 9740281

Please sign in to comment.