1 | import { getNumberFormattedVal } from './helper';
|
2 |
|
3 | /**
|
4 | * The wrapper class on top of the primitive value of a field.
|
5 | *
|
6 | * @todo Need to have support for StringValue, NumberValue, DateTimeValue
|
7 | * and GeoValue. These types should expose predicate API mostly.
|
8 | */
|
9 | class Value {
|
10 |
|
11 | /**
|
12 | * Creates new Value instance.
|
13 | *
|
14 | * @param {*} val - the primitive value from the field cell.
|
15 | * @param {string | Field} field - The field from which the value belongs.
|
16 | */
|
17 | constructor (value, rawValue, field) {
|
18 | const formattedValue = getNumberFormattedVal(field, value);
|
19 |
|
20 | Object.defineProperties(this, {
|
21 | _value: {
|
22 | enumerable: false,
|
23 | configurable: false,
|
24 | writable: false,
|
25 | value
|
26 | },
|
27 | _formattedValue: {
|
28 | enumerable: false,
|
29 | configurable: false,
|
30 | writable: false,
|
31 | value: formattedValue
|
32 | },
|
33 | _internalValue: {
|
34 | enumerable: false,
|
35 | configurable: false,
|
36 | writable: false,
|
37 | value: rawValue
|
38 | }
|
39 | });
|
40 |
|
41 | this.field = field;
|
42 | }
|
43 |
|
44 | /**
|
45 | * Returns the field value.
|
46 | *
|
47 | * @return {*} Returns the current value.
|
48 | */
|
49 | get value () {
|
50 | return this._value;
|
51 | }
|
52 |
|
53 | /**
|
54 | * Returns the parsed value of field
|
55 | */
|
56 | get formattedValue () {
|
57 | return this._formattedValue;
|
58 | }
|
59 |
|
60 | /**
|
61 | * Returns the internal value of field
|
62 | */
|
63 | get internalValue () {
|
64 | return this._internalValue;
|
65 | }
|
66 |
|
67 | /**
|
68 | * Converts to human readable string.
|
69 | *
|
70 | * @override
|
71 | * @return {string} Returns a human readable string of the field value.
|
72 | *
|
73 | */
|
74 | toString () {
|
75 | return String(this.value);
|
76 | }
|
77 |
|
78 | /**
|
79 | * Returns the value of the field.
|
80 | *
|
81 | * @override
|
82 | * @return {*} Returns the field value.
|
83 | */
|
84 | valueOf () {
|
85 | return this.value;
|
86 | }
|
87 | }
|
88 |
|
89 | export default Value;
|