utils.mjs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import { extend, padZero, makeArrayProp, clamp } from "../utils/index.mjs";
  2. import { pickerSharedProps } from "../picker/Picker.mjs";
  3. const sharedProps = extend({}, pickerSharedProps, {
  4. modelValue: makeArrayProp(),
  5. filter: Function,
  6. formatter: {
  7. type: Function,
  8. default: (type, option) => option
  9. }
  10. });
  11. const pickerInheritKeys = Object.keys(pickerSharedProps);
  12. function times(n, iteratee) {
  13. if (n < 0) {
  14. return [];
  15. }
  16. const result = Array(n);
  17. let index = -1;
  18. while (++index < n) {
  19. result[index] = iteratee(index);
  20. }
  21. return result;
  22. }
  23. const getMonthEndDay = (year, month) => 32 - new Date(year, month - 1, 32).getDate();
  24. const genOptions = (min, max, type, formatter, filter, values) => {
  25. const options = times(max - min + 1, (index) => {
  26. const value = padZero(min + index);
  27. return formatter(type, {
  28. text: value,
  29. value
  30. });
  31. });
  32. return filter ? filter(type, options, values) : options;
  33. };
  34. const formatValueRange = (values, columns) => values.map((value, index) => {
  35. const column = columns[index];
  36. if (column.length) {
  37. const minValue = +column[0].value;
  38. const maxValue = +column[column.length - 1].value;
  39. return padZero(clamp(+value, minValue, maxValue));
  40. }
  41. return value;
  42. });
  43. export {
  44. formatValueRange,
  45. genOptions,
  46. getMonthEndDay,
  47. pickerInheritKeys,
  48. sharedProps,
  49. times
  50. };