Source: collections/EqualityComparer.js

  1. /**
  2. * A base-class for equality comparers.
  3. *
  4. * @template T
  5. * @author Sebastian Hönel <development@hoenel.net>
  6. */
  7. class EqualityComparer {
  8. /**
  9. * Returns an instance of the DefaultEqualityComparer<T> that uses the identity-operator.
  10. *
  11. * @template TDefault
  12. * @type {DefaultEqualityComparer<TDefault>}
  13. */
  14. static get default() {
  15. return new DefaultEqualityComparer();
  16. };
  17. /**
  18. * @param {T} x
  19. * @param {T} y
  20. * @throws {Error} This is an abstract method.
  21. * @returns {Boolean} True, iff the two items x and y are considered
  22. * to be equal.
  23. */
  24. equals(x, y) {
  25. throw new Error('Abstract method');
  26. };
  27. };
  28. /**
  29. * The DefaultEqualityComparer uses the identity-operator to compare equality.
  30. *
  31. * @template T
  32. * @author Sebastian Hönel <development@hoenel.net>
  33. */
  34. class DefaultEqualityComparer extends EqualityComparer {
  35. /**
  36. * Checks equality using the identity-operator (===).
  37. *
  38. * @param {T} x
  39. * @param {T} y
  40. * @returns {Boolean} True, iff the two items are identical.
  41. */
  42. equals(x, y) {
  43. return x === y;
  44. };
  45. };
  46. module.exports = Object.freeze({
  47. EqualityComparer,
  48. DefaultEqualityComparer
  49. });