react-chrome-redux
Version:
A set of utilities for building Redux applications in Google Chrome Extensions.
42 lines (36 loc) • 998 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = shallowDiff;
var _constants = require('../constants');
/**
* Returns a new Object containing only the fields in `new` that differ from `old`
*
* @param {Object} old
* @param {Object} new
* @return {Array} An array of changes. The changes have a `key`, `value`, and `change`.
* The change is either `updated`, which is if the value has changed or been added,
* or `removed`.
*/
function shallowDiff(oldObj, newObj) {
var difference = [];
Object.keys(newObj).forEach(function (key) {
if (oldObj[key] !== newObj[key]) {
difference.push({
key: key,
value: newObj[key],
change: _constants.DIFF_STATUS_UPDATED
});
}
});
Object.keys(oldObj).forEach(function (key) {
if (!newObj.hasOwnProperty(key)) {
difference.push({
key: key,
change: _constants.DIFF_STATUS_REMOVED
});
}
});
return difference;
}