"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.setup = void 0; const defaultOptions = { elementDelimiter: "__", modifierDelimiter: "--", namespace: "", namespaceDelimiter: "-", strict: true, }; /** * Set up the default options. * * @param options - Options to control a generated class name. */ function setup({ elementDelimiter, modifierDelimiter, namespace, namespaceDelimiter, strict, }) { if (typeof elementDelimiter === "string") { defaultOptions.elementDelimiter = elementDelimiter; } if (typeof modifierDelimiter === "string") { defaultOptions.modifierDelimiter = modifierDelimiter; } if (typeof namespace === "string" || Array.isArray(namespace)) { defaultOptions.namespace = namespace; } if (typeof namespaceDelimiter === "string") { defaultOptions.namespaceDelimiter = namespaceDelimiter; } if (typeof strict === "boolean") { defaultOptions.strict = strict; } } exports.setup = setup; const uniqueChars = (list) => list .join("") .split("") .filter((value, index, self) => self.indexOf(value) === index); const includesChars = (str, chars) => chars.some((char) => str.includes(char)); const invalidMessage = (subject, subjectValue, delimiters) => { const delims = `"${delimiters.join('", "')}"`; return `The ${subject} ("${subjectValue}") must not use the characters contained within the delimiters (${delims}).`; }; /** * Creates a function to generate a BEM class name. * * @param block - A BEM block name. * @param options - Options to control a generated class name. * @returns A function to generate a BEM class name. */ function bem(block, options = {}) { const { elementDelimiter, modifierDelimiter, namespace, namespaceDelimiter, strict } = { ...defaultOptions, ...options, }; const namespaces = [] .concat(namespace) .filter(Boolean) // compact .reduce((joined, ns) => `${joined}${ns}${namespaceDelimiter}`, ""); const namespaceBlock = `${namespaces}${block}`; const delimiters = strict ? [namespaceDelimiter, elementDelimiter, modifierDelimiter] : []; const delimiterChars = strict ? uniqueChars(delimiters) : []; return function bemBlock(elementOrModifiers, modifiers) { if (elementOrModifiers == null) { return namespaceBlock; } const element = typeof elementOrModifiers === "string" ? elementOrModifiers : null; if (strict && element != null && includesChars(element, delimiterChars)) { throw new Error(invalidMessage("element", element, delimiters)); } const base = element == null ? namespaceBlock : `${namespaceBlock}${elementDelimiter}${element}`; const mods = typeof elementOrModifiers === "string" ? modifiers : elementOrModifiers; if (mods == null) { return base; } const addModifiers = (className, modifier) => { if (modifier != null && modifier !== "") { if (strict && includesChars(modifier, delimiterChars)) { throw new Error(invalidMessage("modifier", modifier, delimiters)); } return `${className} ${base}${modifierDelimiter}${modifier}`; } return className; }; if (Array.isArray(mods)) { return mods.reduce(addModifiers, base); } return Object.keys(mods) .filter((mod) => mods[mod]) .reduce(addModifiers, base); }; } exports.default = bem;