valueUtil.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.cloneByNamePathList = cloneByNamePathList;
  7. exports.containsNamePath = containsNamePath;
  8. exports.defaultGetValueFromEvent = defaultGetValueFromEvent;
  9. exports.getNamePath = getNamePath;
  10. Object.defineProperty(exports, "getValue", {
  11. enumerable: true,
  12. get: function get() {
  13. return _get.default;
  14. }
  15. });
  16. exports.isSimilar = isSimilar;
  17. exports.matchNamePath = matchNamePath;
  18. exports.move = move;
  19. Object.defineProperty(exports, "setValue", {
  20. enumerable: true,
  21. get: function get() {
  22. return _set.default;
  23. }
  24. });
  25. var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
  26. var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
  27. var _get = _interopRequireDefault(require("rc-util/lib/utils/get"));
  28. var _set = _interopRequireDefault(require("rc-util/lib/utils/set"));
  29. var _typeUtil = require("./typeUtil");
  30. /**
  31. * Convert name to internal supported format.
  32. * This function should keep since we still thinking if need support like `a.b.c` format.
  33. * 'a' => ['a']
  34. * 123 => [123]
  35. * ['a', 123] => ['a', 123]
  36. */
  37. function getNamePath(path) {
  38. return (0, _typeUtil.toArray)(path);
  39. }
  40. function cloneByNamePathList(store, namePathList) {
  41. var newStore = {};
  42. namePathList.forEach(function (namePath) {
  43. var value = (0, _get.default)(store, namePath);
  44. newStore = (0, _set.default)(newStore, namePath, value);
  45. });
  46. return newStore;
  47. }
  48. /**
  49. * Check if `namePathList` includes `namePath`.
  50. * @param namePathList A list of `InternalNamePath[]`
  51. * @param namePath Compare `InternalNamePath`
  52. * @param partialMatch True will make `[a, b]` match `[a, b, c]`
  53. */
  54. function containsNamePath(namePathList, namePath) {
  55. var partialMatch = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  56. return namePathList && namePathList.some(function (path) {
  57. return matchNamePath(namePath, path, partialMatch);
  58. });
  59. }
  60. /**
  61. * Check if `namePath` is super set or equal of `subNamePath`.
  62. * @param namePath A list of `InternalNamePath[]`
  63. * @param subNamePath Compare `InternalNamePath`
  64. * @param partialMatch True will make `[a, b]` match `[a, b, c]`
  65. */
  66. function matchNamePath(namePath, subNamePath) {
  67. var partialMatch = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
  68. if (!namePath || !subNamePath) {
  69. return false;
  70. }
  71. if (!partialMatch && namePath.length !== subNamePath.length) {
  72. return false;
  73. }
  74. return subNamePath.every(function (nameUnit, i) {
  75. return namePath[i] === nameUnit;
  76. });
  77. }
  78. // Like `shallowEqual`, but we not check the data which may cause re-render
  79. function isSimilar(source, target) {
  80. if (source === target) {
  81. return true;
  82. }
  83. if (!source && target || source && !target) {
  84. return false;
  85. }
  86. if (!source || !target || (0, _typeof2.default)(source) !== 'object' || (0, _typeof2.default)(target) !== 'object') {
  87. return false;
  88. }
  89. var sourceKeys = Object.keys(source);
  90. var targetKeys = Object.keys(target);
  91. var keys = new Set([].concat(sourceKeys, targetKeys));
  92. return (0, _toConsumableArray2.default)(keys).every(function (key) {
  93. var sourceValue = source[key];
  94. var targetValue = target[key];
  95. if (typeof sourceValue === 'function' && typeof targetValue === 'function') {
  96. return true;
  97. }
  98. return sourceValue === targetValue;
  99. });
  100. }
  101. function defaultGetValueFromEvent(valuePropName) {
  102. var event = arguments.length <= 1 ? undefined : arguments[1];
  103. if (event && event.target && (0, _typeof2.default)(event.target) === 'object' && valuePropName in event.target) {
  104. return event.target[valuePropName];
  105. }
  106. return event;
  107. }
  108. /**
  109. * Moves an array item from one position in an array to another.
  110. *
  111. * Note: This is a pure function so a new array will be returned, instead
  112. * of altering the array argument.
  113. *
  114. * @param array Array in which to move an item. (required)
  115. * @param moveIndex The index of the item to move. (required)
  116. * @param toIndex The index to move item at moveIndex to. (required)
  117. */
  118. function move(array, moveIndex, toIndex) {
  119. var length = array.length;
  120. if (moveIndex < 0 || moveIndex >= length || toIndex < 0 || toIndex >= length) {
  121. return array;
  122. }
  123. var item = array[moveIndex];
  124. var diff = moveIndex - toIndex;
  125. if (diff > 0) {
  126. // move left
  127. return [].concat((0, _toConsumableArray2.default)(array.slice(0, toIndex)), [item], (0, _toConsumableArray2.default)(array.slice(toIndex, moveIndex)), (0, _toConsumableArray2.default)(array.slice(moveIndex + 1, length)));
  128. }
  129. if (diff < 0) {
  130. // move right
  131. return [].concat((0, _toConsumableArray2.default)(array.slice(0, moveIndex)), (0, _toConsumableArray2.default)(array.slice(moveIndex + 1, toIndex + 1)), [item], (0, _toConsumableArray2.default)(array.slice(toIndex + 1, length)));
  132. }
  133. return array;
  134. }