UNPKG

dtable-utils

Version:

dtable common utils

63 lines (56 loc) 2.22 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var column = require('../constants/column.js'); require('../constants/filter/filter-column-options.js'); require('../constants/filter/filter-modifier.js'); require('../constants/filter/filter-predicate.js'); require('../constants/filter/filter-is-within.js'); require('../constants/formula.js'); require('../constants/sort.js'); require('../constants/group.js'); var number = require('../number.js'); var formatTextToDuration = function formatTextToDuration(value, data) { var _ref = data || {}, duration_format = _ref.duration_format; duration_format = duration_format || column.DURATION_FORMATS_MAP.H_MM; if (column.DURATION_FORMATS.findIndex(function (format) { return format.type === duration_format; }) < 0) { return null; } if (value === 0) return 0; if (!value) return null; // value is number or string number var numericDuration = value - 0; if (number.isNumber(numericDuration)) return numericDuration; // value is string var stringifyDuration = value + ''; var isNegative = stringifyDuration[0] === '-'; if (isNegative) { stringifyDuration = stringifyDuration.substring(1); } var timeParts = stringifyDuration.split(/[::]/); // support ':' and ':'. var timePartsLen = timeParts.length; if (timePartsLen === 0) return ''; var hoursIndex; var minutesIndex; var secondsIndex; if (duration_format === column.DURATION_FORMATS_MAP.H_MM) { hoursIndex = timePartsLen - 2; minutesIndex = timePartsLen - 1; } else { hoursIndex = timePartsLen - 3; minutesIndex = timePartsLen - 2; secondsIndex = timePartsLen - 1; } var hours = timeParts[hoursIndex] - 0; var minutes = timeParts[minutesIndex] - 0; var seconds = timeParts[secondsIndex] - 0; if (!number.isNumber(hours) && !number.isNumber(minutes) && !number.isNumber(seconds)) return null; hours = number.isNumber(hours) ? hours : 0; minutes = number.isNumber(minutes) ? minutes : 0; seconds = number.isNumber(seconds) ? seconds : 0; var result = hours * 3600 + minutes * 60 + seconds; return isNegative ? -result : result; }; exports.formatTextToDuration = formatTextToDuration;