data.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.data = exports.setData = exports.getData = void 0;
  4. const string_1 = require("../string");
  5. const dataset = new WeakMap();
  6. function getData(elem, name) {
  7. const key = string_1.StringExt.camelCase(name);
  8. const cache = dataset.get(elem);
  9. if (cache) {
  10. return cache[key];
  11. }
  12. }
  13. exports.getData = getData;
  14. function setData(elem, name, value) {
  15. const key = string_1.StringExt.camelCase(name);
  16. const cache = dataset.get(elem);
  17. if (cache) {
  18. cache[key] = value;
  19. }
  20. else {
  21. dataset.set(elem, {
  22. [key]: value,
  23. });
  24. }
  25. }
  26. exports.setData = setData;
  27. function data(elem, name, value) {
  28. if (!name) {
  29. const datas = {};
  30. Object.keys(dataset).forEach((key) => {
  31. datas[key] = getData(elem, key);
  32. });
  33. return datas;
  34. }
  35. if (typeof name === 'string') {
  36. if (value === undefined) {
  37. return getData(elem, name);
  38. }
  39. setData(elem, name, value);
  40. return;
  41. }
  42. // eslint-disable-next-line
  43. for (const key in name) {
  44. data(elem, key, name[key]);
  45. }
  46. }
  47. exports.data = data;
  48. //# sourceMappingURL=data.js.map