@sproutsocial/racine 24.1.0 → 24.2.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/CHANGELOG.md +8 -0
- package/commonjs/Duration/Duration.js +54 -45
- package/commonjs/Duration/constants.js +21 -2
- package/commonjs/Duration/index.js +9 -1
- package/commonjs/Duration/utils.js +42 -0
- package/dist/types/Duration/Duration.d.ts.map +1 -1
- package/dist/types/Duration/DurationTypes.d.ts +16 -4
- package/dist/types/Duration/DurationTypes.d.ts.map +1 -1
- package/dist/types/Duration/constants.d.ts +18 -1
- package/dist/types/Duration/constants.d.ts.map +1 -1
- package/dist/types/Duration/index.d.ts +3 -0
- package/dist/types/Duration/index.d.ts.map +1 -1
- package/dist/types/Duration/utils.d.ts +8 -0
- package/dist/types/Duration/utils.d.ts.map +1 -0
- package/lib/Duration/Duration.js +54 -45
- package/lib/Duration/constants.js +20 -1
- package/lib/Duration/index.js +2 -0
- package/lib/Duration/utils.js +36 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
+
## 24.2.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- 538d167: - Added `displayUnits` prop to the Duration component to give users manual control over which time units are displayed
|
|
8
|
+
- `display` prop on the Duration component now defaults to "narrow" instead of "long"
|
|
9
|
+
- Exporting a new helper function for Duration named `getDurationMaxDisplayUnits` that gets the largest number of display units based on the milliseconds value
|
|
10
|
+
|
|
3
11
|
## 24.1.0
|
|
4
12
|
|
|
5
13
|
### Minor Changes
|
|
@@ -11,6 +11,7 @@ var _constants = require("../utils/constants");
|
|
|
11
11
|
var _VisuallyHidden = require("../VisuallyHidden");
|
|
12
12
|
var _constants2 = require("./constants");
|
|
13
13
|
var _styles = require("./styles");
|
|
14
|
+
var _utils = require("./utils");
|
|
14
15
|
var _jsxRuntime = require("react/jsx-runtime");
|
|
15
16
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
16
17
|
function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(e) { return e ? t : r; })(e); }
|
|
@@ -20,16 +21,34 @@ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t =
|
|
|
20
21
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
21
22
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
22
23
|
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
23
|
-
function
|
|
24
|
-
function
|
|
24
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
25
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
25
26
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
27
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
28
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
26
29
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
30
|
+
var getLowestUnit = function getLowestUnit(displayUnits) {
|
|
31
|
+
return _toConsumableArray(_constants2.ORDERED_UNITS).reverse().find(function (unit) {
|
|
32
|
+
return displayUnits[unit];
|
|
33
|
+
}) || _constants2.UNITS.milliseconds;
|
|
31
34
|
};
|
|
32
|
-
var
|
|
35
|
+
var splitMillisecondsIntoUnits = function splitMillisecondsIntoUnits(milliseconds, displayUnits) {
|
|
36
|
+
var lowestUnit = getLowestUnit(displayUnits);
|
|
37
|
+
var remainder = milliseconds % _constants2.MILLISECONDS_IN[lowestUnit];
|
|
38
|
+
if (2 * remainder >= _constants2.MILLISECONDS_IN[lowestUnit]) {
|
|
39
|
+
// if the remainder is large, add enough seconds to increse the lowest unit
|
|
40
|
+
milliseconds += _constants2.MILLISECONDS_IN[lowestUnit] - remainder;
|
|
41
|
+
}
|
|
42
|
+
var units = {};
|
|
43
|
+
_constants2.ORDERED_UNITS.forEach(function (unit) {
|
|
44
|
+
if (displayUnits[unit]) {
|
|
45
|
+
units[unit] = Math.floor(milliseconds / _constants2.MILLISECONDS_IN[unit]);
|
|
46
|
+
milliseconds -= units[unit] * _constants2.MILLISECONDS_IN[unit];
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
return units;
|
|
50
|
+
};
|
|
51
|
+
var _createDurationFormatter = function _createDurationFormatter(locale, unitDisplay, displayUnits) {
|
|
33
52
|
var timeUnitFormatter = function timeUnitFormatter(locale, unit, unitDisplay) {
|
|
34
53
|
return Intl.NumberFormat(locale, {
|
|
35
54
|
style: "unit",
|
|
@@ -37,41 +56,31 @@ var _createDurationFormatter = function _createDurationFormatter(locale, unitDis
|
|
|
37
56
|
unitDisplay: unitDisplay
|
|
38
57
|
}).format;
|
|
39
58
|
};
|
|
40
|
-
var
|
|
41
|
-
var formatHours = timeUnitFormatter(locale, "hour", unitDisplay);
|
|
42
|
-
var formatMinutes = timeUnitFormatter(locale, "minute", unitDisplay);
|
|
43
|
-
var formatSeconds = timeUnitFormatter(locale, "second", unitDisplay);
|
|
44
|
-
var formatMilliseconds = timeUnitFormatter(locale, "millisecond", unitDisplay);
|
|
59
|
+
var formatterByUnit = _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, _constants2.UNITS.days, timeUnitFormatter(locale, "day", unitDisplay)), _constants2.UNITS.hours, timeUnitFormatter(locale, "hour", unitDisplay)), _constants2.UNITS.minutes, timeUnitFormatter(locale, "minute", unitDisplay)), _constants2.UNITS.seconds, timeUnitFormatter(locale, "second", unitDisplay)), _constants2.UNITS.milliseconds, timeUnitFormatter(locale, "millisecond", unitDisplay));
|
|
45
60
|
var formatList = new Intl.ListFormat(locale, {
|
|
46
61
|
style: "narrow",
|
|
47
62
|
type: "unit"
|
|
48
63
|
});
|
|
49
|
-
return function (
|
|
50
|
-
|
|
51
|
-
|
|
64
|
+
return function (value) {
|
|
65
|
+
var lowestUnit = getLowestUnit(displayUnits);
|
|
66
|
+
|
|
67
|
+
// if the value is zero or negative, we just want to return 0 for the lowest unit (ex "0 minutes")
|
|
68
|
+
if (value <= 0) {
|
|
69
|
+
return formatterByUnit[lowestUnit](0);
|
|
52
70
|
}
|
|
53
|
-
var
|
|
54
|
-
var
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
var _getDivisionAndRemain6 = _slicedToArray(_getDivisionAndRemain5, 2);
|
|
67
|
-
minutes = _getDivisionAndRemain6[0];
|
|
68
|
-
milliseconds = _getDivisionAndRemain6[1];
|
|
69
|
-
var _getDivisionAndRemain7 = getDivisionAndRemainder(milliseconds, 1000);
|
|
70
|
-
var _getDivisionAndRemain8 = _slicedToArray(_getDivisionAndRemain7, 2);
|
|
71
|
-
seconds = _getDivisionAndRemain8[0];
|
|
72
|
-
milliseconds = _getDivisionAndRemain8[1];
|
|
73
|
-
var list = [days ? formatDays(days) : null, hours ? formatHours(hours) : null, minutes ? formatMinutes(minutes) : null, seconds ? formatSeconds(seconds) : null, milliseconds ? formatMilliseconds(milliseconds) : null].filter(function (listItem) {
|
|
74
|
-
return listItem !== null;
|
|
71
|
+
var millisecondsByUnit = splitMillisecondsIntoUnits(value, displayUnits);
|
|
72
|
+
var list = [];
|
|
73
|
+
_constants2.ORDERED_UNITS.forEach(function (unit) {
|
|
74
|
+
if (unit in millisecondsByUnit) {
|
|
75
|
+
var unitValue = millisecondsByUnit[unit];
|
|
76
|
+
|
|
77
|
+
// we want to add to the list if one of two conditions are met:
|
|
78
|
+
// 1) the unit has a value greater than 0 OR
|
|
79
|
+
// 2) the unit has value 0 AND the unit is the lowest unit AND the list is already empty
|
|
80
|
+
if (unitValue !== 0 || unitValue === 0 && unit === lowestUnit && list.length === 0) {
|
|
81
|
+
list.push(formatterByUnit[unit](millisecondsByUnit[unit]));
|
|
82
|
+
}
|
|
83
|
+
}
|
|
75
84
|
});
|
|
76
85
|
return formatList.format(list);
|
|
77
86
|
};
|
|
@@ -80,22 +89,21 @@ var _createDurationFormatter = function _createDurationFormatter(locale, unitDis
|
|
|
80
89
|
// Memoize to reduce the energy of creating new instances of Intl.NumberFormat
|
|
81
90
|
var memoizer = (0, _lruMemoize.default)(_constants2.MEMO_CACHE_SIZE, _constants2.COMPARE_OBJECTS);
|
|
82
91
|
var createDurationFormatter = memoizer(_createDurationFormatter);
|
|
83
|
-
var isValidNumber = function isValidNumber(value) {
|
|
84
|
-
return typeof value === "number" && isFinite(value);
|
|
85
|
-
};
|
|
86
92
|
var getDuration = function getDuration(_ref) {
|
|
87
93
|
var returnType = _ref.returnType,
|
|
88
94
|
props = _ref.props;
|
|
89
95
|
var _props$display = props.display,
|
|
90
|
-
display = _props$display === void 0 ?
|
|
96
|
+
display = _props$display === void 0 ? _constants2.DEFAULT_DISPLAY : _props$display,
|
|
97
|
+
_props$displayUnits = props.displayUnits,
|
|
98
|
+
displayUnits = _props$displayUnits === void 0 ? _constants2.DEFAULT_DISPLAY_UNITS : _props$displayUnits,
|
|
91
99
|
invalidMillisecondsLabel = props.invalidMillisecondsLabel,
|
|
92
100
|
_props$locale = props.locale,
|
|
93
|
-
locale = _props$locale === void 0 ?
|
|
101
|
+
locale = _props$locale === void 0 ? _constants2.DEFAULT_LOCALE : _props$locale,
|
|
94
102
|
_props$milliseconds = props.milliseconds,
|
|
95
|
-
milliseconds = _props$milliseconds === void 0 ?
|
|
103
|
+
milliseconds = _props$milliseconds === void 0 ? _constants2.DEFAULT_MILLISECONDS : _props$milliseconds,
|
|
96
104
|
qa = props.qa;
|
|
97
105
|
var isReturnTypeString = returnType === "string";
|
|
98
|
-
if (!isValidNumber(milliseconds)) {
|
|
106
|
+
if (!(0, _utils.isValidNumber)(milliseconds)) {
|
|
99
107
|
return isReturnTypeString ? _constants.EM_DASH : /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
|
|
100
108
|
children: [invalidMillisecondsLabel ?
|
|
101
109
|
/*#__PURE__*/
|
|
@@ -109,7 +117,8 @@ var getDuration = function getDuration(_ref) {
|
|
|
109
117
|
}))]
|
|
110
118
|
});
|
|
111
119
|
}
|
|
112
|
-
var
|
|
120
|
+
var validatedDisplayUnits = Object.keys(displayUnits).length === 0 ? _constants2.DEFAULT_DISPLAY_UNITS : displayUnits;
|
|
121
|
+
var fullText = createDurationFormatter(locale, display, validatedDisplayUnits)(milliseconds);
|
|
113
122
|
return isReturnTypeString ? fullText : /*#__PURE__*/(0, _jsxRuntime.jsx)(_styles.Container, _objectSpread(_objectSpread({}, qa), {}, {
|
|
114
123
|
children: fullText
|
|
115
124
|
}));
|
|
@@ -3,6 +3,25 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
exports.MEMO_CACHE_SIZE = exports.COMPARE_OBJECTS = void 0;
|
|
6
|
+
exports.UNITS = exports.ORDERED_UNITS = exports.MILLISECONDS_IN = exports.MEMO_CACHE_SIZE = exports.DEFAULT_MILLISECONDS = exports.DEFAULT_LOCALE = exports.DEFAULT_DISPLAY_UNITS = exports.DEFAULT_DISPLAY = exports.COMPARE_OBJECTS = void 0;
|
|
7
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
8
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
9
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
10
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
11
|
+
var COMPARE_OBJECTS = exports.COMPARE_OBJECTS = true;
|
|
7
12
|
var MEMO_CACHE_SIZE = exports.MEMO_CACHE_SIZE = 10;
|
|
8
|
-
var
|
|
13
|
+
var UNITS = exports.UNITS = {
|
|
14
|
+
days: "days",
|
|
15
|
+
hours: "hours",
|
|
16
|
+
minutes: "minutes",
|
|
17
|
+
seconds: "seconds",
|
|
18
|
+
milliseconds: "milliseconds"
|
|
19
|
+
};
|
|
20
|
+
var MILLISECONDS_IN = exports.MILLISECONDS_IN = _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, UNITS.days, 1 * 1000 * 60 * 60 * 24), UNITS.hours, 1 * 1000 * 60 * 60), UNITS.minutes, 1 * 1000 * 60), UNITS.seconds, 1 * 1000), UNITS.milliseconds, 1);
|
|
21
|
+
var ORDERED_UNITS = exports.ORDERED_UNITS = [UNITS.days, UNITS.hours, UNITS.minutes, UNITS.seconds, UNITS.milliseconds];
|
|
22
|
+
|
|
23
|
+
// Duration props defaults
|
|
24
|
+
var DEFAULT_DISPLAY = exports.DEFAULT_DISPLAY = "narrow";
|
|
25
|
+
var DEFAULT_DISPLAY_UNITS = exports.DEFAULT_DISPLAY_UNITS = _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, UNITS.days, true), UNITS.hours, true), UNITS.minutes, true), UNITS.seconds, true), UNITS.milliseconds, false);
|
|
26
|
+
var DEFAULT_LOCALE = exports.DEFAULT_LOCALE = "en-US";
|
|
27
|
+
var DEFAULT_MILLISECONDS = exports.DEFAULT_MILLISECONDS = null;
|
|
@@ -6,7 +6,8 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
6
6
|
});
|
|
7
7
|
var _exportNames = {
|
|
8
8
|
Duration: true,
|
|
9
|
-
formatDuration: true
|
|
9
|
+
formatDuration: true,
|
|
10
|
+
getDurationMaxDisplayUnits: true
|
|
10
11
|
};
|
|
11
12
|
Object.defineProperty(exports, "Duration", {
|
|
12
13
|
enumerable: true,
|
|
@@ -21,7 +22,14 @@ Object.defineProperty(exports, "formatDuration", {
|
|
|
21
22
|
return _Duration.formatDuration;
|
|
22
23
|
}
|
|
23
24
|
});
|
|
25
|
+
Object.defineProperty(exports, "getDurationMaxDisplayUnits", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function get() {
|
|
28
|
+
return _utils.getDurationMaxDisplayUnits;
|
|
29
|
+
}
|
|
30
|
+
});
|
|
24
31
|
var _Duration = _interopRequireWildcard(require("./Duration"));
|
|
32
|
+
var _utils = require("./utils");
|
|
25
33
|
var _DurationTypes = require("./DurationTypes");
|
|
26
34
|
Object.keys(_DurationTypes).forEach(function (key) {
|
|
27
35
|
if (key === "default" || key === "__esModule") return;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.isValidNumber = exports.getDurationMaxDisplayUnits = void 0;
|
|
7
|
+
var _constants = require("./constants");
|
|
8
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
9
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
10
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
11
|
+
var isValidNumber = exports.isValidNumber = function isValidNumber(value) {
|
|
12
|
+
return typeof value === "number" && isFinite(value);
|
|
13
|
+
};
|
|
14
|
+
var getDurationMaxDisplayUnits = exports.getDurationMaxDisplayUnits = function getDurationMaxDisplayUnits(_ref) {
|
|
15
|
+
var milliseconds = _ref.milliseconds,
|
|
16
|
+
_ref$maxDisplayUnits = _ref.maxDisplayUnits,
|
|
17
|
+
maxDisplayUnits = _ref$maxDisplayUnits === void 0 ? _constants.ORDERED_UNITS.length : _ref$maxDisplayUnits;
|
|
18
|
+
var displayUnits = {};
|
|
19
|
+
if (!isValidNumber(milliseconds)) {
|
|
20
|
+
return displayUnits;
|
|
21
|
+
}
|
|
22
|
+
var _iterator = _createForOfIteratorHelper(_constants.ORDERED_UNITS),
|
|
23
|
+
_step;
|
|
24
|
+
try {
|
|
25
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
26
|
+
var unit = _step.value;
|
|
27
|
+
if (Object.keys(displayUnits).length >= maxDisplayUnits) {
|
|
28
|
+
break;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// @ts-expect-error - stupid typescript isn't smart enough to check the isValidNumber check above ¯\_(ツ)_/¯
|
|
32
|
+
if (milliseconds > _constants.MILLISECONDS_IN[unit]) {
|
|
33
|
+
displayUnits[unit] = true;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
} catch (err) {
|
|
37
|
+
_iterator.e(err);
|
|
38
|
+
} finally {
|
|
39
|
+
_iterator.f();
|
|
40
|
+
}
|
|
41
|
+
return displayUnits;
|
|
42
|
+
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Duration.d.ts","sourceRoot":"","sources":["../../../src/Duration/Duration.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"Duration.d.ts","sourceRoot":"","sources":["../../../src/Duration/Duration.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,KAAK,MAAM,OAAO,CAAC;AAiB/B,OAAO,KAAK,EACV,iBAAiB,EAIlB,MAAM,iBAAiB,CAAC;AAmJzB,eAAO,MAAM,cAAc,UAAW,iBAAiB,KAAG,MAEzD,CAAC;AAEF,QAAA,MAAM,QAAQ,UAAW,iBAAiB,oBAEzC,CAAC;AAEF,eAAe,QAAQ,CAAC"}
|
|
@@ -1,12 +1,24 @@
|
|
|
1
1
|
import { TypeTextProps } from "@sproutsocial/seeds-react-text";
|
|
2
2
|
import { TypeQaProps } from "../types/shared";
|
|
3
|
+
export type TypeDurationMilliseconds = number | null;
|
|
4
|
+
export type TypeDurationLocale = Intl.LocalesArgument;
|
|
5
|
+
export type TypeDurationDisplay = "long" | "narrow";
|
|
6
|
+
export interface TypeDurationDisplayUnits {
|
|
7
|
+
days?: boolean;
|
|
8
|
+
hours?: boolean;
|
|
9
|
+
minutes?: boolean;
|
|
10
|
+
seconds?: boolean;
|
|
11
|
+
milliseconds?: boolean;
|
|
12
|
+
}
|
|
3
13
|
export interface TypeDurationProps extends Omit<TypeTextProps, "children"> {
|
|
4
|
-
/** The
|
|
5
|
-
milliseconds:
|
|
14
|
+
/** The milliseconds to be formatted */
|
|
15
|
+
milliseconds: TypeDurationMilliseconds;
|
|
6
16
|
/** Locale to format. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument */
|
|
7
|
-
locale?:
|
|
17
|
+
locale?: TypeDurationLocale;
|
|
8
18
|
/** The style of the formatted duration */
|
|
9
|
-
display?:
|
|
19
|
+
display?: TypeDurationDisplay;
|
|
20
|
+
/** The units of the duration to render */
|
|
21
|
+
displayUnits?: TypeDurationDisplayUnits;
|
|
10
22
|
/** Text to be read off by screen readers for invalid values (i.e., any value rendered as '—' (em dash)) */
|
|
11
23
|
invalidMillisecondsLabel?: string;
|
|
12
24
|
qa?: TypeQaProps;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"DurationTypes.d.ts","sourceRoot":"","sources":["../../../src/Duration/DurationTypes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,gCAAgC,CAAC;AAC/D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,MAAM,WAAW,iBAAkB,SAAQ,IAAI,CAAC,aAAa,EAAE,UAAU,CAAC;IACxE,
|
|
1
|
+
{"version":3,"file":"DurationTypes.d.ts","sourceRoot":"","sources":["../../../src/Duration/DurationTypes.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,gCAAgC,CAAC;AAC/D,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAEhD,MAAM,MAAM,wBAAwB,GAAG,MAAM,GAAG,IAAI,CAAC;AAErD,MAAM,MAAM,kBAAkB,GAAG,IAAI,CAAC,eAAe,CAAC;AAEtD,MAAM,MAAM,mBAAmB,GAAG,MAAM,GAAG,QAAQ,CAAC;AAEpD,MAAM,WAAW,wBAAwB;IACvC,IAAI,CAAC,EAAE,OAAO,CAAC;IACf,KAAK,CAAC,EAAE,OAAO,CAAC;IAChB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,OAAO,CAAC,EAAE,OAAO,CAAC;IAClB,YAAY,CAAC,EAAE,OAAO,CAAC;CACxB;AAED,MAAM,WAAW,iBAAkB,SAAQ,IAAI,CAAC,aAAa,EAAE,UAAU,CAAC;IACxE,uCAAuC;IACvC,YAAY,EAAE,wBAAwB,CAAC;IAEvC,mIAAmI;IACnI,MAAM,CAAC,EAAE,kBAAkB,CAAC;IAE5B,0CAA0C;IAC1C,OAAO,CAAC,EAAE,mBAAmB,CAAC;IAE9B,0CAA0C;IAC1C,YAAY,CAAC,EAAE,wBAAwB,CAAC;IAExC,2GAA2G;IAC3G,wBAAwB,CAAC,EAAE,MAAM,CAAC;IAElC,EAAE,CAAC,EAAE,WAAW,CAAC;CAClB"}
|
|
@@ -1,3 +1,20 @@
|
|
|
1
|
-
export declare const MEMO_CACHE_SIZE = 10;
|
|
2
1
|
export declare const COMPARE_OBJECTS = true;
|
|
2
|
+
export declare const MEMO_CACHE_SIZE = 10;
|
|
3
|
+
export declare const UNITS: {
|
|
4
|
+
days: string;
|
|
5
|
+
hours: string;
|
|
6
|
+
minutes: string;
|
|
7
|
+
seconds: string;
|
|
8
|
+
milliseconds: string;
|
|
9
|
+
};
|
|
10
|
+
export declare const MILLISECONDS_IN: {
|
|
11
|
+
[x: string]: number;
|
|
12
|
+
};
|
|
13
|
+
export declare const ORDERED_UNITS: string[];
|
|
14
|
+
export declare const DEFAULT_DISPLAY = "narrow";
|
|
15
|
+
export declare const DEFAULT_DISPLAY_UNITS: {
|
|
16
|
+
[x: string]: boolean;
|
|
17
|
+
};
|
|
18
|
+
export declare const DEFAULT_LOCALE = "en-US";
|
|
19
|
+
export declare const DEFAULT_MILLISECONDS: null;
|
|
3
20
|
//# sourceMappingURL=constants.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../src/Duration/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,eAAe,KAAK,CAAC;AAClC,eAAO,MAAM,eAAe,
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../../src/Duration/constants.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,eAAe,OAAO,CAAC;AACpC,eAAO,MAAM,eAAe,KAAK,CAAC;AAClC,eAAO,MAAM,KAAK;;;;;;CAMjB,CAAC;AACF,eAAO,MAAM,eAAe;;CAM3B,CAAC;AACF,eAAO,MAAM,aAAa,UAMzB,CAAC;AAGF,eAAO,MAAM,eAAe,WAAW,CAAC;AACxC,eAAO,MAAM,qBAAqB;;CAMjC,CAAC;AACF,eAAO,MAAM,cAAc,UAAU,CAAC;AACtC,eAAO,MAAM,oBAAoB,MAAO,CAAC"}
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import Duration, { formatDuration } from "./Duration";
|
|
2
|
+
import { getDurationMaxDisplayUnits, type TypeGetDurationMaxDisplayUnitsProps } from "./utils";
|
|
2
3
|
export default Duration;
|
|
3
4
|
export { Duration };
|
|
4
5
|
export { formatDuration };
|
|
6
|
+
export { getDurationMaxDisplayUnits };
|
|
7
|
+
export type { TypeGetDurationMaxDisplayUnitsProps };
|
|
5
8
|
export * from "./DurationTypes";
|
|
6
9
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/Duration/index.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,EAAE,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/Duration/index.ts"],"names":[],"mappings":"AAAA,OAAO,QAAQ,EAAE,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AACtD,OAAO,EACL,0BAA0B,EAC1B,KAAK,mCAAmC,EACzC,MAAM,SAAS,CAAC;AAEjB,eAAe,QAAQ,CAAC;AACxB,OAAO,EAAE,QAAQ,EAAE,CAAC;AACpB,OAAO,EAAE,cAAc,EAAE,CAAC;AAC1B,OAAO,EAAE,0BAA0B,EAAE,CAAC;AACtC,YAAY,EAAE,mCAAmC,EAAE,CAAC;AACpD,cAAc,iBAAiB,CAAC"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { TypeDurationDisplayUnits, TypeDurationMilliseconds } from "./DurationTypes";
|
|
2
|
+
export declare const isValidNumber: (value: unknown) => boolean;
|
|
3
|
+
export interface TypeGetDurationMaxDisplayUnitsProps {
|
|
4
|
+
milliseconds: TypeDurationMilliseconds;
|
|
5
|
+
maxDisplayUnits: number;
|
|
6
|
+
}
|
|
7
|
+
export declare const getDurationMaxDisplayUnits: ({ milliseconds, maxDisplayUnits, }: TypeGetDurationMaxDisplayUnitsProps) => TypeDurationDisplayUnits;
|
|
8
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../../src/Duration/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,wBAAwB,EACxB,wBAAwB,EACzB,MAAM,iBAAiB,CAAC;AAGzB,eAAO,MAAM,aAAa,UAAW,OAAO,KAAG,OACD,CAAC;AAE/C,MAAM,WAAW,mCAAmC;IAClD,YAAY,EAAE,wBAAwB,CAAC;IACvC,eAAe,EAAE,MAAM,CAAC;CACzB;AAED,eAAO,MAAM,0BAA0B,uCAGpC,mCAAmC,KAAG,wBAmBxC,CAAC"}
|
package/lib/Duration/Duration.js
CHANGED
|
@@ -4,25 +4,44 @@ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t =
|
|
|
4
4
|
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
5
5
|
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
6
6
|
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
7
|
-
function
|
|
8
|
-
function
|
|
7
|
+
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
|
8
|
+
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
|
9
9
|
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
10
|
+
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
|
11
|
+
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
|
10
12
|
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
11
|
-
function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
|
|
12
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
|
13
13
|
import * as React from "react";
|
|
14
14
|
import memoize from "lru-memoize";
|
|
15
15
|
import { EM_DASH } from "../utils/constants";
|
|
16
16
|
import { VisuallyHidden } from "../VisuallyHidden";
|
|
17
|
-
import { MEMO_CACHE_SIZE,
|
|
17
|
+
import { COMPARE_OBJECTS, DEFAULT_DISPLAY, DEFAULT_DISPLAY_UNITS, DEFAULT_LOCALE, DEFAULT_MILLISECONDS, MEMO_CACHE_SIZE, MILLISECONDS_IN, ORDERED_UNITS, UNITS } from "./constants";
|
|
18
18
|
import { Container } from "./styles";
|
|
19
|
+
import { isValidNumber } from "./utils";
|
|
19
20
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
20
21
|
import { Fragment as _Fragment } from "react/jsx-runtime";
|
|
21
22
|
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
22
|
-
var
|
|
23
|
-
return
|
|
23
|
+
var getLowestUnit = function getLowestUnit(displayUnits) {
|
|
24
|
+
return _toConsumableArray(ORDERED_UNITS).reverse().find(function (unit) {
|
|
25
|
+
return displayUnits[unit];
|
|
26
|
+
}) || UNITS.milliseconds;
|
|
24
27
|
};
|
|
25
|
-
var
|
|
28
|
+
var splitMillisecondsIntoUnits = function splitMillisecondsIntoUnits(milliseconds, displayUnits) {
|
|
29
|
+
var lowestUnit = getLowestUnit(displayUnits);
|
|
30
|
+
var remainder = milliseconds % MILLISECONDS_IN[lowestUnit];
|
|
31
|
+
if (2 * remainder >= MILLISECONDS_IN[lowestUnit]) {
|
|
32
|
+
// if the remainder is large, add enough seconds to increse the lowest unit
|
|
33
|
+
milliseconds += MILLISECONDS_IN[lowestUnit] - remainder;
|
|
34
|
+
}
|
|
35
|
+
var units = {};
|
|
36
|
+
ORDERED_UNITS.forEach(function (unit) {
|
|
37
|
+
if (displayUnits[unit]) {
|
|
38
|
+
units[unit] = Math.floor(milliseconds / MILLISECONDS_IN[unit]);
|
|
39
|
+
milliseconds -= units[unit] * MILLISECONDS_IN[unit];
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
return units;
|
|
43
|
+
};
|
|
44
|
+
var _createDurationFormatter = function _createDurationFormatter(locale, unitDisplay, displayUnits) {
|
|
26
45
|
var timeUnitFormatter = function timeUnitFormatter(locale, unit, unitDisplay) {
|
|
27
46
|
return Intl.NumberFormat(locale, {
|
|
28
47
|
style: "unit",
|
|
@@ -30,41 +49,31 @@ var _createDurationFormatter = function _createDurationFormatter(locale, unitDis
|
|
|
30
49
|
unitDisplay: unitDisplay
|
|
31
50
|
}).format;
|
|
32
51
|
};
|
|
33
|
-
var
|
|
34
|
-
var formatHours = timeUnitFormatter(locale, "hour", unitDisplay);
|
|
35
|
-
var formatMinutes = timeUnitFormatter(locale, "minute", unitDisplay);
|
|
36
|
-
var formatSeconds = timeUnitFormatter(locale, "second", unitDisplay);
|
|
37
|
-
var formatMilliseconds = timeUnitFormatter(locale, "millisecond", unitDisplay);
|
|
52
|
+
var formatterByUnit = _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, UNITS.days, timeUnitFormatter(locale, "day", unitDisplay)), UNITS.hours, timeUnitFormatter(locale, "hour", unitDisplay)), UNITS.minutes, timeUnitFormatter(locale, "minute", unitDisplay)), UNITS.seconds, timeUnitFormatter(locale, "second", unitDisplay)), UNITS.milliseconds, timeUnitFormatter(locale, "millisecond", unitDisplay));
|
|
38
53
|
var formatList = new Intl.ListFormat(locale, {
|
|
39
54
|
style: "narrow",
|
|
40
55
|
type: "unit"
|
|
41
56
|
});
|
|
42
|
-
return function (
|
|
43
|
-
|
|
44
|
-
|
|
57
|
+
return function (value) {
|
|
58
|
+
var lowestUnit = getLowestUnit(displayUnits);
|
|
59
|
+
|
|
60
|
+
// if the value is zero or negative, we just want to return 0 for the lowest unit (ex "0 minutes")
|
|
61
|
+
if (value <= 0) {
|
|
62
|
+
return formatterByUnit[lowestUnit](0);
|
|
45
63
|
}
|
|
46
|
-
var
|
|
47
|
-
var
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
var _getDivisionAndRemain6 = _slicedToArray(_getDivisionAndRemain5, 2);
|
|
60
|
-
minutes = _getDivisionAndRemain6[0];
|
|
61
|
-
milliseconds = _getDivisionAndRemain6[1];
|
|
62
|
-
var _getDivisionAndRemain7 = getDivisionAndRemainder(milliseconds, 1000);
|
|
63
|
-
var _getDivisionAndRemain8 = _slicedToArray(_getDivisionAndRemain7, 2);
|
|
64
|
-
seconds = _getDivisionAndRemain8[0];
|
|
65
|
-
milliseconds = _getDivisionAndRemain8[1];
|
|
66
|
-
var list = [days ? formatDays(days) : null, hours ? formatHours(hours) : null, minutes ? formatMinutes(minutes) : null, seconds ? formatSeconds(seconds) : null, milliseconds ? formatMilliseconds(milliseconds) : null].filter(function (listItem) {
|
|
67
|
-
return listItem !== null;
|
|
64
|
+
var millisecondsByUnit = splitMillisecondsIntoUnits(value, displayUnits);
|
|
65
|
+
var list = [];
|
|
66
|
+
ORDERED_UNITS.forEach(function (unit) {
|
|
67
|
+
if (unit in millisecondsByUnit) {
|
|
68
|
+
var unitValue = millisecondsByUnit[unit];
|
|
69
|
+
|
|
70
|
+
// we want to add to the list if one of two conditions are met:
|
|
71
|
+
// 1) the unit has a value greater than 0 OR
|
|
72
|
+
// 2) the unit has value 0 AND the unit is the lowest unit AND the list is already empty
|
|
73
|
+
if (unitValue !== 0 || unitValue === 0 && unit === lowestUnit && list.length === 0) {
|
|
74
|
+
list.push(formatterByUnit[unit](millisecondsByUnit[unit]));
|
|
75
|
+
}
|
|
76
|
+
}
|
|
68
77
|
});
|
|
69
78
|
return formatList.format(list);
|
|
70
79
|
};
|
|
@@ -73,19 +82,18 @@ var _createDurationFormatter = function _createDurationFormatter(locale, unitDis
|
|
|
73
82
|
// Memoize to reduce the energy of creating new instances of Intl.NumberFormat
|
|
74
83
|
var memoizer = memoize(MEMO_CACHE_SIZE, COMPARE_OBJECTS);
|
|
75
84
|
var createDurationFormatter = memoizer(_createDurationFormatter);
|
|
76
|
-
var isValidNumber = function isValidNumber(value) {
|
|
77
|
-
return typeof value === "number" && isFinite(value);
|
|
78
|
-
};
|
|
79
85
|
var getDuration = function getDuration(_ref) {
|
|
80
86
|
var returnType = _ref.returnType,
|
|
81
87
|
props = _ref.props;
|
|
82
88
|
var _props$display = props.display,
|
|
83
|
-
display = _props$display === void 0 ?
|
|
89
|
+
display = _props$display === void 0 ? DEFAULT_DISPLAY : _props$display,
|
|
90
|
+
_props$displayUnits = props.displayUnits,
|
|
91
|
+
displayUnits = _props$displayUnits === void 0 ? DEFAULT_DISPLAY_UNITS : _props$displayUnits,
|
|
84
92
|
invalidMillisecondsLabel = props.invalidMillisecondsLabel,
|
|
85
93
|
_props$locale = props.locale,
|
|
86
|
-
locale = _props$locale === void 0 ?
|
|
94
|
+
locale = _props$locale === void 0 ? DEFAULT_LOCALE : _props$locale,
|
|
87
95
|
_props$milliseconds = props.milliseconds,
|
|
88
|
-
milliseconds = _props$milliseconds === void 0 ?
|
|
96
|
+
milliseconds = _props$milliseconds === void 0 ? DEFAULT_MILLISECONDS : _props$milliseconds,
|
|
89
97
|
qa = props.qa;
|
|
90
98
|
var isReturnTypeString = returnType === "string";
|
|
91
99
|
if (!isValidNumber(milliseconds)) {
|
|
@@ -102,7 +110,8 @@ var getDuration = function getDuration(_ref) {
|
|
|
102
110
|
}))]
|
|
103
111
|
});
|
|
104
112
|
}
|
|
105
|
-
var
|
|
113
|
+
var validatedDisplayUnits = Object.keys(displayUnits).length === 0 ? DEFAULT_DISPLAY_UNITS : displayUnits;
|
|
114
|
+
var fullText = createDurationFormatter(locale, display, validatedDisplayUnits)(milliseconds);
|
|
106
115
|
return isReturnTypeString ? fullText : /*#__PURE__*/_jsx(Container, _objectSpread(_objectSpread({}, qa), {}, {
|
|
107
116
|
children: fullText
|
|
108
117
|
}));
|
|
@@ -1,2 +1,21 @@
|
|
|
1
|
+
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
|
|
2
|
+
function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
3
|
+
function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
|
|
4
|
+
function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); }
|
|
5
|
+
export var COMPARE_OBJECTS = true;
|
|
1
6
|
export var MEMO_CACHE_SIZE = 10;
|
|
2
|
-
export var
|
|
7
|
+
export var UNITS = {
|
|
8
|
+
days: "days",
|
|
9
|
+
hours: "hours",
|
|
10
|
+
minutes: "minutes",
|
|
11
|
+
seconds: "seconds",
|
|
12
|
+
milliseconds: "milliseconds"
|
|
13
|
+
};
|
|
14
|
+
export var MILLISECONDS_IN = _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, UNITS.days, 1 * 1000 * 60 * 60 * 24), UNITS.hours, 1 * 1000 * 60 * 60), UNITS.minutes, 1 * 1000 * 60), UNITS.seconds, 1 * 1000), UNITS.milliseconds, 1);
|
|
15
|
+
export var ORDERED_UNITS = [UNITS.days, UNITS.hours, UNITS.minutes, UNITS.seconds, UNITS.milliseconds];
|
|
16
|
+
|
|
17
|
+
// Duration props defaults
|
|
18
|
+
export var DEFAULT_DISPLAY = "narrow";
|
|
19
|
+
export var DEFAULT_DISPLAY_UNITS = _defineProperty(_defineProperty(_defineProperty(_defineProperty(_defineProperty({}, UNITS.days, true), UNITS.hours, true), UNITS.minutes, true), UNITS.seconds, true), UNITS.milliseconds, false);
|
|
20
|
+
export var DEFAULT_LOCALE = "en-US";
|
|
21
|
+
export var DEFAULT_MILLISECONDS = null;
|
package/lib/Duration/index.js
CHANGED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it.return != null) it.return(); } finally { if (didErr) throw err; } } }; }
|
|
2
|
+
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
|
3
|
+
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
|
4
|
+
import { MILLISECONDS_IN, ORDERED_UNITS } from "./constants";
|
|
5
|
+
export var isValidNumber = function isValidNumber(value) {
|
|
6
|
+
return typeof value === "number" && isFinite(value);
|
|
7
|
+
};
|
|
8
|
+
export var getDurationMaxDisplayUnits = function getDurationMaxDisplayUnits(_ref) {
|
|
9
|
+
var milliseconds = _ref.milliseconds,
|
|
10
|
+
_ref$maxDisplayUnits = _ref.maxDisplayUnits,
|
|
11
|
+
maxDisplayUnits = _ref$maxDisplayUnits === void 0 ? ORDERED_UNITS.length : _ref$maxDisplayUnits;
|
|
12
|
+
var displayUnits = {};
|
|
13
|
+
if (!isValidNumber(milliseconds)) {
|
|
14
|
+
return displayUnits;
|
|
15
|
+
}
|
|
16
|
+
var _iterator = _createForOfIteratorHelper(ORDERED_UNITS),
|
|
17
|
+
_step;
|
|
18
|
+
try {
|
|
19
|
+
for (_iterator.s(); !(_step = _iterator.n()).done;) {
|
|
20
|
+
var unit = _step.value;
|
|
21
|
+
if (Object.keys(displayUnits).length >= maxDisplayUnits) {
|
|
22
|
+
break;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// @ts-expect-error - stupid typescript isn't smart enough to check the isValidNumber check above ¯\_(ツ)_/¯
|
|
26
|
+
if (milliseconds > MILLISECONDS_IN[unit]) {
|
|
27
|
+
displayUnits[unit] = true;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
} catch (err) {
|
|
31
|
+
_iterator.e(err);
|
|
32
|
+
} finally {
|
|
33
|
+
_iterator.f();
|
|
34
|
+
}
|
|
35
|
+
return displayUnits;
|
|
36
|
+
};
|