Skip to content

Commit fa90902

Browse files
pimlieTheAlexLichter
authored andcommitted
refactor: extract applyTemplate method and make it more generic
1 parent 15eb9cc commit fa90902

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/shared/applyTemplate.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { isUndefined, isFunction } from './typeof'
2+
3+
export default function applyTemplate({ component, metaTemplateKeyName, contentKeyName }, headObject, template, chunk) {
4+
if (isUndefined(template)) {
5+
template = headObject[metaTemplateKeyName]
6+
delete headObject[metaTemplateKeyName]
7+
}
8+
9+
// return early if no template defined
10+
if (!template) {
11+
return false
12+
}
13+
14+
if (isUndefined(chunk)) {
15+
chunk = headObject[contentKeyName]
16+
}
17+
18+
if (isFunction(template)) {
19+
headObject[contentKeyName] = template.call(component, chunk)
20+
} else {
21+
headObject[contentKeyName] = template.replace(/%s/g, chunk)
22+
}
23+
return true
24+
}

0 commit comments

Comments
 (0)