Skip to content

Commit

Permalink
feat(code): server exports
Browse files Browse the repository at this point in the history
Refactor exports for more fine-grained control
  • Loading branch information
adam-26 committed Apr 14, 2018
1 parent 6617397 commit 2a28df3
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 55 deletions.
6 changes: 6 additions & 0 deletions capUtils.es.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import {
deepEqual,
mapStateToComponents,
mapStateToAttributes,
mapStateToHead,
reducePropsToState,
renderAttributes,
HEAD_TAG_NAMES
} from './lib/capUtils';

export {
deepEqual,
mapStateToComponents,
mapStateToAttributes,
mapStateToHead,
reducePropsToState,
renderAttributes,
HEAD_TAG_NAMES
};
3 changes: 3 additions & 0 deletions capUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ var capUtils = require('./lib/capUtils');
exports.deepEqual = capUtils.deepEqual;
exports.reducePropsToState = capUtils.reducePropsToState;
exports.mapStateToComponents = capUtils.mapStateToComponents;
exports.mapStateToAttributes = capUtils.mapStateToAttributes;
exports.mapStateToHead = capUtils.mapStateToHead;
exports.renderAttributes = capUtils.renderAttributes;
exports.HEAD_TAG_NAMES = capUtils.HEAD_TAG_NAMES;
4 changes: 2 additions & 2 deletions server.es.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
import { mapStateOnServer } from './lib/server';
export { mapStateOnServer };
import { mapStateOnServer, mapAttributeStateOnServer, mapComponentStateOnServer } from './lib/server';
export { mapStateOnServer, mapAttributeStateOnServer, mapComponentStateOnServer };
2 changes: 2 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
var server = require('./lib/server');
exports.mapStateOnServer = server.mapStateOnServer;
exports.mapAttributeStateOnServer = server.mapAttributeStateOnServer;
exports.mapComponentStateOnServer = server.mapComponentStateOnServer;
147 changes: 108 additions & 39 deletions src/CapUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,55 +25,122 @@ const getComponentForTag = (type, tags, options) => {
}
};

const mapStateToComponents = (
const mapStateToAttributes = (
{bodyAttributes, htmlAttributes},
componentOptions
) => {
const components = {
bodyAttributes: [],
htmlAttributes: []
};

if (bodyAttributes) {
components.bodyAttributes = getComponentForTag(
ATTRIBUTE_NAMES.BODY,
bodyAttributes,
componentOptions
);
}

if (htmlAttributes) {
components.htmlAttributes = getComponentForTag(
ATTRIBUTE_NAMES.HTML,
htmlAttributes,
componentOptions
);
}

return components;
};

const mapStateToHead = (
{
baseTag,
bodyAttributes,
htmlAttributes,
linkTags,
metaTags,
noscriptTags,
scriptTags,
styleTags,
title = "",
title,
titleAttributes
},
componentOptions
) => ({
base: getComponentForTag(HEAD_TAG_NAMES.BASE, baseTag, componentOptions),
bodyAttributes: getComponentForTag(
ATTRIBUTE_NAMES.BODY,
bodyAttributes,
componentOptions
),
htmlAttributes: getComponentForTag(
ATTRIBUTE_NAMES.HTML,
htmlAttributes,
componentOptions
),
link: getComponentForTag(HEAD_TAG_NAMES.LINK, linkTags, componentOptions),
meta: getComponentForTag(HEAD_TAG_NAMES.META, metaTags, componentOptions),
noscript: getComponentForTag(
HEAD_TAG_NAMES.NOSCRIPT,
noscriptTags,
componentOptions
),
script: getComponentForTag(
HEAD_TAG_NAMES.SCRIPT,
scriptTags,
componentOptions
),
style: getComponentForTag(
HEAD_TAG_NAMES.STYLE,
styleTags,
componentOptions
),
title: getComponentForTag(
HEAD_TAG_NAMES.TITLE,
{title, titleAttributes},
componentOptions
)
});
) => {
const components = {
base: [],
link: [],
meta: [],
noscript: [],
script: [],
style: [],
title: []
};

if (baseTag) {
components.base = getComponentForTag(
HEAD_TAG_NAMES.BASE,
baseTag,
componentOptions
);
}

if (linkTags) {
components.link = getComponentForTag(
HEAD_TAG_NAMES.LINK,
linkTags,
componentOptions
);
}

if (metaTags) {
components.meta = getComponentForTag(
HEAD_TAG_NAMES.META,
metaTags,
componentOptions
);
}

if (noscriptTags) {
components.noscript = getComponentForTag(
HEAD_TAG_NAMES.NOSCRIPT,
noscriptTags,
componentOptions
);
}

if (scriptTags) {
components.script = getComponentForTag(
HEAD_TAG_NAMES.SCRIPT,
scriptTags,
componentOptions
);
}

if (styleTags) {
components.style = getComponentForTag(
HEAD_TAG_NAMES.STYLE,
styleTags,
componentOptions
);
}

if (title) {
components.title = getComponentForTag(
HEAD_TAG_NAMES.TITLE,
{title: title || "", titleAttributes},
componentOptions
);
}

return components;
};

const mapStateToComponents = (state, options) => {
return Object.assign(
mapStateToAttributes(state, options),
mapStateToHead(state, options)
);
};

const renderAttributes = ({htmlAttributes, bodyAttributes}) => {
updateAttributes(TAG_NAMES.BODY, bodyAttributes);
Expand All @@ -83,6 +150,8 @@ const renderAttributes = ({htmlAttributes, bodyAttributes}) => {
export {
reducePropsToState,
mapStateToComponents,
mapStateToAttributes,
mapStateToHead,
deepEqual,
renderAttributes,
HEAD_TAG_NAMES
Expand Down
23 changes: 9 additions & 14 deletions src/HelmetServerUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,14 @@ const getMethodsForTag = (type, tags, encode, componentOptions) => {
}
};

const mapStateOnServer = (
const mapStateOnServer = (state, options = {}) => {
return Object.assign(
mapAttributeStateOnServer(state, options),
mapComponentStateOnServer(state, options)
);
};

const mapComponentStateOnServer = (
{
baseTag,
bodyAttributes,
Expand All @@ -128,18 +135,6 @@ const mapStateOnServer = (
componentOptions = {}
) => ({
base: getMethodsForTag(TAG_NAMES.BASE, baseTag, encode, componentOptions),
bodyAttributes: getMethodsForTag(
ATTRIBUTE_NAMES.BODY,
bodyAttributes,
encode,
componentOptions
),
htmlAttributes: getMethodsForTag(
ATTRIBUTE_NAMES.HTML,
htmlAttributes,
encode,
componentOptions
),
link: getMethodsForTag(TAG_NAMES.LINK, linkTags, encode, componentOptions),
meta: getMethodsForTag(TAG_NAMES.META, metaTags, encode, componentOptions),
noscript: getMethodsForTag(
Expand Down Expand Up @@ -186,4 +181,4 @@ const mapAttributeStateOnServer = (
)
});

export {mapStateOnServer, mapAttributeStateOnServer};
export {mapStateOnServer, mapAttributeStateOnServer, mapComponentStateOnServer};

0 comments on commit 2a28df3

Please sign in to comment.