format_helper.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. * DevExtreme (format_helper.js)
  3. * Version: 19.1.16
  4. * Build date: Tue Oct 18 2022
  5. *
  6. * Copyright (c) 2012 - 2022 Developer Express Inc. ALL RIGHTS RESERVED
  7. * Read about DevExtreme licensing here: https://js.devexpress.com/Licensing/
  8. */
  9. "use strict";
  10. var typeUtils = require("./core/utils/type");
  11. var dateUtils = require("./core/utils/date");
  12. var numberLocalization = require("./localization/number");
  13. var dateLocalization = require("./localization/date");
  14. var dependencyInjector = require("./core/utils/dependency_injector");
  15. require("./localization/currency");
  16. module.exports = dependencyInjector({
  17. format: function(value, _format) {
  18. var formatIsValid = typeUtils.isString(_format) && "" !== _format || typeUtils.isPlainObject(_format) || typeUtils.isFunction(_format);
  19. var valueIsValid = typeUtils.isNumeric(value) || typeUtils.isDate(value);
  20. if (!formatIsValid || !valueIsValid) {
  21. return typeUtils.isDefined(value) ? value.toString() : ""
  22. }
  23. if (typeUtils.isFunction(_format)) {
  24. return _format(value)
  25. }
  26. if (typeUtils.isString(_format)) {
  27. _format = {
  28. type: _format
  29. }
  30. }
  31. if (typeUtils.isNumeric(value)) {
  32. return numberLocalization.format(value, _format)
  33. }
  34. if (typeUtils.isDate(value)) {
  35. return dateLocalization.format(value, _format)
  36. }
  37. },
  38. getTimeFormat: function(showSecond) {
  39. return showSecond ? "longtime" : "shorttime"
  40. },
  41. _normalizeFormat: function(format) {
  42. if (!Array.isArray(format)) {
  43. return format
  44. }
  45. if (1 === format.length) {
  46. return format[0]
  47. }
  48. return function(date) {
  49. return format.map(function(formatPart) {
  50. return dateLocalization.format(date, formatPart)
  51. }).join(" ")
  52. }
  53. },
  54. getDateFormatByDifferences: function(dateDifferences, intervalFormat) {
  55. var resultFormat = [];
  56. var needSpecialSecondFormatter = intervalFormat && dateDifferences.millisecond && !(dateDifferences.year || dateDifferences.month || dateDifferences.day);
  57. if (needSpecialSecondFormatter) {
  58. var secondFormatter = function(date) {
  59. return date.getSeconds() + date.getMilliseconds() / 1e3 + "s"
  60. };
  61. resultFormat.push(secondFormatter)
  62. } else {
  63. if (dateDifferences.millisecond) {
  64. resultFormat.push("millisecond")
  65. }
  66. }
  67. if (dateDifferences.hour || dateDifferences.minute || !needSpecialSecondFormatter && dateDifferences.second) {
  68. resultFormat.unshift(this.getTimeFormat(dateDifferences.second))
  69. }
  70. if (dateDifferences.year && dateDifferences.month && dateDifferences.day) {
  71. if (intervalFormat && "month" === intervalFormat) {
  72. return "monthandyear"
  73. } else {
  74. resultFormat.unshift("shortdate");
  75. return this._normalizeFormat(resultFormat)
  76. }
  77. }
  78. if (dateDifferences.year && dateDifferences.month) {
  79. return "monthandyear"
  80. }
  81. if (dateDifferences.year && dateDifferences.quarter) {
  82. return "quarterandyear"
  83. }
  84. if (dateDifferences.year) {
  85. return "year"
  86. }
  87. if (dateDifferences.quarter) {
  88. return "quarter"
  89. }
  90. if (dateDifferences.month && dateDifferences.day) {
  91. if (intervalFormat) {
  92. var monthDayFormatter = function(date) {
  93. return dateLocalization.getMonthNames("abbreviated")[date.getMonth()] + " " + dateLocalization.format(date, "day")
  94. };
  95. resultFormat.unshift(monthDayFormatter)
  96. } else {
  97. resultFormat.unshift("monthandday")
  98. }
  99. return this._normalizeFormat(resultFormat)
  100. }
  101. if (dateDifferences.month) {
  102. return "month"
  103. }
  104. if (dateDifferences.day) {
  105. if (intervalFormat) {
  106. resultFormat.unshift("day")
  107. } else {
  108. var dayFormatter = function(date) {
  109. return dateLocalization.format(date, "dayofweek") + ", " + dateLocalization.format(date, "day")
  110. };
  111. resultFormat.unshift(dayFormatter)
  112. }
  113. return this._normalizeFormat(resultFormat)
  114. }
  115. return this._normalizeFormat(resultFormat)
  116. },
  117. getDateFormatByTicks: function(ticks) {
  118. var maxDiff;
  119. if (ticks.length > 1) {
  120. maxDiff = dateUtils.getDatesDifferences(ticks[0], ticks[1]);
  121. for (var i = 1; i < ticks.length - 1; i++) {
  122. var currentDiff = dateUtils.getDatesDifferences(ticks[i], ticks[i + 1]);
  123. if (maxDiff.count < currentDiff.count) {
  124. maxDiff = currentDiff
  125. }
  126. }
  127. } else {
  128. maxDiff = {
  129. year: true,
  130. month: true,
  131. day: true,
  132. hour: ticks[0].getHours() > 0,
  133. minute: ticks[0].getMinutes() > 0,
  134. second: ticks[0].getSeconds() > 0,
  135. millisecond: ticks[0].getMilliseconds() > 0
  136. }
  137. }
  138. var resultFormat = this.getDateFormatByDifferences(maxDiff);
  139. return resultFormat
  140. },
  141. getDateFormatByTickInterval: function(startValue, endValue, tickInterval) {
  142. var dateUnitInterval;
  143. var dateDifferencesConverter = {
  144. week: "day"
  145. };
  146. var correctDateDifferences = function(dateDifferences, tickInterval, value) {
  147. switch (tickInterval) {
  148. case "year":
  149. case "quarter":
  150. dateDifferences.month = value;
  151. case "month":
  152. dateDifferences.day = value;
  153. case "week":
  154. case "day":
  155. dateDifferences.hour = value;
  156. case "hour":
  157. dateDifferences.minute = value;
  158. case "minute":
  159. dateDifferences.second = value;
  160. case "second":
  161. dateDifferences.millisecond = value
  162. }
  163. };
  164. var correctDifferencesByMaxDate = function(differences, minDate, maxDate) {
  165. if (!maxDate.getMilliseconds() && maxDate.getSeconds()) {
  166. if (maxDate.getSeconds() - minDate.getSeconds() === 1) {
  167. differences.millisecond = true;
  168. differences.second = false
  169. }
  170. } else {
  171. if (!maxDate.getSeconds() && maxDate.getMinutes()) {
  172. if (maxDate.getMinutes() - minDate.getMinutes() === 1) {
  173. differences.second = true;
  174. differences.minute = false
  175. }
  176. } else {
  177. if (!maxDate.getMinutes() && maxDate.getHours()) {
  178. if (maxDate.getHours() - minDate.getHours() === 1) {
  179. differences.minute = true;
  180. differences.hour = false
  181. }
  182. } else {
  183. if (!maxDate.getHours() && maxDate.getDate() > 1) {
  184. if (maxDate.getDate() - minDate.getDate() === 1) {
  185. differences.hour = true;
  186. differences.day = false
  187. }
  188. } else {
  189. if (1 === maxDate.getDate() && maxDate.getMonth()) {
  190. if (maxDate.getMonth() - minDate.getMonth() === 1) {
  191. differences.day = true;
  192. differences.month = false
  193. }
  194. } else {
  195. if (!maxDate.getMonth() && maxDate.getFullYear()) {
  196. if (maxDate.getFullYear() - minDate.getFullYear() === 1) {
  197. differences.month = true;
  198. differences.year = false
  199. }
  200. }
  201. }
  202. }
  203. }
  204. }
  205. }
  206. };
  207. tickInterval = typeUtils.isString(tickInterval) ? tickInterval.toLowerCase() : tickInterval;
  208. var dateDifferences = dateUtils.getDatesDifferences(startValue, endValue);
  209. if (startValue !== endValue) {
  210. correctDifferencesByMaxDate(dateDifferences, startValue > endValue ? endValue : startValue, startValue > endValue ? startValue : endValue)
  211. }
  212. dateUnitInterval = dateUtils.getDateUnitInterval(dateDifferences);
  213. correctDateDifferences(dateDifferences, dateUnitInterval, true);
  214. dateUnitInterval = dateUtils.getDateUnitInterval(tickInterval || "second");
  215. correctDateDifferences(dateDifferences, dateUnitInterval, false);
  216. dateDifferences[dateDifferencesConverter[dateUnitInterval] || dateUnitInterval] = true;
  217. var resultFormat = this.getDateFormatByDifferences(dateDifferences);
  218. return resultFormat
  219. }
  220. });