vue-remove-attributes
Version:
A vue-template-compiler module that removes unwanted attributes from templates.
54 lines (46 loc) • 1.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
const createAttributeRemover = matcher => {
const testFn = getTestFunction(matcher);
const preTransformNode = AST => {
const {
attrsList,
attrsMap
} = AST;
const attrsToDelete = attrsList.filter(({
name
}) => testFn(name));
attrsToDelete.forEach(({
name: attrName
}) => {
if (attrsMap[attrName] !== undefined) {
delete attrsMap[attrName];
}
const idx = attrsList.findIndex(({
name
}) => attrName === name);
attrsList.splice(idx, 1);
});
return AST;
};
return {
preTransformNode
};
};
const getTestFunction = matcher => {
if (typeof matcher === "string") {
return name => name === matcher;
} else if (matcher instanceof RegExp) {
return matcher.test.bind(matcher);
} else if (Array.isArray(matcher) && matcher.every(matchValue => typeof matchValue === "string")) {
const set = new Set(matcher);
return set.has.bind(set);
}
throw new Error("Matcher must be a string, array of strings, or a regular expression");
};
var _default = createAttributeRemover;
exports.default = _default;
module.exports = exports.default;