@adaskothebeast/hierarchical-convert-to-dayjs 1.1.0 → 2.0.0

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.
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@adaskothebeast/hierarchical-convert-to-dayjs",
3
- "version": "1.1.0",
3
+ "version": "2.0.0",
4
4
  "peerDependencies": {
5
- "dayjs": "^1.11.9",
6
- "tslib": "2.5.3"
5
+ "dayjs": "^1.11.10",
6
+ "tslib": "^2.6.2"
7
7
  },
8
8
  "main": "./src/index.js",
9
- "types": "./src/index.d.ts"
9
+ "type": "commonjs"
10
10
  }
package/src/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../packages/hierarchical-convert-to-dayjs/src/index.ts"],"names":[],"mappings":";;;AAAA,8EAAoD"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../libs/hierarchical-convert-to-dayjs/src/index.ts"],"names":[],"mappings":";;;AAAA,8EAAoD"}
@@ -6,54 +6,44 @@ const duration = require("dayjs/plugin/duration");
6
6
  const utc = require("dayjs/plugin/utc");
7
7
  dayjs.extend(utc);
8
8
  dayjs.extend(duration);
9
- const setterDayjs = (obj, prop, val) => {
10
- obj[prop] = val;
11
- };
9
+ // Regular expression that matches ISO 8601 date strings
10
+ const dateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z$/;
11
+ const durationRegex = /^P(?:(0|[1-9]\d*)Y)?(?:(0|[1-9]\d*)M)?(?:(0|[1-9]\d*)W)?(?:(0|[1-9]\d*)D)?(?:T(?:(0|[1-9]\d*)H)?(?:(0|[1-9]\d*)M)?(?:(0|[1-9]\d*)S)?)?$/;
12
12
  /**
13
13
  * Function to recursively traverse the object and convert date strings to Dayjs and Duration objects in place.
14
14
  * @param obj Object to traverse
15
15
  * @returns Void.
16
16
  */
