wecui
Version:
一款基于Vue2.x版本的移动端web组件
125 lines (109 loc) • 4.02 kB
JavaScript
var assert = require('./assert');
var isObject = require('./isObject');
var isFunction = require('./isFunction');
var isArray = require('./isArray');
var isNumber = require('./isNumber');
var mixin = require('./mixin');
function getShallowCopy(x) {
if (isArray(x)) {
return x.concat();
}
if (x instanceof Date || x instanceof RegExp) {
return x;
}
if (isObject(x)) {
return mixin({}, x);
}
return x;
}
function update(instance, patch) {
if (process.env.NODE_ENV !== 'production') {
assert(isObject(patch), function () { return 'Invalid argument patch ' + assert.stringify(patch) + ' supplied to function update(instance, patch): expected an object containing commands'; });
}
var value = getShallowCopy(instance);
var isChanged = false;
for (var k in patch) {
if (patch.hasOwnProperty(k)) {
if (update.commands.hasOwnProperty(k)) {
value = update.commands[k](patch[k], value);
isChanged = true;
}
else {
var newValue = update(value[k], patch[k]);
isChanged = isChanged || ( newValue !== value[k] );
value[k] = newValue;
}
}
}
return isChanged ? value : instance;
}
// built-in commands
function $apply(f, value) {
if (process.env.NODE_ENV !== 'production') {
assert(isFunction(f), 'Invalid argument f supplied to immutability helper { $apply: f } (expected a function)');
}
return f(value);
}
function $push(elements, arr) {
if (process.env.NODE_ENV !== 'production') {
assert(isArray(elements), 'Invalid argument elements supplied to immutability helper { $push: elements } (expected an array)');
assert(isArray(arr), 'Invalid value supplied to immutability helper $push (expected an array)');
}
return arr.concat(elements);
}
function $remove(keys, obj) {
if (process.env.NODE_ENV !== 'production') {
assert(isArray(keys), 'Invalid argument keys supplied to immutability helper { $remove: keys } (expected an array)');
assert(isObject(obj), 'Invalid value supplied to immutability helper $remove (expected an object)');
}
for (var i = 0, len = keys.length; i < len; i++ ) {
delete obj[keys[i]];
}
return obj;
}
function $set(value) {
return value;
}
function $splice(splices, arr) {
if (process.env.NODE_ENV !== 'production') {
assert(isArray(splices) && splices.every(isArray), 'Invalid argument splices supplied to immutability helper { $splice: splices } (expected an array of arrays)');
assert(isArray(arr), 'Invalid value supplied to immutability helper $splice (expected an array)');
}
return splices.reduce(function (acc, splice) {
acc.splice.apply(acc, splice);
return acc;
}, arr);
}
function $swap(config, arr) {
if (process.env.NODE_ENV !== 'production') {
assert(isObject(config), 'Invalid argument config supplied to immutability helper { $swap: config } (expected an object)');
assert(isNumber(config.from), 'Invalid argument config.from supplied to immutability helper { $swap: config } (expected a number)');
assert(isNumber(config.to), 'Invalid argument config.to supplied to immutability helper { $swap: config } (expected a number)');
assert(isArray(arr), 'Invalid value supplied to immutability helper $swap (expected an array)');
}
var element = arr[config.to];
arr[config.to] = arr[config.from];
arr[config.from] = element;
return arr;
}
function $unshift(elements, arr) {
if (process.env.NODE_ENV !== 'production') {
assert(isArray(elements), 'Invalid argument elements supplied to immutability helper {$unshift: elements} (expected an array)');
assert(isArray(arr), 'Invalid value supplied to immutability helper $unshift (expected an array)');
}
return elements.concat(arr);
}
function $merge(obj, value) {
return mixin(mixin({}, value), obj, true);
}
update.commands = {
$apply: $apply,
$push: $push,
$remove: $remove,
$set: $set,
$splice: $splice,
$swap: $swap,
$unshift: $unshift,
$merge: $merge
};
module.exports = update;