processDataSet.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
  3. function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
  4. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  5. /**
  6. * 把props的data属性,处理成dataset
  7. * { 'data-alpha-beta': 3 }
  8. * { alphaBeta: 3 }
  9. */
  10. function processDataSet(e) {
  11. var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  12. if (e.timeStamp === undefined) {
  13. e = _objectSpread(_objectSpread({}, e), {}, {
  14. target: {
  15. dataset: {}
  16. },
  17. currentTarget: {
  18. dataset: {}
  19. }
  20. });
  21. }
  22. Object.keys(props).forEach(function (prop) {
  23. var matched = prop.match(/^data-(.+)/);
  24. if (matched) {
  25. var key = matched[1].replace(/-(\w)/, function ($0, $1) {
  26. return $1.toUpperCase();
  27. });
  28. e.target.dataset[key] = props[prop];
  29. e.currentTarget.dataset[key] = props[prop];
  30. }
  31. });
  32. return e;
  33. }
  34. module.exports = processDataSet;