valueUtil.js 4.0 KB

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