-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (36 loc) · 1006 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const get = require('lodash.get');
const logger = require('log4js').getLogger('orderAlign');
module.exports = function orderAlign(orderList, dataArr, targetKey) {
return dataArr
.reduce((result, next) => {
if (typeof next === 'string') {
return add(result, orderList, next, next);
} else if (typeof next === 'object') {
if (typeof targetKey === 'undefined') {
logger.warn('`targetKey` is required');
}
const key = get(next, targetKey);
if (typeof key === 'undefined') {
return result;
}
return add(result, orderList, key, next);
}
return result;
}, [])
.reduce((result, next) => {
if (typeof next === 'undefined') {
return result;
}
result.push(next);
return result;
}, []);
};
function add(arr, list, key, item) {
const idx = list.indexOf(key);
if (~idx) {
arr[idx] = item;
return arr;
}
logger.warn(`\`${key}\` isn't on the list`);
return arr;
}