element-vir
Version:
Heroic. Reactive. Declarative. Type safe. Web components without compromise.
75 lines (74 loc) • 2.11 kB
JavaScript
import { arrayToObject } from '@augment-vir/common';
/**
* Asserts that all the given string names for the given element are valid.
*
* @category Internal
*/
export function assertValidStringNames(elementTagName, stringNames) {
const requiredNameStart = [
elementTagName,
'-',
].join('');
stringNames.forEach((stringName) => {
if (!stringName.startsWith(requiredNameStart)) {
throw new Error(`Invalid element string name '${stringName}' in '${elementTagName}': element string names must begin with the element's tag name.`);
}
});
}
/**
* Converts an array of string names into a `StringNameMap`.
*
* @category Internal
*/
export function createStringNameMap(elementTagName, nameType, stringNames) {
if (!stringNames) {
return {};
}
const stringNameMap = arrayToObject(stringNames, (stringName) => {
return {
key: stringName,
value: [
elementTagName,
nameType,
stringName,
].join('-'),
};
}, {
useRequired: true,
});
return stringNameMap;
}
/**
* Converts an array of slot names into a {@link SlotNamesMap}.
*
* Slot names that start with the element's tag name pass through unchanged. Slot names that do not
* are transformed into the legacy `${tagName}-slot-${name}` format so existing elements that
* pre-date the tag prefix convention keep working.
*
* @category Internal
*/
export function createSlotNamesMap(elementTagName, slotNames) {
if (!slotNames) {
return {};
}
const requiredNameStart = [
elementTagName,
'-',
].join('');
const slotNamesMap = arrayToObject(slotNames, (slotName) => {
const value = slotName.startsWith(requiredNameStart)
? slotName
: [
elementTagName,
'slot',
slotName,
].join('-');
return {
key: slotName,
value,
};
}, {
useRequired: true,
});
return slotNamesMap;
}