@sankhyalabs/core 1.0.42 → 1.0.45
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.
- package/.eslintignore +1 -1
- package/.eslintrc.cjs +34 -34
- package/README.md +54 -54
- package/dist/dataunit/DataUnit.d.ts +87 -82
- package/dist/dataunit/DataUnit.js +290 -278
- package/dist/dataunit/DataUnit.js.map +1 -1
- package/dist/dataunit/metadata/DataType.d.ts +9 -9
- package/dist/dataunit/metadata/DataType.js +36 -36
- package/dist/dataunit/metadata/UnitMetadata.d.ts +71 -71
- package/dist/dataunit/metadata/UnitMetadata.js +31 -31
- package/dist/dataunit/state/HistReducer.d.ts +10 -10
- package/dist/dataunit/state/HistReducer.js +27 -27
- package/dist/dataunit/state/StateManager.d.ts +57 -57
- package/dist/dataunit/state/StateManager.js +96 -96
- package/dist/dataunit/state/action/DataUnitAction.d.ts +29 -28
- package/dist/dataunit/state/action/DataUnitAction.js +34 -33
- package/dist/dataunit/state/action/DataUnitAction.js.map +1 -1
- package/dist/dataunit/state/slice/AddedRecordsSlice.d.ts +11 -11
- package/dist/dataunit/state/slice/AddedRecordsSlice.js +25 -25
- package/dist/dataunit/state/slice/ChangesSlice.d.ts +12 -12
- package/dist/dataunit/state/slice/ChangesSlice.js +76 -72
- package/dist/dataunit/state/slice/ChangesSlice.js.map +1 -1
- package/dist/dataunit/state/slice/CurrentRecordsSlice.d.ts +11 -11
- package/dist/dataunit/state/slice/CurrentRecordsSlice.js +45 -45
- package/dist/dataunit/state/slice/RecordsSlice.d.ts +10 -10
- package/dist/dataunit/state/slice/RecordsSlice.js +43 -43
- package/dist/dataunit/state/slice/RemovedRecordsSlice.d.ts +9 -9
- package/dist/dataunit/state/slice/RemovedRecordsSlice.js +24 -24
- package/dist/dataunit/state/slice/SelectionSlice.d.ts +11 -11
- package/dist/dataunit/state/slice/SelectionSlice.js +111 -111
- package/dist/dataunit/state/slice/UnitMetadataSlice.d.ts +11 -11
- package/dist/dataunit/state/slice/UnitMetadataSlice.js +20 -20
- package/dist/dataunit/state/slice/WaitingChangesSlice.d.ts +9 -0
- package/dist/dataunit/state/slice/WaitingChangesSlice.js +32 -0
- package/dist/dataunit/state/slice/WaitingChangesSlice.js.map +1 -0
- package/dist/http/AuthorizedServiceCaller.d.ts +11 -11
- package/dist/http/AuthorizedServiceCaller.js +53 -53
- package/dist/http/HttpProvider.d.ts +25 -25
- package/dist/http/HttpProvider.js +73 -73
- package/dist/http/RequestMetadata.d.ts +30 -30
- package/dist/http/RequestMetadata.js +24 -24
- package/dist/http/SkwHttpProvider.d.ts +9 -9
- package/dist/http/SkwHttpProvider.js +66 -66
- package/dist/index.d.ts +16 -16
- package/dist/index.js +17 -17
- package/dist/index.js.map +1 -1
- package/dist/ui/FloatingManager.d.ts +25 -24
- package/dist/ui/FloatingManager.js +124 -122
- package/dist/ui/FloatingManager.js.map +1 -1
- package/dist/utils/ApplicationContext.d.ts +5 -5
- package/dist/utils/ApplicationContext.js +16 -16
- package/dist/utils/CriteriaModel.d.ts +109 -109
- package/dist/utils/CriteriaModel.js +173 -173
- package/dist/utils/CriteriaParameter.d.ts +69 -69
- package/dist/utils/CriteriaParameter.js +82 -82
- package/dist/utils/DateUtils.d.ts +5 -5
- package/dist/utils/DateUtils.js +42 -42
- package/dist/utils/MaskFormatter.d.ts +126 -126
- package/dist/utils/MaskFormatter.js +275 -275
- package/dist/utils/NumberUtils.d.ts +53 -53
- package/dist/utils/NumberUtils.js +141 -141
- package/dist/utils/StringUtils.d.ts +18 -18
- package/dist/utils/StringUtils.js +53 -53
- package/dist/utils/TimeFormatter.d.ts +33 -33
- package/dist/utils/TimeFormatter.js +97 -97
- package/jest.config.ts +16 -16
- package/mock/http/XMLHttpRequest-mock.js +25 -25
- package/package.json +38 -37
- package/scripts/runlink.bat +1 -0
- package/src/dataunit/DataUnit.ts +374 -356
- package/src/dataunit/metadata/DataType.ts +37 -37
- package/src/dataunit/metadata/UnitMetadata.ts +82 -82
- package/src/dataunit/state/HistReducer.ts +33 -33
- package/src/dataunit/state/StateManager.ts +141 -141
- package/src/dataunit/state/action/DataUnitAction.ts +52 -50
- package/src/dataunit/state/slice/AddedRecordsSlice.ts +32 -32
- package/src/dataunit/state/slice/ChangesSlice.ts +95 -90
- package/src/dataunit/state/slice/CurrentRecordsSlice.ts +53 -53
- package/src/dataunit/state/slice/RecordsSlice.ts +56 -56
- package/src/dataunit/state/slice/RemovedRecordsSlice.ts +29 -29
- package/src/dataunit/state/slice/SelectionSlice.ts +130 -130
- package/src/dataunit/state/slice/UnitMetadataSlice.ts +29 -29
- package/src/dataunit/state/slice/WaitingChangesSlice.ts +44 -0
- package/src/http/AuthorizedServiceCaller.ts +58 -58
- package/src/http/HttpProvider.ts +93 -93
- package/src/http/RequestMetadata.ts +41 -41
- package/src/http/SkwHttpProvider.ts +77 -77
- package/src/index.ts +45 -44
- package/src/ui/FloatingManager.ts +167 -165
- package/src/utils/ApplicationContext.ts +18 -18
- package/src/utils/CriteriaModel.ts +207 -207
- package/src/utils/CriteriaParameter.ts +107 -107
- package/src/utils/DateUtils.ts +52 -52
- package/src/utils/MaskFormatter.ts +322 -322
- package/src/utils/NumberUtils.ts +174 -174
- package/src/utils/StringUtils.ts +60 -60
- package/src/utils/TimeFormatter.ts +97 -97
- package/test/http/HttpProvider.spec.ts +34 -34
- package/test/http/SkwHttpProvider.ts.spec.ts +33 -33
- package/test/util/CriteriaModel.spec.ts +231 -231
- package/test/util/CriteriaParameter.spec.ts +51 -51
- package/test/util/MaskFormatter.spec.ts +138 -138
- package/test/util/NumberUtils.spec.ts +156 -156
- package/test/util/StringUtils.spec.ts +43 -43
- package/test/util/TimeFormatter.spec.ts +25 -25
- package/tsconfig.json +15 -15
- package/dist/application/Application.d.ts +0 -8
- package/dist/application/Application.js +0 -19
- package/dist/application/Application.js.map +0 -1
- package/dist/http/data-fetcher/HttpFetcher.d.ts +0 -14
- package/dist/http/data-fetcher/HttpFetcher.js +0 -164
- package/dist/http/data-fetcher/HttpFetcher.js.map +0 -1
- package/dist/http/data-fetcher/default/DefaultGraphQL.d.ts +0 -3
- package/dist/http/data-fetcher/default/DefaultGraphQL.js +0 -18
- package/dist/http/data-fetcher/default/DefaultGraphQL.js.map +0 -1
- package/dist/http/data-fetcher/default/application-config-fetcher.d.ts +0 -6
- package/dist/http/data-fetcher/default/application-config-fetcher.js +0 -21
- package/dist/http/data-fetcher/default/application-config-fetcher.js.map +0 -1
- package/dist/http/data-fetcher/default/data-unit-graphql.d.ts +0 -3
- package/dist/http/data-fetcher/default/data-unit-graphql.js +0 -18
- package/dist/http/data-fetcher/default/data-unit-graphql.js.map +0 -1
- package/dist/http/data-fetcher/default/dataunit-executor.d.ts +0 -10
- package/dist/http/data-fetcher/default/dataunit-executor.js +0 -56
- package/dist/http/data-fetcher/default/dataunit-executor.js.map +0 -1
- package/dist/http/data-fetcher/default/dataunit-fetcher.d.ts +0 -9
- package/dist/http/data-fetcher/default/dataunit-fetcher.js +0 -69
- package/dist/http/data-fetcher/default/dataunit-fetcher.js.map +0 -1
- package/dist/http/data-fetcher/default/form-config-fetcher.d.ts +0 -2
- package/dist/http/data-fetcher/default/form-config-fetcher.js +0 -3
- package/dist/http/data-fetcher/default/form-config-fetcher.js.map +0 -1
- package/dist/http/data-fetcher/default/grid-config-fetcher.d.ts +0 -2
- package/dist/http/data-fetcher/default/grid-config-fetcher.js +0 -3
- package/dist/http/data-fetcher/default/grid-config-fetcher.js.map +0 -1
- package/dist/http/data-fetcher/default/index.d.ts +0 -0
- package/dist/http/data-fetcher/default/index.js +0 -2
- package/dist/http/data-fetcher/default/index.js.map +0 -1
- package/dist/http/data-fetcher/default/sankhya-graphql.d.ts +0 -3
- package/dist/http/data-fetcher/default/sankhya-graphql.js +0 -8
- package/dist/http/data-fetcher/default/sankhya-graphql.js.map +0 -1
- package/dist/http/data-fetcher/graphql/DefaultGraphQl.d.ts +0 -3
- package/dist/http/data-fetcher/graphql/DefaultGraphQl.js +0 -18
- package/dist/http/data-fetcher/graphql/DefaultGraphQl.js.map +0 -1
- package/dist/http/data-fetcher/state/LoadStateManager.d.ts +0 -23
- package/dist/http/data-fetcher/state/LoadStateManager.js +0 -78
- package/dist/http/data-fetcher/state/LoadStateManager.js.map +0 -1
|
@@ -1,276 +1,276 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `MaskFormatter` é usado para formatar strings. Seu comportamento
|
|
3
|
-
* é controlado pela formato do atributo `mask` que especifica quais
|
|
4
|
-
* caracteres são válidos e onde devem estar posicionados, intercalando-os
|
|
5
|
-
* com eventuais caracteres literais expressados no padrão informado.
|
|
6
|
-
* Sua implementação é inspirada pela implementação em Java do [MaskFormatter](https://docs.oracle.com/javase/7/docs/api/javax/swing/text/MaskFormatter.html).
|
|
7
|
-
*
|
|
8
|
-
* Para o padrão da máscara podem ser usados os seguintes caracteres especiais:
|
|
9
|
-
*
|
|
10
|
-
* | Caractere | Comportamento |
|
|
11
|
-
* |:---------:|-------------------------------------------------------------------------------------------------------------|
|
|
12
|
-
* | # | Qualquer número |
|
|
13
|
-
* | ' | "Escapa" o caractere que vem na sequência. Útil quando desejamos converter um caractere especial em literal.|
|
|
14
|
-
* | U | Qualquer letra. Transforma letras maiúsculas em maiúsculas. |
|
|
15
|
-
* | L | Qualquer letra. Transforma letras maiúsculas em minúsculas. |
|
|
16
|
-
* | A | Qualquer letra ou número. |
|
|
17
|
-
* | ? | Qualquer letra. Preserva maiúsculas e minúsculas. |
|
|
18
|
-
* | * | Qualquer caractere. |
|
|
19
|
-
*
|
|
20
|
-
* Os demais caracteres presentes no padrão serão tratados como literais, isto é,
|
|
21
|
-
* serão apenas inseridos naquela posição.
|
|
22
|
-
*
|
|
23
|
-
* Quando o o valor a ser formatado é menor que a máscara um 'placeHolder'
|
|
24
|
-
* será inserido em cada posição ausente, completando a formatação.
|
|
25
|
-
* Por padrão será usado um espaço em branco como 'placeHolder' mas
|
|
26
|
-
* esse valor pode ser alterado.
|
|
27
|
-
*
|
|
28
|
-
* For por exemplo:
|
|
29
|
-
* '''
|
|
30
|
-
* const formatter: MaskFormatter = new MaskFormatter("###-####");
|
|
31
|
-
* formatter.placeholder = '_';
|
|
32
|
-
* console.log(formatter.format("123"));
|
|
33
|
-
* '''
|
|
34
|
-
* resultaria na string '123-____'.
|
|
35
|
-
*
|
|
36
|
-
* ##Veja mais alguns exemplos:
|
|
37
|
-
* |Padrão |Máscara |Entrada |Saída |
|
|
38
|
-
* |----------------|------------------|--------------|------------------|
|
|
39
|
-
* |Telefone |(##) ####-#### |3432192515 |(34) 3219-2515 |
|
|
40
|
-
* |CPF |###.###.###-## |12345678901 |123.456.789-01 |
|
|
41
|
-
* |CNPJ |##.###.###/####-##|12345678901234|12.345.678/9012-34|
|
|
42
|
-
* |CEP |##.###-### |12345678 |12.345-678 |
|
|
43
|
-
* |PLACA (veículo) |UUU-#### |abc1234 |ABC-1234 |
|
|
44
|
-
* |Cor RGB |'#AAAAAA |00000F0 |#0000F0 |
|
|
45
|
-
*
|
|
46
|
-
*/
|
|
47
|
-
export class MaskFormatter {
|
|
48
|
-
constructor(mask) {
|
|
49
|
-
this._mask = '';
|
|
50
|
-
this._maskChars = new Array();
|
|
51
|
-
/**
|
|
52
|
-
* Determina qual caractere será usado dos caracteres não presentes no valor
|
|
53
|
-
* ou seja, aqueles que o usuário ainda não informou. Por padrão usamos um espaço
|
|
54
|
-
*/
|
|
55
|
-
this.placeholder = ' ';
|
|
56
|
-
this.mask = mask;
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Setter para mask. Trata-se do padrão que se espera ao formatar o texto.
|
|
60
|
-
*/
|
|
61
|
-
set mask(mask) {
|
|
62
|
-
this._mask = mask;
|
|
63
|
-
this.updateInternalMask();
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* Getter para mask
|
|
67
|
-
*
|
|
68
|
-
* @return A última máscara informada.
|
|
69
|
-
*/
|
|
70
|
-
get mask() {
|
|
71
|
-
return this._mask;
|
|
72
|
-
}
|
|
73
|
-
/**
|
|
74
|
-
* Formata a string passada baseada na máscara definda pelo atributo mask.
|
|
75
|
-
*
|
|
76
|
-
* @param value Valor a ser formatado
|
|
77
|
-
* @return O valor processado de acordo com o padrão
|
|
78
|
-
*/
|
|
79
|
-
format(value) {
|
|
80
|
-
let result = '';
|
|
81
|
-
const index = [0];
|
|
82
|
-
let counter = 0;
|
|
83
|
-
const maxCounter = this._maskChars.length;
|
|
84
|
-
while (counter < maxCounter) {
|
|
85
|
-
result = this._maskChars[counter].append(result, value, index);
|
|
86
|
-
counter++;
|
|
87
|
-
}
|
|
88
|
-
return result;
|
|
89
|
-
}
|
|
90
|
-
/**
|
|
91
|
-
* Preparamos a formatação internamente de acordo com o padrão.
|
|
92
|
-
*/
|
|
93
|
-
updateInternalMask() {
|
|
94
|
-
this._maskChars.length = 0;
|
|
95
|
-
if (this.mask != null) {
|
|
96
|
-
let counter = 0;
|
|
97
|
-
const maxCounter = this.mask.length;
|
|
98
|
-
while (counter < maxCounter) {
|
|
99
|
-
let maskChar = this.mask.charAt(counter);
|
|
100
|
-
switch (maskChar) {
|
|
101
|
-
case MaskFormatter.DIGIT_KEY:
|
|
102
|
-
this._maskChars.push(new MaskFormatter.DigitMaskCharacter(this, maskChar));
|
|
103
|
-
break;
|
|
104
|
-
case MaskFormatter.LITERAL_KEY:
|
|
105
|
-
if (++counter < maxCounter) {
|
|
106
|
-
maskChar = this.mask.charAt(counter);
|
|
107
|
-
this._maskChars.push(new MaskFormatter.LiteralCharacter(this, maskChar));
|
|
108
|
-
}
|
|
109
|
-
break;
|
|
110
|
-
case MaskFormatter.UPPERCASE_KEY:
|
|
111
|
-
this._maskChars.push(new MaskFormatter.UpperCaseCharacter(this, maskChar));
|
|
112
|
-
break;
|
|
113
|
-
case MaskFormatter.LOWERCASE_KEY:
|
|
114
|
-
this._maskChars.push(new MaskFormatter.LowerCaseCharacter(this, maskChar));
|
|
115
|
-
break;
|
|
116
|
-
case MaskFormatter.ALPHA_NUMERIC_KEY:
|
|
117
|
-
this._maskChars.push(new MaskFormatter.AlphaNumericCharacter(this, maskChar));
|
|
118
|
-
break;
|
|
119
|
-
case MaskFormatter.CHARACTER_KEY:
|
|
120
|
-
this._maskChars.push(new MaskFormatter.CharCharacter(this, maskChar));
|
|
121
|
-
break;
|
|
122
|
-
case MaskFormatter.ANYTHING_KEY:
|
|
123
|
-
this._maskChars.push(new MaskFormatter.MaskCharacter(this, maskChar));
|
|
124
|
-
break;
|
|
125
|
-
default:
|
|
126
|
-
this._maskChars.push(new MaskFormatter.LiteralCharacter(this, maskChar));
|
|
127
|
-
break;
|
|
128
|
-
}
|
|
129
|
-
counter++;
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
MaskFormatter.DIGIT_KEY = "#";
|
|
135
|
-
MaskFormatter.LITERAL_KEY = "'";
|
|
136
|
-
MaskFormatter.UPPERCASE_KEY = "U";
|
|
137
|
-
MaskFormatter.LOWERCASE_KEY = "L";
|
|
138
|
-
MaskFormatter.ALPHA_NUMERIC_KEY = "A";
|
|
139
|
-
MaskFormatter.CHARACTER_KEY = "?";
|
|
140
|
-
MaskFormatter.ANYTHING_KEY = "*";
|
|
141
|
-
//
|
|
142
|
-
// Classes internas usadas para representar a máscara.
|
|
143
|
-
//
|
|
144
|
-
MaskFormatter.MaskCharacter = class {
|
|
145
|
-
constructor(maskFormatter, type) {
|
|
146
|
-
this.maskFormatter = maskFormatter;
|
|
147
|
-
this.type = type;
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Cada subclasse deve sobrescrever o retornando true, caso represente
|
|
151
|
-
* um caractere literal. Por padrão o retorno é false.
|
|
152
|
-
*/
|
|
153
|
-
isLiteral() {
|
|
154
|
-
return false;
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Returns true if <code>aChar</code> is a valid reprensentation of
|
|
158
|
-
* the receiver. The default implementation returns true if the
|
|
159
|
-
* receiver represents a literal character and <code>getChar</code>
|
|
160
|
-
* == aChar. Otherwise, this will return true is <code>aChar</code>
|
|
161
|
-
* is contained in the valid characters and not contained
|
|
162
|
-
* in the invalid characters.
|
|
163
|
-
*/
|
|
164
|
-
isValidCharacter(aChar) {
|
|
165
|
-
if (this.isLiteral()) {
|
|
166
|
-
return (this.getChar(aChar) == aChar);
|
|
167
|
-
}
|
|
168
|
-
aChar = this.getChar(aChar);
|
|
169
|
-
return true;
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Returns the character to insert for <code>aChar</code>. The
|
|
173
|
-
* default implementation returns <code>aChar</code>. Subclasses
|
|
174
|
-
* that wish to do some sort of mapping, perhaps lower case to upper
|
|
175
|
-
* case should override this and do the necessary mapping.
|
|
176
|
-
*/
|
|
177
|
-
getChar(aChar) {
|
|
178
|
-
return aChar;
|
|
179
|
-
}
|
|
180
|
-
/**
|
|
181
|
-
* Appends the necessary character in <code>formatting</code> at
|
|
182
|
-
* <code>index</code> to <code>buff</code>.
|
|
183
|
-
*/
|
|
184
|
-
append(result, formatting, index) {
|
|
185
|
-
const inString = index[0] < formatting.length;
|
|
186
|
-
const aChar = inString ? formatting.charAt(index[0]) : '';
|
|
187
|
-
if (this.isLiteral()) {
|
|
188
|
-
const literal = this.getChar(aChar);
|
|
189
|
-
result += literal;
|
|
190
|
-
if (literal === aChar) {
|
|
191
|
-
index[0] = index[0] + 1;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
else if (index[0] >= formatting.length) {
|
|
195
|
-
result += this.maskFormatter.placeholder;
|
|
196
|
-
index[0] = index[0] + 1;
|
|
197
|
-
}
|
|
198
|
-
else if (this.isValidCharacter(aChar)) {
|
|
199
|
-
result += this.getChar(aChar);
|
|
200
|
-
index[0] = index[0] + 1;
|
|
201
|
-
}
|
|
202
|
-
else {
|
|
203
|
-
throw new Error(`Valor inválido: "${aChar}". Na posição ${index[0] + 1} espera-se ${this.getFormatMessage()}.`);
|
|
204
|
-
}
|
|
205
|
-
return result;
|
|
206
|
-
}
|
|
207
|
-
getFormatMessage() {
|
|
208
|
-
let message;
|
|
209
|
-
switch (this.type) {
|
|
210
|
-
case MaskFormatter.UPPERCASE_KEY:
|
|
211
|
-
case MaskFormatter.LOWERCASE_KEY:
|
|
212
|
-
case MaskFormatter.CHARACTER_KEY:
|
|
213
|
-
message = 'uma letra';
|
|
214
|
-
break;
|
|
215
|
-
case MaskFormatter.DIGIT_KEY:
|
|
216
|
-
message = 'um número';
|
|
217
|
-
break;
|
|
218
|
-
case MaskFormatter.ALPHA_NUMERIC_KEY:
|
|
219
|
-
message = 'uma letra ou um número';
|
|
220
|
-
break;
|
|
221
|
-
default:
|
|
222
|
-
message = '';
|
|
223
|
-
}
|
|
224
|
-
return message;
|
|
225
|
-
}
|
|
226
|
-
};
|
|
227
|
-
MaskFormatter.LiteralCharacter = class extends MaskFormatter.MaskCharacter {
|
|
228
|
-
constructor(maskFormatter, fixedChar) {
|
|
229
|
-
super(maskFormatter, fixedChar);
|
|
230
|
-
this._fixedChar = fixedChar;
|
|
231
|
-
}
|
|
232
|
-
isLiteral() {
|
|
233
|
-
return true;
|
|
234
|
-
}
|
|
235
|
-
getChar(aChar) {
|
|
236
|
-
aChar;
|
|
237
|
-
return this._fixedChar;
|
|
238
|
-
}
|
|
239
|
-
};
|
|
240
|
-
MaskFormatter.DigitMaskCharacter = class extends MaskFormatter.MaskCharacter {
|
|
241
|
-
isValidCharacter(aChar) {
|
|
242
|
-
return (this.isDigit(aChar) && super.isValidCharacter(aChar));
|
|
243
|
-
}
|
|
244
|
-
isDigit(char) {
|
|
245
|
-
return char >= '0' && char <= '9';
|
|
246
|
-
}
|
|
247
|
-
};
|
|
248
|
-
MaskFormatter.UpperCaseCharacter = class extends MaskFormatter.MaskCharacter {
|
|
249
|
-
isValidCharacter(aChar) {
|
|
250
|
-
return (/[a-z]/i.test(aChar) && super.isValidCharacter(aChar));
|
|
251
|
-
}
|
|
252
|
-
getChar(aChar) {
|
|
253
|
-
return aChar.toUpperCase();
|
|
254
|
-
}
|
|
255
|
-
};
|
|
256
|
-
MaskFormatter.LowerCaseCharacter = class extends MaskFormatter.MaskCharacter {
|
|
257
|
-
isValidCharacter(aChar) {
|
|
258
|
-
return (/[a-z]/i.test(aChar) && super.isValidCharacter(aChar));
|
|
259
|
-
}
|
|
260
|
-
getChar(aChar) {
|
|
261
|
-
return aChar.toLocaleLowerCase();
|
|
262
|
-
}
|
|
263
|
-
};
|
|
264
|
-
MaskFormatter.AlphaNumericCharacter = class extends MaskFormatter.MaskCharacter {
|
|
265
|
-
isValidCharacter(aChar) {
|
|
266
|
-
//FIXME: talvez seja problema usar regex aqui... avaliar se existe forma mais barata
|
|
267
|
-
return (/[a-z0-9]/i.test(aChar)) && super.isValidCharacter(aChar);
|
|
268
|
-
}
|
|
269
|
-
};
|
|
270
|
-
MaskFormatter.CharCharacter = class extends MaskFormatter.MaskCharacter {
|
|
271
|
-
isValidCharacter(aChar) {
|
|
272
|
-
//FIXME: talvez seja problema usar regex aqui... avaliar se existe forma mais barata
|
|
273
|
-
return (/[a-z]/i.test(aChar) && super.isValidCharacter(aChar));
|
|
274
|
-
}
|
|
275
|
-
};
|
|
1
|
+
/**
|
|
2
|
+
* `MaskFormatter` é usado para formatar strings. Seu comportamento
|
|
3
|
+
* é controlado pela formato do atributo `mask` que especifica quais
|
|
4
|
+
* caracteres são válidos e onde devem estar posicionados, intercalando-os
|
|
5
|
+
* com eventuais caracteres literais expressados no padrão informado.
|
|
6
|
+
* Sua implementação é inspirada pela implementação em Java do [MaskFormatter](https://docs.oracle.com/javase/7/docs/api/javax/swing/text/MaskFormatter.html).
|
|
7
|
+
*
|
|
8
|
+
* Para o padrão da máscara podem ser usados os seguintes caracteres especiais:
|
|
9
|
+
*
|
|
10
|
+
* | Caractere | Comportamento |
|
|
11
|
+
* |:---------:|-------------------------------------------------------------------------------------------------------------|
|
|
12
|
+
* | # | Qualquer número |
|
|
13
|
+
* | ' | "Escapa" o caractere que vem na sequência. Útil quando desejamos converter um caractere especial em literal.|
|
|
14
|
+
* | U | Qualquer letra. Transforma letras maiúsculas em maiúsculas. |
|
|
15
|
+
* | L | Qualquer letra. Transforma letras maiúsculas em minúsculas. |
|
|
16
|
+
* | A | Qualquer letra ou número. |
|
|
17
|
+
* | ? | Qualquer letra. Preserva maiúsculas e minúsculas. |
|
|
18
|
+
* | * | Qualquer caractere. |
|
|
19
|
+
*
|
|
20
|
+
* Os demais caracteres presentes no padrão serão tratados como literais, isto é,
|
|
21
|
+
* serão apenas inseridos naquela posição.
|
|
22
|
+
*
|
|
23
|
+
* Quando o o valor a ser formatado é menor que a máscara um 'placeHolder'
|
|
24
|
+
* será inserido em cada posição ausente, completando a formatação.
|
|
25
|
+
* Por padrão será usado um espaço em branco como 'placeHolder' mas
|
|
26
|
+
* esse valor pode ser alterado.
|
|
27
|
+
*
|
|
28
|
+
* For por exemplo:
|
|
29
|
+
* '''
|
|
30
|
+
* const formatter: MaskFormatter = new MaskFormatter("###-####");
|
|
31
|
+
* formatter.placeholder = '_';
|
|
32
|
+
* console.log(formatter.format("123"));
|
|
33
|
+
* '''
|
|
34
|
+
* resultaria na string '123-____'.
|
|
35
|
+
*
|
|
36
|
+
* ##Veja mais alguns exemplos:
|
|
37
|
+
* |Padrão |Máscara |Entrada |Saída |
|
|
38
|
+
* |----------------|------------------|--------------|------------------|
|
|
39
|
+
* |Telefone |(##) ####-#### |3432192515 |(34) 3219-2515 |
|
|
40
|
+
* |CPF |###.###.###-## |12345678901 |123.456.789-01 |
|
|
41
|
+
* |CNPJ |##.###.###/####-##|12345678901234|12.345.678/9012-34|
|
|
42
|
+
* |CEP |##.###-### |12345678 |12.345-678 |
|
|
43
|
+
* |PLACA (veículo) |UUU-#### |abc1234 |ABC-1234 |
|
|
44
|
+
* |Cor RGB |'#AAAAAA |00000F0 |#0000F0 |
|
|
45
|
+
*
|
|
46
|
+
*/
|
|
47
|
+
export class MaskFormatter {
|
|
48
|
+
constructor(mask) {
|
|
49
|
+
this._mask = '';
|
|
50
|
+
this._maskChars = new Array();
|
|
51
|
+
/**
|
|
52
|
+
* Determina qual caractere será usado dos caracteres não presentes no valor
|
|
53
|
+
* ou seja, aqueles que o usuário ainda não informou. Por padrão usamos um espaço
|
|
54
|
+
*/
|
|
55
|
+
this.placeholder = ' ';
|
|
56
|
+
this.mask = mask;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Setter para mask. Trata-se do padrão que se espera ao formatar o texto.
|
|
60
|
+
*/
|
|
61
|
+
set mask(mask) {
|
|
62
|
+
this._mask = mask;
|
|
63
|
+
this.updateInternalMask();
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Getter para mask
|
|
67
|
+
*
|
|
68
|
+
* @return A última máscara informada.
|
|
69
|
+
*/
|
|
70
|
+
get mask() {
|
|
71
|
+
return this._mask;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Formata a string passada baseada na máscara definda pelo atributo mask.
|
|
75
|
+
*
|
|
76
|
+
* @param value Valor a ser formatado
|
|
77
|
+
* @return O valor processado de acordo com o padrão
|
|
78
|
+
*/
|
|
79
|
+
format(value) {
|
|
80
|
+
let result = '';
|
|
81
|
+
const index = [0];
|
|
82
|
+
let counter = 0;
|
|
83
|
+
const maxCounter = this._maskChars.length;
|
|
84
|
+
while (counter < maxCounter) {
|
|
85
|
+
result = this._maskChars[counter].append(result, value, index);
|
|
86
|
+
counter++;
|
|
87
|
+
}
|
|
88
|
+
return result;
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Preparamos a formatação internamente de acordo com o padrão.
|
|
92
|
+
*/
|
|
93
|
+
updateInternalMask() {
|
|
94
|
+
this._maskChars.length = 0;
|
|
95
|
+
if (this.mask != null) {
|
|
96
|
+
let counter = 0;
|
|
97
|
+
const maxCounter = this.mask.length;
|
|
98
|
+
while (counter < maxCounter) {
|
|
99
|
+
let maskChar = this.mask.charAt(counter);
|
|
100
|
+
switch (maskChar) {
|
|
101
|
+
case MaskFormatter.DIGIT_KEY:
|
|
102
|
+
this._maskChars.push(new MaskFormatter.DigitMaskCharacter(this, maskChar));
|
|
103
|
+
break;
|
|
104
|
+
case MaskFormatter.LITERAL_KEY:
|
|
105
|
+
if (++counter < maxCounter) {
|
|
106
|
+
maskChar = this.mask.charAt(counter);
|
|
107
|
+
this._maskChars.push(new MaskFormatter.LiteralCharacter(this, maskChar));
|
|
108
|
+
}
|
|
109
|
+
break;
|
|
110
|
+
case MaskFormatter.UPPERCASE_KEY:
|
|
111
|
+
this._maskChars.push(new MaskFormatter.UpperCaseCharacter(this, maskChar));
|
|
112
|
+
break;
|
|
113
|
+
case MaskFormatter.LOWERCASE_KEY:
|
|
114
|
+
this._maskChars.push(new MaskFormatter.LowerCaseCharacter(this, maskChar));
|
|
115
|
+
break;
|
|
116
|
+
case MaskFormatter.ALPHA_NUMERIC_KEY:
|
|
117
|
+
this._maskChars.push(new MaskFormatter.AlphaNumericCharacter(this, maskChar));
|
|
118
|
+
break;
|
|
119
|
+
case MaskFormatter.CHARACTER_KEY:
|
|
120
|
+
this._maskChars.push(new MaskFormatter.CharCharacter(this, maskChar));
|
|
121
|
+
break;
|
|
122
|
+
case MaskFormatter.ANYTHING_KEY:
|
|
123
|
+
this._maskChars.push(new MaskFormatter.MaskCharacter(this, maskChar));
|
|
124
|
+
break;
|
|
125
|
+
default:
|
|
126
|
+
this._maskChars.push(new MaskFormatter.LiteralCharacter(this, maskChar));
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
counter++;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
MaskFormatter.DIGIT_KEY = "#";
|
|
135
|
+
MaskFormatter.LITERAL_KEY = "'";
|
|
136
|
+
MaskFormatter.UPPERCASE_KEY = "U";
|
|
137
|
+
MaskFormatter.LOWERCASE_KEY = "L";
|
|
138
|
+
MaskFormatter.ALPHA_NUMERIC_KEY = "A";
|
|
139
|
+
MaskFormatter.CHARACTER_KEY = "?";
|
|
140
|
+
MaskFormatter.ANYTHING_KEY = "*";
|
|
141
|
+
//
|
|
142
|
+
// Classes internas usadas para representar a máscara.
|
|
143
|
+
//
|
|
144
|
+
MaskFormatter.MaskCharacter = class {
|
|
145
|
+
constructor(maskFormatter, type) {
|
|
146
|
+
this.maskFormatter = maskFormatter;
|
|
147
|
+
this.type = type;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Cada subclasse deve sobrescrever o retornando true, caso represente
|
|
151
|
+
* um caractere literal. Por padrão o retorno é false.
|
|
152
|
+
*/
|
|
153
|
+
isLiteral() {
|
|
154
|
+
return false;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Returns true if <code>aChar</code> is a valid reprensentation of
|
|
158
|
+
* the receiver. The default implementation returns true if the
|
|
159
|
+
* receiver represents a literal character and <code>getChar</code>
|
|
160
|
+
* == aChar. Otherwise, this will return true is <code>aChar</code>
|
|
161
|
+
* is contained in the valid characters and not contained
|
|
162
|
+
* in the invalid characters.
|
|
163
|
+
*/
|
|
164
|
+
isValidCharacter(aChar) {
|
|
165
|
+
if (this.isLiteral()) {
|
|
166
|
+
return (this.getChar(aChar) == aChar);
|
|
167
|
+
}
|
|
168
|
+
aChar = this.getChar(aChar);
|
|
169
|
+
return true;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Returns the character to insert for <code>aChar</code>. The
|
|
173
|
+
* default implementation returns <code>aChar</code>. Subclasses
|
|
174
|
+
* that wish to do some sort of mapping, perhaps lower case to upper
|
|
175
|
+
* case should override this and do the necessary mapping.
|
|
176
|
+
*/
|
|
177
|
+
getChar(aChar) {
|
|
178
|
+
return aChar;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Appends the necessary character in <code>formatting</code> at
|
|
182
|
+
* <code>index</code> to <code>buff</code>.
|
|
183
|
+
*/
|
|
184
|
+
append(result, formatting, index) {
|
|
185
|
+
const inString = index[0] < formatting.length;
|
|
186
|
+
const aChar = inString ? formatting.charAt(index[0]) : '';
|
|
187
|
+
if (this.isLiteral()) {
|
|
188
|
+
const literal = this.getChar(aChar);
|
|
189
|
+
result += literal;
|
|
190
|
+
if (literal === aChar) {
|
|
191
|
+
index[0] = index[0] + 1;
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
else if (index[0] >= formatting.length) {
|
|
195
|
+
result += this.maskFormatter.placeholder;
|
|
196
|
+
index[0] = index[0] + 1;
|
|
197
|
+
}
|
|
198
|
+
else if (this.isValidCharacter(aChar)) {
|
|
199
|
+
result += this.getChar(aChar);
|
|
200
|
+
index[0] = index[0] + 1;
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
throw new Error(`Valor inválido: "${aChar}". Na posição ${index[0] + 1} espera-se ${this.getFormatMessage()}.`);
|
|
204
|
+
}
|
|
205
|
+
return result;
|
|
206
|
+
}
|
|
207
|
+
getFormatMessage() {
|
|
208
|
+
let message;
|
|
209
|
+
switch (this.type) {
|
|
210
|
+
case MaskFormatter.UPPERCASE_KEY:
|
|
211
|
+
case MaskFormatter.LOWERCASE_KEY:
|
|
212
|
+
case MaskFormatter.CHARACTER_KEY:
|
|
213
|
+
message = 'uma letra';
|
|
214
|
+
break;
|
|
215
|
+
case MaskFormatter.DIGIT_KEY:
|
|
216
|
+
message = 'um número';
|
|
217
|
+
break;
|
|
218
|
+
case MaskFormatter.ALPHA_NUMERIC_KEY:
|
|
219
|
+
message = 'uma letra ou um número';
|
|
220
|
+
break;
|
|
221
|
+
default:
|
|
222
|
+
message = '';
|
|
223
|
+
}
|
|
224
|
+
return message;
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
MaskFormatter.LiteralCharacter = class extends MaskFormatter.MaskCharacter {
|
|
228
|
+
constructor(maskFormatter, fixedChar) {
|
|
229
|
+
super(maskFormatter, fixedChar);
|
|
230
|
+
this._fixedChar = fixedChar;
|
|
231
|
+
}
|
|
232
|
+
isLiteral() {
|
|
233
|
+
return true;
|
|
234
|
+
}
|
|
235
|
+
getChar(aChar) {
|
|
236
|
+
aChar;
|
|
237
|
+
return this._fixedChar;
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
MaskFormatter.DigitMaskCharacter = class extends MaskFormatter.MaskCharacter {
|
|
241
|
+
isValidCharacter(aChar) {
|
|
242
|
+
return (this.isDigit(aChar) && super.isValidCharacter(aChar));
|
|
243
|
+
}
|
|
244
|
+
isDigit(char) {
|
|
245
|
+
return char >= '0' && char <= '9';
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
MaskFormatter.UpperCaseCharacter = class extends MaskFormatter.MaskCharacter {
|
|
249
|
+
isValidCharacter(aChar) {
|
|
250
|
+
return (/[a-z]/i.test(aChar) && super.isValidCharacter(aChar));
|
|
251
|
+
}
|
|
252
|
+
getChar(aChar) {
|
|
253
|
+
return aChar.toUpperCase();
|
|
254
|
+
}
|
|
255
|
+
};
|
|
256
|
+
MaskFormatter.LowerCaseCharacter = class extends MaskFormatter.MaskCharacter {
|
|
257
|
+
isValidCharacter(aChar) {
|
|
258
|
+
return (/[a-z]/i.test(aChar) && super.isValidCharacter(aChar));
|
|
259
|
+
}
|
|
260
|
+
getChar(aChar) {
|
|
261
|
+
return aChar.toLocaleLowerCase();
|
|
262
|
+
}
|
|
263
|
+
};
|
|
264
|
+
MaskFormatter.AlphaNumericCharacter = class extends MaskFormatter.MaskCharacter {
|
|
265
|
+
isValidCharacter(aChar) {
|
|
266
|
+
//FIXME: talvez seja problema usar regex aqui... avaliar se existe forma mais barata
|
|
267
|
+
return (/[a-z0-9]/i.test(aChar)) && super.isValidCharacter(aChar);
|
|
268
|
+
}
|
|
269
|
+
};
|
|
270
|
+
MaskFormatter.CharCharacter = class extends MaskFormatter.MaskCharacter {
|
|
271
|
+
isValidCharacter(aChar) {
|
|
272
|
+
//FIXME: talvez seja problema usar regex aqui... avaliar se existe forma mais barata
|
|
273
|
+
return (/[a-z]/i.test(aChar) && super.isValidCharacter(aChar));
|
|
274
|
+
}
|
|
275
|
+
};
|
|
276
276
|
//# sourceMappingURL=MaskFormatter.js.map
|
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* `NumberUtils` é uma biblioteca para manipulação de números
|
|
3
|
-
*
|
|
4
|
-
* `Métodos`:
|
|
5
|
-
*
|
|
6
|
-
* @stringToNumber: converte um número em formato de string em um valor numérico nativo do javascript
|
|
7
|
-
* @format: arredonda um número em formato de string baseado nos parâmetros "presision" e "prettyPrecision";
|
|
8
|
-
*/
|
|
9
|
-
export declare class NumberUtils {
|
|
10
|
-
/**
|
|
11
|
-
* @stringToNumber: converte um numero em formato de string em numero
|
|
12
|
-
*
|
|
13
|
-
* @param value numero em formato de string a ser convertido (Importante: formato PT-BR ou já em formato numérico: ######.##)
|
|
14
|
-
*
|
|
15
|
-
* @returns string based number
|
|
16
|
-
*
|
|
17
|
-
* @Exemples
|
|
18
|
-
* @"100,12" | 100.12
|
|
19
|
-
* @"100.12" | 100.12
|
|
20
|
-
* @"-100,12" | -100.12
|
|
21
|
-
* @"R$100,12" | 100.12
|
|
22
|
-
* @"-R$100,12" | -100.12
|
|
23
|
-
* @"string" | NaN
|
|
24
|
-
*/
|
|
25
|
-
static stringToNumber: (value: string) => number;
|
|
26
|
-
/**
|
|
27
|
-
* @format: converte um numero em formato de string em um numero em formato de string formatado de acordo com os parametros de "precision" e "prettyPrecision"
|
|
28
|
-
*
|
|
29
|
-
* @param value numero em formato de string a ser convertido (Importante: formato PT-BR ou já em formato numérico<sem separadors de milhares>: ######.##)
|
|
30
|
-
* @param precision (numero de decimais)
|
|
31
|
-
* @param prettyPrecision (numero de zeros nos decimais)
|
|
32
|
-
*
|
|
33
|
-
* @returns numero em formato de string formatado em PT-BR
|
|
34
|
-
*/
|
|
35
|
-
static format: (value: string, precision: number, prettyPrecision?: number) => string;
|
|
36
|
-
/**
|
|
37
|
-
* @keepOnlyDecimalSeparator: retira os separadores de milhar de um número em formato de string
|
|
38
|
-
*
|
|
39
|
-
* @param value numero em formato de string a ser convertido
|
|
40
|
-
* @param formatnumber (formatação de ENTRADA e SAÍDA do utilitário: pt-BR="###.###,##" en-US="###,###.##"; Default: "pt-BR")
|
|
41
|
-
*
|
|
42
|
-
* @returns numero em formato de string formatado apenas com separador decimal
|
|
43
|
-
*/
|
|
44
|
-
static keepOnlyDecimalSeparator: (value: string, formatnumber?: string) => string;
|
|
45
|
-
/**
|
|
46
|
-
* @changeFormat: troca o formato do numero string de "PT-BR" para "EN-US" e vice-versa
|
|
47
|
-
*
|
|
48
|
-
* @param value numero em formato de string a ser convertido
|
|
49
|
-
*
|
|
50
|
-
* @returns numero em formato de string formatado de "PT-BR" para "EN-US" e vice-versa
|
|
51
|
-
*/
|
|
52
|
-
static changeFormat: (value: string) => string;
|
|
53
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* `NumberUtils` é uma biblioteca para manipulação de números
|
|
3
|
+
*
|
|
4
|
+
* `Métodos`:
|
|
5
|
+
*
|
|
6
|
+
* @stringToNumber: converte um número em formato de string em um valor numérico nativo do javascript
|
|
7
|
+
* @format: arredonda um número em formato de string baseado nos parâmetros "presision" e "prettyPrecision";
|
|
8
|
+
*/
|
|
9
|
+
export declare class NumberUtils {
|
|
10
|
+
/**
|
|
11
|
+
* @stringToNumber: converte um numero em formato de string em numero
|
|
12
|
+
*
|
|
13
|
+
* @param value numero em formato de string a ser convertido (Importante: formato PT-BR ou já em formato numérico: ######.##)
|
|
14
|
+
*
|
|
15
|
+
* @returns string based number
|
|
16
|
+
*
|
|
17
|
+
* @Exemples
|
|
18
|
+
* @"100,12" | 100.12
|
|
19
|
+
* @"100.12" | 100.12
|
|
20
|
+
* @"-100,12" | -100.12
|
|
21
|
+
* @"R$100,12" | 100.12
|
|
22
|
+
* @"-R$100,12" | -100.12
|
|
23
|
+
* @"string" | NaN
|
|
24
|
+
*/
|
|
25
|
+
static stringToNumber: (value: string) => number;
|
|
26
|
+
/**
|
|
27
|
+
* @format: converte um numero em formato de string em um numero em formato de string formatado de acordo com os parametros de "precision" e "prettyPrecision"
|
|
28
|
+
*
|
|
29
|
+
* @param value numero em formato de string a ser convertido (Importante: formato PT-BR ou já em formato numérico<sem separadors de milhares>: ######.##)
|
|
30
|
+
* @param precision (numero de decimais)
|
|
31
|
+
* @param prettyPrecision (numero de zeros nos decimais)
|
|
32
|
+
*
|
|
33
|
+
* @returns numero em formato de string formatado em PT-BR
|
|
34
|
+
*/
|
|
35
|
+
static format: (value: string, precision: number, prettyPrecision?: number) => string;
|
|
36
|
+
/**
|
|
37
|
+
* @keepOnlyDecimalSeparator: retira os separadores de milhar de um número em formato de string
|
|
38
|
+
*
|
|
39
|
+
* @param value numero em formato de string a ser convertido
|
|
40
|
+
* @param formatnumber (formatação de ENTRADA e SAÍDA do utilitário: pt-BR="###.###,##" en-US="###,###.##"; Default: "pt-BR")
|
|
41
|
+
*
|
|
42
|
+
* @returns numero em formato de string formatado apenas com separador decimal
|
|
43
|
+
*/
|
|
44
|
+
static keepOnlyDecimalSeparator: (value: string, formatnumber?: string) => string;
|
|
45
|
+
/**
|
|
46
|
+
* @changeFormat: troca o formato do numero string de "PT-BR" para "EN-US" e vice-versa
|
|
47
|
+
*
|
|
48
|
+
* @param value numero em formato de string a ser convertido
|
|
49
|
+
*
|
|
50
|
+
* @returns numero em formato de string formatado de "PT-BR" para "EN-US" e vice-versa
|
|
51
|
+
*/
|
|
52
|
+
static changeFormat: (value: string) => string;
|
|
53
|
+
}
|