@player-ui/common-types-plugin 0.0.1-next.1
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/dist/index.cjs.js +756 -0
- package/dist/index.d.ts +250 -0
- package/dist/index.esm.js +743 -0
- package/package.json +19 -0
- package/src/data-types/refs.ts +33 -0
- package/src/data-types/types.ts +113 -0
- package/src/formats/index.ts +425 -0
- package/src/formats/utils.ts +192 -0
- package/src/index.ts +27 -0
- package/src/validators/index.ts +366 -0
|
@@ -0,0 +1,743 @@
|
|
|
1
|
+
import { TypesProviderPlugin } from '@player-ui/types-provider-plugin';
|
|
2
|
+
|
|
3
|
+
const EMAIL_REGEX = /^((([a-z]|\d|[!#$%&'*+\-/=?^_`{|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#$%&'*+-/=?^_`{|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i;
|
|
4
|
+
const PHONE_REGEX = /^\+?[1]?[- ]?\(?\d{3}[)\- ]?\s?\d{3}[ -]?\d{4}$/;
|
|
5
|
+
const ZIP_REGEX = /^\d{5}(-\d{4})?$/;
|
|
6
|
+
function skipNullish(validationFn) {
|
|
7
|
+
return (context, value, options) => {
|
|
8
|
+
if (value === null || value === void 0) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
return validationFn(context, value, options);
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
const string = skipNullish((context, value) => {
|
|
15
|
+
if (typeof value !== "string") {
|
|
16
|
+
const message = context.constants.getConstants("validation.string", "constants", "Value must be a string");
|
|
17
|
+
return {
|
|
18
|
+
message,
|
|
19
|
+
parameters: {
|
|
20
|
+
type: typeof value
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
const readonly = (context) => {
|
|
26
|
+
const message = context.constants.getConstants("validation.readonly", "constants", "Value cannot be modified");
|
|
27
|
+
return { message };
|
|
28
|
+
};
|
|
29
|
+
const collection = skipNullish((context, value) => {
|
|
30
|
+
if (!Array.isArray(value)) {
|
|
31
|
+
const message = context.constants.getConstants("validation.collection", "constants", "Cannot set collection to non-array");
|
|
32
|
+
return { message };
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
const integer$1 = skipNullish((context, value) => {
|
|
36
|
+
if (value && (typeof value !== "number" || Math.floor(value) !== value || Number(value) > Number.MAX_SAFE_INTEGER || Number(value) < Number.MIN_SAFE_INTEGER)) {
|
|
37
|
+
const message = context.constants.getConstants("validation.integer", "constants", "Value must be an integer");
|
|
38
|
+
return {
|
|
39
|
+
message,
|
|
40
|
+
parameters: {
|
|
41
|
+
type: typeof value,
|
|
42
|
+
flooredValue: Math.floor(value)
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
const oneOf = skipNullish((context, value, options) => {
|
|
48
|
+
var _a;
|
|
49
|
+
if ((options == null ? void 0 : options.options) === void 0 || ((_a = options.options) == null ? void 0 : _a.includes(value))) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
const message = context.constants.getConstants("validation.oneOf", "constants", "Invalid entry");
|
|
53
|
+
return { message };
|
|
54
|
+
});
|
|
55
|
+
const expression = (context, value, options) => {
|
|
56
|
+
if ((options == null ? void 0 : options.exp) === void 0) {
|
|
57
|
+
context.logger.warn("No expression defined for validation");
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const result = context.evaluate(options.exp);
|
|
61
|
+
if (!result) {
|
|
62
|
+
const message = context.constants.getConstants("validation.expression", "constants", "Expression evaluation failed");
|
|
63
|
+
return { message };
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const required = (context, value, options) => {
|
|
67
|
+
if ((options == null ? void 0 : options.if) && !context.evaluate(options.if) || (options == null ? void 0 : options.ifNot) && context.evaluate(options.ifNot)) {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
if (value === void 0 || value === null || value === "") {
|
|
71
|
+
const message = context.constants.getConstants("validation.required", "constants", "A value is required");
|
|
72
|
+
return { message, severity: "error" };
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
const regex = skipNullish((context, value, options) => {
|
|
76
|
+
if (value === void 0 || value === null || value === "" || typeof (options == null ? void 0 : options.regex) !== "string") {
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
const patternMatch = options.regex.match(/^\/(.*)\/(\w)*$/);
|
|
80
|
+
const regexp = patternMatch ? new RegExp(patternMatch[1], patternMatch[2]) : new RegExp(options.regex);
|
|
81
|
+
if (!regexp.test(value)) {
|
|
82
|
+
const message = context.constants.getConstants("validation.regex", "constants", "Invalid entry");
|
|
83
|
+
return { message };
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
const length = skipNullish((context, value, options) => {
|
|
87
|
+
if (typeof options !== "object") {
|
|
88
|
+
context.logger.warn("Missing comparison in length validation");
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
let valLength;
|
|
92
|
+
let itemName = "items";
|
|
93
|
+
if (typeof value === "string") {
|
|
94
|
+
valLength = value.length;
|
|
95
|
+
itemName = "characters";
|
|
96
|
+
} else if (typeof value === "object" && value !== null) {
|
|
97
|
+
valLength = Object.keys(value).length;
|
|
98
|
+
}
|
|
99
|
+
if (valLength === void 0) {
|
|
100
|
+
context.logger.warn(`Unable to determine a length for value of type: ${value}`);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if ("exact" in options) {
|
|
104
|
+
if (valLength !== options.exact) {
|
|
105
|
+
return {
|
|
106
|
+
message: `Must be exactly ${options.exact} ${itemName} long`,
|
|
107
|
+
parameters: {
|
|
108
|
+
validationLength: valLength
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (options.min !== void 0 && valLength < options.min) {
|
|
115
|
+
const message = context.constants.getConstants("validation.length.minimum", "constants", `At least ${options.min} ${itemName} needed`);
|
|
116
|
+
return {
|
|
117
|
+
message,
|
|
118
|
+
parameters: {
|
|
119
|
+
validationLength: valLength
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
if (options.max !== void 0 && valLength > options.max) {
|
|
124
|
+
const message = context.constants.getConstants("validation.length.maximum", "constants", `Up to ${options.max} ${itemName} allowed`);
|
|
125
|
+
return {
|
|
126
|
+
message,
|
|
127
|
+
parameters: {
|
|
128
|
+
validationLength: valLength
|
|
129
|
+
}
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
const min = skipNullish((context, value, options) => {
|
|
134
|
+
if (typeof value !== "number" || (options == null ? void 0 : options.value) === void 0) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (value < options.value) {
|
|
138
|
+
const message = context.constants.getConstants("validation.min", "constants", `Must be at least ${options.value}`);
|
|
139
|
+
return { message };
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
const max = skipNullish((context, value, options) => {
|
|
143
|
+
if (typeof value !== "number" || (options == null ? void 0 : options.value) === void 0) {
|
|
144
|
+
return;
|
|
145
|
+
}
|
|
146
|
+
if (value > options.value) {
|
|
147
|
+
const message = context.constants.getConstants("validation.max", "constants", `Cannot exceed ${options.value}`);
|
|
148
|
+
return { message };
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
const stringRegexValidator = (test, messagePath, invalidMessage) => {
|
|
152
|
+
return skipNullish((context, value) => {
|
|
153
|
+
if (typeof value === "string" && value === "") {
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (typeof value !== "string" || !test.test(value)) {
|
|
157
|
+
const message = context.constants.getConstants(messagePath, "constants", invalidMessage);
|
|
158
|
+
return { message };
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
};
|
|
162
|
+
const email = stringRegexValidator(EMAIL_REGEX, "validation.email", "Improper email format");
|
|
163
|
+
const phone$1 = stringRegexValidator(PHONE_REGEX, "validation.phone", "Invalid phone number");
|
|
164
|
+
const zip = stringRegexValidator(ZIP_REGEX, "validation.regex", "Invalid zip code");
|
|
165
|
+
|
|
166
|
+
var validators = /*#__PURE__*/Object.freeze({
|
|
167
|
+
__proto__: null,
|
|
168
|
+
string: string,
|
|
169
|
+
readonly: readonly,
|
|
170
|
+
collection: collection,
|
|
171
|
+
integer: integer$1,
|
|
172
|
+
oneOf: oneOf,
|
|
173
|
+
expression: expression,
|
|
174
|
+
required: required,
|
|
175
|
+
regex: regex,
|
|
176
|
+
length: length,
|
|
177
|
+
min: min,
|
|
178
|
+
max: max,
|
|
179
|
+
email: email,
|
|
180
|
+
phone: phone$1,
|
|
181
|
+
zip: zip
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
const BooleanTypeRef = {
|
|
185
|
+
type: "BooleanType"
|
|
186
|
+
};
|
|
187
|
+
const IntegerTypeRef = {
|
|
188
|
+
type: "IntegerType"
|
|
189
|
+
};
|
|
190
|
+
const IntegerPosTypeRef = {
|
|
191
|
+
type: "IntegerPosType"
|
|
192
|
+
};
|
|
193
|
+
const IntegerNNTypeRef = {
|
|
194
|
+
type: "IntegerNNType"
|
|
195
|
+
};
|
|
196
|
+
const StringTypeRef = {
|
|
197
|
+
type: "StringType"
|
|
198
|
+
};
|
|
199
|
+
const CollectionTypeRef = {
|
|
200
|
+
type: "CollectionType"
|
|
201
|
+
};
|
|
202
|
+
const DateTypeRef = {
|
|
203
|
+
type: "DateType"
|
|
204
|
+
};
|
|
205
|
+
const PhoneTypeRef = {
|
|
206
|
+
type: "PhoneType"
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
var refs = /*#__PURE__*/Object.freeze({
|
|
210
|
+
__proto__: null,
|
|
211
|
+
BooleanTypeRef: BooleanTypeRef,
|
|
212
|
+
IntegerTypeRef: IntegerTypeRef,
|
|
213
|
+
IntegerPosTypeRef: IntegerPosTypeRef,
|
|
214
|
+
IntegerNNTypeRef: IntegerNNTypeRef,
|
|
215
|
+
StringTypeRef: StringTypeRef,
|
|
216
|
+
CollectionTypeRef: CollectionTypeRef,
|
|
217
|
+
DateTypeRef: DateTypeRef,
|
|
218
|
+
PhoneTypeRef: PhoneTypeRef
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
var __defProp$1 = Object.defineProperty;
|
|
222
|
+
var __defProps$1 = Object.defineProperties;
|
|
223
|
+
var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
|
|
224
|
+
var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
|
|
225
|
+
var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
|
|
226
|
+
var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
|
|
227
|
+
var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
228
|
+
var __spreadValues$1 = (a, b) => {
|
|
229
|
+
for (var prop in b || (b = {}))
|
|
230
|
+
if (__hasOwnProp$1.call(b, prop))
|
|
231
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
232
|
+
if (__getOwnPropSymbols$1)
|
|
233
|
+
for (var prop of __getOwnPropSymbols$1(b)) {
|
|
234
|
+
if (__propIsEnum$1.call(b, prop))
|
|
235
|
+
__defNormalProp$1(a, prop, b[prop]);
|
|
236
|
+
}
|
|
237
|
+
return a;
|
|
238
|
+
};
|
|
239
|
+
var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
|
|
240
|
+
const BooleanType = __spreadProps$1(__spreadValues$1({}, BooleanTypeRef), {
|
|
241
|
+
default: false,
|
|
242
|
+
validation: [
|
|
243
|
+
{
|
|
244
|
+
type: "oneOf",
|
|
245
|
+
message: "Value must be true or false",
|
|
246
|
+
options: [true, false]
|
|
247
|
+
}
|
|
248
|
+
]
|
|
249
|
+
});
|
|
250
|
+
const IntegerType = __spreadProps$1(__spreadValues$1({}, IntegerTypeRef), {
|
|
251
|
+
validation: [
|
|
252
|
+
{
|
|
253
|
+
type: "integer"
|
|
254
|
+
}
|
|
255
|
+
],
|
|
256
|
+
format: {
|
|
257
|
+
type: "integer"
|
|
258
|
+
}
|
|
259
|
+
});
|
|
260
|
+
const IntegerPosType = __spreadProps$1(__spreadValues$1({}, IntegerPosTypeRef), {
|
|
261
|
+
validation: [
|
|
262
|
+
{
|
|
263
|
+
type: "integer"
|
|
264
|
+
},
|
|
265
|
+
{
|
|
266
|
+
type: "min",
|
|
267
|
+
value: 1
|
|
268
|
+
}
|
|
269
|
+
],
|
|
270
|
+
format: {
|
|
271
|
+
type: "integer"
|
|
272
|
+
}
|
|
273
|
+
});
|
|
274
|
+
const IntegerNNType = __spreadProps$1(__spreadValues$1({}, IntegerNNTypeRef), {
|
|
275
|
+
validation: [
|
|
276
|
+
{
|
|
277
|
+
type: "integer"
|
|
278
|
+
},
|
|
279
|
+
{
|
|
280
|
+
type: "min",
|
|
281
|
+
value: 0
|
|
282
|
+
}
|
|
283
|
+
],
|
|
284
|
+
format: {
|
|
285
|
+
type: "integer"
|
|
286
|
+
}
|
|
287
|
+
});
|
|
288
|
+
const StringType = __spreadProps$1(__spreadValues$1({}, StringTypeRef), {
|
|
289
|
+
default: "",
|
|
290
|
+
validation: [
|
|
291
|
+
{
|
|
292
|
+
type: "string"
|
|
293
|
+
}
|
|
294
|
+
],
|
|
295
|
+
format: {
|
|
296
|
+
type: "string"
|
|
297
|
+
}
|
|
298
|
+
});
|
|
299
|
+
const CollectionType = __spreadProps$1(__spreadValues$1({}, CollectionTypeRef), {
|
|
300
|
+
validation: [
|
|
301
|
+
{
|
|
302
|
+
type: "collection"
|
|
303
|
+
}
|
|
304
|
+
]
|
|
305
|
+
});
|
|
306
|
+
const DateType = __spreadProps$1(__spreadValues$1({}, DateTypeRef), {
|
|
307
|
+
validation: [
|
|
308
|
+
{
|
|
309
|
+
type: "string"
|
|
310
|
+
}
|
|
311
|
+
],
|
|
312
|
+
format: {
|
|
313
|
+
type: "date"
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
const PhoneType = __spreadProps$1(__spreadValues$1({}, PhoneTypeRef), {
|
|
317
|
+
validation: [
|
|
318
|
+
{
|
|
319
|
+
type: "phone"
|
|
320
|
+
}
|
|
321
|
+
],
|
|
322
|
+
format: {
|
|
323
|
+
type: "phone"
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
var dataTypes = /*#__PURE__*/Object.freeze({
|
|
328
|
+
__proto__: null,
|
|
329
|
+
BooleanType: BooleanType,
|
|
330
|
+
IntegerType: IntegerType,
|
|
331
|
+
IntegerPosType: IntegerPosType,
|
|
332
|
+
IntegerNNType: IntegerNNType,
|
|
333
|
+
StringType: StringType,
|
|
334
|
+
CollectionType: CollectionType,
|
|
335
|
+
DateType: DateType,
|
|
336
|
+
PhoneType: PhoneType
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
const PLACEHOLDER = "#";
|
|
340
|
+
const removeFormatCharactersFromMaskedString = (value, mask, reserved = [PLACEHOLDER]) => {
|
|
341
|
+
return value.split("").reduce((newString, nextChar, nextIndex) => {
|
|
342
|
+
const maskedVal = mask[nextIndex];
|
|
343
|
+
if (reserved.includes(maskedVal)) {
|
|
344
|
+
return newString + nextChar;
|
|
345
|
+
}
|
|
346
|
+
return newString;
|
|
347
|
+
}, "");
|
|
348
|
+
};
|
|
349
|
+
const formatAsEnum = (value, acceptedValues, options) => {
|
|
350
|
+
const autoCompletionsByOverlapCount = acceptedValues.reduce((validCompletions, validValue) => {
|
|
351
|
+
let overlap = 0;
|
|
352
|
+
for (let charIndex = 0; charIndex < Math.min(validValue.length, value.length); charIndex++) {
|
|
353
|
+
const validChar = (options == null ? void 0 : options.ignoreCase) ? validValue[charIndex].toLowerCase() : validValue[charIndex];
|
|
354
|
+
const actualChar = (options == null ? void 0 : options.ignoreCase) ? value[charIndex].toLowerCase() : value[charIndex];
|
|
355
|
+
if (validChar !== actualChar) {
|
|
356
|
+
break;
|
|
357
|
+
}
|
|
358
|
+
overlap += 1;
|
|
359
|
+
}
|
|
360
|
+
if (overlap === 0) {
|
|
361
|
+
return validCompletions;
|
|
362
|
+
}
|
|
363
|
+
return [
|
|
364
|
+
...validCompletions,
|
|
365
|
+
{
|
|
366
|
+
count: overlap,
|
|
367
|
+
target: validValue
|
|
368
|
+
}
|
|
369
|
+
];
|
|
370
|
+
}, []).sort((e) => e.count);
|
|
371
|
+
if (autoCompletionsByOverlapCount.length === 0) {
|
|
372
|
+
return void 0;
|
|
373
|
+
}
|
|
374
|
+
if (autoCompletionsByOverlapCount.length === 1 && (options == null ? void 0 : options.autocomplete)) {
|
|
375
|
+
return autoCompletionsByOverlapCount[0].target;
|
|
376
|
+
}
|
|
377
|
+
return autoCompletionsByOverlapCount[0].target.substr(0, autoCompletionsByOverlapCount[0].count);
|
|
378
|
+
};
|
|
379
|
+
const formatAsMasked = (value, valueCharMaskMatch, mask) => {
|
|
380
|
+
const valStr = String(value);
|
|
381
|
+
let withMask = mask;
|
|
382
|
+
if (valStr.trim() === "") {
|
|
383
|
+
return "";
|
|
384
|
+
}
|
|
385
|
+
valStr.replace(valueCharMaskMatch, (match) => {
|
|
386
|
+
withMask = withMask.replace(PLACEHOLDER, match);
|
|
387
|
+
return match;
|
|
388
|
+
});
|
|
389
|
+
return withMask.split(PLACEHOLDER)[0];
|
|
390
|
+
};
|
|
391
|
+
const createMaskedNumericFormatter = (name, mask) => {
|
|
392
|
+
return {
|
|
393
|
+
name,
|
|
394
|
+
format: (value, options) => {
|
|
395
|
+
if (typeof value !== "string") {
|
|
396
|
+
return value;
|
|
397
|
+
}
|
|
398
|
+
if ((options == null ? void 0 : options.exceptions) && options.exceptions.length > 0) {
|
|
399
|
+
const formattedUsingExceptions = formatAsEnum(value, options.exceptions, {
|
|
400
|
+
autocomplete: true,
|
|
401
|
+
ignoreCase: true
|
|
402
|
+
});
|
|
403
|
+
if (formattedUsingExceptions !== void 0) {
|
|
404
|
+
return formattedUsingExceptions;
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return formatAsMasked(value, /\d/g, mask);
|
|
408
|
+
},
|
|
409
|
+
deformat: (value, options) => {
|
|
410
|
+
if (typeof value !== "string") {
|
|
411
|
+
return value;
|
|
412
|
+
}
|
|
413
|
+
if ((options == null ? void 0 : options.exceptions) && options.exceptions.length > 0) {
|
|
414
|
+
const usingExceptions = formatAsEnum(value, options.exceptions, {
|
|
415
|
+
autocomplete: false,
|
|
416
|
+
ignoreCase: false
|
|
417
|
+
});
|
|
418
|
+
if (usingExceptions !== void 0) {
|
|
419
|
+
return usingExceptions;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
return removeFormatCharactersFromMaskedString(value, mask);
|
|
423
|
+
}
|
|
424
|
+
};
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
var __defProp = Object.defineProperty;
|
|
428
|
+
var __defProps = Object.defineProperties;
|
|
429
|
+
var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
|
|
430
|
+
var __getOwnPropSymbols = Object.getOwnPropertySymbols;
|
|
431
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
432
|
+
var __propIsEnum = Object.prototype.propertyIsEnumerable;
|
|
433
|
+
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
|
|
434
|
+
var __spreadValues = (a, b) => {
|
|
435
|
+
for (var prop in b || (b = {}))
|
|
436
|
+
if (__hasOwnProp.call(b, prop))
|
|
437
|
+
__defNormalProp(a, prop, b[prop]);
|
|
438
|
+
if (__getOwnPropSymbols)
|
|
439
|
+
for (var prop of __getOwnPropSymbols(b)) {
|
|
440
|
+
if (__propIsEnum.call(b, prop))
|
|
441
|
+
__defNormalProp(a, prop, b[prop]);
|
|
442
|
+
}
|
|
443
|
+
return a;
|
|
444
|
+
};
|
|
445
|
+
var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
|
|
446
|
+
const LENGTH_OF_MAX_INT = String(Number.MAX_SAFE_INTEGER).split("").length;
|
|
447
|
+
const integer = {
|
|
448
|
+
name: "integer",
|
|
449
|
+
format: (value) => {
|
|
450
|
+
var _a, _b;
|
|
451
|
+
if (value === "-") {
|
|
452
|
+
return value;
|
|
453
|
+
}
|
|
454
|
+
const formatted = (_b = (_a = integer.deformat) == null ? void 0 : _a.call(integer, value)) != null ? _b : value;
|
|
455
|
+
if (typeof formatted === "number") {
|
|
456
|
+
return String(formatted);
|
|
457
|
+
}
|
|
458
|
+
return "";
|
|
459
|
+
},
|
|
460
|
+
deformat: (value) => {
|
|
461
|
+
if (typeof value === "number") {
|
|
462
|
+
return Math.floor(value) + 0;
|
|
463
|
+
}
|
|
464
|
+
if (typeof value !== "string") {
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
const isNeg = value.replace(/[^0-9.-]/g, "").charAt(0) === "-";
|
|
468
|
+
let digits = value.replace(/[^0-9.]/g, "");
|
|
469
|
+
const decimalPlace = digits.indexOf(".");
|
|
470
|
+
if (decimalPlace > -1) {
|
|
471
|
+
digits = digits.substring(0, decimalPlace);
|
|
472
|
+
}
|
|
473
|
+
if (digits.length === 0) {
|
|
474
|
+
return;
|
|
475
|
+
}
|
|
476
|
+
digits = digits.substr(0, LENGTH_OF_MAX_INT);
|
|
477
|
+
const num = Number(`${isNeg ? "-" : ""}${digits}`);
|
|
478
|
+
return Math.floor(num) + 0;
|
|
479
|
+
}
|
|
480
|
+
};
|
|
481
|
+
const commaNumber = {
|
|
482
|
+
name: "commaNumber",
|
|
483
|
+
format: (_value, options) => {
|
|
484
|
+
if (_value === void 0 || _value === "") {
|
|
485
|
+
return _value;
|
|
486
|
+
}
|
|
487
|
+
if (typeof _value !== "string" && typeof _value !== "number") {
|
|
488
|
+
return "";
|
|
489
|
+
}
|
|
490
|
+
const value = String(_value);
|
|
491
|
+
const isNeg = value.replace(/[^0-9.-]/g, "").charAt(0) === "-";
|
|
492
|
+
let digitAndDecimal = value.replace(/[^0-9.]/g, "");
|
|
493
|
+
digitAndDecimal = digitAndDecimal.replace(/^(0*)((0.)?\d)/g, "$2");
|
|
494
|
+
const firstDecimal = digitAndDecimal.indexOf(".");
|
|
495
|
+
const digitsOnly = digitAndDecimal.replace(/[^0-9]/g, "");
|
|
496
|
+
let preDecDigits = digitsOnly;
|
|
497
|
+
let postDecDigits = "";
|
|
498
|
+
if (firstDecimal >= 0) {
|
|
499
|
+
preDecDigits = digitsOnly.substring(0, firstDecimal).substr(0, LENGTH_OF_MAX_INT);
|
|
500
|
+
postDecDigits = digitsOnly.substring(firstDecimal);
|
|
501
|
+
} else {
|
|
502
|
+
preDecDigits = preDecDigits.substr(0, LENGTH_OF_MAX_INT);
|
|
503
|
+
}
|
|
504
|
+
if ((options == null ? void 0 : options.precision) !== void 0) {
|
|
505
|
+
postDecDigits = postDecDigits.substring(0, options.precision).padEnd(options.precision, "0");
|
|
506
|
+
}
|
|
507
|
+
preDecDigits = preDecDigits.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
508
|
+
if (preDecDigits === "" && firstDecimal === 0) {
|
|
509
|
+
preDecDigits = "0";
|
|
510
|
+
}
|
|
511
|
+
let retVal = preDecDigits;
|
|
512
|
+
if (isNeg) {
|
|
513
|
+
retVal = `-${retVal}`;
|
|
514
|
+
}
|
|
515
|
+
if (firstDecimal >= 0 || (options == null ? void 0 : options.precision) !== void 0) {
|
|
516
|
+
retVal += `.${postDecDigits}`;
|
|
517
|
+
}
|
|
518
|
+
return retVal;
|
|
519
|
+
},
|
|
520
|
+
deformat: (value) => {
|
|
521
|
+
if (typeof value !== "string") {
|
|
522
|
+
return value;
|
|
523
|
+
}
|
|
524
|
+
const strValue = value.replace(/,/g, "");
|
|
525
|
+
if (strValue === "") {
|
|
526
|
+
return void 0;
|
|
527
|
+
}
|
|
528
|
+
const number = Number(strValue);
|
|
529
|
+
return isNaN(number) || number > Number.MAX_SAFE_INTEGER || number < Number.MIN_SAFE_INTEGER ? void 0 : number;
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
const date = {
|
|
533
|
+
name: "date",
|
|
534
|
+
format: (_value, options) => {
|
|
535
|
+
var _a, _b;
|
|
536
|
+
let value = typeof _value === "number" ? String(_value) : _value;
|
|
537
|
+
if (_value === void 0) {
|
|
538
|
+
return void 0;
|
|
539
|
+
}
|
|
540
|
+
if (typeof value !== "string" || value === "") {
|
|
541
|
+
return "";
|
|
542
|
+
}
|
|
543
|
+
if (value.match(/^\d{4}[-]\d{1,2}[-]\d{1,2}$/)) {
|
|
544
|
+
const tempVal = value.split("-");
|
|
545
|
+
value = `${tempVal[1]}/${tempVal[2]}/${tempVal[0]}`;
|
|
546
|
+
}
|
|
547
|
+
const dateFormat = (_b = (_a = options == null ? void 0 : options.mask) == null ? void 0 : _a.toUpperCase()) != null ? _b : "MM/DD/YYYY";
|
|
548
|
+
const delimiter = dateFormat.replace(/[^/.-]/g, "").charAt(0);
|
|
549
|
+
const formatParts = dateFormat.split(delimiter);
|
|
550
|
+
const valueParts = value.split(delimiter);
|
|
551
|
+
const processedValueParts = [];
|
|
552
|
+
let lastMatchIsFull = true;
|
|
553
|
+
for (let index = 0; index < valueParts.length; index++) {
|
|
554
|
+
let part = valueParts[index];
|
|
555
|
+
if (lastMatchIsFull && index < formatParts.length) {
|
|
556
|
+
part = part.replace(/[^0-9]/g, "");
|
|
557
|
+
const isLastExpectedField = formatParts.length - 1 === index;
|
|
558
|
+
const hasDelimiterAfter = valueParts.length - 1 > index;
|
|
559
|
+
const curFormat = formatParts[index];
|
|
560
|
+
if (curFormat === "YYYY") {
|
|
561
|
+
if (part.length > 4) {
|
|
562
|
+
valueParts[index + 1] = [
|
|
563
|
+
"*",
|
|
564
|
+
part.substring(4),
|
|
565
|
+
valueParts[index + 1]
|
|
566
|
+
].join("");
|
|
567
|
+
part = part.substring(0, 4);
|
|
568
|
+
}
|
|
569
|
+
if (part.length === 4) {
|
|
570
|
+
lastMatchIsFull = true;
|
|
571
|
+
processedValueParts.push(part);
|
|
572
|
+
}
|
|
573
|
+
if (part.length === 3) {
|
|
574
|
+
if (isLastExpectedField || !hasDelimiterAfter) {
|
|
575
|
+
lastMatchIsFull = false;
|
|
576
|
+
processedValueParts.push(part);
|
|
577
|
+
} else {
|
|
578
|
+
valueParts[index + 1] = `*${part.substring(2)}${valueParts[index + 1]}`;
|
|
579
|
+
part = part.substring(0, 2);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
if (part.length === 2) {
|
|
583
|
+
let autocomplete;
|
|
584
|
+
if (part.length === 2 && (hasDelimiterAfter || isLastExpectedField && part !== "19" && part !== "20")) {
|
|
585
|
+
autocomplete = `20${part}`;
|
|
586
|
+
if (part > (new Date().getFullYear() + 5).toString().substring(2)) {
|
|
587
|
+
autocomplete = `19${part}`;
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
if (autocomplete) {
|
|
591
|
+
lastMatchIsFull = true;
|
|
592
|
+
processedValueParts.push(autocomplete);
|
|
593
|
+
} else {
|
|
594
|
+
lastMatchIsFull = false;
|
|
595
|
+
processedValueParts.push(part);
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
if (part.length === 1 || part.length === 0) {
|
|
599
|
+
lastMatchIsFull = false;
|
|
600
|
+
processedValueParts.push(part);
|
|
601
|
+
}
|
|
602
|
+
} else if (curFormat === "YY") {
|
|
603
|
+
if (part.length > 2) {
|
|
604
|
+
valueParts[index + 1] = [
|
|
605
|
+
"*",
|
|
606
|
+
part.substring(2),
|
|
607
|
+
valueParts[index + 1]
|
|
608
|
+
].join("");
|
|
609
|
+
part = part.substring(0, 2);
|
|
610
|
+
}
|
|
611
|
+
if (part.length === 2) {
|
|
612
|
+
lastMatchIsFull = true;
|
|
613
|
+
processedValueParts.push(part);
|
|
614
|
+
}
|
|
615
|
+
if (part.length === 1 || part.length === 0) {
|
|
616
|
+
lastMatchIsFull = false;
|
|
617
|
+
processedValueParts.push(part);
|
|
618
|
+
}
|
|
619
|
+
} else {
|
|
620
|
+
if (part.length > 2) {
|
|
621
|
+
valueParts[index + 1] = [
|
|
622
|
+
"*",
|
|
623
|
+
part.substring(2),
|
|
624
|
+
valueParts[index + 1]
|
|
625
|
+
].join("");
|
|
626
|
+
part = part.substring(0, 2);
|
|
627
|
+
}
|
|
628
|
+
if (part.length === 2) {
|
|
629
|
+
if (part === "00" && !hasDelimiterAfter) {
|
|
630
|
+
lastMatchIsFull = false;
|
|
631
|
+
processedValueParts.push("0");
|
|
632
|
+
} else {
|
|
633
|
+
lastMatchIsFull = true;
|
|
634
|
+
processedValueParts.push(part);
|
|
635
|
+
}
|
|
636
|
+
}
|
|
637
|
+
if (part.length === 1) {
|
|
638
|
+
if (hasDelimiterAfter) {
|
|
639
|
+
lastMatchIsFull = true;
|
|
640
|
+
processedValueParts.push(`0${part}`);
|
|
641
|
+
} else {
|
|
642
|
+
lastMatchIsFull = false;
|
|
643
|
+
processedValueParts.push(part);
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
if (part.length === 0) {
|
|
647
|
+
lastMatchIsFull = false;
|
|
648
|
+
processedValueParts.push(part);
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
}
|
|
652
|
+
}
|
|
653
|
+
return processedValueParts.join(delimiter);
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
const currency = {
|
|
657
|
+
name: "currency",
|
|
658
|
+
format: (_value, options) => {
|
|
659
|
+
const value = typeof _value === "number" ? String(_value) : _value;
|
|
660
|
+
const {
|
|
661
|
+
currencySymbol = "",
|
|
662
|
+
useParensForNeg = false,
|
|
663
|
+
precision = 2
|
|
664
|
+
} = options != null ? options : {};
|
|
665
|
+
if (value === void 0 || value === "") {
|
|
666
|
+
return value;
|
|
667
|
+
}
|
|
668
|
+
if (typeof value !== "string") {
|
|
669
|
+
return value;
|
|
670
|
+
}
|
|
671
|
+
const sign = /^\s*-/.test(value) ? -1 : 1;
|
|
672
|
+
const dotIndex = value.indexOf(".");
|
|
673
|
+
let preDecimal;
|
|
674
|
+
let postDecimal;
|
|
675
|
+
if (dotIndex >= 0) {
|
|
676
|
+
preDecimal = value.substr(0, dotIndex).replace(/\D+/g, "");
|
|
677
|
+
postDecimal = value.substr(dotIndex + 1).replace(/\D+/g, "");
|
|
678
|
+
} else {
|
|
679
|
+
preDecimal = value.replace(/\D+/g, "");
|
|
680
|
+
postDecimal = "0";
|
|
681
|
+
}
|
|
682
|
+
const numericalValue = sign * Number(`${preDecimal}.${postDecimal}`);
|
|
683
|
+
const fixedString = numericalValue.toFixed(precision);
|
|
684
|
+
const prettyString = fixedString.replace(/\B(?=(\d{3})+(?!\d))/g, ",");
|
|
685
|
+
if (prettyString.charAt(0) === "-") {
|
|
686
|
+
if (useParensForNeg) {
|
|
687
|
+
return `(${currencySymbol}${prettyString.substring(1)})`;
|
|
688
|
+
}
|
|
689
|
+
return `-${currencySymbol}${prettyString.substring(1)}`;
|
|
690
|
+
}
|
|
691
|
+
return currencySymbol + prettyString;
|
|
692
|
+
},
|
|
693
|
+
deformat: (value, options) => {
|
|
694
|
+
var _a;
|
|
695
|
+
if (typeof value === "number") {
|
|
696
|
+
return value;
|
|
697
|
+
}
|
|
698
|
+
if (typeof value !== "string") {
|
|
699
|
+
return void 0;
|
|
700
|
+
}
|
|
701
|
+
let deformatted = value;
|
|
702
|
+
if (options == null ? void 0 : options.currencySymbol) {
|
|
703
|
+
deformatted = value.replace(options.currencySymbol, "");
|
|
704
|
+
}
|
|
705
|
+
return (_a = commaNumber.deformat) == null ? void 0 : _a.call(commaNumber, deformatted);
|
|
706
|
+
}
|
|
707
|
+
};
|
|
708
|
+
const basePhoneFormatter = createMaskedNumericFormatter("phone", "(###) ###-####");
|
|
709
|
+
const phone = __spreadProps(__spreadValues({}, basePhoneFormatter), {
|
|
710
|
+
deformat: (value) => {
|
|
711
|
+
var _a;
|
|
712
|
+
return (_a = basePhoneFormatter.deformat) == null ? void 0 : _a.call(basePhoneFormatter, value);
|
|
713
|
+
},
|
|
714
|
+
format: (value) => {
|
|
715
|
+
var _a, _b;
|
|
716
|
+
return (_b = (_a = basePhoneFormatter.format) == null ? void 0 : _a.call(basePhoneFormatter, value === "(" ? "" : value)) != null ? _b : value;
|
|
717
|
+
}
|
|
718
|
+
});
|
|
719
|
+
|
|
720
|
+
var formats = /*#__PURE__*/Object.freeze({
|
|
721
|
+
__proto__: null,
|
|
722
|
+
integer: integer,
|
|
723
|
+
commaNumber: commaNumber,
|
|
724
|
+
date: date,
|
|
725
|
+
currency: currency,
|
|
726
|
+
phone: phone
|
|
727
|
+
});
|
|
728
|
+
|
|
729
|
+
class CommonTypesPlugin {
|
|
730
|
+
constructor() {
|
|
731
|
+
this.name = "CommonTypes";
|
|
732
|
+
}
|
|
733
|
+
apply(player) {
|
|
734
|
+
player.registerPlugin(new TypesProviderPlugin({
|
|
735
|
+
types: Object.values(dataTypes),
|
|
736
|
+
formats: Object.values(formats),
|
|
737
|
+
validators: Object.entries(validators)
|
|
738
|
+
}));
|
|
739
|
+
}
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
export { CommonTypesPlugin, PLACEHOLDER, createMaskedNumericFormatter, refs as dataRefs, dataTypes, formatAsEnum, formatAsMasked, formats, removeFormatCharactersFromMaskedString, validators };
|
|
743
|
+
//# sourceMappingURL=index.esm.js.map
|