-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from github/feat-add-createprocessor-for-simpl…
…e-processor-creation feat: add createProcessor for simple processor creation
- Loading branch information
Showing
3 changed files
with
71 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,43 @@ | ||
import type {TemplatePart} from './types.js' | ||
import type {TemplatePart, TemplateTypeInit} from './types.js' | ||
import type {TemplateInstance} from './template-instance.js' | ||
import {AttributeTemplatePart} from './attribute-template-part.js' | ||
|
||
export const propertyIdentity = { | ||
createCallback(instance: TemplateInstance, parts: Iterable<TemplatePart>, params: unknown): void { | ||
this.processCallback(instance, parts, params) | ||
}, | ||
processCallback(instance: TemplateInstance, parts: Iterable<TemplatePart>, params: unknown): void { | ||
if (typeof params !== 'object' || !params) return | ||
for (const part of parts) { | ||
if (!(part.expression in params)) continue | ||
part.value = String((params as Record<string, unknown>)[part.expression] ?? '') | ||
type PartProcessor = (part: TemplatePart, value: unknown) => void | ||
|
||
export function createProcessor(processPart: PartProcessor): TemplateTypeInit { | ||
return { | ||
createCallback(instance: TemplateInstance, parts: Iterable<TemplatePart>, params: unknown): void { | ||
this.processCallback(instance, parts, params) | ||
}, | ||
processCallback(_: TemplateInstance, parts: Iterable<TemplatePart>, params: unknown): void { | ||
if (typeof params !== 'object' || !params) return | ||
for (const part of parts) { | ||
if (part.expression in params) { | ||
const value = (params as Record<string, unknown>)[part.expression] ?? '' | ||
processPart(part, value) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
export const propertyIdentityOrBooleanAttribute = { | ||
createCallback(instance: TemplateInstance, parts: Iterable<TemplatePart>, params: unknown): void { | ||
this.processCallback(instance, parts, params) | ||
}, | ||
processCallback(instance: TemplateInstance, parts: Iterable<TemplatePart>, params: unknown): void { | ||
if (typeof params !== 'object' || !params) return | ||
for (const part of parts) { | ||
if (!(part.expression in params)) continue | ||
const value = (params as Record<string, unknown>)[part.expression] ?? '' | ||
if ( | ||
typeof value === 'boolean' && | ||
part instanceof AttributeTemplatePart && | ||
typeof part.element[part.attributeName as keyof Element] === 'boolean' | ||
) { | ||
part.booleanValue = value | ||
} else { | ||
part.value = String(value) | ||
} | ||
} | ||
export function processPropertyIdentity(part: TemplatePart, value: unknown): void { | ||
part.value = String(value) | ||
} | ||
|
||
export function processBooleanAttribute(part: TemplatePart, value: unknown): boolean { | ||
if ( | ||
typeof value === 'boolean' && | ||
part instanceof AttributeTemplatePart && | ||
typeof part.element[part.attributeName as keyof Element] === 'boolean' | ||
) { | ||
part.booleanValue = value | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
export const propertyIdentity = createProcessor(processPropertyIdentity) | ||
export const propertyIdentityOrBooleanAttribute = createProcessor((part: TemplatePart, value: unknown) => { | ||
processBooleanAttribute(part, value) || processPropertyIdentity(part, value) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import {TemplateInstance} from '../lib/template-instance.js' | ||
import {createProcessor} from '../lib/processors.js' | ||
describe('createProcessor', () => { | ||
let calls = 0 | ||
let processor | ||
let template | ||
const originalHTML = `Hello {{x}}!` | ||
beforeEach(() => { | ||
calls = 0 | ||
processor = createProcessor(() => (calls += 1)) | ||
template = document.createElement('template') | ||
template.innerHTML = originalHTML | ||
}) | ||
|
||
it('creates a processor calling the given function when the param exists', () => { | ||
const instance = new TemplateInstance(template, {x: 'world'}, processor) | ||
expect(calls).to.eql(1) | ||
instance.update({x: 'foo'}) | ||
expect(calls).to.eql(2) | ||
instance.update({}) | ||
expect(calls).to.eql(2) | ||
}) | ||
|
||
it('does not process parts with no param for the expression', () => { | ||
const instance = new TemplateInstance(template, {}, processor) | ||
expect(calls).to.eql(0) | ||
instance.update({y: 'world'}) | ||
expect(calls).to.eql(0) | ||
}) | ||
}) |