| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- const BaseXform = require('../base-xform');
- class DateXform extends BaseXform {
- constructor(options) {
- super();
- this.tag = options.tag;
- this.attr = options.attr;
- this.attrs = options.attrs;
- this._format =
- options.format ||
- function(dt) {
- try {
- if (Number.isNaN(dt.getTime())) return '';
- return dt.toISOString();
- } catch (e) {
- return '';
- }
- };
- this._parse =
- options.parse ||
- function(str) {
- return new Date(str);
- };
- }
- render(xmlStream, model) {
- if (model) {
- xmlStream.openNode(this.tag);
- if (this.attrs) {
- xmlStream.addAttributes(this.attrs);
- }
- if (this.attr) {
- xmlStream.addAttribute(this.attr, this._format(model));
- } else {
- xmlStream.writeText(this._format(model));
- }
- xmlStream.closeNode();
- }
- }
- parseOpen(node) {
- if (node.name === this.tag) {
- if (this.attr) {
- this.model = this._parse(node.attributes[this.attr]);
- } else {
- this.text = [];
- }
- }
- }
- parseText(text) {
- if (!this.attr) {
- this.text.push(text);
- }
- }
- parseClose() {
- if (!this.attr) {
- this.model = this._parse(this.text.join(''));
- }
- return false;
- }
- }
- module.exports = DateXform;
|