cmp.js 947 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. const eq = require('./eq')
  2. const neq = require('./neq')
  3. const gt = require('./gt')
  4. const gte = require('./gte')
  5. const lt = require('./lt')
  6. const lte = require('./lte')
  7. const cmp = (a, op, b, loose) => {
  8. switch (op) {
  9. case '===':
  10. if (typeof a === 'object') {
  11. a = a.version
  12. }
  13. if (typeof b === 'object') {
  14. b = b.version
  15. }
  16. return a === b
  17. case '!==':
  18. if (typeof a === 'object') {
  19. a = a.version
  20. }
  21. if (typeof b === 'object') {
  22. b = b.version
  23. }
  24. return a !== b
  25. case '':
  26. case '=':
  27. case '==':
  28. return eq(a, b, loose)
  29. case '!=':
  30. return neq(a, b, loose)
  31. case '>':
  32. return gt(a, b, loose)
  33. case '>=':
  34. return gte(a, b, loose)
  35. case '<':
  36. return lt(a, b, loose)
  37. case '<=':
  38. return lte(a, b, loose)
  39. default:
  40. throw new TypeError(`Invalid operator: ${op}`)
  41. }
  42. }
  43. module.exports = cmp