@onehat/data 1.8.2 → 1.8.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.
@@ -21,4 +21,26 @@ describe('PercentProperty', function() {
21
21
  expect(this.property.displayValue).to.be.eq('12.325%');
22
22
  });
23
23
 
24
+ it('omitZeros', function() {
25
+ this.property.setValue('1234.56');
26
+ expect(this.property.displayValue).to.be.eq('1234.560%');
27
+
28
+ this.property.setValue('1234.50');
29
+ expect(this.property.displayValue).to.be.eq('1234.500%');
30
+
31
+ this.property.setValue('1234.00');
32
+ expect(this.property.displayValue).to.be.eq('1234.000%');
33
+
34
+ this.property.omitZeros = true;
35
+
36
+ this.property.setValue('1234.56');
37
+ expect(this.property.displayValue).to.be.eq('1234.56%');
38
+
39
+ this.property.setValue('1234.50');
40
+ expect(this.property.displayValue).to.be.eq('1234.5%');
41
+
42
+ this.property.setValue('1234.00');
43
+ expect(this.property.displayValue).to.be.eq('1234%');
44
+ });
45
+
24
46
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onehat/data",
3
- "version": "1.8.2",
3
+ "version": "1.8.3",
4
4
  "description": "JS data modeling package with adapters for many storage mediums.",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
@@ -8,12 +8,22 @@ import Formatters from '../Util/Formatters';
8
8
  * @extends PercentProperty
9
9
  */
10
10
  export default class PercentIntProperty extends FloatProperty {
11
+
12
+ constructor(config = {}) {
13
+ super(...arguments);
14
+
15
+ const defaults = {
16
+ omitZeros: false, // Should we omit any .00 at the end?
17
+ };
18
+
19
+ _.merge(this, defaults, config);
20
+ }
11
21
 
12
22
  getDisplayValue = () => {
13
23
  if (this.isDestroyed) {
14
24
  throw Error('this.getDisplayValue is no longer valid. Property has been destroyed.');
15
25
  }
16
- return Formatters.FormatPercentInt(this.parsedValue);
26
+ return Formatters.FormatPercentInt(this.parsedValue, this.omitZeros);
17
27
  }
18
28
 
19
29
  };
@@ -168,10 +168,13 @@ class Formatters {
168
168
  return value.toFixed(precision) + '%';
169
169
  }
170
170
 
171
- static FormatPercentInt = (str) => {
171
+ static FormatPercentInt = (str, omitZeros = false) => {
172
172
  if (_.isNil(str)) {
173
173
  return str;
174
174
  }
175
+ if (omitZeros) {
176
+ str = parseFloat(str).toString();
177
+ }
175
178
  return '' + str + '%';
176
179
  }
177
180