17
17
  function hierarchicalConvertToDayjs(obj) {
18
- // Regular expression that matches ISO 8601 date strings
19
- const dateRegex = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z$/;
20
- const iso8601DurationRegex = /^P(?:(0|[1-9]\d*)Y)?(?:(0|[1-9]\d*)M)?(?:(0|[1-9]\d*)W)?(?:(0|[1-9]\d*)D)?(?:T(?:(0|[1-9]\d*)H)?(?:(0|[1-9]\d*)M)?(?:(0|[1-9]\d*)S)?)?$/;
21
- if (typeof obj === 'object') {
22
- const o = obj;
23
- for (const key in o) {
24
- if (!Object.prototype.hasOwnProperty.call(o, key)) {
25
- continue;
26
- }
27
- const k = key;
28
- const v = o[k];
29
- if (typeof v === 'string') {
30
- if (dateRegex.test(v)) {
31
- // Convert string to Dayjs object if it matches the date regex
32
- setterDayjs(o, k, dayjs.utc(v));
33
- }
34
- else if (iso8601DurationRegex.test(v)) {
35
- // Convert string to Duration object if it matches the duration regex
36
- setterDayjs(o, k, dayjs.duration(v));
37
- }
38
- }
39
- else if (typeof v === 'object') {
40
- // Recurse into the object if it's not a string (could be an array or object)
41
- hierarchicalConvertToDayjs(v);
42
- }
43
- }
44
- }
45
- else if (Array.isArray(obj)) {
46
- // Recurse into the array if it's an array
47
- obj.forEach(hierarchicalConvertToDayjs);
18
+ if (typeof obj !== 'object') {
19
+ return;
48
20
  }
49
- else if (typeof obj === 'string') {
50
- if (dateRegex.test(obj)) {
51
- obj = dayjs.utc(obj);
21
+ const o = obj;
22
+ for (const key in o) {
23
+ if (!Object.prototype.hasOwnProperty.call(o, key)) {
24
+ continue;
25
+ }
26
+ const v = o[key];
27
+ if (typeof v === 'string') {
28
+ adjust(o, key, v);
52
29
  }
53
- else if (iso8601DurationRegex.test(obj)) {
54
- obj = dayjs.duration(obj);
30
+ else if (typeof v === 'object') {
31
+ // Recurse into the object if it's not a string (could be an array or object)
32
+ hierarchicalConvertToDayjs(v);
55
33
  }
56
34
  }
57
35
  }
58
36
  exports.hierarchicalConvertToDayjs = hierarchicalConvertToDayjs;
37
+ function adjust(o, k, v) {
38
+ if (dateRegex.test(v)) {
39
+ // Convert string to Dayjs object if it matches the date regex
40
+ o[k] = dayjs.utc(v);
41
+ return;
42
+ }
43
+ const match = durationRegex.exec(v);
44
+ if (match) {
45
+ // Convert string to Duration object if it matches the duration regex
46
+ o[k] = dayjs.duration(v);
47
+ }
48
+ }
59
49
  //# sourceMappingURL=hierarchical-convert-to-dayjs.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"hierarchical-convert-to-dayjs.js","sourceRoot":"","sources":["../../../../../packages/hierarchical-convert-to-dayjs/src/lib/hierarchical-convert-to-dayjs.ts"],"names":[],"mappings":";;;AAAA,+BAA+B;AAC/B,kDAAkD;AAClD,wCAAwC;AAExC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AAClB,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAOvB,MAAM,WAAW,GAAG,CAClB,GAAM,EACN,IAAO,EACP,GAAS,EACH,EAAE;IACR,GAAG,CAAC,IAAI,CAAC,GAAG,GAAG,CAAC;AAClB,CAAC,CAAC;AAEF;;;;GAIG;AACH,SAAgB,0BAA0B,CAAC,GAAY;IACrD,wDAAwD;IACxD,MAAM,SAAS,GAAW,kDAAkD,CAAC;IAC7E,MAAM,oBAAoB,GACxB,yIAAyI,CAAC;IAE5I,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;QAC3B,MAAM,CAAC,GAAG,GAAqB,CAAC;QAEhC,KAAK,MAAM,GAAG,IAAI,CAAC,EAAE;YACnB,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,cAAc,CAAC,IAAI,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE;gBACjD,SAAS;aACV;YAED,MAAM,CAAC,GAAG,GAAqB,CAAC;YAChC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;YACf,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE;gBACzB,IAAI,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE;oBACrB,8DAA8D;oBAC9D,WAAW,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;iBACjC;qBAAM,IAAI,oBAAoB,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE;oBACvC,qEAAqE;oBACrE,WAAW,CAAC,CAAC,EAAE,CAAC,EAAE,KAAK,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAC,CAAC;iBACtC;aACF;iBAAM,IAAI,OAAO,CAAC,KAAK,QAAQ,EAAE;gBAChC,6EAA6E;gBAC7E,0BAA0B,CAAC,CAAC,CAAC,CAAC;aAC/B;SACF;KACF;SAAM,IAAI,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;QAC7B,0CAA0C;QAC1C,GAAG,CAAC,OAAO,CAAC,0BAA0B,CAAC,CAAC;KACzC;SAAM,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;QAClC,IAAI,SAAS,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;YACvB,GAAG,GAAG,KAAK,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;SACtB;aAAM,IAAI,oBAAoB,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE;YACzC,GAAG,GAAG,KAAK,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC;SAC3B;KACF;AACH,CAAC;AAvCD,gEAuCC"}
1
+ {"version":3,"file":"hierarchical-convert-to-dayjs.js","sourceRoot":"","sources":["../../../../../libs/hierarchical-convert-to-dayjs/src/lib/hierarchical-convert-to-dayjs.ts"],"names":[],"mappings":";;;AAAA,+BAA+B;AAC/B,kDAAkD;AAClD,wCAAwC;AAExC,KAAK,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AAClB,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;AAOvB,wDAAwD;AACxD,MAAM,SAAS,GAAW,kDAAkD,CAAC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