@develia/commons 0.4.16 → 0.4.19

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.
Files changed (287) hide show
  1. package/dist/cjs/array.js +22 -2
  2. package/dist/cjs/array.js.map +1 -1
  3. package/dist/cjs/async-pool-executor.js +26 -11
  4. package/dist/cjs/async-pool-executor.js.map +1 -1
  5. package/dist/cjs/browser/form-utilities.js +35 -34
  6. package/dist/cjs/browser/form-utilities.js.map +1 -1
  7. package/dist/cjs/browser/geo-location.js +1 -1
  8. package/dist/cjs/browser/geo-location.js.map +1 -1
  9. package/dist/cjs/browser/index.js +0 -0
  10. package/dist/cjs/browser/index.js.map +0 -0
  11. package/dist/cjs/browser/native-bridge.js +3 -1
  12. package/dist/cjs/browser/native-bridge.js.map +1 -1
  13. package/dist/cjs/cancellable-promise.js +34 -13
  14. package/dist/cjs/cancellable-promise.js.map +1 -1
  15. package/dist/cjs/clean-url-search-params.js +26 -3
  16. package/dist/cjs/clean-url-search-params.js.map +1 -1
  17. package/dist/cjs/collections/cache-dictionary.js +3 -3
  18. package/dist/cjs/collections/cache-dictionary.js.map +1 -1
  19. package/dist/cjs/collections/default-map.js +0 -0
  20. package/dist/cjs/collections/default-map.js.map +1 -1
  21. package/dist/cjs/collections/default-weak-map.js +0 -0
  22. package/dist/cjs/collections/default-weak-map.js.map +1 -1
  23. package/dist/cjs/collections/from.js +3 -3
  24. package/dist/cjs/collections/from.js.map +1 -1
  25. package/dist/cjs/collections/index.js +0 -0
  26. package/dist/cjs/collections/index.js.map +1 -1
  27. package/dist/cjs/draft.js +46 -11
  28. package/dist/cjs/draft.js.map +1 -1
  29. package/dist/cjs/event-bus.js +1 -1
  30. package/dist/cjs/event-bus.js.map +1 -1
  31. package/dist/cjs/functions.js +348 -0
  32. package/dist/cjs/functions.js.map +1 -0
  33. package/dist/cjs/index.js +1 -0
  34. package/dist/cjs/index.js.map +1 -1
  35. package/dist/cjs/key-value-pair.js +0 -0
  36. package/dist/cjs/key-value-pair.js.map +1 -1
  37. package/dist/cjs/lazy.js +0 -0
  38. package/dist/cjs/lazy.js.map +1 -1
  39. package/dist/cjs/math.js +3 -0
  40. package/dist/cjs/math.js.map +1 -1
  41. package/dist/cjs/order.js +0 -0
  42. package/dist/cjs/order.js.map +1 -1
  43. package/dist/cjs/parse.js +10 -1
  44. package/dist/cjs/parse.js.map +1 -1
  45. package/dist/cjs/patch.js +0 -0
  46. package/dist/cjs/patch.js.map +1 -1
  47. package/dist/cjs/range.js +0 -0
  48. package/dist/cjs/range.js.map +0 -0
  49. package/dist/cjs/stopwatch.js +0 -0
  50. package/dist/cjs/stopwatch.js.map +1 -1
  51. package/dist/cjs/string.js +8 -5
  52. package/dist/cjs/string.js.map +1 -1
  53. package/dist/cjs/timer.js +1 -1
  54. package/dist/cjs/timer.js.map +1 -1
  55. package/dist/cjs/timespan.js +21 -8
  56. package/dist/cjs/timespan.js.map +1 -1
  57. package/dist/cjs/type-code.js +0 -0
  58. package/dist/cjs/type-code.js.map +1 -1
  59. package/dist/cjs/type.js +20 -19
  60. package/dist/cjs/type.js.map +1 -1
  61. package/dist/cjs/types.js +0 -0
  62. package/dist/cjs/types.js.map +1 -1
  63. package/dist/cjs/utilities.js +175 -39
  64. package/dist/cjs/utilities.js.map +1 -1
  65. package/dist/esm/array.js +21 -2
  66. package/dist/esm/array.js.map +1 -1
  67. package/dist/esm/async-pool-executor.js +26 -11
  68. package/dist/esm/async-pool-executor.js.map +1 -1
  69. package/dist/esm/browser/form-utilities.js +35 -34
  70. package/dist/esm/browser/form-utilities.js.map +1 -1
  71. package/dist/esm/browser/geo-location.js +1 -1
  72. package/dist/esm/browser/geo-location.js.map +1 -1
  73. package/dist/esm/browser/index.js +0 -0
  74. package/dist/esm/browser/index.js.map +0 -0
  75. package/dist/esm/browser/native-bridge.js +3 -1
  76. package/dist/esm/browser/native-bridge.js.map +1 -1
  77. package/dist/esm/cancellable-promise.js +34 -13
  78. package/dist/esm/cancellable-promise.js.map +1 -1
  79. package/dist/esm/clean-url-search-params.js +26 -3
  80. package/dist/esm/clean-url-search-params.js.map +1 -1
  81. package/dist/esm/collections/cache-dictionary.js +3 -3
  82. package/dist/esm/collections/cache-dictionary.js.map +1 -1
  83. package/dist/esm/collections/default-map.js +0 -0
  84. package/dist/esm/collections/default-map.js.map +1 -1
  85. package/dist/esm/collections/default-weak-map.js +0 -0
  86. package/dist/esm/collections/default-weak-map.js.map +1 -1
  87. package/dist/esm/collections/from.js +3 -3
  88. package/dist/esm/collections/from.js.map +1 -1
  89. package/dist/esm/collections/index.js +0 -0
  90. package/dist/esm/collections/index.js.map +1 -1
  91. package/dist/esm/draft.js +46 -11
  92. package/dist/esm/draft.js.map +1 -1
  93. package/dist/esm/event-bus.js +1 -1
  94. package/dist/esm/event-bus.js.map +1 -1
  95. package/dist/esm/functions.js +336 -0
  96. package/dist/esm/functions.js.map +1 -0
  97. package/dist/esm/index.js +1 -0
  98. package/dist/esm/index.js.map +1 -1
  99. package/dist/esm/key-value-pair.js +0 -0
  100. package/dist/esm/key-value-pair.js.map +1 -1
  101. package/dist/esm/lazy.js +0 -0
  102. package/dist/esm/lazy.js.map +1 -1
  103. package/dist/esm/math.js +3 -0
  104. package/dist/esm/math.js.map +1 -1
  105. package/dist/esm/order.js +0 -0
  106. package/dist/esm/order.js.map +1 -1
  107. package/dist/esm/parse.js +10 -1
  108. package/dist/esm/parse.js.map +1 -1
  109. package/dist/esm/patch.js +0 -0
  110. package/dist/esm/patch.js.map +1 -1
  111. package/dist/esm/range.js +0 -0
  112. package/dist/esm/range.js.map +0 -0
  113. package/dist/esm/stopwatch.js +0 -0
  114. package/dist/esm/stopwatch.js.map +1 -1
  115. package/dist/esm/string.js +8 -5
  116. package/dist/esm/string.js.map +1 -1
  117. package/dist/esm/timer.js +1 -1
  118. package/dist/esm/timer.js.map +1 -1
  119. package/dist/esm/timespan.js +21 -8
  120. package/dist/esm/timespan.js.map +1 -1
  121. package/dist/esm/type-code.js +0 -0
  122. package/dist/esm/type-code.js.map +1 -1
  123. package/dist/esm/type.js +20 -19
  124. package/dist/esm/type.js.map +1 -1
  125. package/dist/esm/types.js +0 -0
  126. package/dist/esm/types.js.map +1 -1
  127. package/dist/esm/utilities.js +174 -39
  128. package/dist/esm/utilities.js.map +1 -1
  129. package/dist/types/array.d.ts +20 -0
  130. package/dist/types/array.d.ts.map +1 -1
  131. package/dist/types/async-pool-executor.d.ts +2 -2
  132. package/dist/types/async-pool-executor.d.ts.map +1 -1
  133. package/dist/types/browser/form-utilities.d.ts +0 -0
  134. package/dist/types/browser/form-utilities.d.ts.map +1 -1
  135. package/dist/types/browser/geo-location.d.ts +0 -0
  136. package/dist/types/browser/geo-location.d.ts.map +0 -0
  137. package/dist/types/browser/index.d.ts +0 -0
  138. package/dist/types/browser/index.d.ts.map +0 -0
  139. package/dist/types/browser/native-bridge.d.ts +0 -0
  140. package/dist/types/browser/native-bridge.d.ts.map +1 -1
  141. package/dist/types/cancellable-promise.d.ts +0 -0
  142. package/dist/types/cancellable-promise.d.ts.map +1 -1
  143. package/dist/types/clean-url-search-params.d.ts +3 -1
  144. package/dist/types/clean-url-search-params.d.ts.map +1 -1
  145. package/dist/types/collections/cache-dictionary.d.ts +0 -0
  146. package/dist/types/collections/cache-dictionary.d.ts.map +0 -0
  147. package/dist/types/collections/default-map.d.ts +0 -0
  148. package/dist/types/collections/default-map.d.ts.map +0 -0
  149. package/dist/types/collections/default-weak-map.d.ts +0 -0
  150. package/dist/types/collections/default-weak-map.d.ts.map +0 -0
  151. package/dist/types/collections/from.d.ts +1 -1
  152. package/dist/types/collections/from.d.ts.map +0 -0
  153. package/dist/types/collections/index.d.ts +0 -0
  154. package/dist/types/collections/index.d.ts.map +0 -0
  155. package/dist/types/draft.d.ts +0 -0
  156. package/dist/types/draft.d.ts.map +1 -1
  157. package/dist/types/event-bus.d.ts +0 -0
  158. package/dist/types/event-bus.d.ts.map +0 -0
  159. package/dist/types/functions.d.ts +231 -0
  160. package/dist/types/functions.d.ts.map +1 -0
  161. package/dist/types/index.d.ts +1 -0
  162. package/dist/types/index.d.ts.map +1 -1
  163. package/dist/types/key-value-pair.d.ts +0 -0
  164. package/dist/types/key-value-pair.d.ts.map +0 -0
  165. package/dist/types/lazy.d.ts +0 -0
  166. package/dist/types/lazy.d.ts.map +0 -0
  167. package/dist/types/math.d.ts +0 -0
  168. package/dist/types/math.d.ts.map +1 -1
  169. package/dist/types/order.d.ts +0 -0
  170. package/dist/types/order.d.ts.map +0 -0
  171. package/dist/types/parse.d.ts +0 -0
  172. package/dist/types/parse.d.ts.map +1 -1
  173. package/dist/types/patch.d.ts +0 -0
  174. package/dist/types/patch.d.ts.map +0 -0
  175. package/dist/types/range.d.ts +0 -0
  176. package/dist/types/range.d.ts.map +0 -0
  177. package/dist/types/stopwatch.d.ts +0 -0
  178. package/dist/types/stopwatch.d.ts.map +0 -0
  179. package/dist/types/string.d.ts +0 -0
  180. package/dist/types/string.d.ts.map +1 -1
  181. package/dist/types/timer.d.ts +1 -1
  182. package/dist/types/timer.d.ts.map +0 -0
  183. package/dist/types/timespan.d.ts +0 -0
  184. package/dist/types/timespan.d.ts.map +1 -1
  185. package/dist/types/type-code.d.ts +0 -0
  186. package/dist/types/type-code.d.ts.map +0 -0
  187. package/dist/types/type.d.ts +2 -0
  188. package/dist/types/type.d.ts.map +1 -1
  189. package/dist/types/types.d.ts +1 -1
  190. package/dist/types/types.d.ts.map +1 -1
  191. package/dist/types/utilities.d.ts +21 -3
  192. package/dist/types/utilities.d.ts.map +1 -1
  193. package/dist/web/array.js +21 -2
  194. package/dist/web/array.js.map +1 -1
  195. package/dist/web/async-pool-executor.js +26 -11
  196. package/dist/web/async-pool-executor.js.map +1 -1
  197. package/dist/web/browser/form-utilities.js +35 -34
  198. package/dist/web/browser/form-utilities.js.map +1 -1
  199. package/dist/web/browser/geo-location.js +1 -1
  200. package/dist/web/browser/geo-location.js.map +1 -1
  201. package/dist/web/browser/index.js +0 -0
  202. package/dist/web/browser/index.js.map +0 -0
  203. package/dist/web/browser/native-bridge.js +3 -1
  204. package/dist/web/browser/native-bridge.js.map +1 -1
  205. package/dist/web/cancellable-promise.js +34 -13
  206. package/dist/web/cancellable-promise.js.map +1 -1
  207. package/dist/web/clean-url-search-params.js +26 -3
  208. package/dist/web/clean-url-search-params.js.map +1 -1
  209. package/dist/web/collections/cache-dictionary.js +3 -3
  210. package/dist/web/collections/cache-dictionary.js.map +1 -1
  211. package/dist/web/collections/default-map.js +0 -0
  212. package/dist/web/collections/default-map.js.map +1 -1
  213. package/dist/web/collections/default-weak-map.js +0 -0
  214. package/dist/web/collections/default-weak-map.js.map +1 -1
  215. package/dist/web/collections/from.js +3 -3
  216. package/dist/web/collections/from.js.map +1 -1
  217. package/dist/web/collections/index.js +0 -0
  218. package/dist/web/collections/index.js.map +1 -1
  219. package/dist/web/draft.js +46 -11
  220. package/dist/web/draft.js.map +1 -1
  221. package/dist/web/event-bus.js +1 -1
  222. package/dist/web/event-bus.js.map +1 -1
  223. package/dist/web/functions.js +336 -0
  224. package/dist/web/functions.js.map +1 -0
  225. package/dist/web/index.js +1 -0
  226. package/dist/web/index.js.map +1 -1
  227. package/dist/web/key-value-pair.js +0 -0
  228. package/dist/web/key-value-pair.js.map +1 -1
  229. package/dist/web/lazy.js +0 -0
  230. package/dist/web/lazy.js.map +1 -1
  231. package/dist/web/math.js +3 -0
  232. package/dist/web/math.js.map +1 -1
  233. package/dist/web/order.js +0 -0
  234. package/dist/web/order.js.map +1 -1
  235. package/dist/web/parse.js +10 -1
  236. package/dist/web/parse.js.map +1 -1
  237. package/dist/web/patch.js +0 -0
  238. package/dist/web/patch.js.map +1 -1
  239. package/dist/web/range.js +0 -0
  240. package/dist/web/range.js.map +0 -0
  241. package/dist/web/stopwatch.js +0 -0
  242. package/dist/web/stopwatch.js.map +1 -1
  243. package/dist/web/string.js +8 -5
  244. package/dist/web/string.js.map +1 -1
  245. package/dist/web/timer.js +1 -1
  246. package/dist/web/timer.js.map +1 -1
  247. package/dist/web/timespan.js +21 -8
  248. package/dist/web/timespan.js.map +1 -1
  249. package/dist/web/type-code.js +0 -0
  250. package/dist/web/type-code.js.map +1 -1
  251. package/dist/web/type.js +20 -19
  252. package/dist/web/type.js.map +1 -1
  253. package/dist/web/types.js +0 -0
  254. package/dist/web/types.js.map +1 -1
  255. package/dist/web/utilities.js +174 -39
  256. package/dist/web/utilities.js.map +1 -1
  257. package/package.json +84 -60
  258. package/src/array.ts +116 -84
  259. package/src/async-pool-executor.ts +107 -85
  260. package/src/browser/form-utilities.ts +32 -31
  261. package/src/browser/geo-location.ts +113 -113
  262. package/src/browser/native-bridge.ts +118 -116
  263. package/src/cancellable-promise.ts +102 -76
  264. package/src/clean-url-search-params.ts +36 -11
  265. package/src/collections/cache-dictionary.ts +115 -115
  266. package/src/collections/default-map.ts +48 -48
  267. package/src/collections/default-weak-map.ts +51 -51
  268. package/src/collections/from.ts +859 -859
  269. package/src/collections/index.ts +3 -3
  270. package/src/draft.ts +55 -16
  271. package/src/event-bus.ts +83 -83
  272. package/src/functions.ts +496 -0
  273. package/src/index.ts +1 -0
  274. package/src/key-value-pair.ts +24 -24
  275. package/src/lazy.ts +32 -32
  276. package/src/math.ts +33 -30
  277. package/src/order.ts +5 -5
  278. package/src/parse.ts +251 -240
  279. package/src/patch.ts +79 -79
  280. package/src/stopwatch.ts +43 -43
  281. package/src/string.ts +8 -5
  282. package/src/timer.ts +67 -67
  283. package/src/timespan.ts +412 -398
  284. package/src/type-code.ts +15 -15
  285. package/src/type.ts +372 -371
  286. package/src/types.ts +34 -30
  287. package/src/utilities.ts +653 -480
