valueUtil.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
  2. import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
  3. import _typeof from "@babel/runtime/helpers/esm/typeof";
  4. import { toArray } from './typeUtil';
  5. import get from '../../vc-util/get';
  6. import set from '../../vc-util/set';
  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 getValue(store, namePath) {
  18. var value = get(store, namePath);
  19. return value;
  20. }
  21. export function setValue(store, namePath, value) {
  22. var removeIfUndefined = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  23. var newStore = set(store, namePath, value, removeIfUndefined);
  24. return newStore;
  25. }
  26. export function containsNamePath(namePathList, namePath) {
  27. return namePathList && namePathList.some(function (path) {
  28. return matchNamePath(path, namePath);
  29. });
  30. }
  31. function isObject(obj) {
  32. return _typeof(obj) === 'object' && obj !== null && Object.getPrototypeOf(obj) === Object.prototype;
  33. }
  34. /**
  35. * Copy values into store and return a new values object
  36. * ({ a: 1, b: { c: 2 } }, { a: 4, b: { d: 5 } }) => { a: 4, b: { c: 2, d: 5 } }
  37. */
  38. function internalSetValues(store, values) {
  39. var newStore = Array.isArray(store) ? _toConsumableArray(store) : _objectSpread({}, store);
  40. if (!values) {
  41. return newStore;
  42. }
  43. Object.keys(values).forEach(function (key) {
  44. var prevValue = newStore[key];
  45. var value = values[key];
  46. // If both are object (but target is not array), we use recursion to set deep value
  47. var recursive = isObject(prevValue) && isObject(value);
  48. newStore[key] = recursive ? internalSetValues(prevValue, value || {}) : value;
  49. });
  50. return newStore;
  51. }
  52. export function setValues(store) {
  53. for (var _len = arguments.length, restValues = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  54. restValues[_key - 1] = arguments[_key];
  55. }
  56. return restValues.reduce(function (current, newStore) {
  57. return internalSetValues(current, newStore);
  58. }, store);
  59. }
  60. export function cloneByNamePathList(store, namePathList) {
  61. var newStore = {};
  62. namePathList.forEach(function (namePath) {
  63. var value = getValue(store, namePath);
  64. newStore = setValue(newStore, namePath, value);
  65. });
  66. return newStore;
  67. }
  68. export function matchNamePath(namePath, changedNamePath) {
  69. if (!namePath || !changedNamePath || namePath.length !== changedNamePath.length) {
  70. return false;
  71. }
  72. return namePath.every(function (nameUnit, i) {
  73. return changedNamePath[i] === nameUnit;
  74. });
  75. }