@sproutsocial/racine 24.0.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.
Files changed (34) hide show
  1. package/CHANGELOG.md +23 -0
  2. package/commonjs/Duration/Duration.js +138 -0
  3. package/commonjs/Duration/DurationTypes.js +5 -0
  4. package/commonjs/Duration/constants.js +27 -0
  5. package/commonjs/Duration/index.js +47 -0
  6. package/commonjs/Duration/styles.js +13 -0
  7. package/commonjs/Duration/utils.js +42 -0
  8. package/commonjs/index.js +12 -0
  9. package/dist/types/Duration/Duration.d.ts +6 -0
  10. package/dist/types/Duration/Duration.d.ts.map +1 -0
  11. package/dist/types/Duration/DurationTypes.d.ts +26 -0
  12. package/dist/types/Duration/DurationTypes.d.ts.map +1 -0
  13. package/dist/types/Duration/__tests__/Duration.typetest.d.ts +2 -0
  14. package/dist/types/Duration/__tests__/Duration.typetest.d.ts.map +1 -0
  15. package/dist/types/Duration/__tests__/features/testDuration.d.ts +3 -0
  16. package/dist/types/Duration/__tests__/features/testDuration.d.ts.map +1 -0
  17. package/dist/types/Duration/constants.d.ts +20 -0
  18. package/dist/types/Duration/constants.d.ts.map +1 -0
  19. package/dist/types/Duration/index.d.ts +9 -0
  20. package/dist/types/Duration/index.d.ts.map +1 -0
  21. package/dist/types/Duration/styles.d.ts +11 -0
  22. package/dist/types/Duration/styles.d.ts.map +1 -0
  23. package/dist/types/Duration/utils.d.ts +8 -0
  24. package/dist/types/Duration/utils.d.ts.map +1 -0
  25. package/dist/types/index.d.ts +1 -0
  26. package/dist/types/index.d.ts.map +1 -1
  27. package/lib/Duration/Duration.js +131 -0
  28. package/lib/Duration/DurationTypes.js +1 -0
  29. package/lib/Duration/constants.js +21 -0
  30. package/lib/Duration/index.js +7 -0
  31. package/lib/Duration/styles.js +6 -0
  32. package/lib/Duration/utils.js +36 -0
  33. package/lib/index.js +1 -0
  34. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
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
+
11
+ ## 24.1.0
12
+
13
+ ### Minor Changes
14
+
15
+ - 25d94a2: Racine now includes a new Duration component. It is a primitive component used to format durations of time according to our date & time formatting guidelines Additionally, the package includes a formatDuration helper, which can be used to get a stringified version of a duration.
16
+ - bd3fc39: ### Deprecation Notice: Flow Type System
17
+
18
+ We are officially deprecating the use of the Flow type system in `@sproutsocial/racine`. This change affects all Flow types used in this library. Flow support will be discontinued entirely on **October 24th**.
19
+
20
+ To ensure a smooth transition, please migrate your projects away from Flow types and start using TypeScript as soon as possible.
21
+
22
+ If you have any questions or need assistance, feel free to reach out to the team.
23
+
24
+ Thank you for your understanding and cooperation as we work towards improving our codebase.
25
+
3
26
  ## 24.0.0
4
27
 
