@genrojs/bag 0.10.1 → 0.10.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/bag.js +21 -1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@genrojs/bag",
3
- "version": "0.10.1",
3
+ "version": "0.10.2",
4
4
  "description": "JavaScript port of genro-bag - hierarchical data container",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
package/src/bag.js CHANGED
@@ -19,6 +19,22 @@ function* serializationNodes(bag, prefix = '') {
19
19
  }
20
20
  }
21
21
 
22
+ function xmlDateText(value) {
23
+ const iso = value.toISOString();
24
+ const isEpochDate = value.getUTCFullYear() === 1970 &&
25
+ value.getUTCMonth() === 0 && value.getUTCDate() === 1;
26
+ const isMidnight = value.getUTCHours() === 0 &&
27
+ value.getUTCMinutes() === 0 && value.getUTCSeconds() === 0 &&
28
+ value.getUTCMilliseconds() === 0;
29
+
30
+ // Date is also used for date-only and time-only values. Keep their XML
31
+ // lexical forms distinct while using UTC for every representation.
32
+ if (isMidnight && !isEpochDate) return iso.slice(0, iso.indexOf('T'));
33
+ const timestamp = iso.replace(/\.000Z$/, 'Z');
34
+ if (isEpochDate && !isMidnight) return timestamp.slice(timestamp.indexOf('T') + 1);
35
+ return timestamp;
36
+ }
37
+
22
38
  /**
23
39
  * Bag - Hierarchical data container with path-based access.
24
40
  *
@@ -1930,6 +1946,7 @@ export class Bag {
1930
1946
  * Serialize Bag to XML format.
1931
1947
  *
1932
1948
  * All values are converted to strings without type information.
1949
+ * Date values use ISO 8601 text in UTC (date, time, or datetime).
1933
1950
  * For type-preserving serialization, use toTytx() instead.
1934
1951
  *
1935
1952
  * @param {Object} [options={}] - Serialization options.
@@ -2024,7 +2041,10 @@ export class Bag {
2024
2041
  return `<${tag}${attrsStr}></${tag}>`;
2025
2042
  }
2026
2043
 
2027
- const text = this._escapeXml(String(value));
2044
+ const scalar = value instanceof Date && !Number.isNaN(value.getTime())
2045
+ ? xmlDateText(value)
2046
+ : String(value);
2047
+ const text = this._escapeXml(scalar);
2028
2048
  return `<${tag}${attrsStr}>${text}</${tag}>`;
2029
2049
  }
2030
2050