@react-pakistan/util-functions 1.23.85 → 1.23.87
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/general/format-time.d.ts +5 -0
- package/general/format-time.js +37 -0
- package/package.json +1 -1
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/* eslint-disable no-restricted-syntax */
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.formatDuration = void 0;
|
|
5
|
+
var units = [
|
|
6
|
+
{ divisor: 86400, singular: 'day', plural: 'days' },
|
|
7
|
+
{ divisor: 3600, singular: 'hour', plural: 'hours' },
|
|
8
|
+
{ divisor: 60, singular: 'min', plural: 'mins' },
|
|
9
|
+
{ divisor: 1, singular: 'sec', plural: 'secs' },
|
|
10
|
+
];
|
|
11
|
+
var formatDuration = function (_a) {
|
|
12
|
+
var seconds = _a.seconds;
|
|
13
|
+
/* Edge Case Handling */
|
|
14
|
+
if (typeof seconds !== 'number' || seconds < 0 || !Number.isInteger(seconds)) {
|
|
15
|
+
throw new Error('Invalid input: must be a non-negative integer');
|
|
16
|
+
}
|
|
17
|
+
var remaining = seconds;
|
|
18
|
+
var parts = [];
|
|
19
|
+
for (var _i = 0, units_1 = units; _i < units_1.length; _i++) {
|
|
20
|
+
var unit = units_1[_i];
|
|
21
|
+
var value = Math.floor(remaining / unit.divisor);
|
|
22
|
+
remaining %= unit.divisor;
|
|
23
|
+
if (value > 0) {
|
|
24
|
+
parts.push("".concat(value, " ").concat(value === 1 ? unit.singular : unit.plural));
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/* Handle 0 seconds case */
|
|
28
|
+
if (parts.length === 0) {
|
|
29
|
+
var smallestUnit = units[units.length - 1];
|
|
30
|
+
return "0 ".concat(smallestUnit.plural);
|
|
31
|
+
}
|
|
32
|
+
/* Human-friendly joining */
|
|
33
|
+
return parts.length > 1
|
|
34
|
+
? "".concat(parts.slice(0, -1).join(', '), " and ").concat(parts[parts.length - 1])
|
|
35
|
+
: parts[0];
|
|
36
|
+
};
|
|
37
|
+
exports.formatDuration = formatDuration;
|