UNPKG

3.98 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.getNamePath = getNamePath;
7exports.containsNamePath = containsNamePath;
8exports.setValues = setValues;
9exports.matchNamePath = matchNamePath;
10
11var _typeUtil = require("./typeUtil");
12
13function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
14
15function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
16
17function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
18
19function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
20
21function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
22
23function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
24
25function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
26
27function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); }
28
29/**
30 * Convert name to internal supported format.
31 * This function should keep since we still thinking if need support like `a.b.c` format.
32 * 'a' => ['a']
33 * 123 => [123]
34 * ['a', 123] => ['a', 123]
35 */
36function getNamePath(path) {
37 return (0, _typeUtil.toArray)(path);
38}
39
40function containsNamePath(namePathList, namePath) {
41 return namePathList && namePathList.some(function (path) {
42 return matchNamePath(path, namePath);
43 });
44}
45
46function isObject(obj) {
47 return _typeof(obj) === 'object' && obj !== null && Object.getPrototypeOf(obj) === Object.prototype;
48}
49/**
50 * Copy values into store and return a new values object
51 * ({ a: 1, b: { c: 2 } }, { a: 4, b: { d: 5 } }) => { a: 4, b: { c: 2, d: 5 } }
52 */
53
54
55function internalSetValues(store, values) {
56 var newStore = Array.isArray(store) ? _toConsumableArray(store) : _extends({}, store);
57
58 if (!values) {
59 return newStore;
60 }
61
62 Object.keys(values).forEach(function (key) {
63 var prevValue = newStore[key];
64 var value = values[key]; // If both are object (but target is not array), we use recursion to set deep value
65
66 var recursive = isObject(prevValue) && isObject(value);
67 newStore[key] = recursive ? internalSetValues(prevValue, value || {}) : value;
68 });
69 return newStore;
70}
71
72function setValues(store) {
73 for (var _len = arguments.length, restValues = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
74 restValues[_key - 1] = arguments[_key];
75 }
76
77 return restValues.reduce(function (current, newStore) {
78 return internalSetValues(current, newStore);
79 }, store);
80}
81
82function matchNamePath(namePath, changedNamePath) {
83 if (!namePath || !changedNamePath || namePath.length !== changedNamePath.length) {
84 return false;
85 }
86
87 return namePath.every(function (nameUnit, i) {
88 return changedNamePath[i] === nameUnit;
89 });
90}
\No newline at end of file