@velony/utils 1.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/LICENSE +21 -0
- package/README.md +138 -0
- package/package.json +53 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 VelonY
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# @velony/utils
|
|
2
|
+
|
|
3
|
+
A TypeScript utility library providing common helper functions.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @velony/utils
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Utilities
|
|
12
|
+
|
|
13
|
+
### Time Conversion
|
|
14
|
+
|
|
15
|
+
Convert time strings to multiple time units:
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { convertTime } from '@velony/utils';
|
|
19
|
+
|
|
20
|
+
const result = convertTime("1h30m");
|
|
21
|
+
console.log(result);
|
|
22
|
+
// {
|
|
23
|
+
// seconds: 5400,
|
|
24
|
+
// milliseconds: 5400000,
|
|
25
|
+
// minutes: 90,
|
|
26
|
+
// hours: 1.5,
|
|
27
|
+
// days: 0.0625,
|
|
28
|
+
// weeks: 0.008928571428571428
|
|
29
|
+
// }
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Supported Time Units
|
|
33
|
+
|
|
34
|
+
- `ms` - Milliseconds
|
|
35
|
+
- `s` - Seconds
|
|
36
|
+
- `m` - Minutes
|
|
37
|
+
- `h` - Hours
|
|
38
|
+
- `d` - Days
|
|
39
|
+
- `w` - Weeks
|
|
40
|
+
|
|
41
|
+
### Compound Time Strings
|
|
42
|
+
|
|
43
|
+
You can combine multiple units in a single string:
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { convertTime } from '@velony/utils';
|
|
47
|
+
|
|
48
|
+
// Complex time duration
|
|
49
|
+
const result = convertTime("2d3h15m30s500ms");
|
|
50
|
+
console.log(result.hours); // 51.258055555555554
|
|
51
|
+
|
|
52
|
+
// Simple durations
|
|
53
|
+
convertTime("500ms"); // 0.5 seconds
|
|
54
|
+
convertTime("1h"); // 1 hour
|
|
55
|
+
convertTime("1w"); // 1 week = 604800 seconds
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Validation
|
|
59
|
+
|
|
60
|
+
Check if a string is a valid time format:
|
|
61
|
+
|
|
62
|
+
```typescript
|
|
63
|
+
import { isTimeString } from '@velony/utils';
|
|
64
|
+
|
|
65
|
+
isTimeString("1h30m"); // true
|
|
66
|
+
isTimeString("500ms"); // true
|
|
67
|
+
isTimeString("2x"); // false
|
|
68
|
+
isTimeString(""); // false
|
|
69
|
+
isTimeString("1h30m15s"); // true
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Error Handling
|
|
73
|
+
|
|
74
|
+
The `convertTime` function throws an `InvalidTimeStringError` if the input format is invalid:
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { convertTime, InvalidTimeStringError } from '@velony/utils';
|
|
78
|
+
|
|
79
|
+
try {
|
|
80
|
+
convertTime("invalid");
|
|
81
|
+
} catch (error) {
|
|
82
|
+
if (error instanceof InvalidTimeStringError) {
|
|
83
|
+
console.error("Invalid time format provided");
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## API Reference
|
|
89
|
+
|
|
90
|
+
### `convertTime(input: string)`
|
|
91
|
+
|
|
92
|
+
Converts a valid time string into multiple time units.
|
|
93
|
+
|
|
94
|
+
**Parameters:**
|
|
95
|
+
- `input: string` - The time string to convert (e.g., `"1h30m"`, `"500ms"`, `"2d3h"`)
|
|
96
|
+
|
|
97
|
+
**Returns:**
|
|
98
|
+
```typescript
|
|
99
|
+
{
|
|
100
|
+
seconds: number;
|
|
101
|
+
milliseconds: number;
|
|
102
|
+
minutes: number;
|
|
103
|
+
hours: number;
|
|
104
|
+
days: number;
|
|
105
|
+
weeks: number;
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
**Throws:**
|
|
110
|
+
- `InvalidTimeStringError` - If the input format is invalid
|
|
111
|
+
|
|
112
|
+
### `isTimeString(input: string): boolean`
|
|
113
|
+
|
|
114
|
+
Checks if a given string is a valid time format.
|
|
115
|
+
|
|
116
|
+
**Parameters:**
|
|
117
|
+
- `input: string` - The time string to validate
|
|
118
|
+
|
|
119
|
+
**Returns:**
|
|
120
|
+
- `boolean` - `true` if the input is valid, otherwise `false`
|
|
121
|
+
|
|
122
|
+
### `InvalidTimeStringError`
|
|
123
|
+
|
|
124
|
+
Custom error class thrown when an invalid time string format is provided.
|
|
125
|
+
|
|
126
|
+
**Extends:** `Error`
|
|
127
|
+
|
|
128
|
+
## License
|
|
129
|
+
|
|
130
|
+
MIT
|
|
131
|
+
|
|
132
|
+
## Repository
|
|
133
|
+
|
|
134
|
+
[https://github.com/velony-ai/utils](https://github.com/velony-ai/utils)
|
|
135
|
+
|
|
136
|
+
## Issues
|
|
137
|
+
|
|
138
|
+
[https://github.com/velony-ai/utils/issues](https://github.com/velony-ai/utils/issues)
|
package/package.json
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@velony/utils",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "TypeScript utility library providing common helper functions for time conversion and formatting",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"utils",
|
|
7
|
+
"utilities",
|
|
8
|
+
"typescript",
|
|
9
|
+
"helper-functions"
|
|
10
|
+
],
|
|
11
|
+
"main": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.js"
|
|
17
|
+
}
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist",
|
|
21
|
+
"README.md",
|
|
22
|
+
"LICENSE"
|
|
23
|
+
],
|
|
24
|
+
"engines": {
|
|
25
|
+
"node": ">=18.0.0"
|
|
26
|
+
},
|
|
27
|
+
"publishConfig": {
|
|
28
|
+
"access": "public"
|
|
29
|
+
},
|
|
30
|
+
"scripts": {
|
|
31
|
+
"build": "tsc",
|
|
32
|
+
"lint": "eslint src"
|
|
33
|
+
},
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/velony-ai/utils.git"
|
|
37
|
+
},
|
|
38
|
+
"author": "velony-ai",
|
|
39
|
+
"license": "MIT",
|
|
40
|
+
"bugs": {
|
|
41
|
+
"url": "https://github.com/velony-ai/utils/issues"
|
|
42
|
+
},
|
|
43
|
+
"homepage": "https://github.com/velony-ai/utils#readme",
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@eslint/js": "^9.39.2",
|
|
46
|
+
"eslint": "^9.39.2",
|
|
47
|
+
"eslint-config-prettier": "^10.1.8",
|
|
48
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
49
|
+
"eslint-plugin-security": "^3.0.1",
|
|
50
|
+
"typescript": "^5.9.3",
|
|
51
|
+
"typescript-eslint": "^8.51.0"
|
|
52
|
+
}
|
|
53
|
+
}
|