@onehat/data 1.11.2 → 1.11.3

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.
@@ -470,6 +470,14 @@ describe('Entity', function() {
470
470
  expect(_.isEqual(result, this.data)).to.be.true;
471
471
  });
472
472
 
473
+ it('hash', function() {
474
+ expect(this.entity.hash).to.be.eq(4635951664292355);
475
+
476
+ // change a property & check again
477
+ this.entity.foo = 3;
478
+ expect(this.entity.hash).to.be.eq(1510366977941323);
479
+ });
480
+
473
481
  });
474
482
 
475
483
  describe('setters', function() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.11.2",
3
+ "version": "1.11.3",
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
@@ -965,6 +965,41 @@ class Entity extends EventEmitter {
965
965
  return !_.isEqualWith(this._originalDataParsed, this.getParsedValues());
966
966
  }
967
967
 
968
+ /**
969
+ * Gets the a hash of the current submitValues.
970
+ * This allows easy detection of changes in data.
971
+ * @return {integer} hash
972
+ */
973
+ getHash = () => {
974
+ if (this.isDestroyed) {
975
+ throw Error('this.getHash is no longer valid. Entity has been destroyed.');
976
+ }
977
+
978
+ const str = JSON.stringify(this.submitValues);
979
+
980
+ // from https://github.com/bryc/code/blob/master/jshash/experimental/cyrb53.js
981
+ const seed = 0;
982
+ let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
983
+ for (let i = 0, ch; i < str.length; i++) {
984
+ ch = str.charCodeAt(i);
985
+ h1 = Math.imul(h1 ^ ch, 2654435761);
986
+ h2 = Math.imul(h2 ^ ch, 1597334677);
987
+ }
988
+ h1 = Math.imul(h1 ^ (h1>>>16), 2246822507) ^ Math.imul(h2 ^ (h2>>>13), 3266489909);
989
+ h2 = Math.imul(h2 ^ (h2>>>16), 2246822507) ^ Math.imul(h1 ^ (h1>>>13), 3266489909);
990
+ const hash = 4294967296 * (2097151 & h2) + (h1>>>0);
991
+
992
+ return hash;
993
+ }
994
+
995
+ /**
996
+ * Getter of the hash for this Entity.
997
+ * @return {integer} hash
998
+ */
999
+ get hash() {
1000
+ return this.getHash();
1001
+ }
1002
+
968
1003
  /**
969
1004
  * Gets the original data object for this Entity.
970
1005
  * This is either what was persisted to storage medium, or what was