valueUtil.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.cloneByNamePathList = cloneByNamePathList;
  7. exports.containsNamePath = containsNamePath;
  8. exports.getNamePath = getNamePath;
  9. exports.getValue = getValue;
  10. exports.matchNamePath = matchNamePath;
  11. exports.setValue = setValue;
  12. exports.setValues = setValues;
  13. var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
  14. var _typeUtil = require("./typeUtil");
  15. var _get = _interopRequireDefault(require("../../vc-util/get"));
  16. var _set = _interopRequireDefault(require("../../vc-util/set"));
  17. /**
  18. * Convert name to internal supported format.
  19. * This function should keep since we still thinking if need support like `a.b.c` format.
  20. * 'a' => ['a']
  21. * 123 => [123]
  22. * ['a', 123] => ['a', 123]
  23. */
  24. function getNamePath(path) {
  25. return (0, _typeUtil.toArray)(path);
  26. }
  27. function getValue(store, namePath) {
  28. const value = (0, _get.default)(store, namePath);
  29. return value;
  30. }
  31. function setValue(store, namePath, value) {
  32. let removeIfUndefined = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : false;
  33. const newStore = (0, _set.default)(store, namePath, value, removeIfUndefined);
  34. return newStore;
  35. }
  36. function containsNamePath(namePathList, namePath) {
  37. return namePathList && namePathList.some(path => matchNamePath(path, namePath));
  38. }
  39. function isObject(obj) {
  40. return typeof obj === 'object' && obj !== null && Object.getPrototypeOf(obj) === Object.prototype;
  41. }
  42. /**
  43. * Copy values into store and return a new values object
  44. * ({ a: 1, b: { c: 2 } }, { a: 4, b: { d: 5 } }) => { a: 4, b: { c: 2, d: 5 } }
  45. */
  46. function internalSetValues(store, values) {
  47. const newStore = Array.isArray(store) ? [...store] : (0, _extends2.default)({}, store);
  48. if (!values) {
  49. return newStore;
  50. }
  51. Object.keys(values).forEach(key => {
  52. const prevValue = newStore[key];
  53. const value = values[key];
  54. // If both are object (but target is not array), we use recursion to set deep value
  55. const recursive = isObject(prevValue) && isObject(value);
  56. newStore[key] = recursive ? internalSetValues(prevValue, value || {}) : value;
  57. });
  58. return newStore;
  59. }
  60. function setValues(store) {
  61. for (var _len = arguments.length, restValues = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
  62. restValues[_key - 1] = arguments[_key];
  63. }
  64. return restValues.reduce((current, newStore) => internalSetValues(current, newStore), store);
  65. }
  66. function cloneByNamePathList(store, namePathList) {
  67. let newStore = {};
  68. namePathList.forEach(namePath => {
  69. const value = getValue(store, namePath);
  70. newStore = setValue(newStore, namePath, value);
  71. });
  72. return newStore;
  73. }
  74. function matchNamePath(namePath, changedNamePath) {
  75. if (!namePath || !changedNamePath || namePath.length !== changedNamePath.length) {
  76. return false;
  77. }
  78. return namePath.every((nameUnit, i) => changedNamePath[i] === nameUnit);
  79. }