@onehat/data 1.7.9 → 1.7.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -347,6 +347,18 @@ describe('Entity', function() {
347
347
  expect(_.isEqual(result, expected)).to.be.true;
348
348
  });
349
349
 
350
+ it('getChangedValues', function() {
351
+ this.entity.foo = 2;
352
+ const result = this.entity.getChangedValues(),
353
+ expected = {
354
+ foo: {
355
+ original: 1,
356
+ current: 2,
357
+ },
358
+ };
359
+ expect(_.isEqual(result, expected)).to.be.true;
360
+ });
361
+
350
362
  it('data', function() {
351
363
  const result = this.entity.data,
352
364
  expected = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.7.9",
3
+ "version": "1.7.10",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/Entity.js CHANGED
@@ -711,21 +711,39 @@ class Entity extends EventEmitter {
711
711
  * @return {array|boolean} diff - Array of property names that have changed, or false
712
712
  */
713
713
  getChanged = () => {
714
- const obj1 = this._originalDataParsed,
715
- obj2 = this.getRawValues(),
716
- diff = Object.keys(obj1).reduce((result, key) => { // from https://stackoverflow.com/a/40610459/9163076
717
- if (obj2 && !obj2.hasOwnProperty(key)) {
714
+ const original = this._originalDataParsed,
715
+ current = this.getRawValues(),
716
+ diff = Object.keys(original).reduce((result, key) => { // from https://stackoverflow.com/a/40610459/9163076
717
+ if (current && !current.hasOwnProperty(key)) {
718
718
  result.push(key);
719
- } else if (_.isEqual(obj1[key], obj2[key])) {
719
+ } else if (_.isEqual(original[key], current[key])) {
720
720
  const resultKeyIndex = result.indexOf(key);
721
721
  result.splice(resultKeyIndex, 1);
722
722
  }
723
723
  return result;
724
- }, Object.keys(obj2));
724
+ }, Object.keys(current));
725
725
 
726
726
  return !_.isEmpty(diff) ? diff : false;
727
727
  }
728
728
 
729
+ /**
730
+ * Gets a comprehensive analysis of what has changed since the last save
731
+ * @return {object} changedPropertyValues - Object representing each changed field and both its original and current value
732
+ */
733
+ getChangedValues = () => {
734
+ const original = this._originalDataParsed,
735
+ current = this.getRawValues(),
736
+ names = this.getChanged();
737
+ const changedPropertyValues = {};
738
+ _.each(names, (name) => {
739
+ changedPropertyValues[name] = {
740
+ original: original[name],
741
+ current: current[name],
742
+ };
743
+ });
744
+ return changedPropertyValues;
745
+ }
746
+
729
747
  /**
730
748
  * Alias for this.submitValues
731
749
  */