utils.mjs 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { extend } from "../utils/index.mjs";
  2. import { pickerSharedProps } from "../picker/Picker.mjs";
  3. const sharedProps = extend({}, pickerSharedProps, {
  4. filter: Function,
  5. columnsOrder: Array,
  6. formatter: {
  7. type: Function,
  8. default: (type, value) => value
  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. function getTrueValue(value) {
  24. if (!value) {
  25. return 0;
  26. }
  27. while (Number.isNaN(parseInt(value, 10))) {
  28. if (value.length > 1) {
  29. value = value.slice(1);
  30. } else {
  31. return 0;
  32. }
  33. }
  34. return parseInt(value, 10);
  35. }
  36. const getMonthEndDay = (year, month) => 32 - new Date(year, month - 1, 32).getDate();
  37. const proxyPickerMethods = (picker, callback) => {
  38. const methods = [
  39. "setValues",
  40. "setIndexes",
  41. "setColumnIndex",
  42. "setColumnValue"
  43. ];
  44. return new Proxy(picker, {
  45. get: (target, prop) => {
  46. if (methods.includes(prop)) {
  47. return (...args) => {
  48. target[prop](...args);
  49. callback();
  50. };
  51. }
  52. return target[prop];
  53. }
  54. });
  55. };
  56. export {
  57. getMonthEndDay,
  58. getTrueValue,
  59. pickerInheritKeys,
  60. proxyPickerMethods,
  61. sharedProps,
  62. times
  63. };