identifiers.js 409 B

1234567891011121314151617181920212223
  1. const numeric = /^[0-9]+$/
  2. const compareIdentifiers = (a, b) => {
  3. const anum = numeric.test(a)
  4. const bnum = numeric.test(b)
  5. if (anum && bnum) {
  6. a = +a
  7. b = +b
  8. }
  9. return a === b ? 0
  10. : (anum && !bnum) ? -1
  11. : (bnum && !anum) ? 1
  12. : a < b ? -1
  13. : 1
  14. }
  15. const rcompareIdentifiers = (a, b) => compareIdentifiers(b, a)
  16. module.exports = {
  17. compareIdentifiers,
  18. rcompareIdentifiers
  19. }