UNPKG

532 BJavaScriptView Raw
1const entries = require('core-js/fn/object/entries');
2
3function entrySorter(a, b) {
4 const aKey = a[0];
5 const bKey = b[0];
6 if (aKey < bKey) {
7 return -1;
8 } else if (aKey > bKey) {
9 return 1;
10 }
11 return 0;
12}
13
14module.exports = function sort(data) {
15 if (Array.isArray(data)) {
16 return data.map(sort);
17 } else if (Object.prototype.toString.call(data) !== '[object Object]') {
18 return data;
19 }
20
21 return entries(data)
22 .sort(entrySorter)
23 .reduce((sorted, item) => {
24 sorted[item[0]] = item[1];
25 return sorted;
26 }, {});
27};