miscUtil.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.fillIndex = fillIndex;
  7. exports.getFromDate = getFromDate;
  8. exports.getRowFormat = getRowFormat;
  9. exports.leftPad = leftPad;
  10. exports.pickProps = pickProps;
  11. exports.toArray = toArray;
  12. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  13. function leftPad(str, length) {
  14. var fill = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '0';
  15. var current = String(str);
  16. while (current.length < length) {
  17. current = "".concat(fill).concat(current);
  18. }
  19. return current;
  20. }
  21. /**
  22. * Convert `value` to array. Will provide `[]` if is null or undefined.
  23. */
  24. function toArray(val) {
  25. if (val === null || val === undefined) {
  26. return [];
  27. }
  28. return Array.isArray(val) ? val : [val];
  29. }
  30. function fillIndex(ori, index, value) {
  31. var clone = (0, _toConsumableArray2.default)(ori);
  32. clone[index] = value;
  33. return clone;
  34. }
  35. /** Pick props from the key list. Will filter empty value */
  36. function pickProps(props, keys) {
  37. var clone = {};
  38. var mergedKeys = keys || Object.keys(props);
  39. mergedKeys.forEach(function (key) {
  40. if (props[key] !== undefined) {
  41. clone[key] = props[key];
  42. }
  43. });
  44. return clone;
  45. }
  46. function getRowFormat(picker, locale, format) {
  47. if (format) {
  48. return format;
  49. }
  50. switch (picker) {
  51. // All from the `locale.fieldXXXFormat` first
  52. case 'time':
  53. return locale.fieldTimeFormat;
  54. case 'datetime':
  55. return locale.fieldDateTimeFormat;
  56. case 'month':
  57. return locale.fieldMonthFormat;
  58. case 'year':
  59. return locale.fieldYearFormat;
  60. case 'quarter':
  61. return locale.fieldQuarterFormat;
  62. case 'week':
  63. return locale.fieldWeekFormat;
  64. default:
  65. return locale.fieldDateFormat;
  66. }
  67. }
  68. function getFromDate(calendarValues, activeIndexList, activeIndex) {
  69. var mergedActiveIndex = activeIndex !== undefined ? activeIndex : activeIndexList[activeIndexList.length - 1];
  70. var firstValuedIndex = activeIndexList.find(function (index) {
  71. return calendarValues[index];
  72. });
  73. return mergedActiveIndex !== firstValuedIndex ? calendarValues[firstValuedIndex] : undefined;
  74. }