source: trip-planner-front/node_modules/alphanum-sort/lib/index.js

Last change on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 710 bytes
Line 
1var compare = require('./compare');
2
3function mediator(a, b) {
4 return compare(this, a.converted, b.converted);
5}
6
7module.exports = function (array, opts) {
8 if (!Array.isArray(array) || array.length < 2) {
9 return array;
10 }
11 if (typeof opts !== 'object') {
12 opts = {};
13 }
14 opts.sign = !!opts.sign;
15 var insensitive = !!opts.insensitive;
16 var result = Array(array.length);
17 var i, max, value;
18
19 for (i = 0, max = array.length; i < max; i += 1) {
20 value = String(array[i]);
21 result[i] = {
22 value: array[i],
23 converted: insensitive ? value.toLowerCase() : value
24 };
25 }
26
27 result.sort(mediator.bind(opts));
28
29 for (i = result.length - 1; ~i; i -= 1) {
30 result[i] = result[i].value;
31 }
32
33 return result;
34};
Note: See TracBrowser for help on using the repository browser.