Skip to content

Commit

Permalink
Merge pull request #5 from cloudfour/feature-rewrite_options_merging
Browse files Browse the repository at this point in the history
Options Merging Rewrite
  • Loading branch information
lyzadanger committed Mar 4, 2016
2 parents f04042a + 06e5ea7 commit 4e730ee
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 28 deletions.
52 changes: 25 additions & 27 deletions src/options.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Handlebars from 'handlebars';
import { merge } from './utils';

const defaults = {
templates: {
Expand All @@ -15,22 +16,7 @@ const defaults = {
* @return {object} merged options
*/
function mergeDefaults (options = {}) {
/* eslint-disable prefer-const */
let { result = defaults } = options;
let {
templates: {
handlebars = defaults.templates.handlebars,
helpers = defaults.templates.helpers,
layouts = defaults.templates.layouts,
pages = defaults.templates.pages,
partials = defaults.templates.partials
} = {}
} = options;
result = {
templates: { handlebars, helpers, layouts, pages, partials }
};
/* eslint-enable prefer-const */
return result;
return merge(defaults, options);
}

/**
Expand All @@ -45,23 +31,35 @@ function mergeDefaults (options = {}) {
*/
function translateOptions (options = {}) {
/* eslint-disable prefer-const */
let { result = defaults } = options;
let {
templates: {
handlebars = options.handlebars,
helpers = options.helpers,
layouts = options.layouts,
pages = options.views,
partials = options.layoutIncludes
} = {}
const {
handlebars,
helpers,
layouts,
views: pages,
layoutIncludes: partials
} = options;
result = {
templates: { handlebars, helpers, layouts, pages, partials }

const result = {
templates: {
handlebars,
helpers,
layouts,
pages,
partials
}
};
return result;
/* eslint-enable prefer-const */
}

const parseOptions = options => mergeDefaults(translateOptions(options));

/**
* Sigh...
* > Single exports and multiple exports are mutually exclusive. You have to use
* > either one the two styles. Some modules combine both styles as follows:
* http://www.2ality.com/2015/12/babel-commonjs.html
*/
parseOptions.translator = translateOptions;

export default parseOptions;
21 changes: 21 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,31 @@ function titleCase (str) {
.replace(/\w\S*/g, word => word.charAt(0).toUpperCase() + word.substr(1));
}

/**
* Perform a deep merge of two objects.
* @param {Object} target
* @param {Object} source
* @return {Object}
* @example merge(defaults, options);
*/
function merge (target, source) {
Object.keys(source).forEach(key => {
if (Object.isExtensible(source[key])) {
merge(target[key], source[key]);
} else {
if (typeof source[key] !== 'undefined') {
target[key] = source[key];
}
}
});
return target;
}

export { dirname,
getFiles,
isGlob,
keyname,
merge,
parentDirname,
readFiles,
readFilesKeyed,
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var expect = chai.expect;
var builder = require('../dist/');

const options = {
template: {
templates: {
partials: `${__dirname}/fixtures/partials/*`,
helpers: `${__dirname}/fixtures/helpers/*.js`
}
Expand Down
9 changes: 9 additions & 0 deletions test/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
var chai = require('chai');
var expect = chai.expect;
var parseOptions = require('../dist/options');
var translateOptions = parseOptions.translator;

describe ('drizzle-builder', () => {
describe ('options', () => {
Expand All @@ -12,6 +13,14 @@ describe ('drizzle-builder', () => {
});
});
describe ('translating options from fabricator', () => {
// This PASSES. Where on Earth are the `undefined`s coming from?
it ('should work', () => {
var actual = translateOptions({ views: 'myViews' });
var expected = {
templates: { pages: 'myViews' }
};
expect(actual, expected).to.be.equal;
});
it ('should translate template options', () => {
var opts = parseOptions({
layoutIncludes: 'a path',
Expand Down
10 changes: 10 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,4 +81,14 @@ describe ('utils', () => {
expect(parent).to.equal('helpers');
});
});
describe('merge()', () => {
it ('works', () => {
var actual = utils.merge(
{a: 1, c: {d: 3}},
{a: 2, b: 1, c: {e: 4}}
);
var expected = {a: 2, b: 1, c: {d: 3, e: 4}};
expect(actual, expected).to.be.equal;
});
});
});

0 comments on commit 4e730ee

Please sign in to comment.