// template views
function patch(oldEl: HTMLElement | null, newEl: DocumentFragment, fn?: () => any) {
if (!oldEl) return null
oldEl.parentNode?.replaceChild(newEl, oldEl)
return typeof fn === 'function' ? fn() : true
}
function html(templateStr: TemplateStringsArray, ...expressionSet: any[]) {
const template = document.createElement('template')
template.innerHTML = templateStr.map((s, i) => `${s}${expressionSet[i] || ''}`).join('')
return document.importNode(template.content, true)
}
// template helpers
const HTMLNote = {
UNKNOWN: 'unknown',
UNSUPPORTED: 'unsupported',
BLOCKED: 'blocked',
LIED: 'lied',
SECRET: 'secret',
}
const pluralify = (len) => len > 1 ? 's' : ''
const count = (arr) => arr && arr.constructor.name === 'Array' ? '' + (arr.length) : '0'
const getDiffs = ({ stringA, stringB, charDiff = false, decorate = (diff) => `[${diff}]` }) => {
if (!stringA || !stringB) return;
const splitter = charDiff ? '' : ' '
const listA = (''+stringA).split(splitter)
const listB = (''+stringB).split(splitter)
const listBWithDiffs = listB.map((x, i) => {
const matcher = listA[i]
const match = x == matcher
return !match ? decorate(x) : x
})
return listBWithDiffs.join(splitter)
}
// modal component
const modal = (name, result, linkname = 'details') => {
if (!result.length) {
return ''
}
return `
`
}
export { patch, html, HTMLNote, pluralify, getDiffs, count, modal }