@nutui/nutui-react
Version:
京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序
61 lines (60 loc) • 2.24 kB
JavaScript
import { _ as _object_spread } from "@swc/helpers/_/_object_spread";
import { _ as _object_without_properties } from "@swc/helpers/_/_object_without_properties";
import { _ as _to_property_key } from "@swc/helpers/_/_to_property_key";
export var normalizeOptions = function(options, keyMap) {
if (!options) return undefined;
return options.map(function(opt) {
var _keyMap_textKey = keyMap.textKey, _keyMap_valueKey = keyMap.valueKey, _keyMap_childrenKey = keyMap.childrenKey, text = opt[_keyMap_textKey], value = opt[_keyMap_valueKey], children = opt[_keyMap_childrenKey], others = _object_without_properties(opt, [
_keyMap_textKey,
_keyMap_valueKey,
_keyMap_childrenKey
].map(_to_property_key));
return _object_spread({
text: text,
value: value,
children: normalizeOptions(children, keyMap)
}, others);
});
};
export var normalizeListOptions = function(options, format) {
var defaultConvertConfig = {
topId: null,
idKey: 'id',
pidKey: 'pid',
sortKey: 'sortKey'
};
var mergedFormat = _object_spread({}, defaultConvertConfig, format);
var topId = mergedFormat.topId, idKey = mergedFormat.idKey, pidKey = mergedFormat.pidKey, sortKey = mergedFormat.sortKey;
var map = {};
options.forEach(function(opt) {
var pid = opt[pidKey], id = opt[idKey], others = _object_without_properties(opt, [
pidKey,
idKey
].map(_to_property_key));
var newNode = _object_spread({
pid: pid,
id: id
}, others);
if (map[pid]) {
map[pid].push(newNode);
} else {
map[pid] = [
newNode
];
}
});
for(var key in map){
// eslint-disable-next-line no-continue
if (!Object.prototype.hasOwnProperty.call(map, key)) continue;
map[key].sort(function(a, b) {
return a[sortKey] - b[sortKey];
});
map[key].forEach(function(option) {
if (map[option.id]) {
option.children = map[option.id];
}
});
}
// @ts-ignore
return map[topId];
};