integer-xform.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. const BaseXform = require('../base-xform');
  2. class IntegerXform extends BaseXform {
  3. constructor(options) {
  4. super();
  5. this.tag = options.tag;
  6. this.attr = options.attr;
  7. this.attrs = options.attrs;
  8. // option to render zero
  9. this.zero = options.zero;
  10. }
  11. render(xmlStream, model) {
  12. // int is different to float in that zero is not rendered
  13. if (model || this.zero) {
  14. xmlStream.openNode(this.tag);
  15. if (this.attrs) {
  16. xmlStream.addAttributes(this.attrs);
  17. }
  18. if (this.attr) {
  19. xmlStream.addAttribute(this.attr, model);
  20. } else {
  21. xmlStream.writeText(model);
  22. }
  23. xmlStream.closeNode();
  24. }
  25. }
  26. parseOpen(node) {
  27. if (node.name === this.tag) {
  28. if (this.attr) {
  29. this.model = parseInt(node.attributes[this.attr], 10);
  30. } else {
  31. this.text = [];
  32. }
  33. return true;
  34. }
  35. return false;
  36. }
  37. parseText(text) {
  38. if (!this.attr) {
  39. this.text.push(text);
  40. }
  41. }
  42. parseClose() {
  43. if (!this.attr) {
  44. this.model = parseInt(this.text.join('') || 0, 10);
  45. }
  46. return false;
  47. }
  48. }
  49. module.exports = IntegerXform;