valueUtil.js
4.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import _typeof from "@babel/runtime/helpers/esm/typeof";
import get from "rc-util/es/utils/get";
import set from "rc-util/es/utils/set";
import { toArray } from './typeUtil';
/**
* Convert name to internal supported format.
* This function should keep since we still thinking if need support like `a.b.c` format.
* 'a' => ['a']
* 123 => [123]
* ['a', 123] => ['a', 123]
*/
export function getNamePath(path) {
return toArray(path);
}
export function getValue(store, namePath) {
var value = get(store, namePath);
return value;
}
export function setValue(store, namePath, value) {
var newStore = set(store, namePath, value);
return newStore;
}
export function cloneByNamePathList(store, namePathList) {
var newStore = {};
namePathList.forEach(function (namePath) {
var value = getValue(store, namePath);
newStore = setValue(newStore, namePath, value);
});
return newStore;
}
export function containsNamePath(namePathList, namePath) {
return namePathList && namePathList.some(function (path) {
return matchNamePath(path, namePath);
});
}
function isObject(obj) {
return _typeof(obj) === 'object' && obj !== null && Object.getPrototypeOf(obj) === Object.prototype;
}
/**
* Copy values into store and return a new values object
* ({ a: 1, b: { c: 2 } }, { a: 4, b: { d: 5 } }) => { a: 4, b: { c: 2, d: 5 } }
*/
function internalSetValues(store, values) {
var newStore = Array.isArray(store) ? _toConsumableArray(store) : _objectSpread({}, store);
if (!values) {
return newStore;
}
Object.keys(values).forEach(function (key) {
var prevValue = newStore[key];
var value = values[key]; // If both are object (but target is not array), we use recursion to set deep value
var recursive = isObject(prevValue) && isObject(value);
newStore[key] = recursive ? internalSetValues(prevValue, value || {}) : value;
});
return newStore;
}
export function setValues(store) {
for (var _len = arguments.length, restValues = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
restValues[_key - 1] = arguments[_key];
}
return restValues.reduce(function (current, newStore) {
return internalSetValues(current, newStore);
}, store);
}
export function matchNamePath(namePath, changedNamePath) {
if (!namePath || !changedNamePath || namePath.length !== changedNamePath.length) {
return false;
}
return namePath.every(function (nameUnit, i) {
return changedNamePath[i] === nameUnit;
});
}
export function isSimilar(source, target) {
if (source === target) {
return true;
}
if (!source && target || source && !target) {
return false;
}
if (!source || !target || _typeof(source) !== 'object' || _typeof(target) !== 'object') {
return false;
}
var sourceKeys = Object.keys(source);
var targetKeys = Object.keys(target);
var keys = new Set([].concat(_toConsumableArray(sourceKeys), _toConsumableArray(targetKeys)));
return _toConsumableArray(keys).every(function (key) {
var sourceValue = source[key];
var targetValue = target[key];
if (typeof sourceValue === 'function' && typeof targetValue === 'function') {
return true;
}
return sourceValue === targetValue;
});
}
export function defaultGetValueFromEvent(valuePropName) {
var event = arguments.length <= 1 ? undefined : arguments[1];
if (event && event.target && valuePropName in event.target) {
return event.target[valuePropName];
}
return event;
}
/**
* Moves an array item from one position in an array to another.
*
* Note: This is a pure function so a new array will be returned, instead
* of altering the array argument.
*
* @param array Array in which to move an item. (required)
* @param moveIndex The index of the item to move. (required)
* @param toIndex The index to move item at moveIndex to. (required)
*/
export function move(array, moveIndex, toIndex) {
var length = array.length;
if (moveIndex < 0 || moveIndex >= length || toIndex < 0 || toIndex >= length) {
return array;
}
var item = array[moveIndex];
var diff = moveIndex - toIndex;
if (diff > 0) {
// move left
return [].concat(_toConsumableArray(array.slice(0, toIndex)), [item], _toConsumableArray(array.slice(toIndex, moveIndex)), _toConsumableArray(array.slice(moveIndex + 1, length)));
}
if (diff < 0) {
// move right
return [].concat(_toConsumableArray(array.slice(0, moveIndex)), _toConsumableArray(array.slice(moveIndex + 1, toIndex + 1)), [item], _toConsumableArray(array.slice(toIndex + 1, length)));
}
return array;
}