-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremap-indexed-keys-after-omit-sync.js
74 lines (68 loc) · 1.9 KB
/
remap-indexed-keys-after-omit-sync.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import isEqual from 'lodash/isEqual';
import map from 'lodash/map';
import omit from 'lodash/omit';
import mapKeys from 'lodash/mapKeys';
import split from 'lodash/split';
import findIndex from 'lodash/findIndex';
import join from 'lodash/join';
import keys from 'lodash/keys';
/**
* `index-in-key` object,
* remap object which contain `x-index-y` style key
*
* 通过 `key` 删除指定键值对,再重新映射 `key`
*
* 重新映射 `key` 需要区分不同位置的数据,
* - 首位,完整重新分配
* - 当中,仅从删除的数据的下一位开始重新分配
* - 末位,无任何操作
*
* TODO:
*
* - [ ] 分离 `mapKeys` 部分的逻辑
*
* @param {Object} object - 从小到大索引的对象
* @param {String} keyPattern - 对象的键模式
* @param {String} separator - 分裂键的分隔符
* @param {Number} omitIndex - 指定索引以删除这个位置的键值对
* @param {String} indexPlaceholderInKeyPattern - 指定索引占位
* @param {Number} gap - 指定递减 / 递增幅度
*/
export default function remapIndexedKeysAfterOmitSync({
object,
omitIndex,
keyPattern,
separator,
indexPlaceholderInKeyPattern,
gap,
}) {
// 寻找到 `keyPattern` 中索引占位符的位置
const idxPos = findIndex(
split(keyPattern, separator),
item => isEqual(item, indexPlaceholderInKeyPattern),
);
// 寻找到待删除键值对的键
const toBeRemoved = keys(object)[omitIndex];
return mapKeys(
omit(
object,
[toBeRemoved],
),
(val, key) => {
const splitedKey = split(key, separator);
const rowIdx = +splitedKey[idxPos];
if (rowIdx > omitIndex) {
return join(
map(splitedKey, (item, i) => {
if (isEqual(i, idxPos)) {
return rowIdx - gap;
}
return item;
}),
separator,
);
}
return key;
},
);
}