cmp.js 907 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. if (typeof b === 'object')
  13. b = b.version
  14. return a === b
  15. case '!==':
  16. if (typeof a === 'object')
  17. a = a.version
  18. if (typeof b === 'object')
  19. b = b.version
  20. return a !== b
  21. case '':
  22. case '=':
  23. case '==':
  24. return eq(a, b, loose)
  25. case '!=':
  26. return neq(a, b, loose)
  27. case '>':
  28. return gt(a, b, loose)
  29. case '>=':
  30. return gte(a, b, loose)
  31. case '<':
  32. return lt(a, b, loose)
  33. case '<=':
  34. return lte(a, b, loose)
  35. default:
  36. throw new TypeError(`Invalid operator: ${op}`)
  37. }
  38. }
  39. module.exports = cmp