@itfin/components 1.2.24 → 1.2.26
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/package.json +1 -1
- package/src/helpers/formatters.js +15 -5
- package/src/helpers/validators.js +56 -17
- package/src/helpers/validators.spec.js +55 -1
package/package.json
CHANGED
|
@@ -31,6 +31,10 @@ export function parseHours (str, { hoursInDay } = { hoursInDay: 8 }) {
|
|
|
31
31
|
return seconds;
|
|
32
32
|
}
|
|
33
33
|
|
|
34
|
+
export function formatDate (date, inputFormat = 'yyyy-MM-dd', toFormat = 'MM/dd/yyyy') {
|
|
35
|
+
return DateTime.fromFormat(date, inputFormat).toFormat(toFormat);
|
|
36
|
+
}
|
|
37
|
+
|
|
34
38
|
export function formatRangeDates(begin, end = null) {
|
|
35
39
|
const DATE_FULL_FORMAT = 'dd MMM yyyy';
|
|
36
40
|
const DATE_SHORT_FORMAT = 'dd MMM';
|
|
@@ -57,13 +61,19 @@ export function formatRangeDates(begin, end = null) {
|
|
|
57
61
|
}
|
|
58
62
|
|
|
59
63
|
export function firstLetters(name) {
|
|
60
|
-
|
|
64
|
+
let [first, second] = (name || '').trim()
|
|
61
65
|
.replace(/[\u0250-\u0400\u04FF-\ue0070-9]/g, '')
|
|
62
66
|
.replace(/ +/g, ' ')
|
|
63
|
-
.split(' ')
|
|
64
|
-
|
|
67
|
+
.split(' ');
|
|
68
|
+
|
|
69
|
+
if (first === 'undefined') {
|
|
70
|
+
first = '';
|
|
71
|
+
}
|
|
72
|
+
if (second === 'undefined') {
|
|
73
|
+
second = '';
|
|
74
|
+
}
|
|
65
75
|
if (!second) {
|
|
66
|
-
return first[0];
|
|
76
|
+
return first[0] || '';
|
|
67
77
|
}
|
|
68
|
-
return first[0] + second[0];
|
|
78
|
+
return (first[0] || '') + (second[0] || '');
|
|
69
79
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DateTime } from 'luxon';
|
|
2
|
-
import { parseHours } from './formatters';
|
|
2
|
+
import { parseHours, formatDate } from './formatters';
|
|
3
3
|
|
|
4
4
|
const URL_REGEXP = /^(?:http(s)?:\/\/)?[\w.-]+(?:\.[\w.-]+)+[\w\-._~:/?#[\]@!$&'()*+,;=]+$/gm;
|
|
5
5
|
const LINK_URL_REGEXP = /(http[s]?:\/\/|www\.)[a-zA-Z0-9.-]+\.[a-zA-Z]{2,5}[.]{0,1}/gi;
|
|
@@ -13,6 +13,8 @@ const EMAIL_REGEXP = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~
|
|
|
13
13
|
const EMAIL_LIST_REGEXP = /^(\w+((-\w+)|(\.\w+))*@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]{2,4}\s*?,?\s*?)+$/g;
|
|
14
14
|
const HEX_REGEXP = /[0-9A-Fa-f]{6}/;
|
|
15
15
|
|
|
16
|
+
const STANDART_DATE_FORMAT = 'yyyy-MM-dd';
|
|
17
|
+
|
|
16
18
|
export const MEDIUM_TEXT_LENGTH = 16777215;
|
|
17
19
|
|
|
18
20
|
function isEmpty(v) {
|
|
@@ -23,6 +25,10 @@ export function differentValidation (value, message) {
|
|
|
23
25
|
return (v, $t) => !v || !(v === value) || (message || $t('components.theValueMustBeDifferent'));
|
|
24
26
|
}
|
|
25
27
|
|
|
28
|
+
export function mediumTextValidation (message) {
|
|
29
|
+
return (v, $t) => !v || ((v || '').toString().trim().length <= MEDIUM_TEXT_LENGTH) || (message || $t('components.mediumTextLength'));
|
|
30
|
+
}
|
|
31
|
+
|
|
26
32
|
export function lengthValidation (length, message) {
|
|
27
33
|
return (v, $t) => !v || ((v || '').toString().trim().length <= length) || (message || $t('components.mustBeLessThanLengthCharacters', {length}));
|
|
28
34
|
}
|
|
@@ -115,26 +121,10 @@ export function allowDigitsOnlyValidation (message) {
|
|
|
115
121
|
return (v, $t) => !v || !!v.match(/^\d+$/) || (message || $t('components.thisFieldMustContainsDigits'));
|
|
116
122
|
}
|
|
117
123
|
|
|
118
|
-
// export function dateBeforeValidation (dateCompare, message = `The date should be before ${moment(dateCompare).format('MM/DD/YYYY')}`) {
|
|
119
|
-
// return (v, $t) => !v || moment(v).isBefore(dateCompare, 'd') || (message || $t('components.'));
|
|
120
|
-
// }
|
|
121
|
-
|
|
122
124
|
export function dateSameOrBeforeValidationLuxon (dateCompare, message) {
|
|
123
125
|
return (v, $t) => !v || DateTime.fromISO(v) <= DateTime.fromISO(dateCompare) || (message || $t('components.thedateShouldBeSameOrBefore', {date: DateTime.fromISO(dateCompare).toFormat('dd-MM-yyyy')}));
|
|
124
126
|
}
|
|
125
127
|
|
|
126
|
-
// export function dateAfterValidation (dateCompare, message = `The date should be after ${moment(dateCompare).format('MM/DD/YYYY')}`) {
|
|
127
|
-
// return (v, $t) => !v || moment(v).isAfter(dateCompare, 'd') || (message || $t('components.'));
|
|
128
|
-
// }
|
|
129
|
-
//
|
|
130
|
-
// export function dateSameOrAfterValidation (dateCompare, message = `The date should be after ${moment(dateCompare).format('MM/DD/YYYY')}`) {
|
|
131
|
-
// return (v, $t) => !v || moment(v).isSameOrAfter(dateCompare, 'd') || (message || $t('components.'));
|
|
132
|
-
// }
|
|
133
|
-
//
|
|
134
|
-
// export function dateSameOrBeforeValidation (dateCompare, message = `The date should be same or before ${moment(dateCompare).format('MM/DD/YYYY')}`) {
|
|
135
|
-
// return (v, $t) => !v || moment(v).isSameOrBefore(dateCompare, 'd') || (message || $t('components.'));
|
|
136
|
-
// }
|
|
137
|
-
|
|
138
128
|
export function dateAfterValidationLuxon (dateCompare, message) {
|
|
139
129
|
return (v, $t) => !v || DateTime.fromISO(v) > DateTime.fromISO(dateCompare) || (message || $t('components.thedateShouldBeSameOrAfter', {date: DateTime.fromISO(dateCompare).toFormat('yyyy-MM-dd')}));
|
|
140
130
|
}
|
|
@@ -142,3 +132,52 @@ export function dateAfterValidationLuxon (dateCompare, message) {
|
|
|
142
132
|
export function hoursValidation (message) {
|
|
143
133
|
return (v, $t) => !v || parseHours(v) !== false || (message || $t('components.invalidTimeValueShouldBeInFormat'));
|
|
144
134
|
}
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
export function dateBeforeValidation (dateCompare, message) {
|
|
138
|
+
return (v, $t) => {
|
|
139
|
+
if (!v || !dateCompare || typeof dateCompare !== 'string') {
|
|
140
|
+
return true;
|
|
141
|
+
}
|
|
142
|
+
const d1 = DateTime.fromFormat(v, STANDART_DATE_FORMAT).startOf('day');
|
|
143
|
+
const d2 = DateTime.fromFormat(dateCompare, STANDART_DATE_FORMAT).startOf('day');
|
|
144
|
+
|
|
145
|
+
return d1 < d2 || (message || $t('validators.dateBefore', { date: formatDate(dateCompare) }));
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function dateAfterValidation (dateCompare, message) {
|
|
150
|
+
return (v, $t) => {
|
|
151
|
+
if (!v || !dateCompare || typeof dateCompare !== 'string') {
|
|
152
|
+
return true;
|
|
153
|
+
}
|
|
154
|
+
const d1 = DateTime.fromFormat(v, STANDART_DATE_FORMAT).startOf('day');
|
|
155
|
+
const d2 = DateTime.fromFormat(dateCompare, STANDART_DATE_FORMAT).startOf('day');
|
|
156
|
+
|
|
157
|
+
return d1 > d2 || (message || $t('validators.dateAfter', { date: formatDate(dateCompare) }));
|
|
158
|
+
};
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
export function dateSameOrAfterValidation (dateCompare, message) {
|
|
162
|
+
return (v, $t) => {
|
|
163
|
+
if (!v || !dateCompare || typeof dateCompare !== 'string') {
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
const d1 = DateTime.fromFormat(v, STANDART_DATE_FORMAT).startOf('day');
|
|
167
|
+
const d2 = DateTime.fromFormat(dateCompare, STANDART_DATE_FORMAT).startOf('day');
|
|
168
|
+
|
|
169
|
+
return d1 >= d2 || (message || $t('validators.dateSameOrAfter', { date: formatDate(dateCompare) }));
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
export function dateSameOrBeforeValidation (dateCompare, message) {
|
|
174
|
+
return (v, $t) => {
|
|
175
|
+
if (!v || !dateCompare || typeof dateCompare !== 'string') {
|
|
176
|
+
return true;
|
|
177
|
+
}
|
|
178
|
+
const d1 = DateTime.fromFormat(v, STANDART_DATE_FORMAT).startOf('day');
|
|
179
|
+
const d2 = DateTime.fromFormat(dateCompare, STANDART_DATE_FORMAT).startOf('day');
|
|
180
|
+
|
|
181
|
+
return d1 <= d2 || (message || $t('validators.dateSameOrBefore', { date: formatDate(dateCompare) }));
|
|
182
|
+
};
|
|
183
|
+
}
|
|
@@ -1,4 +1,13 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
emptyValidation,
|
|
3
|
+
mediumTextValidation,
|
|
4
|
+
minValidation,
|
|
5
|
+
maxValidation,
|
|
6
|
+
dateBeforeValidation,
|
|
7
|
+
dateAfterValidation,
|
|
8
|
+
dateSameOrAfterValidation,
|
|
9
|
+
dateSameOrBeforeValidation,
|
|
10
|
+
} from './validators';
|
|
2
11
|
|
|
3
12
|
describe('Validators', () => {
|
|
4
13
|
const $t = (str) => str;
|
|
@@ -25,4 +34,49 @@ describe('Validators', () => {
|
|
|
25
34
|
expect(maxValidation(1)(0, $t)).toEqual(true);
|
|
26
35
|
expect(maxValidation(1)('0', $t)).toEqual(true);
|
|
27
36
|
});
|
|
37
|
+
|
|
38
|
+
test('dateBeforeValidation', () => {
|
|
39
|
+
expect(dateBeforeValidation()(0, $t)).toEqual(true);
|
|
40
|
+
expect(dateBeforeValidation()(null, $t)).toEqual(true);
|
|
41
|
+
expect(dateBeforeValidation()(undefined, $t)).toEqual(true);
|
|
42
|
+
expect(dateBeforeValidation('2020-01-02')('2020-01-01', $t)).toEqual(true);
|
|
43
|
+
expect(dateBeforeValidation('2020-01-01')('2020-01-02', $t)).toEqual('validators.dateBefore');
|
|
44
|
+
expect(dateBeforeValidation('2020-01-01')('2020-01-01', $t)).toEqual('validators.dateBefore');
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
test('dateAfterValidation', () => {
|
|
48
|
+
expect(dateAfterValidation()(0, $t)).toEqual(true);
|
|
49
|
+
expect(dateAfterValidation()(null, $t)).toEqual(true);
|
|
50
|
+
expect(dateAfterValidation()(undefined, $t)).toEqual(true);
|
|
51
|
+
expect(dateAfterValidation('2020-01-02')('2020-01-01', $t)).toEqual('validators.dateAfter');
|
|
52
|
+
expect(dateAfterValidation('2020-01-01')('2020-01-02', $t)).toEqual(true);
|
|
53
|
+
expect(dateAfterValidation('2020-01-01')('2020-01-01', $t)).toEqual('validators.dateAfter');
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
test('dateSameOrAfterValidation', () => {
|
|
57
|
+
expect(dateSameOrAfterValidation()(0, $t)).toEqual(true);
|
|
58
|
+
expect(dateSameOrAfterValidation()(null, $t)).toEqual(true);
|
|
59
|
+
expect(dateSameOrAfterValidation()(undefined, $t)).toEqual(true);
|
|
60
|
+
expect(dateSameOrAfterValidation('2020-01-02')('2020-01-01', $t)).toEqual('validators.dateSameOrAfter');
|
|
61
|
+
expect(dateSameOrAfterValidation('2020-01-01')('2020-01-02', $t)).toEqual(true);
|
|
62
|
+
expect(dateSameOrAfterValidation('2020-01-01')('2020-01-01', $t)).toEqual(true);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('dateSameOrBeforeValidation', () => {
|
|
66
|
+
expect(dateSameOrBeforeValidation()(0, $t)).toEqual(true);
|
|
67
|
+
expect(dateSameOrBeforeValidation()(null, $t)).toEqual(true);
|
|
68
|
+
expect(dateSameOrBeforeValidation()(undefined, $t)).toEqual(true);
|
|
69
|
+
expect(dateSameOrBeforeValidation('2020-01-02')('2020-01-01', $t)).toEqual(true);
|
|
70
|
+
expect(dateSameOrBeforeValidation('2020-01-01')('2020-01-02', $t)).toEqual('validators.dateSameOrBefore');
|
|
71
|
+
expect(dateSameOrBeforeValidation('2020-01-01')('2020-01-01', $t)).toEqual(true);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test('mediumTextValidation', () => {
|
|
75
|
+
expect(mediumTextValidation()(0, $t)).toEqual(true);
|
|
76
|
+
expect(mediumTextValidation()(null, $t)).toEqual(true);
|
|
77
|
+
expect(mediumTextValidation()(undefined, $t)).toEqual(true);
|
|
78
|
+
expect(mediumTextValidation()('abc', $t)).toEqual(true);
|
|
79
|
+
expect(mediumTextValidation()('0'.repeat(16777215), $t)).toEqual(true);
|
|
80
|
+
expect(mediumTextValidation()('0'.repeat(16777216), $t)).toEqual('components.mediumTextLength');
|
|
81
|
+
});
|
|
28
82
|
});
|