@pact-foundation/pact
Version:
Pact for all things Javascript
73 lines • 2.31 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.XmlElement = void 0;
const matchers_1 = require("../v3/matchers");
const xmlNode_1 = require("./xmlNode");
const xmlText_1 = require("./xmlText");
const modifyElementWithCallback = (el, cb) => {
if (cb) {
cb(el);
}
};
class XmlElement extends xmlNode_1.XmlNode {
name;
children = [];
attributes;
constructor(name) {
super();
this.name = name;
}
setName(name) {
this.name = name;
return this;
}
setAttributes(attributes) {
this.attributes = attributes;
return this;
}
/**
* Creates a new element with the given name and attributes and then sets it's text content (can be a matcher)
* @param name Element name
* @param attributes Map of element attributes
* @param arg Callback to configure the new element, or text content to create the new element with (can be a matcher)
*/
appendElement(name, attributes, arg) {
const el = new XmlElement(name).setAttributes(attributes);
if (arg) {
if (typeof arg !== 'function') {
el.appendText(arg);
}
else {
modifyElementWithCallback(el, arg);
}
}
this.children.push(el);
return this;
}
appendText(content) {
if (typeof content === 'object' && content['pact:matcher:type']) {
this.children.push(new xmlText_1.XmlText((0, matchers_1.isMatcher)(content) &&
'value' in content &&
content.value !== undefined &&
typeof content.value === 'string'
? content.value
: '', content));
}
else {
this.children.push(new xmlText_1.XmlText(content.toString()));
}
return this;
}
eachLike(name, attributes, cb, options = { examples: 1 }) {
const el = new XmlElement(name).setAttributes(attributes);
modifyElementWithCallback(el, cb);
this.children.push({
'pact:matcher:type': 'type',
value: el,
examples: options.examples,
});
return this;
}
}
exports.XmlElement = XmlElement;
//# sourceMappingURL=xmlElement.js.map