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