einvoicing
Version:
A JavaScript library for creating and parsing electronic invoices compliant with the eInvoicing Directive, EN 16931, and popular extensions
59 lines • 1.55 kB
JavaScript
;
/**
* Helpers
*
* @copyright Vitalii Savchuk <esvit666@gmail.com>
* @package esvit/einvoicing
* @licence MIT https://opensource.org/licenses/MIT
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.strOrUnd = strOrUnd;
exports.numOrUnd = numOrUnd;
exports.getArray = getArray;
/**
* Returns the string value of a node or undefined if the node is null or undefined.
*
* @param node xml node
*/
function strOrUnd(node) {
if (!node) {
return undefined;
}
if (typeof node === 'object') {
if (typeof node['#text'] === 'undefined') {
throw new Error('Invalid node');
}
return node['#text'].toString();
}
return node.toString();
}
/**
* Returns the number value of a node or undefined if the node is null or undefined.
*
* @param node xml node
*/
function numOrUnd(node) {
return strOrUnd(node) ? parseFloat(strOrUnd(node)) : undefined;
}
/**
* Returns the array of nodes or an empty array if the node is null or undefined.
*
* @param node xml node
* @param path path of nodes to get the value from
*/
function getArray(node, path) {
if (path === void 0) { path = []; }
var initNode = node;
for (var _i = 0, path_1 = path; _i < path_1.length; _i++) {
var key = path_1[_i];
if (!initNode) {
return [];
}
initNode = initNode[key];
}
if (!initNode) {
return [];
}
return Array.isArray(initNode) ? initNode : [initNode];
}
//# sourceMappingURL=helpers.js.map