isIndexSpecEqual.js 945 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. /**
  3. * Compares two index specifications to determine if they are equal.
  4. *
  5. * #### Example:
  6. * isIndexSpecEqual({ a: 1, b: 1 }, { a: 1, b: 1 }); // true
  7. * isIndexSpecEqual({ a: 1, b: 1 }, { b: 1, a: 1 }); // false
  8. * isIndexSpecEqual({ a: 1, b: -1 }, { a: 1, b: 1 }); // false
  9. *
  10. * @param {Object} spec1 The first index specification to compare.
  11. * @param {Object} spec2 The second index specification to compare.
  12. * @returns {Boolean} Returns true if the index specifications are equal, otherwise returns false.
  13. */
  14. module.exports = function isIndexSpecEqual(spec1, spec2) {
  15. const spec1Keys = Object.keys(spec1);
  16. const spec2Keys = Object.keys(spec2);
  17. if (spec1Keys.length !== spec2Keys.length) {
  18. return false;
  19. }
  20. for (let i = 0; i < spec1Keys.length; i++) {
  21. const key = spec1Keys[i];
  22. if (key !== spec2Keys[i] || spec1[key] !== spec2[key]) {
  23. return false;
  24. }
  25. }
  26. return true;
  27. };