miscUtil.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = getDataOrAriaProps;
  6. exports.getValue = getValue;
  7. exports.leftPad = leftPad;
  8. exports.toArray = toArray;
  9. exports.tuple = void 0;
  10. exports.updateValues = updateValues;
  11. function leftPad(str, length) {
  12. let fill = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : '0';
  13. let current = String(str);
  14. while (current.length < length) {
  15. current = `${fill}${str}`;
  16. }
  17. return current;
  18. }
  19. const tuple = function () {
  20. for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
  21. args[_key] = arguments[_key];
  22. }
  23. return args;
  24. };
  25. exports.tuple = tuple;
  26. function toArray(val) {
  27. if (val === null || val === undefined) {
  28. return [];
  29. }
  30. return Array.isArray(val) ? val : [val];
  31. }
  32. function getDataOrAriaProps(props) {
  33. const retProps = {};
  34. Object.keys(props).forEach(key => {
  35. if ((key.startsWith('data-') || key.startsWith('aria-') || key === 'role' || key === 'name') && !key.startsWith('data-__')) {
  36. retProps[key] = props[key];
  37. }
  38. });
  39. return retProps;
  40. }
  41. function getValue(values, index) {
  42. return values ? values[index] : null;
  43. }
  44. function updateValues(values, value, index) {
  45. const newValues = [getValue(values, 0), getValue(values, 1)];
  46. newValues[index] = typeof value === 'function' ? value(newValues[index]) : value;
  47. if (!newValues[0] && !newValues[1]) {
  48. return null;
  49. }
  50. return newValues;
  51. }