element-vir
Version:
Heroic. Reactive. Declarative. Type safe. Web components without compromise.
31 lines (30 loc) • 1.31 kB
JavaScript
/* eslint-disable @typescript-eslint/no-empty-object-type */
import { defineElement } from './define-element.js';
/**
* Wraps {@link defineElement} in a superset of requirements. For example:
*
* - You could create element definition functions that require all elements to start with a common
* prefix, like `vir-`.
* - You could create element definition functions that require all elements to have _at least_ a
* specified set of input properties.
* - Etc.
*
* @category Element Definition
* @throws If `options.transformInputs` returns a different `tagName` than it was given.
*/
export function wrapDefineElement(options) {
const { assertInputs, transformInputs } = {
assertInputs: options?.assertInputs ?? (() => { }),
transformInputs: options?.transformInputs ?? ((inputInit) => inputInit),
};
return (...errorParams) => {
return (inputs) => {
assertInputs(inputs);
const transformedInputs = transformInputs(inputs);
if (transformedInputs.tagName !== inputs.tagName) {
throw new Error(`transformInputs cannot change tagName: '${inputs.tagName}' was transformed into '${transformedInputs.tagName}'.`);
}
return defineElement(...errorParams)(transformedInputs);
};
};
}