UNPKG

@enact/sandstone

Version:

Large-screen/TV support library for Enact, containing a variety of UI components.

83 lines (78 loc) 2.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.secondsToTime = exports.secondsToPeriod = exports.parseTime = exports.countReactChildren = void 0; var _react = require("react"); // MediaPlayer utils.js // /** * Create a time object (hour, minute, second) from an amount of seconds. * * @param {Number|String} value A duration of time represented in seconds * * @returns {Object} An object with keys {hour, minute, second} representing the duration * seconds provided as an argument. * @private */ var parseTime = exports.parseTime = function parseTime(value) { value = parseFloat(value); var time = {}; var hour = Math.floor(value / (60 * 60)); time.minute = Math.floor(value / 60 % 60); time.second = Math.floor(value % 60); if (hour) { time.hour = hour; } return time; }; /** * Generate a time usable by <time datetime />. * * @param {Number|String} seconds A duration of time represented in seconds * * @returns {String} String formatted for use in a `datetime` field of a `<time>` tag. * @private */ var secondsToPeriod = exports.secondsToPeriod = function secondsToPeriod(seconds) { return 'P' + seconds + 'S'; }; /** * Formats a duration in seconds into a human-readable time. * * @type {Function} * @param {Number|String} seconds A duration of time represented in seconds * @param {DurationFmt} durfmt An instance of a `ilib.DurationFmt` object from iLib configured * to display time * @param {Object} config Additional configuration object that includes `includeHour` * * @returns {String} Formatted duration string * @memberof sandstone/MediaPlayer * @public */ var secondsToTime = exports.secondsToTime = function secondsToTime(seconds, durfmt, config) { var includeHour = config && config.includeHour; if (durfmt) { var parsedTime = parseTime(seconds); var timeString = durfmt.format(parsedTime).toString(); if (includeHour && !parsedTime.hour) { return '00:' + timeString; } else { return timeString; } } return includeHour ? '00:00:00' : '00:00'; }; /** * Safely count the children nodes and exclude null & undefined values for an accurate count of * real children * * @param {component} children React.Component or React.PureComponent * @returns {Number} Number of children nodes * @private */ var countReactChildren = exports.countReactChildren = function countReactChildren(children) { return _react.Children.toArray(children).filter(function (n) { return n != null; }).length; };