nodejs-google-adwords
Version:
Google Ads API Client Library for Node.js
50 lines (49 loc) • 1.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var xml2js_1 = require("xml2js");
var XMLService = /** @class */ (function () {
function XMLService() {
}
/**
* parse XML to JSON
*
* @author dulin
* @static
* @template Rval
* @param {convertableToString} xml
* @param {OptionsV2} [options={ trim: true }]
* @returns {Promise<Rval>}
* @memberof XMLService
*/
XMLService.parseStringPromise = function (xml, options) {
if (options === void 0) { options = { trim: true }; }
return new Promise(function (resolve, reject) {
xml2js_1.parseString(xml, options, function (err, result) {
if (err) {
return reject(err);
}
resolve(result);
});
});
};
/**
* extract value from a XML string by RegExp
*
* @author dulin
* @static
* @param {string} xml
* @param {string} element
* @returns {(string | undefined)}
* @memberof XMLService
*/
XMLService.extractValueFromElement = function (xml, element) {
var pattern = "<" + element + ">(.*?)</" + element + ">";
var matcher = new RegExp(pattern, 'g');
var matchArray = xml.match(matcher);
if (matchArray && matchArray.length) {
return matchArray[0].replace(/(<([^>]+)>)/gi, '');
}
};
return XMLService;
}());
exports.XMLService = XMLService;