50f425b34fb6c8cd78955a93729d7a960a1b9cd6e13b88fb47e7f40ad53e2773382430961ec8a47a5f4a1bc7dc8dc2a8c6caf783c451a04d6047d5953a5875 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. const cloneDeep = require("lodash.clonedeep");
  4. const isEqual = require("lodash.isequal");
  5. var AttributeMap;
  6. (function (AttributeMap) {
  7. function compose(a = {}, b = {}, keepNull = false) {
  8. if (typeof a !== 'object') {
  9. a = {};
  10. }
  11. if (typeof b !== 'object') {
  12. b = {};
  13. }
  14. let attributes = cloneDeep(b);
  15. if (!keepNull) {
  16. attributes = Object.keys(attributes).reduce((copy, key) => {
  17. if (attributes[key] != null) {
  18. copy[key] = attributes[key];
  19. }
  20. return copy;
  21. }, {});
  22. }
  23. for (const key in a) {
  24. if (a[key] !== undefined && b[key] === undefined) {
  25. attributes[key] = a[key];
  26. }
  27. }
  28. return Object.keys(attributes).length > 0 ? attributes : undefined;
  29. }
  30. AttributeMap.compose = compose;
  31. function diff(a = {}, b = {}) {
  32. if (typeof a !== 'object') {
  33. a = {};
  34. }
  35. if (typeof b !== 'object') {
  36. b = {};
  37. }
  38. const attributes = Object.keys(a)
  39. .concat(Object.keys(b))
  40. .reduce((attrs, key) => {
  41. if (!isEqual(a[key], b[key])) {
  42. attrs[key] = b[key] === undefined ? null : b[key];
  43. }
  44. return attrs;
  45. }, {});
  46. return Object.keys(attributes).length > 0 ? attributes : undefined;
  47. }
  48. AttributeMap.diff = diff;
  49. function invert(attr = {}, base = {}) {
  50. attr = attr || {};
  51. const baseInverted = Object.keys(base).reduce((memo, key) => {
  52. if (base[key] !== attr[key] && attr[key] !== undefined) {
  53. memo[key] = base[key];
  54. }
  55. return memo;
  56. }, {});
  57. return Object.keys(attr).reduce((memo, key) => {
  58. if (attr[key] !== base[key] && base[key] === undefined) {
  59. memo[key] = null;
  60. }
  61. return memo;
  62. }, baseInverted);
  63. }
  64. AttributeMap.invert = invert;
  65. function transform(a, b, priority = false) {
  66. if (typeof a !== 'object') {
  67. return b;
  68. }
  69. if (typeof b !== 'object') {
  70. return undefined;
  71. }
  72. if (!priority) {
  73. return b; // b simply overwrites us without priority
  74. }
  75. const attributes = Object.keys(b).reduce((attrs, key) => {
  76. if (a[key] === undefined) {
  77. attrs[key] = b[key]; // null is a valid value
  78. }
  79. return attrs;
  80. }, {});
  81. return Object.keys(attributes).length > 0 ? attributes : undefined;
  82. }
  83. AttributeMap.transform = transform;
  84. })(AttributeMap || (AttributeMap = {}));
  85. exports.default = AttributeMap;
  86. //# sourceMappingURL=AttributeMap.js.map