UNPKG

regular-redux-undo

Version:

the plugin of regular-redux to archieve undo and redo

98 lines (87 loc) 2.51 kB
/** * 类型判断 * @param {item} target 检测对象 */ export function typeOf(target) { return Object.prototype.toString.call(target).slice(8, -1).toLowerCase(); } /** * shallowly copy of an object or an array * @param {Object or array} target * @return {Object or array} the new one */ export function clone(target) { let type = typeOf(target); if (type === 'array') { return [].slice.call(target); } if (type === 'object') { return Object.assign({}, target); } return target; } /** * to extend an object, when the parameter(overwrite) is true , It equals to Object.assign * @param {Object} to the target object * @param {Object} from the reference object * @param {Boolean} overwrite force to overwrite the value whether the old value is existed or not * @return the target object after extended */ export function extend(to, from, overwrite) { if (typeOf(to) !== 'object' || typeOf(from) !== 'object') { return to; } for (let i in from) { if (overwrite || to[i] === undefined || to[i] == null) { to[i] = from[i]; } } return to; } /** * to see an object or an array is empty or not * @param {Object or Array} target * @return {Boolean} the result */ export function isEmpty(target) { let type = typeOf(target); if (type === 'object') { return !Object.keys(target).length; } if (type === 'array') { return !target.length; } return true; } /** * the forEach of an object * @param {Object} obj the target to traverse * @param {*} fn */ export function forEachValue(obj = {}, fn) { Object.keys(obj).forEach(key => fn(obj[key], key)) } /** * determine whether a condition is true or not * @param {Boolean Expression} condition the expression to be judged * @param {String} msg the message to throw when the condition is false */ export function assert(condition, msg) { if (!condition) throw new Error(`[regular-redux] ${msg}`) } /** * log a warning by some condition * @param {Boolean Expression} condition the expression to be judged * @param {String} msg the message to throw when the condition is true */ export function warning(condition, msg) { if (condition) console.warn(`[regular-redux] ${msg}`) } /** * log a error by some condition * @param {Boolean Expression} condition the expression to be judged * @param {String} msg the message to throw when the condition is true */ export function error(condition, msg) { if (condition) console.error(`[regular-redux] ${msg}`) }