5
28
  ### Major Changes
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+
3
+ 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); }
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ exports.formatDuration = exports.default = void 0;
8
+ var React = _interopRequireWildcard(require("react"));
9
+ var _lruMemoize = _interopRequireDefault(require("lru-memoize"));
10
+ var _constants = require("../utils/constants");
11
+ var _VisuallyHidden = require("../VisuallyHidden");
12
+ var _constants2 = require("./constants");
13
+ var _styles = require("./styles");
14
+ var _utils = require("./utils");
15
+ var _jsxRuntime = require("react/jsx-runtime");
16
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
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); }
18
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
19
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
20
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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; }
22
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
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); }
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."); }
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); }
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; }
30
+ var getLowestUnit = function getLowestUnit(displayUnits) {
31
+ return _toConsumableArray(_constants2.ORDERED_UNITS).reverse().find(function (unit) {
32
+ return displayUnits[unit];
33
+ }) || _constants2.UNITS.milliseconds;
34
+ };
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) {
52
+ var timeUnitFormatter = function timeUnitFormatter(locale, unit, unitDisplay) {
53
+ return Intl.NumberFormat(locale, {
54
+ style: "unit",
55
+ unit: unit,
56
+ unitDisplay: unitDisplay
57
+ }).format;
58
+ };
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));
60
+ var formatList = new Intl.ListFormat(locale, {
61
+ style: "narrow",
62
+ type: "unit"
63
+ });
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);
70
+ }
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
+ }
84
+ });
85
+ return formatList.format(list);
86
+ };
87
+ };
88
+
89
+ // Memoize to reduce the energy of creating new instances of Intl.NumberFormat
90
+ var memoizer = (0, _lruMemoize.default)(_constants2.MEMO_CACHE_SIZE, _constants2.COMPARE_OBJECTS);
91
+ var createDurationFormatter = memoizer(_createDurationFormatter);
92
+ var getDuration = function getDuration(_ref) {
93
+ var returnType = _ref.returnType,
94
+ props = _ref.props;
95
+ var _props$display = props.display,
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,
99
+ invalidMillisecondsLabel = props.invalidMillisecondsLabel,
100
+ _props$locale = props.locale,
101
+ locale = _props$locale === void 0 ? _constants2.DEFAULT_LOCALE : _props$locale,
102
+ _props$milliseconds = props.milliseconds,
103
+ milliseconds = _props$milliseconds === void 0 ? _constants2.DEFAULT_MILLISECONDS : _props$milliseconds,
104
+ qa = props.qa;
105
+ var isReturnTypeString = returnType === "string";
106
+ if (!(0, _utils.isValidNumber)(milliseconds)) {
107
+ return isReturnTypeString ? _constants.EM_DASH : /*#__PURE__*/(0, _jsxRuntime.jsxs)(_jsxRuntime.Fragment, {
108
+ children: [invalidMillisecondsLabel ?
109
+ /*#__PURE__*/
110
+ // Give screen readers something useful to read off + hide the em dash
111
+ (0, _jsxRuntime.jsx)(_VisuallyHidden.VisuallyHidden, {
112
+ children: invalidMillisecondsLabel
113
+ }) : null, /*#__PURE__*/(0, _jsxRuntime.jsx)(_styles.Container, _objectSpread(_objectSpread({
114
+ "aria-hidden": true
115
+ }, qa), {}, {
116
+ children: _constants.EM_DASH
117
+ }))]
118
+ });
119
+ }
120
+ var validatedDisplayUnits = Object.keys(displayUnits).length === 0 ? _constants2.DEFAULT_DISPLAY_UNITS : displayUnits;
121
+ var fullText = createDurationFormatter(locale, display, validatedDisplayUnits)(milliseconds);
122
+ return isReturnTypeString ? fullText : /*#__PURE__*/(0, _jsxRuntime.jsx)(_styles.Container, _objectSpread(_objectSpread({}, qa), {}, {
123
+ children: fullText
124
+ }));
125
+ };
126
+ var formatDuration = exports.formatDuration = function formatDuration(props) {
127
+ return getDuration({
128
+ returnType: "string",
129
+ props: props
130
+ });
131
+ };
132
+ var Duration = function Duration(props) {
133
+ return getDuration({
134
+ returnType: "component",
135
+ props: props
136
+ });
137
+ };
138
+ var _default = exports.default = Duration;
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
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;
12
+ var MEMO_CACHE_SIZE = exports.MEMO_CACHE_SIZE = 10;
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;
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+
3
+ 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); }
4
+ Object.defineProperty(exports, "__esModule", {
5
+ value: true
6
+ });
7
+ var _exportNames = {
8
+ Duration: true,
9
+ formatDuration: true,
10
+ getDurationMaxDisplayUnits: true
11
+ };
12
+ Object.defineProperty(exports, "Duration", {
13
+ enumerable: true,
14
+ get: function get() {
15
+ return _Duration.default;
16
+ }
17
+ });
18
+ exports.default = void 0;
19
+ Object.defineProperty(exports, "formatDuration", {
20
+ enumerable: true,
21
+ get: function get() {
22
+ return _Duration.formatDuration;
23
+ }
24
+ });
25
+ Object.defineProperty(exports, "getDurationMaxDisplayUnits", {
26
+ enumerable: true,
27
+ get: function get() {
28
+ return _utils.getDurationMaxDisplayUnits;
29
+ }
30
+ });
31
+ var _Duration = _interopRequireWildcard(require("./Duration"));
32
+ var _utils = require("./utils");
33
+ var _DurationTypes = require("./DurationTypes");
34
+ Object.keys(_DurationTypes).forEach(function (key) {
35
+ if (key === "default" || key === "__esModule") return;
36
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
37
+ if (key in exports && exports[key] === _DurationTypes[key]) return;
38
+ Object.defineProperty(exports, key, {
39
+ enumerable: true,
40
+ get: function get() {
41
+ return _DurationTypes[key];
42
+ }
43
+ });
44
+ });
45
+ 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); }
46
+ function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != _typeof(e) && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; }
47
+ var _default = exports.default = _Duration.default;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.Container = void 0;
7
+ var _styledComponents = _interopRequireDefault(require("styled-components"));
8
+ var _seedsReactText = _interopRequireDefault(require("@sproutsocial/seeds-react-text"));
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+ var Container = exports.Container = (0, _styledComponents.default)(_seedsReactText.default).withConfig({
11
+ displayName: "styles__Container",
12
+ componentId: "sc-6o8ar7-0"
13
+ })(["font-variant-numeric:tabular-nums;"]);
@@ -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
+ };
package/commonjs/index.js CHANGED
@@ -264,6 +264,18 @@ Object.keys(_Drawer).forEach(function (key) {
264
264
  }
265
265
  });
