duplicates.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. 'use strict';
  2. function _typeof(obj) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (obj) { return typeof obj; } : function (obj) { return obj && "function" == typeof Symbol && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }, _typeof(obj); }
  3. module.exports = function duplicates() {
  4. var _this = this;
  5. var occuredValues = [];
  6. var duplicateValues = {};
  7. var stringifiedValue = function stringifiedValue(value) {
  8. if (Array.isArray(value) || _typeof(value) === 'object') {
  9. return JSON.stringify(value);
  10. }
  11. return value;
  12. };
  13. if (Array.isArray(this.items)) {
  14. this.items.forEach(function (value, index) {
  15. var valueAsString = stringifiedValue(value);
  16. if (occuredValues.indexOf(valueAsString) === -1) {
  17. occuredValues.push(valueAsString);
  18. } else {
  19. duplicateValues[index] = value;
  20. }
  21. });
  22. } else if (_typeof(this.items) === 'object') {
  23. Object.keys(this.items).forEach(function (key) {
  24. var valueAsString = stringifiedValue(_this.items[key]);
  25. if (occuredValues.indexOf(valueAsString) === -1) {
  26. occuredValues.push(valueAsString);
  27. } else {
  28. duplicateValues[key] = _this.items[key];
  29. }
  30. });
  31. }
  32. return new this.constructor(duplicateValues);
  33. };