@leancodepl/api-date-utils 8.5.0 → 8.6.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/README.md +64 -0
- package/index.cjs.default.js +1 -0
- package/index.cjs.js +34 -0
- package/index.cjs.mjs +2 -0
- package/index.d.ts +1 -0
- package/index.esm.js +32 -0
- package/package.json +2 -7
- package/src/index.d.ts +1 -0
- package/src/lib/parseApiTimeSpan.d.ts +25 -0
package/README.md
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# @leancodepl/api-date-utils
|
|
2
|
+
|
|
3
|
+
Utilities for parsing and working with API date types.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @leancodepl/api-date-utils
|
|
9
|
+
# or
|
|
10
|
+
yarn add @leancodepl/api-date-utils
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## API
|
|
14
|
+
|
|
15
|
+
### `parseApiTimeSpan(timespan)`
|
|
16
|
+
|
|
17
|
+
Parses ApiTimeSpan string into structured time components.
|
|
18
|
+
|
|
19
|
+
**Parameters:**
|
|
20
|
+
|
|
21
|
+
- `timespan: ApiTimeSpan` - The ApiTimeSpan string to parse
|
|
22
|
+
|
|
23
|
+
**Returns:** Object with sign and time component values
|
|
24
|
+
|
|
25
|
+
## Usage Examples
|
|
26
|
+
|
|
27
|
+
### Basic Parsing
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { parseApiTimeSpan } from "@leancodepl/api-date-utils"
|
|
31
|
+
|
|
32
|
+
const result = parseApiTimeSpan("1.23:45:67.890")
|
|
33
|
+
console.log(result.values.hours) // 23
|
|
34
|
+
console.log(result.values.days) // 1
|
|
35
|
+
console.log(result.sign) // undefined (positive)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Negative TimeSpan
|
|
39
|
+
|
|
40
|
+
```typescript
|
|
41
|
+
import { parseApiTimeSpan } from "@leancodepl/api-date-utils"
|
|
42
|
+
|
|
43
|
+
const result = parseApiTimeSpan("-2.10:30:45.123")
|
|
44
|
+
console.log(result.sign) // '-'
|
|
45
|
+
console.log(result.values.days) // 2
|
|
46
|
+
console.log(result.values.hours) // 10
|
|
47
|
+
console.log(result.values.minutes) // 30
|
|
48
|
+
console.log(result.values.milliseconds) // 123
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
### Duration Calculations
|
|
52
|
+
|
|
53
|
+
```typescript
|
|
54
|
+
import { parseApiTimeSpan } from "@leancodepl/api-date-utils"
|
|
55
|
+
|
|
56
|
+
const calculateTotalMilliseconds = (timespan: ApiTimeSpan) => {
|
|
57
|
+
const parsed = parseApiTimeSpan(timespan)
|
|
58
|
+
const { days, hours, minutes, seconds, milliseconds } = parsed.values
|
|
59
|
+
|
|
60
|
+
const total = days * 86400000 + hours * 3600000 + minutes * 60000 + seconds * 1000 + milliseconds
|
|
61
|
+
|
|
62
|
+
return parsed.sign === "-" ? -total : total
|
|
63
|
+
}
|
|
64
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
exports._default = require('./index.cjs.js').default;
|
package/index.cjs.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const parseIntMatched = (value)=>parseInt(value != null ? value : "0");
|
|
4
|
+
/**
|
|
5
|
+
* Parses ApiTimeSpan string into structured time components.
|
|
6
|
+
*
|
|
7
|
+
* Extracts sign, days, hours, minutes, seconds, and milliseconds from
|
|
8
|
+
* ApiTimeSpan string format using regex pattern matching.
|
|
9
|
+
*
|
|
10
|
+
* @param timespan - The ApiTimeSpan string to parse
|
|
11
|
+
* @returns Object with sign and time component values
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const result = parseApiTimeSpan('1.23:45:67.890');
|
|
15
|
+
* console.log(result.values.hours); // 23
|
|
16
|
+
* ```
|
|
17
|
+
*/ function parseApiTimeSpan(timespan) {
|
|
18
|
+
/**
|
|
19
|
+
* This regex returns tuple of matched strings (either of string type or undefined) and default match function parameters
|
|
20
|
+
* following [timeSpan, sign, days, hours, minutes, seconds, milliseconds, index of search at which the result was found, input (search string), groups] schema
|
|
21
|
+
*/ const matched = timespan.match(/^(-)?([0-9]+)?\.?([0-9]{2}):([0-9]{2}):([0-9]{2})\.?([0-9]{3})?$/);
|
|
22
|
+
return {
|
|
23
|
+
sign: matched == null ? void 0 : matched[1],
|
|
24
|
+
values: {
|
|
25
|
+
days: parseIntMatched(matched == null ? void 0 : matched[2]),
|
|
26
|
+
hours: parseIntMatched(matched == null ? void 0 : matched[3]),
|
|
27
|
+
minutes: parseIntMatched(matched == null ? void 0 : matched[4]),
|
|
28
|
+
seconds: parseIntMatched(matched == null ? void 0 : matched[5]),
|
|
29
|
+
milliseconds: parseIntMatched(matched == null ? void 0 : matched[6])
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
exports.parseApiTimeSpan = parseApiTimeSpan;
|
package/index.cjs.mjs
ADDED
package/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/index";
|
package/index.esm.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
const parseIntMatched = (value)=>parseInt(value != null ? value : "0");
|
|
2
|
+
/**
|
|
3
|
+
* Parses ApiTimeSpan string into structured time components.
|
|
4
|
+
*
|
|
5
|
+
* Extracts sign, days, hours, minutes, seconds, and milliseconds from
|
|
6
|
+
* ApiTimeSpan string format using regex pattern matching.
|
|
7
|
+
*
|
|
8
|
+
* @param timespan - The ApiTimeSpan string to parse
|
|
9
|
+
* @returns Object with sign and time component values
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const result = parseApiTimeSpan('1.23:45:67.890');
|
|
13
|
+
* console.log(result.values.hours); // 23
|
|
14
|
+
* ```
|
|
15
|
+
*/ function parseApiTimeSpan(timespan) {
|
|
16
|
+
/**
|
|
17
|
+
* This regex returns tuple of matched strings (either of string type or undefined) and default match function parameters
|
|
18
|
+
* following [timeSpan, sign, days, hours, minutes, seconds, milliseconds, index of search at which the result was found, input (search string), groups] schema
|
|
19
|
+
*/ const matched = timespan.match(/^(-)?([0-9]+)?\.?([0-9]{2}):([0-9]{2}):([0-9]{2})\.?([0-9]{3})?$/);
|
|
20
|
+
return {
|
|
21
|
+
sign: matched == null ? void 0 : matched[1],
|
|
22
|
+
values: {
|
|
23
|
+
days: parseIntMatched(matched == null ? void 0 : matched[2]),
|
|
24
|
+
hours: parseIntMatched(matched == null ? void 0 : matched[3]),
|
|
25
|
+
minutes: parseIntMatched(matched == null ? void 0 : matched[4]),
|
|
26
|
+
seconds: parseIntMatched(matched == null ? void 0 : matched[5]),
|
|
27
|
+
milliseconds: parseIntMatched(matched == null ? void 0 : matched[6])
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export { parseApiTimeSpan };
|
package/package.json
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leancodepl/api-date-utils",
|
|
3
|
-
"version": "8.
|
|
3
|
+
"version": "8.6.0",
|
|
4
4
|
"license": "Apache-2.0",
|
|
5
5
|
"dependencies": {
|
|
6
|
-
"@leancodepl/api-date": "8.
|
|
6
|
+
"@leancodepl/api-date": "8.6.0"
|
|
7
7
|
},
|
|
8
8
|
"publishConfig": {
|
|
9
9
|
"access": "public",
|
|
@@ -36,11 +36,6 @@
|
|
|
36
36
|
"name": "LeanCode",
|
|
37
37
|
"url": "https://leancode.co"
|
|
38
38
|
},
|
|
39
|
-
"files": [
|
|
40
|
-
"dist",
|
|
41
|
-
"README.md",
|
|
42
|
-
"CHANGELOG.md"
|
|
43
|
-
],
|
|
44
39
|
"sideEffects": false,
|
|
45
40
|
"exports": {
|
|
46
41
|
"./package.json": "./package.json",
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./lib/parseApiTimeSpan";
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { ApiTimeSpan } from "@leancodepl/api-date";
|
|
2
|
+
/**
|
|
3
|
+
* Parses ApiTimeSpan string into structured time components.
|
|
4
|
+
*
|
|
5
|
+
* Extracts sign, days, hours, minutes, seconds, and milliseconds from
|
|
6
|
+
* ApiTimeSpan string format using regex pattern matching.
|
|
7
|
+
*
|
|
8
|
+
* @param timespan - The ApiTimeSpan string to parse
|
|
9
|
+
* @returns Object with sign and time component values
|
|
10
|
+
* @example
|
|
11
|
+
* ```typescript
|
|
12
|
+
* const result = parseApiTimeSpan('1.23:45:67.890');
|
|
13
|
+
* console.log(result.values.hours); // 23
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
export declare function parseApiTimeSpan(timespan: ApiTimeSpan): {
|
|
17
|
+
sign: any;
|
|
18
|
+
values: {
|
|
19
|
+
days: number;
|
|
20
|
+
hours: number;
|
|
21
|
+
minutes: number;
|
|
22
|
+
seconds: number;
|
|
23
|
+
milliseconds: number;
|
|
24
|
+
};
|
|
25
|
+
};
|