name:
src/main/js/utils/Converters.ts
-rw-r--r--
4578
1import { F7Exception } from "../errors/F7Exception";
2import { ValueException } from "../errors/ValueException";
3import { Grid } from "../models/common/Grid";
4import { Complex } from "../models/common/Types";
5import { Numbers } from "./Numbers";
6import { isNull } from "./Other";
7
8export class Converters {
9 /**
10 * Convert a value to a number.
11 *
12 * @param value - to convert to number.
13 * @return number for sure, or exception thrown.
14 */
15 static toNumber(value: any): number {
16 if (typeof value === "number") {
17 return Converters.toPositiveZero(Converters.castAsNumber(value));
18 }
19 if (typeof value === "string") {
20 const converted = Numbers.toNumberOrNull(Converters.castAsString(value));
21 if (isNull(converted)) {
22 throw new ValueException("Cannot coerce to number");
23 }
24 return converted;
25 }
26 if (typeof value === "boolean") {
27 return Converters.castAsBoolean(value) ? 1 : 0;
28 }
29 if (value instanceof F7Exception) {
30 throw Converters.castAsF7Exception(value);
31 }
32 if (isNull(value)) {
33 return 0;
34 }
35 throw new ValueException("Cannot coerce to number");
36 }
37
38 /**
39 * Convert a value to a string.
40 *
41 * @param value - to convert to text.
42 * @return string for sure, or exception thrown.
43 */
44 static toText(value: any): string {
45 if (typeof value === "number") {
46 return Converters.castAsNumber(value).toString();
47 }
48 if (typeof value === "string") {
49 return Converters.castAsString(value);
50 }
51 if (typeof value === "boolean") {
52 return Converters.castAsBoolean(value) ? "TRUE" : "FALSE";
53 }
54 if (value instanceof F7Exception) {
55 throw Converters.castAsF7Exception(value);
56 }
57 if (isNull(value)) {
58 return "";
59 }
60 throw new ValueException("Cannot coerce to text");
61 }
62
63 /**
64 * Convert a value to a boolean.
65 *
66 * @param value - to convert to boolean.
67 * @return boolean for sure, or exception thrown.
68 */
69 static toBoolean(value: any): boolean {
70 if (typeof value === "number") {
71 return Converters.castAsNumber(value) !== 0;
72 }
73 if (typeof value === "string") {
74 const rawValue = Converters.castAsString(value).toLowerCase();
75 if (rawValue === "true") {
76 return true;
77 }
78 if (rawValue === "false" || rawValue === "") {
79 return false;
80 }
81 throw new ValueException("Cannot coerce to boolean");
82 }
83 if (typeof value === "boolean") {
84 return Converters.castAsBoolean(value);
85 }
86 if (value instanceof F7Exception) {
87 throw Converters.castAsF7Exception(value);
88 }
89 if (isNull(value)) {
90 return false;
91 }
92 throw new ValueException("Cannot coerce to boolean");
93 }
94
95 /**
96 * Convert string to zero. Return value if not string.
97 * @param value to possibly convert, or pass through.
98 */
99 static textToZero(value: Complex): Complex {
100 if (value instanceof Grid) {
101 const grid = Converters.castAsGrid(value);
102 if (grid.getColumns() == 1 && grid.getRows() == 1) {
103 return Converters.textToZero(grid.get(0, 0));
104 } else {
105 throw new ValueException("Grid value could not be found.");
106 }
107 }
108 if (typeof value === "string") {
109 return 0;
110 }
111 return value;
112 }
113
114 /**
115 * Pull first value.
116 * @param value - one or more values.
117 */
118 static first(value: any) {
119 if (value instanceof Grid) {
120 return Converters.castAsGrid(value).get(0, 0);
121 }
122 return value;
123 }
124
125 /**
126 * Convert negative zero to positive zero, if the value is zero. If not, just pass through.
127 * @param value - to convert maybe.
128 */
129 static toPositiveZero(value: any) {
130 if (typeof value === "number" && Numbers.isZero(value)) {
131 return 0;
132 }
133 return value;
134 }
135
136 /**
137 * Clean helper to cast a value as a number.
138 * @param value to cast.
139 */
140 static castAsNumber(value: any) {
141 return <number>value;
142 }
143
144 /**
145 * Clean helper to cast a value as an F7Exception.
146 * @param value to cast.
147 */
148 static castAsF7Exception(value: any) {
149 return <F7Exception>value;
150 }
151
152 /**
153 * Clean helper to cast a value as a Grid.
154 * @param value to cast.
155 */
156 static castAsGrid(value: Complex) {
157 return <Grid<Complex>>value;
158 }
159
160 /**
161 * Clean helper to cast a value as a string.
162 * @param value to cast.
163 */
164 static castAsString(value: any) {
165 return <string>value;
166 }
167
168 /**
169 * Clean helper to cast a value as a boolean.
170 * @param value to cast.
171 */
172 static castAsBoolean(value: any) {
173 return <boolean>value;
174 }
175}