From a3c17f1c14e163f71e1419d9c4e7b46efc861f6d Mon Sep 17 00:00:00 2001 From: Declan de Wet Date: Wed, 2 Nov 2016 22:59:13 +0200 Subject: [PATCH] extract generators to their own modules --- src/attrsGenerator.js | 29 +++++++++++++++++++++++++++++ src/generateServerInjector.js | 22 ++++------------------ src/titleGenerator.js | 16 ++++++++++++++++ 3 files changed, 49 insertions(+), 18 deletions(-) create mode 100644 src/attrsGenerator.js create mode 100644 src/titleGenerator.js diff --git a/src/attrsGenerator.js b/src/attrsGenerator.js new file mode 100644 index 0000000..6a0d562 --- /dev/null +++ b/src/attrsGenerator.js @@ -0,0 +1,29 @@ +import { VUE_META_ATTRIBUTE } from './constants' + +/** + * Generates tag attributes for use on the server. + * + * @param {('bodyAttrs'|'htmlAttrs')} type - the type of attributes to generate + * @param {Object} data - the attributes to generate + * @return {Object} - the attribute generator + */ +export default function attrsGenerator (type, data) { + return { + text () { + let attributeStr = '' + let watchedAttrs = [] + for (let attr in data) { + if (data.hasOwnProperty(attr)) { + watchedAttrs.push(attr) + attributeStr += `${ + typeof data[attr] !== 'undefined' + ? `${attr}="${data[attr]}"` + : attr + } ` + } + } + attributeStr += `${VUE_META_ATTRIBUTE}="${watchedAttrs.join(',')}"` + return attributeStr.trim() + } + } +} diff --git a/src/generateServerInjector.js b/src/generateServerInjector.js index e3545b5..72c74b7 100644 --- a/src/generateServerInjector.js +++ b/src/generateServerInjector.js @@ -1,4 +1,5 @@ -import { VUE_META_ATTRIBUTE } from './constants' +import titleGenerator from './titleGenerator' +import attrsGenerator from './attrsGenerator' /** * Converts a meta info property to one that can be stringified on the server @@ -10,24 +11,9 @@ import { VUE_META_ATTRIBUTE } from './constants' export default function generateServerInjector (type, data) { switch (type) { case 'title': - return { - text: () => `<${type} ${VUE_META_ATTRIBUTE}="true">${data}` - } + return titleGenerator(type, data) case 'htmlAttrs': case 'bodyAttrs': - return { - text () { - let attributeStr = '' - let watchedAttrs = [] - for (let attr in data) { - if (data.hasOwnProperty(attr)) { - watchedAttrs.push(attr) - attributeStr += `${typeof data[attr] !== 'undefined' ? `${attr}="${data[attr]}"` : attr} ` - } - } - attributeStr += `${VUE_META_ATTRIBUTE}="${watchedAttrs.join(',')}"` - return attributeStr.trim() - } - } + return attrsGenerator(type, data) } } diff --git a/src/titleGenerator.js b/src/titleGenerator.js new file mode 100644 index 0000000..dc8b440 --- /dev/null +++ b/src/titleGenerator.js @@ -0,0 +1,16 @@ +import { VUE_META_ATTRIBUTE } from './constants' + +/** + * Generates title output for the server + * + * @param {'title'} type - the string "title" + * @param {String} data - the title text + * @return {Object} - the title generator + */ +export default function titleGenerator (type, data) { + return { + text () { + return `<${type} ${VUE_META_ATTRIBUTE}="true">${data}` + } + } +}