identifiers.js 424 B

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