arb-convert
Version:
Convert Application Resource Bundle (ARB) translation files to other translation formats and back
82 lines (81 loc) • 2.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
function xmlQuery(node) {
if (typeof node === 'object' && '__xmlQuery' in node) {
return node;
}
var wrappedNode = {
__xmlQuery: true,
originalNode: node,
attributes: {},
elements: [], // default
};
if (Array.isArray(node)) {
wrappedNode.elements = node;
}
else if (node != null && typeof node !== 'string' && typeof node !== 'number') {
Object.assign(wrappedNode, node);
}
wrappedNode.innerElements = innerElements.bind(wrappedNode);
wrappedNode.innerText = innerText.bind(wrappedNode);
wrappedNode.query = query.bind(wrappedNode);
wrappedNode.queryAll = queryAll.bind(wrappedNode);
wrappedNode.forEach = forEach.bind(wrappedNode);
wrappedNode.map = map.bind(wrappedNode);
return wrappedNode;
}
exports.default = xmlQuery;
function innerElements() {
return Array.isArray(this.originalNode)
// flatMap equivalent
? this.elements.reduce(function (acc, el) { return acc.concat(el.elements || []); }, [])
: this.elements;
}
function innerText() {
return this.innerElements()
.filter(function (el) { return el.type === 'text'; })
.map(function (el) { return el.text; })
.join('');
}
function query(predicate) {
switch (typeof predicate) {
case 'string':
return xmlQuery(this.innerElements()
.map(xmlQuery)
.find(function (el) { return el.name === predicate; }));
case 'number':
return xmlQuery(this.innerElements()
.map(xmlQuery)[predicate]);
default:
return xmlQuery(this.innerElements()
.map(xmlQuery)
.find(predicate));
}
}
function queryAll(predicate) {
switch (typeof predicate) {
case 'string':
return xmlQuery(this.innerElements()
.map(xmlQuery)
.filter(function (el) { return el.name === predicate; }));
case 'number':
return xmlQuery([
this.innerElements()
.map(xmlQuery)[predicate],
]);
default:
return xmlQuery(this.innerElements()
.map(xmlQuery)
.filter(predicate));
}
}
function forEach(cb, thisArg) {
return this.elements
.map(xmlQuery)
.forEach(cb, thisArg);
}
function map(cb, thisArg) {
return xmlQuery(this.elements
.map(xmlQuery)
.map(cb, thisArg));
}