@velony/utils 1.0.0 → 1.0.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/dist/time.d.ts ADDED
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Error thrown when a time string format is invalid.
3
+ *
4
+ * @example
5
+ * throw new InvalidTimeStringError('Invalid format: "2x"');
6
+ */
7
+ export declare class InvalidTimeStringError extends Error {
8
+ constructor(message?: string);
9
+ }
10
+ /**
11
+ * Checks if a given string is a valid time format.
12
+ * Supports compound inputs like "1h30m15s500ms".
13
+ *
14
+ * @param input - The time string to validate.
15
+ * @returns `true` if the input is valid, otherwise `false`.
16
+ *
17
+ * @example
18
+ * isTimeString("1h30m"); // true
19
+ * isTimeString("2x"); // false
20
+ * isTimeString("500ms"); // true
21
+ * isTimeString("2d3h15m30s"); // true
22
+ * isTimeString(""); // false
23
+ */
24
+ export declare const isTimeString: (input: string) => boolean;
25
+ /**
26
+ * Converts a valid time string into multiple time units.
27
+ * Supports compound inputs such as "1h30m15s500ms".
28
+ *
29
+ * @param input - The time string to convert (e.g., `"1h30m"`, `"500ms"`, `"2d3h"`).
30
+ * @returns An object containing equivalent durations in seconds, milliseconds, minutes, hours, days, and weeks.
31
+ * @throws {@link InvalidTimeStringError} If the input format is invalid.
32
+ *
33
+ * @example
34
+ * convertTime("1h30m");
35
+ * // Returns:
36
+ * // {
37
+ * // seconds: 5400,
38
+ * // milliseconds: 5400000,
39
+ * // minutes: 90,
40
+ * // hours: 1.5,
41
+ * // days: 0.0625,
42
+ * // weeks: 0.008928571428571428
43
+ * // }
44
+ *
45
+ * @example
46
+ * convertTime("500ms");
47
+ * // Returns: { seconds: 0.5, milliseconds: 500, ... }
48
+ */
49
+ export declare const convertTime: (input: string) => {
50
+ seconds: number;
51
+ milliseconds: number;
52
+ minutes: number;
53
+ hours: number;
54
+ days: number;
55
+ weeks: number;
56
+ };
package/dist/time.js ADDED
@@ -0,0 +1,108 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.convertTime = exports.isTimeString = exports.InvalidTimeStringError = void 0;
4
+ /**
5
+ * Error thrown when a time string format is invalid.
6
+ *
7
+ * @example
8
+ * throw new InvalidTimeStringError('Invalid format: "2x"');
9
+ */
10
+ class InvalidTimeStringError extends Error {
11
+ constructor(message = 'Invalid time string format') {
12
+ super(message);
13
+ this.name = 'InvalidTimeStringError';
14
+ }
15
+ }
16
+ exports.InvalidTimeStringError = InvalidTimeStringError;
17
+ /**
18
+ * List of valid time units supported by the converter.
19
+ */
20
+ const VALID_UNITS = ['ms', 's', 'm', 'h', 'd', 'w'];
21
+ /**
22
+ * Checks if a given string is a valid time format.
23
+ * Supports compound inputs like "1h30m15s500ms".
24
+ *
25
+ * @param input - The time string to validate.
26
+ * @returns `true` if the input is valid, otherwise `false`.
27
+ *
28
+ * @example
29
+ * isTimeString("1h30m"); // true
30
+ * isTimeString("2x"); // false
31
+ * isTimeString("500ms"); // true
32
+ * isTimeString("2d3h15m30s"); // true
33
+ * isTimeString(""); // false
34
+ */
35
+ const isTimeString = (input) => {
36
+ if (typeof input !== 'string' || !input.trim()) {
37
+ return false;
38
+ }
39
+ const regex = /(\d+)([a-z]+)/g;
40
+ let match;
41
+ let found = false;
42
+ while ((match = regex.exec(input)) !== null) {
43
+ found = true;
44
+ const unit = match[2];
45
+ if (!VALID_UNITS.includes(unit)) {
46
+ return false;
47
+ }
48
+ }
49
+ return found;
50
+ };
51
+ exports.isTimeString = isTimeString;
52
+ /**
53
+ * Converts a valid time string into multiple time units.
54
+ * Supports compound inputs such as "1h30m15s500ms".
55
+ *
56
+ * @param input - The time string to convert (e.g., `"1h30m"`, `"500ms"`, `"2d3h"`).
57
+ * @returns An object containing equivalent durations in seconds, milliseconds, minutes, hours, days, and weeks.
58
+ * @throws {@link InvalidTimeStringError} If the input format is invalid.
59
+ *
60
+ * @example
61
+ * convertTime("1h30m");
62
+ * // Returns:
63
+ * // {
64
+ * // seconds: 5400,
65
+ * // milliseconds: 5400000,
66
+ * // minutes: 90,
67
+ * // hours: 1.5,
68
+ * // days: 0.0625,
69
+ * // weeks: 0.008928571428571428
70
+ * // }
71
+ *
72
+ * @example
73
+ * convertTime("500ms");
74
+ * // Returns: { seconds: 0.5, milliseconds: 500, ... }
75
+ */
76
+ const convertTime = (input) => {
77
+ if (!(0, exports.isTimeString)(input)) {
78
+ throw new InvalidTimeStringError();
79
+ }
80
+ const units = {
81
+ ms: 1 / 1000,
82
+ s: 1,
83
+ m: 60,
84
+ h: 3600,
85
+ d: 86400,
86
+ w: 604800,
87
+ };
88
+ const regex = /(\d+)(ms|s|m|h|d|w)/g;
89
+ let match;
90
+ let totalSeconds = 0;
91
+ while ((match = regex.exec(input)) !== null) {
92
+ const value = parseInt(match[1]);
93
+ const unit = match[2];
94
+ // eslint-disable-next-line security/detect-object-injection
95
+ const multiplier = units[unit];
96
+ totalSeconds += value * multiplier;
97
+ }
98
+ return {
99
+ seconds: totalSeconds,
100
+ milliseconds: totalSeconds * 1000,
101
+ minutes: totalSeconds / 60,
102
+ hours: totalSeconds / 3600,
103
+ days: totalSeconds / 86400,
104
+ weeks: totalSeconds / 604800,
105
+ };
106
+ };
107
+ exports.convertTime = convertTime;
108
+ //# sourceMappingURL=time.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"time.js","sourceRoot":"","sources":["../src/time.ts"],"names":[],"mappings":";;;AAAA;;;;;GAKG;AACH,MAAa,sBAAuB,SAAQ,KAAK;IAC/C,YAAY,OAAO,GAAG,4BAA4B;QAChD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,wBAAwB,CAAC;IACvC,CAAC;CACF;AALD,wDAKC;AAED;;GAEG;AACH,MAAM,WAAW,GAAG,CAAC,IAAI,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,CAAU,CAAC;AAG7D;;;;;;;;;;;;;GAaG;AACI,MAAM,YAA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CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;QACjC,MAAM,IAAI,GAAG,KAAK,CAAC,CAAC,CAAa,CAAC;QAElC,4DAA4D;QAC5D,MAAM,UAAU,GAAG,KAAK,CAAC,IAAI,CAAC,CAAC;QAC/B,YAAY,IAAI,KAAK,GAAG,UAAU,CAAC;IACrC,CAAC;IAED,OAAO;QACL,OAAO,EAAE,YAAY;QACrB,YAAY,EAAE,YAAY,GAAG,IAAI;QACjC,OAAO,EAAE,YAAY,GAAG,EAAE;QAC1B,KAAK,EAAE,YAAY,GAAG,IAAI;QAC1B,IAAI,EAAE,YAAY,GAAG,KAAK;QAC1B,KAAK,EAAE,YAAY,GAAG,MAAM;KAC7B,CAAC;AACJ,CAAC,CAAC;AA5CW,QAAA,WAAW,eA4CtB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@velony/utils",
3
- "version": "1.0.0",
3
+ "version": "1.0.1",
4
4
  "description": "TypeScript utility library providing common helper functions for time conversion and formatting",
5
5
  "keywords": [
6
6
  "utils",