identifiers.js 525 B

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