UNPKG

@nutui/nutui-react-taro

Version:

京东风格的轻量级移动端 React 组件库,支持一套代码生成 H5 和小程序

61 lines (60 loc) 2.17 kB
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 normalizeOptions1(options, keyMap) { if (!options) return undefined; return options.map(function(opt) { var _key = keyMap.textKey, _key1 = keyMap.valueKey, _key2 = keyMap.childrenKey, text = opt[_key], value = opt[_key1], children = opt[_key2], others = _object_without_properties(opt, [ _key, _key1, _key2 ].map(_to_property_key)); return _object_spread({ text: text, value: value, children: normalizeOptions(children, keyMap) }, others); }); }; export var normalizeListOptions = function normalizeListOptions(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]; };