@obliczeniowo/elementary
Version:
Library made in Angular version 20
329 lines (324 loc) • 8.46 kB
JavaScript
const firstToUpper = (text) => {
if (!text.length) {
return text;
}
return text.charAt(0).toUpperCase() + text.slice(1);
};
const allWordsFirstToUpper = (text) => {
return text.split(' ').map(text => firstToUpper(text)).join(' ');
};
const toCamelCase = (text) => {
return allWordsFirstToUpper(text.replace(/\s/g, ' ').trim()).replace(/\s/g, '');
};
class OblValidators {
/**
* Expressions used to validate, you can override them on very start of your application to apply your own specific
* validation for e-mail or what so ever
*/
static regs = {
email: /^(([^<>()\[\]\.,;:\s@\"]+(\.[^<>()\[\]\.,;:\s@\"]+)*)|(\".+\"))@(([^<>()[\]\.,;:\s@\"]+\.)+[^<>()[\]\.,;:\s@\"]{2,})$/i,
noWhiteSpaces: /^[^\s]*$/,
login: /^[a-zA-Z0-9]*$/,
uLogin: /^[\p{Alphabetic}0-9]*$/u
};
/**
* Test if email is valid
*
* In case you want to override it you can just change OblValidators.regs.email to what you want
*
* @example
*
* "test@test.pl" => null
* "test@test" => { 'invalid-email': true }
* "test" => { 'invalid-email': true }
* "test@" => { 'invalid-email': true }
*/
static email(control) {
const email = control.value;
const ok = null;
if (!email) {
return ok;
}
if (email !== '' && !OblValidators.regs.email.test(email)) {
return { 'invalid-email': true };
}
return ok;
}
/**
* Test Polish NIP (number of task identify)
*/
static nip(control) {
let nip = control.value?.toString() || '';
nip = nip.replace(/[\ \-]/gi, '');
if (nip.length !== 10) {
return { 'nip-length': true };
}
const weights = [6, 5, 7, 2, 3, 4, 5, 6, 7];
let sum = 0;
const controlNumber = parseInt(nip.substring(9, 10));
const weightCount = weights.length;
for (let i = 0; i < weightCount; i++) {
sum += (parseInt(nip.substr(i, 1)) * weights[i]);
}
return sum % 11 === controlNumber ? null : { 'nip-sum': true };
}
/**
* Test Polish PESEL number
*/
static pesel(control) {
let pesel = control.value?.toString() || '';
pesel = pesel.replace(/[\ \-]/gi, '');
if (pesel.length !== 10) {
return { 'pesel-length': true };
}
const weight = [1, 3, 7, 9, 1, 3, 7, 9, 1, 3];
let sum = 0;
const controlNumber = parseInt(pesel.substring(10, 11));
for (let i = 0; i < weight.length; i++) {
sum += (parseInt(pesel.substring(i, i + 1)) * weight[i]);
}
sum = sum % 10;
return (10 - sum) % 10 === controlNumber ? null : { 'pesel-sum': true };
}
/**
* Validate Polish REGON number
*/
static regon(control) {
let rawRegon = control.value?.toString();
rawRegon = rawRegon.replace(/[\ \-]/gi, '');
const weights9 = [8, 9, 2, 3, 4, 5, 6, 7];
const weights14 = [2, 4, 8, 5, 0, 9, 7, 3, 6, 1, 2, 4, 8];
if (!rawRegon) {
return null;
}
const regon = rawRegon.toString().replace(/[^a-z\d]/gi, '');
if (regon.length !== 9 && regon.length !== 14) {
return { 'regon-length': true };
}
// calculate checksum
let sum = 0;
weights9.forEach((weight, position) => {
const digit = Number(regon[position]);
sum += weight * digit;
});
const checksum = (sum % 11) % 10;
if (checksum !== Number(regon[8])) {
return { 'regon-checksum': true };
}
if (regon.length === 14) {
// calculate second checksum
let sum2 = 0;
weights14.forEach((weight, position) => {
const digit = Number(regon[position]);
sum2 += weight * digit;
});
const checksum2 = (sum2 % 11) % 10;
if (checksum2 !== Number(regon[13])) {
return { 'regon-checksum': true };
}
}
return null;
}
/**
* Test if contains white spaces (space, tabulator, new line)
*
* @example
*
* "test@#$%_01234" => null
* "test@#$% 01234" => { 'white-spaces': true }
*/
static noWhiteSpaces(control) {
return OblValidators.regs.noWhiteSpaces.test(control.value) ? null : { 'white-spaces': true };
}
/**
* Test if value from AbstractControl contain only any alphabetical unicode sign or number 0-9 excluding _
* this unicode version can have some problem on different types of browsers
*
* In case you want to override it you can just change OblValidators.regs.login to what you want
*
* @example
*
* "zazolcGesiaJazn" => null
* "zażółćGęsiąJaźń" => { 'non-digit-or-alphabetical': true }
* "zazolcGesiaJazn0123456789" => null
* "zazolcGesiaJaznń0123456789_" => { 'non-digit-or-alphabetical': true }
* "zazolcGesiaJazn0123456789$%^" => { 'non-digit-or-alphabetical': true }
* "zazolc gesia jazn" => { 'white-spaces': true }
*/
static login(control) {
const err = OblValidators.noWhiteSpaces(control);
if (err) {
return err;
}
return OblValidators.regs.login.test(control.value) ? null : { 'non-digit-or-alphabetical': true };
}
/**
* Test if value from AbstractControl contain only any alphabetical unicode sign or number 0-9 excluding _
* this unicode version can have some problem on different types of browsers, if it's ok for you to have
* ASCII only version then please use login version
*
* In case you want to override it you can just change OblValidators.regs.uLogin to what you want
*
* @example
*
* "zażółćGęsiąJaźń" => null
* "zażółćGęsiąJaźń0123456789" => null
* "zażółćGęsiąJaźń0123456789_" => { 'non-digit-or-alphabetical': true }
* "zażółćGęsiąJaźń0123456789$%^" => { 'non-digit-or-alphabetical': true }
* "zażółć gęsią jaźń" => { 'white-spaces': true }
*/
static uLogin(control) {
const err = OblValidators.noWhiteSpaces(control);
if (err) {
return err;
}
return OblValidators.regs.uLogin.test(control.value) ? null : { 'non-digit-or-alphabetical': true };
}
/**
* validate if values matching
*/
static equalValidator(value) {
return (control) => {
if (control.value !== value) {
return { 'not-equal': true };
}
return null;
};
}
/**
* validate if value from form group with name is equal to value to validated field
*/
static equalFormFieldValidator(form, name) {
return (control) => {
if (control.value !== form.get(name)?.getRawValue()) {
return { 'not-equal': true };
}
return null;
};
}
}
const symbols = [
'H',
'He',
'Li',
'Be',
'B',
'C',
'N',
'O',
'F',
'Ne',
'Na',
'Mg',
'Al',
'Si',
'P',
'S',
'Cl',
'Ar',
'K',
'Ca',
'Sc',
'Ti',
'V',
'Cr',
'Mn',
'Fe',
'Co',
'Ni',
'Cu',
'Zn',
'Ga',
'Ge',
'As',
'Se',
'Br',
'Kr',
'Rb',
'Sr',
'Y',
'Zr',
'Nb',
'Mo',
'Tc',
'Ru',
'Rh',
'Pd',
'Ag',
'Cd',
'In',
'Sn',
'Sb',
'Te',
'I',
'Xe',
'Cs',
'Ba',
'La',
'Ce',
'Pr',
'Nd',
'Pm',
'Sm',
'Eu',
'Gd',
'Tb',
'Dy',
'Ho',
'Er',
'Tm',
'Yb',
'Lu',
'Hf',
'Ta',
'W',
'Re',
'Os',
'Ir',
'Pt',
'Au',
'Hg',
'Tl',
'Pb',
'Bi',
'Po',
'At',
'Rn',
'Fr',
'Ra',
'Ac',
'Th',
'Pa',
'U',
'Np',
'Pu',
'Am',
'Cm',
'Bk',
'Cf',
'Es',
'Fm',
'Md',
'No',
'Lr',
'Rf',
'Db',
'Sg',
'Bh',
'Hs',
'Mt',
'Ds',
'Rg',
'Cn',
'Nh',
'Fl',
'Mc',
'Lv',
'Ts',
'Og',
];
/**
* Generated bundle index. Do not edit.
*/
export { OblValidators, allWordsFirstToUpper, firstToUpper, symbols, toCamelCase };
//# sourceMappingURL=obliczeniowo-elementary-utils.mjs.map