@onehat/data 1.4.9 → 1.4.13

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.
@@ -60,6 +60,28 @@ describe('CurrencyProperty', function() {
60
60
  expect(formatted).to.be.eq('$1,234.56');
61
61
  });
62
62
 
63
+ it('omitZeros', function() {
64
+ this.property.setValue('1234.56');
65
+ expect(this.property.displayValue).to.be.eq('$1,234.56');
66
+
67
+ this.property.setValue('1234.50');
68
+ expect(this.property.displayValue).to.be.eq('$1,234.50');
69
+
70
+ this.property.setValue('1234.00');
71
+ expect(this.property.displayValue).to.be.eq('$1,234.00');
72
+
73
+ this.property.omitZeros = true;
74
+
75
+ this.property.setValue('1234.56');
76
+ expect(this.property.displayValue).to.be.eq('$1,234.56');
77
+
78
+ this.property.setValue('1234.50');
79
+ expect(this.property.displayValue).to.be.eq('$1,234.50');
80
+
81
+ this.property.setValue('1234.00');
82
+ expect(this.property.displayValue).to.be.eq('$1,234');
83
+ });
84
+
63
85
  it('submitValue', function() {
64
86
  this.property.setValue(123.156);
65
87
  const formatted = this.property.submitValue;
@@ -30,6 +30,43 @@ describe('OneBuildRepository', function() {
30
30
  });
31
31
 
32
32
  describe('Params', function() {
33
+
34
+ it('setParam', function() {
35
+ const r = this.repository;
36
+ r.setParam('test', 1);
37
+
38
+ expect(r._params.test).to.be.eq(1);
39
+ });
40
+
41
+ it('setParams', function() {
42
+ const r = this.repository;
43
+ r.setParams({
44
+ test1: 1,
45
+ test2: 2,
46
+ });
47
+
48
+ expect(r._params.test1).to.be.eq(1);
49
+ expect(r._params.test2).to.be.eq(2);
50
+ });
51
+
52
+ it('setBaseParam', function() {
53
+ const r = this.repository;
54
+ r.setBaseParam('test', 1);
55
+
56
+ expect(r._baseParams.test).to.be.eq(1);
57
+ });
58
+
59
+ it('setBaseParams', function() {
60
+ const r = this.repository;
61
+ r.setBaseParams({
62
+ test1: 1,
63
+ test2: 2,
64
+ });
65
+
66
+ expect(r._baseParams.test1).to.be.eq(1);
67
+ expect(r._baseParams.test2).to.be.eq(2);
68
+ });
69
+
33
70
  it('setValuelessParam', function() {
34
71
  const r = this.repository;
35
72
  r.setParam('conditions[field]', 1);
@@ -38,6 +75,24 @@ describe('OneBuildRepository', function() {
38
75
  expect(r._params.conditions.field).to.be.eq(1);
39
76
  expect(r._params.conditions[0]).to.be.eq('field IS NOT NULL');
40
77
  });
78
+
79
+ it('clearParams', function() {
80
+ const r = this.repository;
81
+ r.setParams({
82
+ test1: 1,
83
+ test2: 2,
84
+ });
85
+ r.setBaseParams({
86
+ test1: 1,
87
+ test2: 2,
88
+ });
89
+
90
+ r.clearParams(false, true);
91
+
92
+ expect(r._params).to.be.empty;
93
+ expect(r._baseParams).to.be.empty;
94
+ });
95
+
41
96
  });
42
97
 
43
98
  describe('custom', function() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.4.9",
3
+ "version": "1.4.13",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -38,26 +38,26 @@
38
38
  "@onehat/events": "^1.6.5",
39
39
  "accounting-js": "^1.1.1",
40
40
  "axios": "^0.21.4",
41
- "chrono-node": "^2.3.0",
41
+ "chrono-node": "^2.3.5",
42
42
  "fast-xml-parser": "^3.19.0",
43
43
  "he": "^1.2.0",
44
44
  "js-base64": "^3.6.1",
45
45
  "lodash": "^4.17.21",
46
46
  "moment": "^2.29.1",
47
47
  "numeral": "^2.0.6",
48
- "qs": "^6.10.1",
48
+ "qs": "^6.10.3",
49
49
  "relative-time-parser": "^1.0.13",
50
50
  "uuid": "^8.3.2"
51
51
  },
52
52
  "devDependencies": {
53
- "@babel/core": "^7.14.6",
54
- "@babel/node": "^7.14.7",
55
- "@babel/plugin-proposal-class-properties": "^7.14.5",
56
- "@babel/plugin-transform-runtime": "^7.14.5",
57
- "@babel/preset-env": "^7.14.7",
58
- "@babel/register": "^7.14.5",
59
- "@babel/runtime": "^7.14.6",
60
- "@cypress/webpack-preprocessor": "^5.9.1",
53
+ "@babel/core": "^7.16.7",
54
+ "@babel/node": "^7.16.8",
55
+ "@babel/plugin-proposal-class-properties": "^7.16.7",
56
+ "@babel/plugin-transform-runtime": "^7.16.8",
57
+ "@babel/preset-env": "^7.16.8",
58
+ "@babel/register": "^7.16.9",
59
+ "@babel/runtime": "^7.16.7",
60
+ "@cypress/webpack-preprocessor": "^5.11.0",
61
61
  "babel-loader": "^8.2.2",
62
62
  "cypress": "5.2.0",
63
63
  "ink-docstrap": "^1.3.2",
@@ -26,6 +26,7 @@ export default class CurrencyProperty extends Property {
26
26
  },
27
27
  submitAsString: true, // NOTE, we want to use the accounting.toFixed() method by default
28
28
  defaultValue: 0.00,
29
+ omitZeros: false, // Should we omit any .00 at the end?
29
30
  };
30
31
 
31
32
  _.merge(this, defaults, config);
@@ -45,7 +46,12 @@ export default class CurrencyProperty extends Property {
45
46
  if (this.isDestroyed) {
46
47
  throw Error('this.getDisplayValue is no longer valid. Property has been destroyed.');
47
48
  }
48
- return accounting.formatMoney(this.parsedValue, this.displayOptions);
49
+
50
+ let ret = accounting.formatMoney(this.parsedValue, this.displayOptions)
51
+ if (this.omitZeros && ret.match(/\.00$/)) {
52
+ ret = ret.replace(/\.00$/, '');
53
+ }
54
+ return ret;
49
55
  }
50
56
 
51
57
  getSubmitValue = () => {
@@ -87,9 +87,9 @@ class AjaxRepository extends Repository {
87
87
  headers: {},
88
88
 
89
89
  /**
90
- * @member {object} baseParams - Params that will be applied to every request
90
+ * @member {object} _baseParams - Params that will be applied to every request
91
91
  */
92
- baseParams: {},
92
+ _baseParams: {},
93
93
 
94
94
  };
95
95
  _.merge(this, defaults, config);
@@ -202,8 +202,7 @@ class AjaxRepository extends Repository {
202
202
  setParam = (name, value, isBaseParam = false) => {
203
203
  const re = /^([^\[]+)\[([^\]]+)\](.*)$/,
204
204
  matches = name.match(re),
205
- paramsToChange = isBaseParam ? this.baseParams : this._params;
206
-
205
+ paramsToChange = isBaseParam ? this._baseParams : this._params;
207
206
 
208
207
  if (matches) { // name has array notation like 'conditions[username]'
209
208
  const first = matches[1],
@@ -234,7 +233,7 @@ class AjaxRepository extends Repository {
234
233
  setValuelessParam = (name, isBaseParam = false) => {
235
234
  const re = /^([^\[]+)\[([^\]]+)\](.*)$/,
236
235
  matches = name.match(re),
237
- paramsToChange = isBaseParam ? this.baseParams : this._params;
236
+ paramsToChange = isBaseParam ? this._baseParams : this._params;
238
237
 
239
238
  if (matches) { // name has array notation like 'conditions[username]'
240
239
  const first = matches[1],
@@ -262,24 +261,35 @@ class AjaxRepository extends Repository {
262
261
  });
263
262
  }
264
263
 
264
+ /**
265
+ * Sets base query param
266
+ * @param {string} name - Param name to set.
267
+ * @param {any} value - Param value to set.
268
+ */
269
+ setBaseParam = (name, value) => {
270
+ this.setParam(name, value, true);
271
+ }
272
+
265
273
  /**
266
274
  * Sets base query params. These params are sent on every request.
267
275
  * @param {object} params - Base params to set. Key is parameter name, value is parameter value
268
276
  */
269
277
  setBaseParams = (params) => {
270
278
  _.each(params, (value, name) => {
271
- this.setParam(name, value, true);
279
+ this.setBaseParam(name, value);
272
280
  });
273
281
  }
274
282
 
275
283
  /**
276
- * Manually clears all existing (non-base) params
277
- * including sorting, filtering, and pagination.
284
+ * Manually clears all (non-base) params including filtering.
278
285
  * *Not intended for normal usage,* but rather for testing.
279
286
  * @param {boolean} reload - Whether to reload repository. Defaults to false.
280
287
  */
281
- clearParams = (reload = false) => {
288
+ clearParams = (reload = false, clearBase = false) => {
282
289
  this._params = {};
290
+ if (clearBase) {
291
+ this._baseParams = {};
292
+ }
283
293
  if (reload && this.isLoaded) {
284
294
  return this.reload();
285
295
  }
@@ -292,8 +302,8 @@ class AjaxRepository extends Repository {
292
302
  */
293
303
  _onChangeSorters = () => {
294
304
  const sorter = this.sorters[0];
295
- this.setParam(this.paramSort, sorter.name, true); // true to set baseParam
296
- this.setParam(this.paramDirection, sorter.direction, true);
305
+ this.setBaseParam(this.paramSort, sorter.name);
306
+ this.setBaseParam(this.paramDirection, sorter.direction);
297
307
 
298
308
  if (this.isLoaded) {
299
309
  return this.reload();
@@ -319,8 +329,8 @@ class AjaxRepository extends Repository {
319
329
  * Refreshes entities.
320
330
  */
321
331
  _onChangePagination = () => {
322
- this.setParam(this.paramPageNum, this.page, true); // true to set baseParam
323
- this.setParam(this.paramPageSize, this.pageSize, true);
332
+ this.setBaseParam(this.paramPageNum, this.page);
333
+ this.setBaseParam(this.paramPageSize, this.pageSize);
324
334
 
325
335
  if (this.isLoaded) {
326
336
  return this.reload();
@@ -360,7 +370,7 @@ class AjaxRepository extends Repository {
360
370
  }
361
371
 
362
372
  const repository = this;
363
- const data = _.assign({}, this.baseParams, this._params);
373
+ const data = _.assign({}, this._baseParams, this._params);
364
374
 
365
375
  return this._send(this.methods.get, this.api.get, data)
366
376
  .then(result => {
@@ -462,7 +472,7 @@ class AjaxRepository extends Repository {
462
472
  const params = {
463
473
  id: entity.id,
464
474
  };
465
- return _.assign({}, this.baseParams, params);
475
+ return _.assign({}, this._baseParams, params);
466
476
  }
467
477
 
468
478
  /**
@@ -173,7 +173,7 @@ class OneBuildRepository extends AjaxRepository {
173
173
  });
174
174
 
175
175
  if (!_.isEmpty(sorterStrings)) {
176
- this.setParam('order', sorterStrings.join(','));
176
+ this.setBaseParam('order', sorterStrings.join(','));
177
177
  }
178
178
 
179
179
  if (this.isLoaded) {