140 lines
4.9 KiB
JavaScript
140 lines
4.9 KiB
JavaScript
|
"use strict";
|
||
|
|
||
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
exports.cloneByNamePathList = cloneByNamePathList;
|
||
|
exports.containsNamePath = containsNamePath;
|
||
|
exports.defaultGetValueFromEvent = defaultGetValueFromEvent;
|
||
|
exports.getNamePath = getNamePath;
|
||
|
Object.defineProperty(exports, "getValue", {
|
||
|
enumerable: true,
|
||
|
get: function get() {
|
||
|
return _get.default;
|
||
|
}
|
||
|
});
|
||
|
exports.isSimilar = isSimilar;
|
||
|
exports.matchNamePath = matchNamePath;
|
||
|
exports.move = move;
|
||
|
Object.defineProperty(exports, "setValue", {
|
||
|
enumerable: true,
|
||
|
get: function get() {
|
||
|
return _set.default;
|
||
|
}
|
||
|
});
|
||
|
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
|
||
|
var _typeof2 = _interopRequireDefault(require("@babel/runtime/helpers/typeof"));
|
||
|
var _get = _interopRequireDefault(require("rc-util/lib/utils/get"));
|
||
|
var _set = _interopRequireDefault(require("rc-util/lib/utils/set"));
|
||
|
var _typeUtil = require("./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]
|
||
|
*/
|
||
|
function getNamePath(path) {
|
||
|
return (0, _typeUtil.toArray)(path);
|
||
|
}
|
||
|
function cloneByNamePathList(store, namePathList) {
|
||
|
var newStore = {};
|
||
|
namePathList.forEach(function (namePath) {
|
||
|
var value = (0, _get.default)(store, namePath);
|
||
|
newStore = (0, _set.default)(newStore, namePath, value);
|
||
|
});
|
||
|
return newStore;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Check if `namePathList` includes `namePath`.
|
||
|
* @param namePathList A list of `InternalNamePath[]`
|
||
|
* @param namePath Compare `InternalNamePath`
|
||
|
* @param partialMatch True will make `[a, b]` match `[a, b, c]`
|
||
|
*/
|
||
|
function containsNamePath(namePathList, namePath) {
|
||
|
var partialMatch = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
||
|
return namePathList && namePathList.some(function (path) {
|
||
|
return matchNamePath(namePath, path, partialMatch);
|
||
|
});
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Check if `namePath` is super set or equal of `subNamePath`.
|
||
|
* @param namePath A list of `InternalNamePath[]`
|
||
|
* @param subNamePath Compare `InternalNamePath`
|
||
|
* @param partialMatch True will make `[a, b]` match `[a, b, c]`
|
||
|
*/
|
||
|
function matchNamePath(namePath, subNamePath) {
|
||
|
var partialMatch = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
||
|
if (!namePath || !subNamePath) {
|
||
|
return false;
|
||
|
}
|
||
|
if (!partialMatch && namePath.length !== subNamePath.length) {
|
||
|
return false;
|
||
|
}
|
||
|
return subNamePath.every(function (nameUnit, i) {
|
||
|
return namePath[i] === nameUnit;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
// Like `shallowEqual`, but we not check the data which may cause re-render
|
||
|
|
||
|
function isSimilar(source, target) {
|
||
|
if (source === target) {
|
||
|
return true;
|
||
|
}
|
||
|
if (!source && target || source && !target) {
|
||
|
return false;
|
||
|
}
|
||
|
if (!source || !target || (0, _typeof2.default)(source) !== 'object' || (0, _typeof2.default)(target) !== 'object') {
|
||
|
return false;
|
||
|
}
|
||
|
var sourceKeys = Object.keys(source);
|
||
|
var targetKeys = Object.keys(target);
|
||
|
var keys = new Set([].concat(sourceKeys, targetKeys));
|
||
|
return (0, _toConsumableArray2.default)(keys).every(function (key) {
|
||
|
var sourceValue = source[key];
|
||
|
var targetValue = target[key];
|
||
|
if (typeof sourceValue === 'function' && typeof targetValue === 'function') {
|
||
|
return true;
|
||
|
}
|
||
|
return sourceValue === targetValue;
|
||
|
});
|
||
|
}
|
||
|
function defaultGetValueFromEvent(valuePropName) {
|
||
|
var event = arguments.length <= 1 ? undefined : arguments[1];
|
||
|
if (event && event.target && (0, _typeof2.default)(event.target) === 'object' && 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)
|
||
|
*/
|
||
|
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((0, _toConsumableArray2.default)(array.slice(0, toIndex)), [item], (0, _toConsumableArray2.default)(array.slice(toIndex, moveIndex)), (0, _toConsumableArray2.default)(array.slice(moveIndex + 1, length)));
|
||
|
}
|
||
|
if (diff < 0) {
|
||
|
// move right
|
||
|
return [].concat((0, _toConsumableArray2.default)(array.slice(0, moveIndex)), (0, _toConsumableArray2.default)(array.slice(moveIndex + 1, toIndex + 1)), [item], (0, _toConsumableArray2.default)(array.slice(toIndex + 1, length)));
|
||
|
}
|
||
|
return array;
|
||
|
}
|