租房小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.4 KiB

3 months ago
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.obj2xml = void 0;
  4. const formatObjKey_1 = require("./formatObjKey");
  5. function type(params) {
  6. return Object.prototype.toString
  7. .call(params)
  8. .replace(/(.*? |])/g, '')
  9. .toLowerCase();
  10. }
  11. function obj2xml(obj, options) {
  12. let s = '';
  13. if (options && options.headers) {
  14. s = '<?xml version="1.0" encoding="UTF-8"?>\n';
  15. }
  16. if (options && options.firstUpperCase) {
  17. obj = formatObjKey_1.formatObjKey(obj, 'firstUpperCase');
  18. }
  19. if (type(obj) === 'object') {
  20. Object.keys(obj).forEach(key => {
  21. // filter undefined or null
  22. if (type(obj[key]) !== 'undefined' && type(obj[key]) !== 'null') {
  23. if (type(obj[key]) === 'string' || type(obj[key]) === 'number') {
  24. s += `<${key}>${obj[key]}</${key}>`;
  25. }
  26. else if (type(obj[key]) === 'object') {
  27. s += `<${key}>${obj2xml(obj[key])}</${key}>`;
  28. }
  29. else if (type(obj[key]) === 'array') {
  30. s += obj[key].map(keyChild => `<${key}>${obj2xml(keyChild)}</${key}>`).join('');
  31. }
  32. else {
  33. s += `<${key}>${obj[key].toString()}</${key}>`;
  34. }
  35. }
  36. });
  37. }
  38. else {
  39. s += obj.toString();
  40. }
  41. return s;
  42. }
  43. exports.obj2xml = obj2xml;