UNPKG

dynamicsnode

Version:

Create simple scripts to interact with Dynamics CRM using Node.js

71 lines 3.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var XmlEncode = (function () { function XmlEncode() { } XmlEncode.encodeName = function (value) { var encodedValues = []; var encodedName = null; if (value !== null && value.length > 0) { // first character if (XmlEncode.XML_BEGINNING_NAME_NOT_ALLOWED_CHARACTERS.indexOf(value[0]) != -1 || XmlEncode.XML_NAME_NOT_ALLOWED_CHARACTERS.indexOf(value[0]) != -1) { encodedValues.push("_x00" + value.charCodeAt(0).toString(16) + "_"); } else { encodedValues.push(value[0]); } for (var i = 1; i < value.length; i++) { var char = value[i]; if (XmlEncode.XML_NAME_NOT_ALLOWED_CHARACTERS.indexOf(value[i]) != -1) { encodedValues.push("_x00" + value.charCodeAt(i).toString(16) + "_"); } else { encodedValues.push(value[i]); } } } if (encodedValues.length > 0) { encodedName = encodedValues.join(''); } return encodedName; }; XmlEncode.decodeName = function (encodedName) { var decodedNameParts = []; var decodedName = null; if (encodedName !== null && encodedName.length > 0) { for (var i = 0; i < encodedName.length; i++) { var char = encodedName[i]; var encodedChar = false; if (char === '_') { if (encodedName.length - i > 6 && encodedName[i + 1] === 'x' && encodedName[i + 6] === '_') { var charCodeStr = encodedName.substr(i + 2, 4); var charCode = parseInt(charCodeStr, 16); if (!isNaN(charCode) && isFinite(charCode)) { encodedChar = true; var decodedChar = String.fromCharCode(charCode); decodedNameParts.push(decodedChar); i += 6; } } } if (!encodedChar) { // just a regular char not encoded decodedNameParts.push(char); } } } if (decodedNameParts.length > 0) { decodedName = decodedNameParts.join(''); } return decodedName; }; return XmlEncode; }()); // https://www.w3.org/TR/REC-xml/#sec-common-syn // http://www.java2s.com/Code/Java/XML/EncoderandDecoderforXMLelementandattributenames.htm // http://www.fileformat.info/info/charset/UTF-16/list.htm XmlEncode.XML_NAME_NOT_ALLOWED_CHARACTERS = [' ', '<', '>', '"', '\'', '&', ';']; XmlEncode.XML_BEGINNING_NAME_NOT_ALLOWED_CHARACTERS = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']; exports.XmlEncode = XmlEncode; //# sourceMappingURL=XmlEncode.js.map