UNPKG

@nutui/nutui-react

Version:

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

79 lines (78 loc) 2.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { normalizeListOptions: function() { return normalizeListOptions; }, normalizeOptions: function() { return normalizeOptions; } }); var _object_spread = require("@swc/helpers/_/_object_spread"); var _object_without_properties = require("@swc/helpers/_/_object_without_properties"); var _to_property_key = require("@swc/helpers/_/_to_property_key"); var normalizeOptions = function normalizeOptions1(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 = (0, _object_without_properties._)(opt, [ _keyMap_textKey, _keyMap_valueKey, _keyMap_childrenKey ].map(_to_property_key._)); return (0, _object_spread._)({ text: text, value: value, children: normalizeOptions(children, keyMap) }, others); }); }; var normalizeListOptions = function normalizeListOptions(options, format) { var defaultConvertConfig = { topId: null, idKey: 'id', pidKey: 'pid', sortKey: 'sortKey' }; var mergedFormat = (0, _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 = (0, _object_without_properties._)(opt, [ pidKey, idKey ].map(_to_property_key._)); var newNode = (0, _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]; };