@leancodepl/api-date-datefns 8.5.0 → 8.5.1

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/README.md ADDED
@@ -0,0 +1,130 @@
1
+ # @leancodepl/api-date-datefns
2
+
3
+ Date conversion utilities using date-fns for API date types.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @leancodepl/api-date-datefns
9
+ # or
10
+ yarn add @leancodepl/api-date-datefns
11
+ ```
12
+
13
+ ## API
14
+
15
+ ### `fromApiDate(date)`
16
+
17
+ Converts ApiDateOnly to JavaScript Date using date-fns.
18
+
19
+ **Parameters:**
20
+
21
+ - `date: ApiDateOnly` - The API date string to convert
22
+
23
+ **Returns:** JavaScript Date object
24
+
25
+ ### `toApiDate(date)`
26
+
27
+ Converts JavaScript Date to ApiDateOnly using date-fns.
28
+
29
+ **Parameters:**
30
+
31
+ - `date: Date` - The JavaScript Date to convert
32
+
33
+ **Returns:** ApiDateOnly string
34
+
35
+ ### `fromApiTime(time)`
36
+
37
+ Converts ApiTimeOnly to JavaScript Date using date-fns.
38
+
39
+ **Parameters:**
40
+
41
+ - `time: ApiTimeOnly` - The API time string to convert
42
+
43
+ **Returns:** JavaScript Date object
44
+
45
+ ### `toApiTime(time)`
46
+
47
+ Converts JavaScript Date to ApiTimeOnly using date-fns.
48
+
49
+ **Parameters:**
50
+
51
+ - `time: Date` - The JavaScript Date to convert
52
+
53
+ **Returns:** ApiTimeOnly string
54
+
55
+ ### `fromApiDateTimeOffset(dateTimeOffset)`
56
+
57
+ Converts ApiDateTimeOffset to JavaScript Date using date-fns.
58
+
59
+ **Parameters:**
60
+
61
+ - `dateTimeOffset: ApiDateTimeOffset` - The API datetime with offset string to convert
62
+
63
+ **Returns:** JavaScript Date object
64
+
65
+ ### `toApiDateTimeOffset(dateTimeOffset)`
66
+
67
+ Converts JavaScript Date to ApiDateTimeOffset using date-fns.
68
+
69
+ **Parameters:**
70
+
71
+ - `dateTimeOffset: Date` - The JavaScript Date to convert
72
+
73
+ **Returns:** ApiDateTimeOffset string
74
+
75
+ ### `fromApiTimeSpan(timeSpan)`
76
+
77
+ Converts ApiTimeSpan to milliseconds using date-fns.
78
+
79
+ **Parameters:**
80
+
81
+ - `timeSpan: ApiTimeSpan` - The API timespan string to convert
82
+
83
+ **Returns:** Duration in milliseconds
84
+
85
+ ### `toApiTimeSpan(differenceInMilliseconds)`
86
+
87
+ Converts milliseconds to ApiTimeSpan using date-fns.
88
+
89
+ **Parameters:**
90
+
91
+ - `differenceInMilliseconds: number` - The duration in milliseconds to convert
92
+
93
+ **Returns:** ApiTimeSpan string
94
+
95
+ ## Usage Examples
96
+
97
+ ### Date Conversion
98
+
99
+ ```typescript
100
+ import { fromApiDate, toApiDate } from "@leancodepl/api-date-datefns"
101
+
102
+ const apiDate = "2023-12-25"
103
+ const jsDate = fromApiDate(apiDate)
104
+ console.log(jsDate) // Date object
105
+
106
+ const convertedBack = toApiDate(jsDate)
107
+ console.log(convertedBack) // '2023-12-25'
108
+ ```
109
+
110
+ ### Time Conversion
111
+
112
+ ```typescript
113
+ import { fromApiTime, toApiTime } from "@leancodepl/api-date-datefns"
114
+
115
+ const apiTime = "14:30:00"
116
+ const jsDate = fromApiTime(apiTime)
117
+ const convertedBack = toApiTime(jsDate)
118
+ console.log(convertedBack) // '14:30:00'
119
+ ```
120
+
121
+ ### DateTime with Offset
122
+
123
+ ```typescript
124
+ import { fromApiDateTimeOffset, toApiDateTimeOffset } from "@leancodepl/api-date-datefns"
125
+
126
+ const apiDateTime = "2023-12-25T14:30:00+01:00"
127
+ const jsDate = fromApiDateTimeOffset(apiDateTime)
128
+ const convertedBack = toApiDateTimeOffset(jsDate)
129
+ console.log(convertedBack) // '2023-12-25T14:30:00+01:00'
130
+ ```
@@ -0,0 +1 @@
1
+ exports._default = require('./index.cjs.js').default;
package/index.cjs.js ADDED
@@ -0,0 +1,119 @@
1
+ 'use strict';
2
+
3
+ var apiDate = require('@leancodepl/api-date');
4
+ var dateFns = require('date-fns');
5
+ var apiDateUtils = require('@leancodepl/api-date-utils');
6
+
7
+ function fromApiTime(time) {
8
+ if (!time) {
9
+ return undefined;
10
+ }
11
+ return dateFns.parse(time, "HH:mm:ss.SSS", new Date());
12
+ }
13
+
14
+ function toApiTime(time) {
15
+ if (!time) {
16
+ return undefined;
17
+ }
18
+ return dateFns.format(time, "HH:mm:ss.SSS");
19
+ }
20
+
21
+ function fromApiDate(date) {
22
+ if (date && typeof date === "string") {
23
+ return dateFns.parse(date, "yyyy-MM-dd", new Date());
24
+ }
25
+ return undefined;
26
+ }
27
+
28
+ function toApiDate(date) {
29
+ if (!date) {
30
+ return undefined;
31
+ }
32
+ return dateFns.format(date, "yyyy-MM-dd");
33
+ }
34
+
35
+ function fromApiDateTimeOffset(dateTimeOffset) {
36
+ if (!dateTimeOffset) {
37
+ return undefined;
38
+ }
39
+ return dateFns.parse(dateTimeOffset, "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", new Date());
40
+ }
41
+
42
+ function toApiDateTimeOffset(dateTimeOffset) {
43
+ if (!dateTimeOffset) {
44
+ return undefined;
45
+ }
46
+ return dateFns.format(dateTimeOffset, "yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
47
+ }
48
+
49
+ function fromApiTimeSpan(timeSpan) {
50
+ if (!timeSpan) {
51
+ return undefined;
52
+ }
53
+ const parsedApiTimeSpan = apiDateUtils.parseApiTimeSpan(timeSpan);
54
+ const parsedApiTimeSpanValues = parsedApiTimeSpan.values;
55
+ const daysInMilliseconds = dateFns.hoursToMilliseconds(parsedApiTimeSpanValues.days * 24);
56
+ const hoursInMilliseconds = dateFns.hoursToMilliseconds(parsedApiTimeSpanValues.hours);
57
+ const minutesInMilliseconds = dateFns.minutesToMilliseconds(parsedApiTimeSpanValues.minutes);
58
+ const secondsInMilliseconds = dateFns.secondsToMilliseconds(parsedApiTimeSpanValues.seconds);
59
+ const differenceInMilliseconds = daysInMilliseconds + hoursInMilliseconds + minutesInMilliseconds + secondsInMilliseconds + parsedApiTimeSpanValues.milliseconds;
60
+ const signBasedMultiplier = parsedApiTimeSpan.sign === "-" ? -1 : 1;
61
+ return signBasedMultiplier * differenceInMilliseconds;
62
+ }
63
+
64
+ function padTo2(x) {
65
+ var _x_toFixed;
66
+ const stringified = (_x_toFixed = x == null ? void 0 : x.toFixed(0)) != null ? _x_toFixed : "0";
67
+ return stringified.padStart(2, "0");
68
+ }
69
+
70
+ function parseDifferenceInMilliseconds(differenceInMilliseconds) {
71
+ const milliseconds = differenceInMilliseconds % 1000;
72
+ const seconds = Math.floor(differenceInMilliseconds / 1000 % 60);
73
+ const minutes = Math.floor(differenceInMilliseconds / (1000 * 60) % 60);
74
+ const hours = Math.floor(differenceInMilliseconds / (1000 * 60 * 60) % 24);
75
+ const days = Math.floor(differenceInMilliseconds / (1000 * 60 * 60 * 24));
76
+ return {
77
+ milliseconds,
78
+ seconds,
79
+ minutes,
80
+ hours,
81
+ days
82
+ };
83
+ }
84
+
85
+ function toApiTimeSpan(differenceInMilliseconds) {
86
+ let stringTimeSpan = "";
87
+ if (!differenceInMilliseconds) {
88
+ return undefined;
89
+ }
90
+ const isNegative = differenceInMilliseconds < 0;
91
+ const absDifferenceInMilliseconds = Math.abs(differenceInMilliseconds);
92
+ const { milliseconds, seconds, minutes, hours, days } = parseDifferenceInMilliseconds(absDifferenceInMilliseconds);
93
+ if (isNegative) {
94
+ stringTimeSpan += "-";
95
+ }
96
+ if (days > 0) {
97
+ stringTimeSpan += `${days}.`;
98
+ }
99
+ stringTimeSpan += `${padTo2(hours)}:${padTo2(minutes)}:${padTo2(seconds)}`;
100
+ if (milliseconds > 0) {
101
+ stringTimeSpan += `.${milliseconds}`;
102
+ }
103
+ return stringTimeSpan;
104
+ }
105
+
106
+ exports.fromApiDate = fromApiDate;
107
+ exports.fromApiDateTimeOffset = fromApiDateTimeOffset;
108
+ exports.fromApiTime = fromApiTime;
109
+ exports.fromApiTimeSpan = fromApiTimeSpan;
110
+ exports.toApiDate = toApiDate;
111
+ exports.toApiDateTimeOffset = toApiDateTimeOffset;
112
+ exports.toApiTime = toApiTime;
113
+ exports.toApiTimeSpan = toApiTimeSpan;
114
+ Object.keys(apiDate).forEach(function (k) {
115
+ if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
116
+ enumerable: true,
117
+ get: function () { return apiDate[k]; }
118
+ });
119
+ });
package/index.cjs.mjs ADDED
@@ -0,0 +1,2 @@
1
+ export * from './index.cjs.js';
2
+ export { _default as default } from './index.cjs.default.js';
package/index.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from "./src/index";
package/index.esm.js ADDED
@@ -0,0 +1,104 @@
1
+ export * from '@leancodepl/api-date';
2
+ import { parse, format, hoursToMilliseconds, minutesToMilliseconds, secondsToMilliseconds } from 'date-fns';
3
+ import { parseApiTimeSpan } from '@leancodepl/api-date-utils';
4
+
5
+ function fromApiTime(time) {
6
+ if (!time) {
7
+ return undefined;
8
+ }
9
+ return parse(time, "HH:mm:ss.SSS", new Date());
10
+ }
11
+
12
+ function toApiTime(time) {
13
+ if (!time) {
14
+ return undefined;
15
+ }
16
+ return format(time, "HH:mm:ss.SSS");
17
+ }
18
+
19
+ function fromApiDate(date) {
20
+ if (date && typeof date === "string") {
21
+ return parse(date, "yyyy-MM-dd", new Date());
22
+ }
23
+ return undefined;
24
+ }
25
+
26
+ function toApiDate(date) {
27
+ if (!date) {
28
+ return undefined;
29
+ }
30
+ return format(date, "yyyy-MM-dd");
31
+ }
32
+
33
+ function fromApiDateTimeOffset(dateTimeOffset) {
34
+ if (!dateTimeOffset) {
35
+ return undefined;
36
+ }
37
+ return parse(dateTimeOffset, "yyyy-MM-dd'T'HH:mm:ss.SSSXXX", new Date());
38
+ }
39
+
40
+ function toApiDateTimeOffset(dateTimeOffset) {
41
+ if (!dateTimeOffset) {
42
+ return undefined;
43
+ }
44
+ return format(dateTimeOffset, "yyyy-MM-dd'T'HH:mm:ss.SSSXXX");
45
+ }
46
+
47
+ function fromApiTimeSpan(timeSpan) {
48
+ if (!timeSpan) {
49
+ return undefined;
50
+ }
51
+ const parsedApiTimeSpan = parseApiTimeSpan(timeSpan);
52
+ const parsedApiTimeSpanValues = parsedApiTimeSpan.values;
53
+ const daysInMilliseconds = hoursToMilliseconds(parsedApiTimeSpanValues.days * 24);
54
+ const hoursInMilliseconds = hoursToMilliseconds(parsedApiTimeSpanValues.hours);
55
+ const minutesInMilliseconds = minutesToMilliseconds(parsedApiTimeSpanValues.minutes);
56
+ const secondsInMilliseconds = secondsToMilliseconds(parsedApiTimeSpanValues.seconds);
57
+ const differenceInMilliseconds = daysInMilliseconds + hoursInMilliseconds + minutesInMilliseconds + secondsInMilliseconds + parsedApiTimeSpanValues.milliseconds;
58
+ const signBasedMultiplier = parsedApiTimeSpan.sign === "-" ? -1 : 1;
59
+ return signBasedMultiplier * differenceInMilliseconds;
60
+ }
61
+
62
+ function padTo2(x) {
63
+ var _x_toFixed;
64
+ const stringified = (_x_toFixed = x == null ? void 0 : x.toFixed(0)) != null ? _x_toFixed : "0";
65
+ return stringified.padStart(2, "0");
66
+ }
67
+
68
+ function parseDifferenceInMilliseconds(differenceInMilliseconds) {
69
+ const milliseconds = differenceInMilliseconds % 1000;
70
+ const seconds = Math.floor(differenceInMilliseconds / 1000 % 60);
71
+ const minutes = Math.floor(differenceInMilliseconds / (1000 * 60) % 60);
72
+ const hours = Math.floor(differenceInMilliseconds / (1000 * 60 * 60) % 24);
73
+ const days = Math.floor(differenceInMilliseconds / (1000 * 60 * 60 * 24));
74
+ return {
75
+ milliseconds,
76
+ seconds,
77
+ minutes,
78
+ hours,
79
+ days
80
+ };
81
+ }
82
+
83
+ function toApiTimeSpan(differenceInMilliseconds) {
84
+ let stringTimeSpan = "";
85
+ if (!differenceInMilliseconds) {
86
+ return undefined;
87
+ }
88
+ const isNegative = differenceInMilliseconds < 0;
89
+ const absDifferenceInMilliseconds = Math.abs(differenceInMilliseconds);
90
+ const { milliseconds, seconds, minutes, hours, days } = parseDifferenceInMilliseconds(absDifferenceInMilliseconds);
91
+ if (isNegative) {
92
+ stringTimeSpan += "-";
93
+ }
94
+ if (days > 0) {
95
+ stringTimeSpan += `${days}.`;
96
+ }
97
+ stringTimeSpan += `${padTo2(hours)}:${padTo2(minutes)}:${padTo2(seconds)}`;
98
+ if (milliseconds > 0) {
99
+ stringTimeSpan += `.${milliseconds}`;
100
+ }
101
+ return stringTimeSpan;
102
+ }
103
+
104
+ export { fromApiDate, fromApiDateTimeOffset, fromApiTime, fromApiTimeSpan, toApiDate, toApiDateTimeOffset, toApiTime, toApiTimeSpan };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@leancodepl/api-date-datefns",
3
- "version": "8.5.0",
3
+ "version": "8.5.1",
4
4
  "license": "Apache-2.0",
5
5
  "dependencies": {
6
- "@leancodepl/api-date": "8.5.0",
7
- "@leancodepl/api-date-utils": "8.5.0",
6
+ "@leancodepl/api-date": "8.5.1",
7
+ "@leancodepl/api-date-utils": "8.5.1",
8
8
  "date-fns": ">=2.0.0"
9
9
  },
10
10
  "devDependencies": {
@@ -40,11 +40,6 @@
40
40
  "name": "LeanCode",
41
41
  "url": "https://leancode.co"
42
42
  },
43
- "files": [
44
- "dist",
45
- "README.md",
46
- "CHANGELOG.md"
47
- ],
48
43
  "sideEffects": false,
49
44
  "exports": {
50
45
  "./package.json": "./package.json",
package/src/index.d.ts ADDED
@@ -0,0 +1,9 @@
1
+ export * from "@leancodepl/api-date";
2
+ export * from "./lib/time/fromApiTime";
3
+ export * from "./lib/time/toApiTime";
4
+ export * from "./lib/date/fromApiDate";
5
+ export * from "./lib/date/toApiDate";
6
+ export * from "./lib/dateTimeOffset/fromApiDateTimeOffset";
7
+ export * from "./lib/dateTimeOffset/toApiDateTimeOffset";
8
+ export * from "./lib/timeSpan/fromApiTimeSpan";
9
+ export * from "./lib/timeSpan/toApiTimeSpan";
@@ -0,0 +1,19 @@
1
+ import type { ApiDateOnly } from "@leancodepl/api-date";
2
+ /**
3
+ * Converts ApiDateOnly to JavaScript Date using date-fns.
4
+ *
5
+ * Parses API date string format to JavaScript Date object with support
6
+ * for optional parameters. Uses date-fns parse function internally.
7
+ *
8
+ * @param date - The API date string to convert
9
+ * @returns JavaScript Date object or undefined if date is undefined
10
+ * @example
11
+ * ```typescript
12
+ * import { fromApiDate } from '@leancodepl/api-date-datefns';
13
+ *
14
+ * const jsDate = fromApiDate('2023-12-25');
15
+ * console.log(jsDate); // Date object for December 25, 2023
16
+ * ```
17
+ */
18
+ export declare function fromApiDate(date: ApiDateOnly): Date;
19
+ export declare function fromApiDate(date: ApiDateOnly | undefined): Date | undefined;
@@ -0,0 +1,19 @@
1
+ import type { ApiDateOnly } from "@leancodepl/api-date";
2
+ /**
3
+ * Converts JavaScript Date to ApiDateOnly using date-fns.
4
+ *
5
+ * Formats JavaScript Date object to API date string format with support
6
+ * for optional parameters. Uses date-fns format function internally.
7
+ *
8
+ * @param date - The JavaScript Date to convert
9
+ * @returns ApiDateOnly string or undefined if date is undefined
10
+ * @example
11
+ * ```typescript
12
+ * import { toApiDate } from '@leancodepl/api-date-datefns';
13
+ *
14
+ * const apiDate = toApiDate(new Date('2023-12-25'));
15
+ * console.log(apiDate); // '2023-12-25'
16
+ * ```
17
+ */
18
+ export declare function toApiDate(date: Date): ApiDateOnly;
19
+ export declare function toApiDate(date: Date | undefined): ApiDateOnly | undefined;
@@ -0,0 +1,6 @@
1
+ import type { ApiDateTimeOffset } from "@leancodepl/api-date";
2
+ /**
3
+ *This function handles at most milliseconds precision, smaller units are lost in conversion process
4
+ */
5
+ export declare function fromApiDateTimeOffset(dateTimeOffset: ApiDateTimeOffset): Date;
6
+ export declare function fromApiDateTimeOffset(dateTimeOffset: ApiDateTimeOffset | undefined): Date | undefined;
@@ -0,0 +1,6 @@
1
+ import type { ApiDateTimeOffset } from "@leancodepl/api-date";
2
+ /**
3
+ *This function handles at most milliseconds precision, smaller units are lost in conversion process
4
+ */
5
+ export declare function toApiDateTimeOffset(dateTimeOffset: Date): ApiDateTimeOffset;
6
+ export declare function toApiDateTimeOffset(dateTimeOffset: Date | undefined): ApiDateTimeOffset | undefined;
@@ -0,0 +1,6 @@
1
+ import type { ApiTimeOnly } from "@leancodepl/api-date";
2
+ /**
3
+ *This function handles at most milliseconds precision, smaller units are lost in conversion process
4
+ */
5
+ export declare function fromApiTime(time: ApiTimeOnly): Date;
6
+ export declare function fromApiTime(time: ApiTimeOnly | undefined): Date | undefined;
@@ -0,0 +1,6 @@
1
+ import type { ApiTimeOnly } from "@leancodepl/api-date";
2
+ /**
3
+ *This function handles at most milliseconds precision, smaller units are lost in conversion process
4
+ */
5
+ export declare function toApiTime(time: Date): ApiTimeOnly;
6
+ export declare function toApiTime(time: Date | undefined): ApiTimeOnly | undefined;
@@ -0,0 +1,3 @@
1
+ import type { ApiTimeSpan } from "@leancodepl/api-date";
2
+ export declare function fromApiTimeSpan(timeSpan: ApiTimeSpan): number;
3
+ export declare function fromApiTimeSpan(timeSpan: ApiTimeSpan | undefined): number | undefined;
@@ -0,0 +1,3 @@
1
+ import type { ApiTimeSpan } from "@leancodepl/api-date";
2
+ export declare function toApiTimeSpan(differenceInMilliseconds: number): ApiTimeSpan;
3
+ export declare function toApiTimeSpan(differenceInMilliseconds: number | undefined): ApiTimeSpan | undefined;
@@ -0,0 +1 @@
1
+ export declare function padTo2(x?: number): string;
@@ -0,0 +1,7 @@
1
+ export declare function parseDifferenceInMilliseconds(differenceInMilliseconds: number): {
2
+ milliseconds: number;
3
+ seconds: number;
4
+ minutes: number;
5
+ hours: number;
6
+ days: number;
7
+ };