index.js 710 B

12345678910111213141516171819202122232425262728293031323334
  1. var compare = require('./compare');
  2. function mediator(a, b) {
  3. return compare(this, a.converted, b.converted);
  4. }
  5. module.exports = function (array, opts) {
  6. if (!Array.isArray(array) || array.length < 2) {
  7. return array;
  8. }
  9. if (typeof opts !== 'object') {
  10. opts = {};
  11. }
  12. opts.sign = !!opts.sign;
  13. var insensitive = !!opts.insensitive;
  14. var result = Array(array.length);
  15. var i, max, value;
  16. for (i = 0, max = array.length; i < max; i += 1) {
  17. value = String(array[i]);
  18. result[i] = {
  19. value: array[i],
  20. converted: insensitive ? value.toLowerCase() : value
  21. };
  22. }
  23. result.sort(mediator.bind(opts));
  24. for (i = result.length - 1; ~i; i -= 1) {
  25. result[i] = result[i].value;
  26. }
  27. return result;
  28. };