266
266
  });
267
+ var _Duration = require("./Duration");
268
+ Object.keys(_Duration).forEach(function (key) {
269
+ if (key === "default" || key === "__esModule") return;
270
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
271
+ if (key in exports && exports[key] === _Duration[key]) return;
272
+ Object.defineProperty(exports, key, {
273
+ enumerable: true,
274
+ get: function get() {
275
+ return _Duration[key];
276
+ }
277
+ });
278
+ });
267
279
  var _EmptyState = require("./EmptyState");
268
280
  Object.keys(_EmptyState).forEach(function (key) {
269
281
  if (key === "default" || key === "__esModule") return;
@@ -0,0 +1,6 @@
1
+ import * as React from "react";
2
+ import type { TypeDurationProps } from "./DurationTypes";
3
+ export declare const formatDuration: (props: TypeDurationProps) => string;
4
+ declare const Duration: (props: TypeDurationProps) => React.ReactNode;
5
+ export default Duration;
6
+ //# sourceMappingURL=Duration.d.ts.map
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,26 @@
1
+ import { TypeTextProps } from "@sproutsocial/seeds-react-text";
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
+ }
13
+ export interface TypeDurationProps extends Omit<TypeTextProps, "children"> {
14
+ /** The milliseconds to be formatted */
15
+ milliseconds: TypeDurationMilliseconds;
16
+ /** Locale to format. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument */
17
+ locale?: TypeDurationLocale;
18
+ /** The style of the formatted duration */
19
+ display?: TypeDurationDisplay;
20
+ /** The units of the duration to render */
21
+ displayUnits?: TypeDurationDisplayUnits;
22
+ /** Text to be read off by screen readers for invalid values (i.e., any value rendered as '—' (em dash)) */
23
+ invalidMillisecondsLabel?: string;
24
+ qa?: TypeQaProps;
25
+ }
26
+ //# sourceMappingURL=DurationTypes.d.ts.map
@@ -0,0 +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,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"}
@@ -0,0 +1,2 @@
1
+ export declare const DurationTypeTest: () => import("react/jsx-runtime").JSX.Element;
2
+ //# sourceMappingURL=Duration.typetest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Duration.typetest.d.ts","sourceRoot":"","sources":["../../../../src/Duration/__tests__/Duration.typetest.tsx"],"names":[],"mappings":"AAQA,eAAO,MAAM,gBAAgB,+CAoB5B,CAAC"}
@@ -0,0 +1,3 @@
1
+ declare const testDuration: (milliseconds: any, options: {} | undefined, texts: any) => void;
2
+ export default testDuration;
3
+ //# sourceMappingURL=testDuration.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"testDuration.d.ts","sourceRoot":"","sources":["../../../../../src/Duration/__tests__/features/testDuration.tsx"],"names":[],"mappings":"AAWA,QAAA,MAAM,YAAY,kEAsBjB,CAAC;AAEF,eAAe,YAAY,CAAC"}
@@ -0,0 +1,20 @@
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;
20
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
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"}
@@ -0,0 +1,9 @@
1
+ import Duration, { formatDuration } from "./Duration";
2
+ import { getDurationMaxDisplayUnits, type TypeGetDurationMaxDisplayUnitsProps } from "./utils";
3
+ export default Duration;
4
+ export { Duration };
5
+ export { formatDuration };
6
+ export { getDurationMaxDisplayUnits };
7
+ export type { TypeGetDurationMaxDisplayUnitsProps };
8
+ export * from "./DurationTypes";
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +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;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,11 @@
1
+ export declare const Container: import("styled-components").StyledComponent<{
2
+ ({ fontSize, children, qa, color, ...rest }: import("@sproutsocial/seeds-react-text").TypeTextProps): import("react/jsx-runtime").JSX.Element;
3
+ Headline: import("styled-components").StyledComponent<"span", any, import("@sproutsocial/seeds-react-text").TypeTextProps, never>;
4
+ SubHeadline: import("styled-components").StyledComponent<"span", any, import("@sproutsocial/seeds-react-text").TypeTextProps, never>;
5
+ SmallSubHeadline: import("styled-components").StyledComponent<"span", any, import("@sproutsocial/seeds-react-text").TypeTextProps, never>;
6
+ Byline: import("styled-components").StyledComponent<"span", any, import("@sproutsocial/seeds-react-text").TypeTextProps, never>;
7
+ SmallByline: import("styled-components").StyledComponent<"span", any, import("@sproutsocial/seeds-react-text").TypeTextProps, never>;
8
+ BodyCopy: import("styled-components").StyledComponent<"span", any, import("@sproutsocial/seeds-react-text").TypeTextProps, never>;
9
+ SmallBodyCopy: import("styled-components").StyledComponent<"span", any, import("@sproutsocial/seeds-react-text").TypeTextProps, never>;
10
+ }, import("styled-components").DefaultTheme, {}, never>;
11
+ //# sourceMappingURL=styles.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"styles.d.ts","sourceRoot":"","sources":["../../../src/Duration/styles.ts"],"names":[],"mappings":"AAGA,eAAO,MAAM,SAAS;;;;;;;;;uDAErB,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"}
@@ -21,6 +21,7 @@ export * from "@sproutsocial/seeds-react-checkbox";
21
21
  export * from "./Collapsible";
22
22
  export * from "./DatePicker";
23
23
  export * from "./Drawer";
24
+ export * from "./Duration";
24
25
  export * from "./EmptyState";
25
26
  export * from "./Fieldset";
26
27
  export * from "./FormField";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AACA,YAAY,EAAE,aAAa,EAAE,MAAM,2BAA2B,CAAC;AAC/D,YAAY,EAAE,qBAAqB,EAAE,MAAM,mCAAmC,CAAC;AAC/E,YAAY,EAAE,4BAA4B,IAAI,aAAa,EAAE,MAAM,mCAAmC,CAAC;AAEvG,OAAO,EAAE,iBAAiB,IAAI,qBAAqB,EAAE,MAA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AAc,WAAW,CAAC;AAC1B,cAAc,kBAAkB,CAAC;AAEjC;;GAEG;AACH,OAAO,EAAE,MAAM,IAAI,KAAK,EAAE,MAAM,UAAU,CAAC"}
@@ -0,0 +1,131 @@
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 ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
3
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { _defineProperty(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
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
+ function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); }
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 _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
+ 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); }
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; }
13
+ import * as React from "react";
14
+ import memoize from "lru-memoize";
15
+ import { EM_DASH } from "../utils/constants";
16
+ import { VisuallyHidden } from "../VisuallyHidden";
17
+ import { COMPARE_OBJECTS, DEFAULT_DISPLAY, DEFAULT_DISPLAY_UNITS, DEFAULT_LOCALE, DEFAULT_MILLISECONDS, MEMO_CACHE_SIZE, MILLISECONDS_IN, ORDERED_UNITS, UNITS } from "./constants";
18
+ import { Container } from "./styles";
19
+ import { isValidNumber } from "./utils";
20
+ import { jsx as _jsx } from "react/jsx-runtime";
21
+ import { Fragment as _Fragment } from "react/jsx-runtime";
22
+ import { jsxs as _jsxs } from "react/jsx-runtime";
23
+ var getLowestUnit = function getLowestUnit(displayUnits) {
24
+ return _toConsumableArray(ORDERED_UNITS).reverse().find(function (unit) {
25
+ return displayUnits[unit];
26
+ }) || UNITS.milliseconds;
27
+ };
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) {
45
+ var timeUnitFormatter = function timeUnitFormatter(locale, unit, unitDisplay) {
46
+ return Intl.NumberFormat(locale, {
47
+ style: "unit",
48
+ unit: unit,
49
+ unitDisplay: unitDisplay
50
+ }).format;
51
+ };
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));
53
+ var formatList = new Intl.ListFormat(locale, {
54
+ style: "narrow",
55
+ type: "unit"
56
+ });
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);
63
+ }
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
+ }
77
+ });
78
+ return formatList.format(list);
79
+ };
80
+ };
81
+
82
+ // Memoize to reduce the energy of creating new instances of Intl.NumberFormat
83
+ var memoizer = memoize(MEMO_CACHE_SIZE, COMPARE_OBJECTS);
84
+ var createDurationFormatter = memoizer(_createDurationFormatter);
85
+ var getDuration = function getDuration(_ref) {
86
+ var returnType = _ref.returnType,
87
+ props = _ref.props;
88
+ var _props$display = props.display,
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,
92
+ invalidMillisecondsLabel = props.invalidMillisecondsLabel,
93
+ _props$locale = props.locale,
94
+ locale = _props$locale === void 0 ? DEFAULT_LOCALE : _props$locale,
95
+ _props$milliseconds = props.milliseconds,
96
+ milliseconds = _props$milliseconds === void 0 ? DEFAULT_MILLISECONDS : _props$milliseconds,
97
+ qa = props.qa;
98
+ var isReturnTypeString = returnType === "string";
99
+ if (!isValidNumber(milliseconds)) {
100
+ return isReturnTypeString ? EM_DASH : /*#__PURE__*/_jsxs(_Fragment, {
101
+ children: [invalidMillisecondsLabel ?
102
+ /*#__PURE__*/
103
+ // Give screen readers something useful to read off + hide the em dash
104
+ _jsx(VisuallyHidden, {
105
+ children: invalidMillisecondsLabel
106
+ }) : null, /*#__PURE__*/_jsx(Container, _objectSpread(_objectSpread({
107
+ "aria-hidden": true
108
+ }, qa), {}, {
109
+ children: EM_DASH
110
+ }))]
111
+ });
112
+ }
113
+ var validatedDisplayUnits = Object.keys(displayUnits).length === 0 ? DEFAULT_DISPLAY_UNITS : displayUnits;
114
+ var fullText = createDurationFormatter(locale, display, validatedDisplayUnits)(milliseconds);
115
+ return isReturnTypeString ? fullText : /*#__PURE__*/_jsx(Container, _objectSpread(_objectSpread({}, qa), {}, {
116
+ children: fullText
117
+ }));
118
+ };
119
+ export var formatDuration = function formatDuration(props) {
120
+ return getDuration({
121
+ returnType: "string",
122
+ props: props
123
+ });
124
+ };
125
+ var Duration = function Duration(props) {
126
+ return getDuration({
127
+ returnType: "component",
128
+ props: props
129
+ });
130
+ };
131
+ export default Duration;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +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;
6
+ export var MEMO_CACHE_SIZE = 10;
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;
@@ -0,0 +1,7 @@
1
+ import Duration, { formatDuration } from "./Duration";
2
+ import { getDurationMaxDisplayUnits } from "./utils";
3
+ export default Duration;
4
+ export { Duration };
5
+ export { formatDuration };
6
+ export { getDurationMaxDisplayUnits };
7
+ export * from "./DurationTypes";
@@ -0,0 +1,6 @@
1
+ import styled from "styled-components";
2
+ import Text from "@sproutsocial/seeds-react-text";
3
+ export var Container = styled(Text).withConfig({
4
+ displayName: "styles__Container",
5
+ componentId: "sc-6o8ar7-0"
6
+ })(["font-variant-numeric:tabular-nums;"]);
@@ -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
+ };
package/lib/index.js CHANGED
@@ -26,6 +26,7 @@ export * from "@sproutsocial/seeds-react-checkbox";
26
26
  export * from "./Collapsible";
27
27
  export * from "./DatePicker";
28
28
  export * from "./Drawer";
29
+ export * from "./Duration";
29
30
  export * from "./EmptyState";
30
31
  export * from "./Fieldset";
31
32
  export * from "./FormField";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sproutsocial/racine",
3
- "version": "24.0.0",
3
+ "version": "24.2.0",
4
4
  "license": "MIT",
5
5
  "engines": {
6
6
  "node": ">=18"