@01-edu/shared 1.2.16 → 1.2.17
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/package.json +1 -1
- package/toolbox.js +37 -1
package/package.json
CHANGED
package/toolbox.js
CHANGED
|
@@ -175,9 +175,14 @@ export const timeUnits = [
|
|
|
175
175
|
]
|
|
176
176
|
export const formatedDuration = (
|
|
177
177
|
seconds,
|
|
178
|
-
{ noSeconds, stopFirst, useFullLabel } = {},
|
|
178
|
+
{ noSeconds, stopFirst, useFullLabel, stopLabel } = {},
|
|
179
179
|
) => {
|
|
180
180
|
const parts = []
|
|
181
|
+
if (
|
|
182
|
+
stopLabel &&
|
|
183
|
+
seconds < timeUnits.find(u => u.fullLabel === stopLabel)?.size
|
|
184
|
+
)
|
|
185
|
+
return `less than 1${useFullLabel ? ` ${stopLabel}` : timeUnits.find(u => u.fullLabel === stopLabel).label}`
|
|
181
186
|
for (const { label, size, fullLabel } of timeUnits) {
|
|
182
187
|
const value = Math.floor(seconds / size)
|
|
183
188
|
if (value !== 0) {
|
|
@@ -187,6 +192,9 @@ export const formatedDuration = (
|
|
|
187
192
|
if (stopFirst) return parts
|
|
188
193
|
seconds -= value * size
|
|
189
194
|
}
|
|
195
|
+
if (fullLabel === stopLabel) {
|
|
196
|
+
return parts.join(' ') || (noSeconds ? '0m' : '0s')
|
|
197
|
+
}
|
|
190
198
|
}
|
|
191
199
|
if (!noSeconds && seconds !== 0) {
|
|
192
200
|
parts.push(`${seconds}s`)
|
|
@@ -533,3 +541,31 @@ export const createFrequencyMap = arr =>
|
|
|
533
541
|
map[item] = (map[item] || 0) + 1
|
|
534
542
|
return map
|
|
535
543
|
}, {})
|
|
544
|
+
|
|
545
|
+
export const postgresIntervalToMS = intervalStr => {
|
|
546
|
+
if (!intervalStr) return 0
|
|
547
|
+
// Regex to capture years, months, days, hours, minutes, seconds
|
|
548
|
+
const regex =
|
|
549
|
+
/(?:(\d+)\s+years?)?\s*(?:(\d+)\s+mons?)?\s*(?:(\d+)\s+days?)?\s*(?:(\d+):(\d+):([\d.]+))?/
|
|
550
|
+
const match = intervalStr.match(regex)
|
|
551
|
+
|
|
552
|
+
if (!match) return 0
|
|
553
|
+
|
|
554
|
+
const years = Number.parseInt(match[1] || 0, 10)
|
|
555
|
+
const months = Number.parseInt(match[2] || 0, 10)
|
|
556
|
+
const days = Number.parseInt(match[3] || 0, 10)
|
|
557
|
+
const hours = Number.parseInt(match[4] || 0, 10)
|
|
558
|
+
const minutes = Number.parseInt(match[5] || 0, 10)
|
|
559
|
+
const seconds = Number.parseFloat(match[6] || 0)
|
|
560
|
+
|
|
561
|
+
// Convert everything to milliseconds
|
|
562
|
+
const ms =
|
|
563
|
+
years * 365 * 24 * 60 * 60 * 1000 + // assume 1 year = 365 days
|
|
564
|
+
months * 30 * 24 * 60 * 60 * 1000 + // assume 1 month = 30 days
|
|
565
|
+
days * 24 * 60 * 60 * 1000 +
|
|
566
|
+
hours * 60 * 60 * 1000 +
|
|
567
|
+
minutes * 60 * 1000 +
|
|
568
|
+
seconds * 1000
|
|
569
|
+
|
|
570
|
+
return ms
|
|
571
|
+
}
|