Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 

96 linhas
3.3 KiB

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