package/src/timespan.ts CHANGED
@@ -1,399 +1,413 @@
1
- /**
2
- * Represents a duration of time in milliseconds.
3
- */
4
- export default class TimeSpan {
5
- public milliseconds: number;
6
-
7
- private constructor(milliseconds: number) {
8
- this.milliseconds = milliseconds;
9
- }
10
-
11
-
12
- /**
13
- * Gets the time interval in seconds.
14
- * @returns {number} The duration in seconds.
15
- */
16
- seconds(): number {
17
- return this.milliseconds / 1000;
18
- }
19
-
20
- /**
21
- * Gets the time interval in minutes.
22
- * @returns {number} The duration in minutes.
23
- */
24
- minutes(): number {
25
- return this.milliseconds / (1000 * 60);
26
- }
27
-
28
- /**
29
- * Gets the time interval in hours.
30
- * @returns {number} The duration in hours.
31
- */
32
- hours(): number {
33
- return this.milliseconds / (1000 * 60 * 60);
34
- }
35
-
36
- /**
37
- * Gets the time interval in days.
38
- * @returns {number} The duration in days.
39
- */
40
- days(): number {
41
- return this.milliseconds / (1000 * 60 * 60 * 24);
42
- }
43
-
44
- /**
45
- * Gets the time interval in weeks.
46
- * @returns {number} The duration in weeks.
47
- */
48
- weeks(): number {
49
- return this.milliseconds / (1000 * 60 * 60 * 24 * 7);
50
- }
51
-
52
- /**
53
- * Creates a TimeSpan from a specified number of milliseconds.
54
- * @param {number} milliseconds - The number of milliseconds.
55
- * @returns {TimeSpan} A new TimeSpan instance.
56
- */
57
- static fromMilliseconds(milliseconds: number): TimeSpan {
58
- return new TimeSpan(milliseconds);
59
- }
60
-
61
- /**
62
- * Creates a TimeSpan from a specified number of seconds.
63
- * @param {number} seconds - The number of seconds.
64
- * @returns {TimeSpan} A new TimeSpan instance.
65
- */
66
- static fromSeconds(seconds: number): TimeSpan {
67
- return new TimeSpan(seconds * 1000);
68
- }
69
-
70
- /**
71
- * Creates a TimeSpan from a specified number of minutes.
72
- * @param {number} minutes - The number of minutes.
73
- * @returns {TimeSpan} A new TimeSpan instance.
74
- */
75
- static fromMinutes(minutes: number): TimeSpan {
76
- return new TimeSpan(minutes * 1000 * 60);
77
- }
78
-
79
- /**
80
- * Creates a TimeSpan from a specified number of hours.
81
- * @param {number} hours - The number of hours.
82
- * @returns {TimeSpan} A new TimeSpan instance.
83
- */
84
- static fromHours(hours: number): TimeSpan {
85
- return new TimeSpan(hours * 1000 * 60 * 60);
86
- }
87
-
88
- /**
89
- * Creates a TimeSpan from a specified number of days.
90
- * @param {number} days - The number of days.
91
- * @returns {TimeSpan} A new TimeSpan instance.
92
- */
93
- static fromDays(days: number): TimeSpan {
94
- return new TimeSpan(days * 1000 * 60 * 60 * 24);
95
- }
96
-
97
- /**
98
- * Creates a TimeSpan from a specified number of weeks.
99
- * @param {number} weeks - The number of weeks.
100
- * @returns {TimeSpan} A new TimeSpan instance.
101
- */
102
- static fromWeeks(weeks: number): TimeSpan {
103
- return new TimeSpan(weeks * 1000 * 60 * 60 * 24 * 7);
104
- }
105
-
106
- /**
107
- * Returns a new TimeSpan that adds the specified number of milliseconds to this instance.
108
- * @param {number} milliseconds - The milliseconds to add.
109
- * @returns {TimeSpan} A new TimeSpan instance.
110
- */
111
- addMilliseconds(milliseconds: number): TimeSpan {
112
- return new TimeSpan(this.milliseconds + milliseconds);
113
- }
114
-
115
- /**
116
- * Returns a new TimeSpan that adds the specified number of seconds to this instance.
117
- * @param {number} seconds - The seconds to add.
118
- * @returns {TimeSpan} A new TimeSpan instance.
119
- */
120
- addSeconds(seconds: number): TimeSpan {
121
- return this.addMilliseconds(seconds * 1000);
122
- }
123
-
124
- /**
125
- * Returns a new TimeSpan that adds the specified number of minutes to this instance.
126
- * @param {number} minutes - The minutes to add.
127
- * @returns {TimeSpan} A new TimeSpan instance.
128
- */
129
- addMinutes(minutes: number): TimeSpan {
130
- return this.addMilliseconds(minutes * 1000 * 60);
131
- }
132
-
133
- /**
134
- * Returns a new TimeSpan that adds the specified number of hours to this instance.
135
- * @param {number} hours - The hours to add.
136
- * @returns {TimeSpan} A new TimeSpan instance.
137
- */
138
- addHours(hours: number): TimeSpan {
139
- return this.addMilliseconds(hours * 1000 * 60 * 60);
140
- }
141
-
142
- /**
143
- * Returns a new TimeSpan that adds the specified number of days to this instance.
144
- * @param {number} days - The days to add.
145
- * @returns {TimeSpan} A new TimeSpan instance.
146
- */
147
- addDays(days: number): TimeSpan {
148
- return this.addMilliseconds(days * 1000 * 60 * 60 * 24);
149
- }
150
-
151
- /**
152
- * Returns a new TimeSpan that adds the specified number of weeks to this instance.
153
- * @param {number} weeks - The weeks to add.
154
- * @returns {TimeSpan} A new TimeSpan instance.
155
- */
156
- addWeeks(weeks: number): TimeSpan {
157
- return this.addMilliseconds(weeks * 1000 * 60 * 60 * 24 * 7);
158
- }
159
-
160
- /**
161
- * Returns a new TimeSpan that subtracts the specified number of milliseconds from this instance.
162
- * @param {number} milliseconds - The milliseconds to subtract.
163
- * @returns {TimeSpan} A new TimeSpan instance.
164
- */
165
- subtractMilliseconds(milliseconds: number): TimeSpan {
166
- return new TimeSpan(this.milliseconds - milliseconds);
167
- }
168
-
169
- /**
170
- * Returns a new TimeSpan that subtracts the specified number of seconds from this instance.
171
- * @param {number} seconds - The seconds to subtract.
172
- * @returns {TimeSpan} A new TimeSpan instance.
173
- */
174
- subtractSeconds(seconds: number): TimeSpan {
175
- return this.subtractMilliseconds(seconds * 1000);
176
- }
177
-
178
- /**
179
- * Returns a new TimeSpan that subtracts the specified number of minutes from this instance.
180
- * @param {number} minutes - The minutes to subtract.
181
- * @returns {TimeSpan} A new TimeSpan instance.
182
- */
183
- subtractMinutes(minutes: number): TimeSpan {
184
- return this.subtractMilliseconds(minutes * 1000 * 60);
185
- }
186
-
187
- /**
188
- * Returns a new TimeSpan that subtracts the specified number of hours from this instance.
189
- * @param {number} hours - The hours to subtract.
190
- * @returns {TimeSpan} A new TimeSpan instance.
191
- */
192
- subtractHours(hours: number): TimeSpan {
193
- return this.subtractMilliseconds(hours * 1000 * 60 * 60);
194
- }
195
-
196
- /**
197
- * Returns a new TimeSpan that subtracts the specified number of days from this instance.
198
- * @param {number} days - The days to subtract.
199
- * @returns {TimeSpan} A new TimeSpan instance.
200
- */
201
- subtractDays(days: number): TimeSpan {
202
- return this.subtractMilliseconds(days * 1000 * 60 * 60 * 24);
203
- }
204
-
205
- /**
206
- * Returns a new TimeSpan that subtracts the specified number of weeks from this instance.
207
- * @param {number} weeks - The weeks to subtract.
208
- * @returns {TimeSpan} A new TimeSpan instance.
209
- */
210
- subtractWeeks(weeks: number): TimeSpan {
211
- return this.subtractMilliseconds(weeks * 1000 * 60 * 60 * 24 * 7);
212
- }
213
-
214
- /**
215
- * Returns a new TimeSpan that adds the specified TimeSpan to this instance.
216
- * @param {TimeSpan} other - The TimeSpan to add.
217
- * @returns {TimeSpan} A new TimeSpan instance.
218
- */
219
- add(other: TimeSpan): TimeSpan {
220
- return new TimeSpan(this.milliseconds + other.milliseconds);
221
- }
222
-
223
- /**
224
- * Returns a new TimeSpan that subtracts the specified TimeSpan from this instance.
225
- * @param {TimeSpan} other - The TimeSpan to subtract.
226
- * @returns {TimeSpan} A new TimeSpan instance.
227
- */
228
- subtract(other: TimeSpan): TimeSpan {
229
- return new TimeSpan(this.milliseconds - other.milliseconds);
230
- }
231
-
232
- /**
233
- * Adds this TimeSpan to a specified Date.
234
- * @param {Date} date - The date to add to.
235
- * @returns {Date} A new Date instance.
236
- */
237
- addTo(date: Date): Date {
238
- return new Date(date.getTime() + this.milliseconds);
239
- }
240
-
241
- /**
242
- * Subtracts this TimeSpan from a specified Date.
243
- * @param {Date} date - The date to subtract from.
244
- * @returns {Date} A new Date instance.
245
- */
246
- subtractFrom(date: Date): Date {
247
- return new Date(date.getTime() - this.milliseconds);
248
- }
249
-
250
- /**
251
- * Creates a TimeSpan from the difference between two dates.
252
- * @param {Date} earlierDate - The earlier date.
253
- * @param {Date} laterDate - The later date.
254
- * @returns {TimeSpan} A new TimeSpan instance representing the difference.
255
- */
256
- static fromDifference(earlierDate: Date, laterDate: Date): TimeSpan {
257
- return new TimeSpan(laterDate.getTime() - earlierDate.getTime());
258
- }
259
-
260
- /**
261
- * Formats the TimeSpan as a string.
262
- * Supported tokens: hh, h, mm, m, ss, s.
263
- * @param {string} format - The format string (default: 'hh:mm:ss').
264
- * @returns {string} The formatted string.
265
- */
266
- format(format: string = 'hh:mm:ss'): string {
267
- const formatLower = format.toLowerCase();
268
- const hasHours = formatLower.includes("h");
269
- const hasMinutes = formatLower.includes("m");
270
-
271
- let hours = 0, minutes = 0, seconds = Math.floor(this.milliseconds / 1000);
272
-
273
- if (hasHours) {
274
- hours = Math.floor(seconds / 3600);
275
- seconds -= hours * 3600;
276
- }
277
- if (hasMinutes) {
278
- minutes = Math.floor(seconds / 60);
279
- seconds -= minutes * 60;
280
- }
281
-
282
- const hoursPadded = String(hours).padStart(2, '0');
283
- const minutesPadded = String(minutes).padStart(2, '0');
284
- const secondsPadded = String(seconds).padStart(2, '0');
285
-
286
- return formatLower
287
- .replace('hh', hoursPadded)
288
- .replace('h', hours.toString())
289
- .replace('mm', minutesPadded)
290
- .replace('m', minutes.toString())
291
- .replace('ss', secondsPadded)
292
- .replace('s', seconds.toString());
293
- }
294
-
295
- /**
296
- * Checks if this TimeSpan is equal to another.
297
- * @param {TimeSpan} other - The other TimeSpan.
298
- * @returns {boolean} True if equal.
299
- */
300
- eq(other: TimeSpan): boolean {
301
- return this.milliseconds === other.milliseconds;
302
- }
303
-
304
- /**
305
- * Checks if this TimeSpan is less than or equal to another.
306
- * @param {TimeSpan} other - The other TimeSpan.
307
- * @returns {boolean} True if less than or equal.
308
- */
309
- le(other: TimeSpan): boolean {
310
- return this.milliseconds <= other.milliseconds;
311
- }
312
-
313
- /**
314
- * Checks if this TimeSpan is less than another.
315
- * @param {TimeSpan} other - The other TimeSpan.
316
- * @returns {boolean} True if less than.
317
- */
318
- lt(other: TimeSpan): boolean {
319
- return this.milliseconds < other.milliseconds;
320
- }
321
-
322
- /**
323
- * Checks if this TimeSpan is greater than or equal to another.
324
- * @param {TimeSpan} other - The other TimeSpan.
325
- * @returns {boolean} True if greater than or equal.
326
- */
327
- ge(other: TimeSpan): boolean {
328
- return this.milliseconds >= other.milliseconds;
329
- }
330
-
331
- /**
332
- * Checks if this TimeSpan is greater than another.
333
- * @param {TimeSpan} other - The other TimeSpan.
334
- * @returns {boolean} True if greater than.
335
- */
336
- gt(other: TimeSpan): boolean {
337
- return this.milliseconds > other.milliseconds;
338
- }
339
-
340
- /**
341
- * Multiplies the TimeSpan by a number.
342
- * @param {number} number - The multiplier.
343
- * @returns {TimeSpan} A new TimeSpan instance.
344
- */
345
- multiply(number: number): TimeSpan {
346
- return new TimeSpan(this.milliseconds * number);
347
- }
348
-
349
- /**
350
- * Divides the TimeSpan by a number.
351
- * @param {number} number - The divisor.
352
- * @returns {TimeSpan} A new TimeSpan instance.
353
- */
354
- divide(number: number): TimeSpan {
355
- return new TimeSpan(this.milliseconds / number);
356
- }
357
-
358
- /**
359
- * Returns the absolute value of the TimeSpan.
360
- * @returns {TimeSpan} A new TimeSpan instance.
361
- */
362
- abs(): TimeSpan {
363
- return new TimeSpan(Math.abs(this.milliseconds));
364
- }
365
-
366
- /**
367
- * Infinite duration.
368
- */
369
- static readonly INFINITE = new TimeSpan(Number.POSITIVE_INFINITY);
370
-
371
- /**
372
- * Negative infinite duration.
373
- */
374
- static readonly NEGATIVE_INFINITE = new TimeSpan(Number.NEGATIVE_INFINITY);
375
-
376
- /**
377
- * Checks if the TimeSpan is infinite.
378
- * @returns {boolean} True if infinite.
379
- */
380
- isInfinite() {
381
- return this.milliseconds === Number.POSITIVE_INFINITY || this.milliseconds === Number.NEGATIVE_INFINITY
382
- }
383
-
384
- /**
385
- * Checks if the TimeSpan is positive infinite.
386
- * @returns {boolean} True if positive infinite.
387
- */
388
- isPositiveInfinite(): boolean {
389
- return this.milliseconds === Number.POSITIVE_INFINITY;
390
- }
391
-
392
- /**
393
- * Checks if the TimeSpan is negative infinite.
394
- * @returns {boolean} True if negative infinite.
395
- */
396
- isNegativeInfinite(): boolean {
397
- return this.milliseconds === Number.NEGATIVE_INFINITY;
398
- }
1
+ /**
2
+ * Represents a duration of time in milliseconds.
3
+ */
4
+ export default class TimeSpan {
5
+ public milliseconds: number;
6
+
7
+ private constructor(milliseconds: number) {
8
+ this.milliseconds = milliseconds;
9
+ }
10
+
11
+
12
+ /**
13
+ * Gets the time interval in seconds.
14
+ * @returns {number} The duration in seconds.
15
+ */
16
+ seconds(): number {
17
+ return this.milliseconds / 1000;
18
+ }
19
+
20
+ /**
21
+ * Gets the time interval in minutes.
22
+ * @returns {number} The duration in minutes.
23
+ */
24
+ minutes(): number {
25
+ return this.milliseconds / (1000 * 60);
26
+ }
27
+
28
+ /**
29
+ * Gets the time interval in hours.
30
+ * @returns {number} The duration in hours.
31
+ */
32
+ hours(): number {
33
+ return this.milliseconds / (1000 * 60 * 60);
34
+ }
35
+
36
+ /**
37
+ * Gets the time interval in days.
38
+ * @returns {number} The duration in days.
39
+ */
40
+ days(): number {
41
+ return this.milliseconds / (1000 * 60 * 60 * 24);
42
+ }
43
+
44
+ /**
45
+ * Gets the time interval in weeks.
46
+ * @returns {number} The duration in weeks.
47
+ */
48
+ weeks(): number {
49
+ return this.milliseconds / (1000 * 60 * 60 * 24 * 7);
50
+ }
51
+
52
+ /**
53
+ * Creates a TimeSpan from a specified number of milliseconds.
54
+ * @param {number} milliseconds - The number of milliseconds.
55
+ * @returns {TimeSpan} A new TimeSpan instance.
56
+ */
57
+ static fromMilliseconds(milliseconds: number): TimeSpan {
58
+ return new TimeSpan(milliseconds);
59
+ }
60
+
61
+ /**
62
+ * Creates a TimeSpan from a specified number of seconds.
63
+ * @param {number} seconds - The number of seconds.
64
+ * @returns {TimeSpan} A new TimeSpan instance.
65
+ */
66
+ static fromSeconds(seconds: number): TimeSpan {
67
+ return new TimeSpan(seconds * 1000);
68
+ }
69
+
70
+ /**
71
+ * Creates a TimeSpan from a specified number of minutes.
72
+ * @param {number} minutes - The number of minutes.
73
+ * @returns {TimeSpan} A new TimeSpan instance.
74
+ */
75
+ static fromMinutes(minutes: number): TimeSpan {
76
+ return new TimeSpan(minutes * 1000 * 60);
77
+ }
78
+
79
+ /**
80
+ * Creates a TimeSpan from a specified number of hours.
81
+ * @param {number} hours - The number of hours.
82
+ * @returns {TimeSpan} A new TimeSpan instance.
83
+ */
84
+ static fromHours(hours: number): TimeSpan {
85
+ return new TimeSpan(hours * 1000 * 60 * 60);
86
+ }
87
+
88
+ /**
89
+ * Creates a TimeSpan from a specified number of days.
90
+ * @param {number} days - The number of days.
91
+ * @returns {TimeSpan} A new TimeSpan instance.
92
+ */
93
+ static fromDays(days: number): TimeSpan {
94
+ return new TimeSpan(days * 1000 * 60 * 60 * 24);
95
+ }
96
+
97
+ /**
98
+ * Creates a TimeSpan from a specified number of weeks.
99
+ * @param {number} weeks - The number of weeks.
100
+ * @returns {TimeSpan} A new TimeSpan instance.
101
+ */
102
+ static fromWeeks(weeks: number): TimeSpan {
103
+ return new TimeSpan(weeks * 1000 * 60 * 60 * 24 * 7);
104
+ }
105
+
106
+ /**
107
+ * Returns a new TimeSpan that adds the specified number of milliseconds to this instance.
108
+ * @param {number} milliseconds - The milliseconds to add.
109
+ * @returns {TimeSpan} A new TimeSpan instance.
110
+ */
111
+ addMilliseconds(milliseconds: number): TimeSpan {
112
+ return new TimeSpan(this.milliseconds + milliseconds);
113
+ }
114
+
115
+ /**
116
+ * Returns a new TimeSpan that adds the specified number of seconds to this instance.
117
+ * @param {number} seconds - The seconds to add.
118
+ * @returns {TimeSpan} A new TimeSpan instance.
119
+ */
120
+ addSeconds(seconds: number): TimeSpan {
121
+ return this.addMilliseconds(seconds * 1000);
122
+ }
123
+
124
+ /**
125
+ * Returns a new TimeSpan that adds the specified number of minutes to this instance.
126
+ * @param {number} minutes - The minutes to add.
127
+ * @returns {TimeSpan} A new TimeSpan instance.
128
+ */
129
+ addMinutes(minutes: number): TimeSpan {
130
+ return this.addMilliseconds(minutes * 1000 * 60);
131
+ }
132
+
133
+ /**
134
+ * Returns a new TimeSpan that adds the specified number of hours to this instance.
135
+ * @param {number} hours - The hours to add.
136
+ * @returns {TimeSpan} A new TimeSpan instance.
137
+ */
138
+ addHours(hours: number): TimeSpan {
139
+ return this.addMilliseconds(hours * 1000 * 60 * 60);
140
+ }
141
+
142
+ /**
143
+ * Returns a new TimeSpan that adds the specified number of days to this instance.
144
+ * @param {number} days - The days to add.
145
+ * @returns {TimeSpan} A new TimeSpan instance.
146
+ */
147
+ addDays(days: number): TimeSpan {
148
+ return this.addMilliseconds(days * 1000 * 60 * 60 * 24);
149
+ }
150
+
151
+ /**
152
+ * Returns a new TimeSpan that adds the specified number of weeks to this instance.
153
+ * @param {number} weeks - The weeks to add.
154
+ * @returns {TimeSpan} A new TimeSpan instance.
155
+ */
156
+ addWeeks(weeks: number): TimeSpan {
157
+ return this.addMilliseconds(weeks * 1000 * 60 * 60 * 24 * 7);
158
+ }
159
+
160
+ /**
161
+ * Returns a new TimeSpan that subtracts the specified number of milliseconds from this instance.
162
+ * @param {number} milliseconds - The milliseconds to subtract.
163
+ * @returns {TimeSpan} A new TimeSpan instance.
164
+ */
165
+ subtractMilliseconds(milliseconds: number): TimeSpan {
166
+ return new TimeSpan(this.milliseconds - milliseconds);
167
+ }
168
+
169
+ /**
170
+ * Returns a new TimeSpan that subtracts the specified number of seconds from this instance.
171
+ * @param {number} seconds - The seconds to subtract.
172
+ * @returns {TimeSpan} A new TimeSpan instance.
173
+ */
174
+ subtractSeconds(seconds: number): TimeSpan {
175
+ return this.subtractMilliseconds(seconds * 1000);
176
+ }
177
+
178
+ /**
179
+ * Returns a new TimeSpan that subtracts the specified number of minutes from this instance.
180
+ * @param {number} minutes - The minutes to subtract.
181
+ * @returns {TimeSpan} A new TimeSpan instance.
182
+ */
183
+ subtractMinutes(minutes: number): TimeSpan {
184
+ return this.subtractMilliseconds(minutes * 1000 * 60);
185
+ }
186
+
187
+ /**
188
+ * Returns a new TimeSpan that subtracts the specified number of hours from this instance.
189
+ * @param {number} hours - The hours to subtract.
190
+ * @returns {TimeSpan} A new TimeSpan instance.
191
+ */
192
+ subtractHours(hours: number): TimeSpan {
193
+ return this.subtractMilliseconds(hours * 1000 * 60 * 60);
194
+ }
195
+
196
+ /**
197
+ * Returns a new TimeSpan that subtracts the specified number of days from this instance.
198
+ * @param {number} days - The days to subtract.
199
+ * @returns {TimeSpan} A new TimeSpan instance.
200
+ */
201
+ subtractDays(days: number): TimeSpan {
202
+ return this.subtractMilliseconds(days * 1000 * 60 * 60 * 24);
203
+ }
204
+
205
+ /**
206
+ * Returns a new TimeSpan that subtracts the specified number of weeks from this instance.
207
+ * @param {number} weeks - The weeks to subtract.
208
+ * @returns {TimeSpan} A new TimeSpan instance.
209
+ */
210
+ subtractWeeks(weeks: number): TimeSpan {
211
+ return this.subtractMilliseconds(weeks * 1000 * 60 * 60 * 24 * 7);
212
+ }
213
+
214
+ /**
215
+ * Returns a new TimeSpan that adds the specified TimeSpan to this instance.
216
+ * @param {TimeSpan} other - The TimeSpan to add.
217
+ * @returns {TimeSpan} A new TimeSpan instance.
218
+ */
219
+ add(other: TimeSpan): TimeSpan {
220
+ return new TimeSpan(this.milliseconds + other.milliseconds);
221
+ }
222
+
223
+ /**
224
+ * Returns a new TimeSpan that subtracts the specified TimeSpan from this instance.
225
+ * @param {TimeSpan} other - The TimeSpan to subtract.
226
+ * @returns {TimeSpan} A new TimeSpan instance.
227
+ */
228
+ subtract(other: TimeSpan): TimeSpan {
229
+ return new TimeSpan(this.milliseconds - other.milliseconds);
230
+ }
231
+
232
+ /**
233
+ * Adds this TimeSpan to a specified Date.
234
+ * @param {Date} date - The date to add to.
235
+ * @returns {Date} A new Date instance.
236
+ */
237
+ addTo(date: Date): Date {
238
+ return new Date(date.getTime() + this.milliseconds);
239
+ }
240
+
241
+ /**
242
+ * Subtracts this TimeSpan from a specified Date.
243
+ * @param {Date} date - The date to subtract from.
244
+ * @returns {Date} A new Date instance.
245
+ */
246
+ subtractFrom(date: Date): Date {
247
+ return new Date(date.getTime() - this.milliseconds);
248
+ }
249
+
250
+ /**
251
+ * Creates a TimeSpan from the difference between two dates.
252
+ * @param {Date} earlierDate - The earlier date.
253
+ * @param {Date} laterDate - The later date.
254
+ * @returns {TimeSpan} A new TimeSpan instance representing the difference.
255
+ */
256
+ static fromDifference(earlierDate: Date, laterDate: Date): TimeSpan {
257
+ return new TimeSpan(laterDate.getTime() - earlierDate.getTime());
258
+ }
259
+
260
+ /**
261
+ * Formats the TimeSpan as a string.
262
+ * Supported tokens: hh, h, mm, m, ss, s.
263
+ * @param {string} format - The format string (default: 'hh:mm:ss').
264
+ * @returns {string} The formatted string.
265
+ */
266
+ format(format: string = 'hh:mm:ss'): string {
267
+ const formatLower = format.toLowerCase();
268
+ const hasHours = formatLower.includes("h");
269
+ const hasMinutes = formatLower.includes("m");
270
+
271
+ const sign = this.milliseconds < 0 ? "-" : "";
272
+ let hours = 0, minutes = 0, seconds = Math.floor(Math.abs(this.milliseconds) / 1000);
273
+
274
+ if (hasHours) {
275
+ hours = Math.floor(seconds / 3600);
276
+ seconds -= hours * 3600;
277
+ }
278
+ if (hasMinutes) {
279
+ minutes = Math.floor(seconds / 60);
280
+ seconds -= minutes * 60;
281
+ }
282
+
283
+ const hoursPadded = String(hours).padStart(2, '0');
284
+ const minutesPadded = String(minutes).padStart(2, '0');
285
+ const secondsPadded = String(seconds).padStart(2, '0');
286
+
287
+ const formatted = formatLower.replace(/hh|h|mm|m|ss|s/g, token => {
288
+ switch (token) {
289
+ case 'hh':
290
+ return hoursPadded;
291
+ case 'h':
292
+ return hours.toString();
293
+ case 'mm':
294
+ return minutesPadded;
295
+ case 'm':
296
+ return minutes.toString();
297
+ case 'ss':
298
+ return secondsPadded;
299
+ case 's':
300
+ return seconds.toString();
301
+ default:
302
+ return token;
303
+ }
304
+ });
305
+
306
+ return sign + formatted;
307
+ }
308
+
309
+ /**
310
+ * Checks if this TimeSpan is equal to another.
311
+ * @param {TimeSpan} other - The other TimeSpan.
312
+ * @returns {boolean} True if equal.
313
+ */
314
+ eq(other: TimeSpan): boolean {
315
+ return this.milliseconds === other.milliseconds;
316
+ }
317
+
318
+ /**
319
+ * Checks if this TimeSpan is less than or equal to another.
320
+ * @param {TimeSpan} other - The other TimeSpan.
321
+ * @returns {boolean} True if less than or equal.
322
+ */
323
+ le(other: TimeSpan): boolean {
324
+ return this.milliseconds <= other.milliseconds;
325
+ }
326
+
327
+ /**
328
+ * Checks if this TimeSpan is less than another.
329
+ * @param {TimeSpan} other - The other TimeSpan.
330
+ * @returns {boolean} True if less than.
331
+ */
332
+ lt(other: TimeSpan): boolean {
333
+ return this.milliseconds < other.milliseconds;
334
+ }
335
+
336
+ /**
337
+ * Checks if this TimeSpan is greater than or equal to another.
338
+ * @param {TimeSpan} other - The other TimeSpan.
339
+ * @returns {boolean} True if greater than or equal.
340
+ */
341
+ ge(other: TimeSpan): boolean {
342
+ return this.milliseconds >= other.milliseconds;
343
+ }
344
+
345
+ /**
346
+ * Checks if this TimeSpan is greater than another.
347
+ * @param {TimeSpan} other - The other TimeSpan.
348
+ * @returns {boolean} True if greater than.
349
+ */
350
+ gt(other: TimeSpan): boolean {
351
+ return this.milliseconds > other.milliseconds;
352
+ }
353
+
354
+ /**
355
+ * Multiplies the TimeSpan by a number.
356
+ * @param {number} number - The multiplier.
357
+ * @returns {TimeSpan} A new TimeSpan instance.
358
+ */
359
+ multiply(number: number): TimeSpan {
360
+ return new TimeSpan(this.milliseconds * number);
361
+ }
362
+
363
+ /**
364
+ * Divides the TimeSpan by a number.
365
+ * @param {number} number - The divisor.
366
+ * @returns {TimeSpan} A new TimeSpan instance.
367
+ */
368
+ divide(number: number): TimeSpan {
369
+ return new TimeSpan(this.milliseconds / number);
370
+ }
371
+
372
+ /**
373
+ * Returns the absolute value of the TimeSpan.
374
+ * @returns {TimeSpan} A new TimeSpan instance.
375
+ */
376
+ abs(): TimeSpan {
377
+ return new TimeSpan(Math.abs(this.milliseconds));
378
+ }
379
+
380
+ /**
381
+ * Infinite duration.
382
+ */
383
+ static readonly INFINITE = new TimeSpan(Number.POSITIVE_INFINITY);
384
+
385
+ /**
386
+ * Negative infinite duration.
387
+ */
388
+ static readonly NEGATIVE_INFINITE = new TimeSpan(Number.NEGATIVE_INFINITY);
389
+
390
+ /**
391
+ * Checks if the TimeSpan is infinite.
392
+ * @returns {boolean} True if infinite.
393
+ */
394
+ isInfinite() {
395
+ return this.milliseconds === Number.POSITIVE_INFINITY || this.milliseconds === Number.NEGATIVE_INFINITY
396
+ }
397
+
398
+ /**
399
+ * Checks if the TimeSpan is positive infinite.
400
+ * @returns {boolean} True if positive infinite.
401
+ */
402
+ isPositiveInfinite(): boolean {
403
+ return this.milliseconds === Number.POSITIVE_INFINITY;
404
+ }
405
+
406
+ /**
407
+ * Checks if the TimeSpan is negative infinite.
408
+ * @returns {boolean} True if negative infinite.
409
+ */
410
+ isNegativeInfinite(): boolean {
411
+ return this.milliseconds === Number.NEGATIVE_INFINITY;
412
+ }
399
413
  }