From fa90902fc79261230e53f1a22c0af8a05eb29c80 Mon Sep 17 00:00:00 2001 From: pimlie Date: Fri, 8 Mar 2019 12:59:16 +0100 Subject: [PATCH] refactor: extract applyTemplate method and make it more generic --- src/shared/applyTemplate.js | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/shared/applyTemplate.js diff --git a/src/shared/applyTemplate.js b/src/shared/applyTemplate.js new file mode 100644 index 0000000..a83e9b3 --- /dev/null +++ b/src/shared/applyTemplate.js @@ -0,0 +1,24 @@ +import { isUndefined, isFunction } from './typeof' + +export default function applyTemplate({ component, metaTemplateKeyName, contentKeyName }, headObject, template, chunk) { + if (isUndefined(template)) { + template = headObject[metaTemplateKeyName] + delete headObject[metaTemplateKeyName] + } + + // return early if no template defined + if (!template) { + return false + } + + if (isUndefined(chunk)) { + chunk = headObject[contentKeyName] + } + + if (isFunction(template)) { + headObject[contentKeyName] = template.call(component, chunk) + } else { + headObject[contentKeyName] = template.replace(/%s/g, chunk) + } + return true +}