@onehat/data 1.11.1 → 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.1",
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
@@ -1,7 +1,7 @@
1
1
  import moment from 'moment';
2
2
  import momentAlt from 'relative-time-parser'; // Notice this version of moment is imported from 'relative-time-parser', and may be out of sync with our general 'moment' package
3
3
  import accounting from 'accounting-js';
4
- // import * as chrono from 'chrono-node';
4
+ import * as chrono from 'chrono-node';
5
5
  import _ from 'lodash';
6
6
 
7
7
  class Parsers {
@@ -85,21 +85,24 @@ class Parsers {
85
85
  * @return {object} moment - A moment object
86
86
  */
87
87
  static ParseDate = (value, format = null) => {
88
+ if (moment.isMoment(value)) {
89
+ return value;
90
+ }
88
91
  let result;
89
92
  try {
90
93
  result = moment(value, format);
91
94
  } catch(err) {}
92
95
 
93
- // if ((!result || !result.isValid() && chrono)) {
94
- // // try using chrono
95
- // const parsed = chrono.parse(value);
96
- // if (parsed && parsed[0] && parsed[0].date) {
97
- // const dateString = parsed[0].date();
98
- // try {
99
- // result = moment(dateString);
100
- // } catch(err) {}
101
- // }
102
- // }
96
+ if ((!result || !result.isValid() && chrono)) {
97
+ // try using chrono
98
+ const parsed = chrono.parse(value);
99
+ if (parsed && parsed[0] && parsed[0].date) {
100
+ const dateString = parsed[0].date();
101
+ try {
102
+ result = moment(dateString);
103
+ } catch(err) {}
104
+ }
105
+ }
103
106
  return result;
104
107
  }
105
108