UNPKG

regular-redux-undo

Version:

the plugin of regular-redux to archieve undo and redo

65 lines (51 loc) 1.4 kB
import {typeOf, clone} from './index'; export const set = function(target, pathes, value, options = {}) { if (options === true) { options = { assign: true } } let {assign, autoCreate} = options; pathes = Array.isArray(pathes) ? pathes : ('' + pathes).split('.'); let targetType = typeOf(target), valueType = typeOf(value), length = pathes.length; if (!length) { return !assign && targetType === 'object' && valueType === 'object' ? Object.assign({}, target, value) : value; } let nextPath = pathes.shift(); target = clone(target); if (target === undefined && options.autoCreate) { target = {}; } if (length === 1 && target[nextPath] === value) { return target; } target[nextPath] = set(target[nextPath], pathes, value, options); return target; } export const get = function(target, pathes) { if (typeof pathes === 'string') { pathes = pathes.split('.'); } target && pathes.some(path => { target = target[path] if (target === null || target === undefined) { return true; } }); return target; } /** * 数组的快速处理 * @param {*} target * @param {*} pathes * @param {*} args */ export const splice = function(target, pathes, ...args) { let list = get(target, pathes).slice(); list.splice.apply(list, args); return set(target, pathes, list, true); }