valueUtil.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.fillFieldNames = fillFieldNames;
  7. exports.flattenOptions = flattenOptions;
  8. exports.getSeparatedContent = void 0;
  9. exports.injectPropsWithOption = injectPropsWithOption;
  10. exports.isValidCount = isValidCount;
  11. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  12. var _toArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toArray"));
  13. var _objectSpread2 = _interopRequireDefault(require("@babel/runtime/helpers/objectSpread2"));
  14. var _warning = _interopRequireDefault(require("rc-util/lib/warning"));
  15. function getKey(data, index) {
  16. var key = data.key;
  17. var value;
  18. if ('value' in data) {
  19. value = data.value;
  20. }
  21. if (key !== null && key !== undefined) {
  22. return key;
  23. }
  24. if (value !== undefined) {
  25. return value;
  26. }
  27. return "rc-index-key-".concat(index);
  28. }
  29. function isValidCount(value) {
  30. return typeof value !== 'undefined' && !Number.isNaN(value);
  31. }
  32. function fillFieldNames(fieldNames, childrenAsData) {
  33. var _ref = fieldNames || {},
  34. label = _ref.label,
  35. value = _ref.value,
  36. options = _ref.options,
  37. groupLabel = _ref.groupLabel;
  38. var mergedLabel = label || (childrenAsData ? 'children' : 'label');
  39. return {
  40. label: mergedLabel,
  41. value: value || 'value',
  42. options: options || 'options',
  43. groupLabel: groupLabel || mergedLabel
  44. };
  45. }
  46. /**
  47. * Flat options into flatten list.
  48. * We use `optionOnly` here is aim to avoid user use nested option group.
  49. * Here is simply set `key` to the index if not provided.
  50. */
  51. function flattenOptions(options) {
  52. var _ref2 = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
  53. fieldNames = _ref2.fieldNames,
  54. childrenAsData = _ref2.childrenAsData;
  55. var flattenList = [];
  56. var _fillFieldNames = fillFieldNames(fieldNames, false),
  57. fieldLabel = _fillFieldNames.label,
  58. fieldValue = _fillFieldNames.value,
  59. fieldOptions = _fillFieldNames.options,
  60. groupLabel = _fillFieldNames.groupLabel;
  61. function dig(list, isGroupOption) {
  62. if (!Array.isArray(list)) {
  63. return;
  64. }
  65. list.forEach(function (data) {
  66. if (isGroupOption || !(fieldOptions in data)) {
  67. var value = data[fieldValue];
  68. // Option
  69. flattenList.push({
  70. key: getKey(data, flattenList.length),
  71. groupOption: isGroupOption,
  72. data: data,
  73. label: data[fieldLabel],
  74. value: value
  75. });
  76. } else {
  77. var grpLabel = data[groupLabel];
  78. if (grpLabel === undefined && childrenAsData) {
  79. grpLabel = data.label;
  80. }
  81. // Option Group
  82. flattenList.push({
  83. key: getKey(data, flattenList.length),
  84. group: true,
  85. data: data,
  86. label: grpLabel
  87. });
  88. dig(data[fieldOptions], true);
  89. }
  90. });
  91. }
  92. dig(options, false);
  93. return flattenList;
  94. }
  95. /**
  96. * Inject `props` into `option` for legacy usage
  97. */
  98. function injectPropsWithOption(option) {
  99. var newOption = (0, _objectSpread2.default)({}, option);
  100. if (!('props' in newOption)) {
  101. Object.defineProperty(newOption, 'props', {
  102. get: function get() {
  103. (0, _warning.default)(false, 'Return type is option instead of Option instance. Please read value directly instead of reading from `props`.');
  104. return newOption;
  105. }
  106. });
  107. }
  108. return newOption;
  109. }
  110. var getSeparatedContent = exports.getSeparatedContent = function getSeparatedContent(text, tokens, end) {
  111. if (!tokens || !tokens.length) {
  112. return null;
  113. }
  114. var match = false;
  115. var separate = function separate(str, _ref3) {
  116. var _ref4 = (0, _toArray2.default)(_ref3),
  117. token = _ref4[0],
  118. restTokens = _ref4.slice(1);
  119. if (!token) {
  120. return [str];
  121. }
  122. var list = str.split(token);
  123. match = match || list.length > 1;
  124. return list.reduce(function (prevList, unitStr) {
  125. return [].concat((0, _toConsumableArray2.default)(prevList), (0, _toConsumableArray2.default)(separate(unitStr, restTokens)));
  126. }, []).filter(Boolean);
  127. };
  128. var list = separate(text, tokens);
  129. if (match) {
  130. return typeof end !== 'undefined' ? list.slice(0, end) : list;
  131. } else {
  132. return null;
  133. }
  134. };