@cauca-911/input-validators
Version:
Run `npm install @cauca-911/input-validators` to add this library to your project
279 lines (270 loc) • 12.1 kB
JavaScript
import * as i0 from '@angular/core';
import { EventEmitter, inject, ElementRef, Directive, HostListener, Output } from '@angular/core';
class Gsm7bitChars {
static { this.gsm7bitChars = '@£$¥èéùìòÇ\\nØø\\rÅåΔ_ΦΓΛΩΠΨΣΘΞÆæßÉ !\\"#¤%&\'()*+,-./0123456789:;<=>?¡ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÑܧ¿abcdefghijklmnopqrstuvwxyzäöñüà'; }
static { this.gsm7bitCharsExt = '\\^{}\\\\\\[~\\]|€'; }
static { this.gsm7bitRegExp = RegExp('^[' + Gsm7bitChars.gsm7bitChars + ']*$'); }
static { this.gsm7bitExtRegExp = RegExp('^[' + Gsm7bitChars.gsm7bitChars + Gsm7bitChars.gsm7bitCharsExt + ']*$'); }
static { this.gsm7bitExtOnlyRegExp = RegExp('^[' + Gsm7bitChars.gsm7bitCharsExt + ']*$'); }
}
class SmsAvoidCharactersDirective {
constructor() {
this.charsToAvoidChanged = new EventEmitter();
this.charsToReplaceChanged = new EventEmitter();
this.charsToAvoid = [];
this.charsToReplace = [];
this.elementRef = inject(ElementRef);
}
ngAfterViewInit() {
this.onInput();
}
onInput() {
this.checkCharacterException(this.elementRef.nativeElement.value);
}
checkCharacterException(text) {
const charsList = this.listUTF16Chars(text);
if (this.compareCharsList(this.charsToAvoid, charsList.charsToAvoid)) {
this.charsToAvoid = this.removeDuplicates(charsList.charsToAvoid);
this.charsToAvoidChanged.emit(this.charsToAvoid);
}
if (this.compareCharsList(this.charsToReplace, charsList.charsToReplace)) {
this.charsToReplace = this.removeDuplicates(charsList.charsToReplace);
this.charsToReplaceChanged.emit(this.charsToReplace);
}
}
listUTF16Chars(text) {
const charToAvoidOrReplace = text.replace(new RegExp('([' + Gsm7bitChars.gsm7bitChars + ']+)', 'g'), '');
const charToReplace = charToAvoidOrReplace.replace(new RegExp('([' + Gsm7bitChars.gsm7bitCharsExt + ']+)', 'g'), '');
const charToAvoid = charToAvoidOrReplace.match(new RegExp('([' + Gsm7bitChars.gsm7bitCharsExt + ']+)', 'g'));
return {
charsToAvoid: charToAvoid ? charToAvoid[0].split('') : [],
charsToReplace: charToReplace ? charToReplace.split('') : []
};
}
compareCharsList(list1, list2) {
return JSON.stringify(list1) !== JSON.stringify(list2);
}
removeDuplicates(items) {
return [...new Set(items)];
}
static { this.ɵfac = function SmsAvoidCharactersDirective_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || SmsAvoidCharactersDirective)(); }; }
static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: SmsAvoidCharactersDirective, selectors: [["", "caucaSmsAvoidCharacters", ""]], hostBindings: function SmsAvoidCharactersDirective_HostBindings(rf, ctx) { if (rf & 1) {
i0.ɵɵlistener("input", function SmsAvoidCharactersDirective_input_HostBindingHandler() { return ctx.onInput(); });
} }, outputs: { charsToAvoidChanged: "charsToAvoidChanged", charsToReplaceChanged: "charsToReplaceChanged" } }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(SmsAvoidCharactersDirective, [{
type: Directive,
args: [{
selector: '[caucaSmsAvoidCharacters]',
}]
}], null, { charsToAvoidChanged: [{
type: Output
}], charsToReplaceChanged: [{
type: Output
}], onInput: [{
type: HostListener,
args: [':input']
}] }); })();
class SmsSegmentCalculatorDirective {
constructor() {
this.segmentCountChanged = new EventEmitter();
this.elementRef = inject(ElementRef);
this.segmentCount = 0;
this.messageLength = {
// eslint-disable-next-line @typescript-eslint/naming-convention
GSM7: 160,
// eslint-disable-next-line @typescript-eslint/naming-convention
GSM7ext: 160,
// eslint-disable-next-line @typescript-eslint/naming-convention
UTF16: 70
};
this.multiMessageLength = {
// eslint-disable-next-line @typescript-eslint/naming-convention
GSM7: 153,
// eslint-disable-next-line @typescript-eslint/naming-convention
GSM7ext: 153,
// eslint-disable-next-line @typescript-eslint/naming-convention
UTF16: 67
};
}
ngAfterViewInit() {
this.onInput();
}
onInput() {
this.checkNumberOfSegment(this.elementRef.nativeElement.value);
}
checkNumberOfSegment(text) {
const segmentCount = this.countNumberOfSegment(text);
if (this.segmentCount !== segmentCount) {
this.segmentCountChanged.emit(segmentCount);
this.segmentCount = segmentCount;
}
}
countNumberOfSegment(text) {
if (!text) {
return 0;
}
const length = this.countSmsChars(text);
const charset = this.getCharset(text);
const perMessage = (length > this.messageLength[charset] ? this.multiMessageLength[charset] : this.messageLength[charset]);
return Math.ceil(length / perMessage);
}
countSmsChars(text) {
let length = text.length;
const charset = this.getCharset(text);
if (charset === 'GSM7ext') {
length += this.countGsm7bitExt(text);
}
return length;
}
countGsm7bitExt(text) {
let char2;
const chars = () => {
let _i;
let _len;
const _results = [];
for (_i = 0, _len = text.length; _i < _len; _i++) {
char2 = text[_i];
if (char2.match(Gsm7bitChars.gsm7bitExtOnlyRegExp) != null) {
_results.push(char2);
}
}
return _results;
};
return chars.length;
}
getCharset(text) {
switch (false) {
case text.match(Gsm7bitChars.gsm7bitRegExp) == null:
return 'GSM7';
case text.match(Gsm7bitChars.gsm7bitExtRegExp) == null:
return 'GSM7ext';
default:
return 'UTF16';
}
}
static { this.ɵfac = function SmsSegmentCalculatorDirective_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || SmsSegmentCalculatorDirective)(); }; }
static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: SmsSegmentCalculatorDirective, selectors: [["", "caucaSmsSegmentCalculator", ""]], hostBindings: function SmsSegmentCalculatorDirective_HostBindings(rf, ctx) { if (rf & 1) {
i0.ɵɵlistener("input", function SmsSegmentCalculatorDirective_input_HostBindingHandler() { return ctx.onInput(); });
} }, outputs: { segmentCountChanged: "segmentCountChanged" } }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(SmsSegmentCalculatorDirective, [{
type: Directive,
args: [{
selector: '[caucaSmsSegmentCalculator]',
}]
}], null, { segmentCountChanged: [{
type: Output
}], onInput: [{
type: HostListener,
args: [':input']
}] }); })();
class PhoneNumberDetectorDirective {
constructor() {
this.hasPhoneNumberChanged = new EventEmitter();
this.hasPhoneNumber = false;
this.elementRef = inject(ElementRef);
}
ngAfterViewInit() {
this.onInput();
}
onInput() {
this.checkIfTextHasPhoneNumber(this.elementRef.nativeElement.value);
}
checkIfTextHasPhoneNumber(text) {
const hasPhoneNumber = this.checkForPhoneNumber(text);
if (this.hasPhoneNumber !== hasPhoneNumber) {
this.hasPhoneNumberChanged.emit(hasPhoneNumber);
this.hasPhoneNumber = hasPhoneNumber;
}
}
checkForPhoneNumber(text) {
if (!text) {
return false;
}
const regex = /\(?(\d{3})\)?[- .]?(\d{3})[- .]?(\d{4})/gm;
return regex.test(text);
}
static { this.ɵfac = function PhoneNumberDetectorDirective_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || PhoneNumberDetectorDirective)(); }; }
static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: PhoneNumberDetectorDirective, selectors: [["", "caucaPhoneNumberDetector", ""]], hostBindings: function PhoneNumberDetectorDirective_HostBindings(rf, ctx) { if (rf & 1) {
i0.ɵɵlistener("input", function PhoneNumberDetectorDirective_input_HostBindingHandler() { return ctx.onInput(); });
} }, outputs: { hasPhoneNumberChanged: "hasPhoneNumberChanged" } }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(PhoneNumberDetectorDirective, [{
type: Directive,
args: [{
selector: '[caucaPhoneNumberDetector]',
}]
}], null, { hasPhoneNumberChanged: [{
type: Output
}], onInput: [{
type: HostListener,
args: [':input']
}] }); })();
class UrlDetectorDirective {
constructor() {
this.hasUrlChanged = new EventEmitter();
this.hasLongUrlChanged = new EventEmitter();
this.hasUrl = false;
this.hasLongUrl = false;
this.elementRef = inject(ElementRef);
}
ngAfterViewInit() {
this.onInput();
}
onInput() {
this.checkIfTextHasUrl(this.elementRef.nativeElement.value);
}
checkIfTextHasUrl(text) {
const hasUrl = this.checkForUrl(text);
if (this.hasUrl !== hasUrl) {
this.hasUrl = hasUrl;
this.hasUrlChanged.emit(hasUrl);
const hasLongUrl = this.checkForLongUrl(text);
if (this.hasLongUrl !== hasLongUrl) {
this.hasLongUrl = hasUrl;
this.hasLongUrlChanged.emit(hasUrl);
}
}
}
checkForUrl(text) {
if (!text) {
return false;
}
const partialUrl = /[a-zA-Z]{3,5}:\/\//gim;
return partialUrl.test(text);
}
checkForLongUrl(text) {
if (!text) {
return false;
}
const fullUrl = /((?:([a-zA-Z]{3,5}):\/\/(?:(?:[a-zA-Z0-9\$\-\_\.\+\!\*\'\(\)\,\;\?\&\=]|(?:\%[a-fA-F0-9]{2})){1,64}(?:\:(?:[a-zA-Z0-9\$\-\_\.\+\!\*\'\(\)\,\;\?\&\=]|(?:\%[a-fA-F0-9]{2})){1,25})?\@)?)?((?:(?:[a-zA-Z0-9][a-zA-Z0-9\-]{0,64}\.)+(?:(?:[a-zA-Z0-9\-]*)))))(\/(?:(?:[a-zA-Z0-9\;\/\?\:\@\&\=\#\~\-\.\+\!\*\'\(\)\,\_])|(?:\%[a-fA-F0-9]{2}))*)?(?:\b|$)/gim;
const urls = text.match(fullUrl);
return urls.some((url) => url.length > 120);
}
static { this.ɵfac = function UrlDetectorDirective_Factory(__ngFactoryType__) { return new (__ngFactoryType__ || UrlDetectorDirective)(); }; }
static { this.ɵdir = /*@__PURE__*/ i0.ɵɵdefineDirective({ type: UrlDetectorDirective, selectors: [["", "caucaUrlDetector", ""]], hostBindings: function UrlDetectorDirective_HostBindings(rf, ctx) { if (rf & 1) {
i0.ɵɵlistener("input", function UrlDetectorDirective_input_HostBindingHandler() { return ctx.onInput(); });
} }, outputs: { hasUrlChanged: "hasUrlChanged", hasLongUrlChanged: "hasLongUrlChanged" } }); }
}
(() => { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(UrlDetectorDirective, [{
type: Directive,
args: [{
selector: '[caucaUrlDetector]',
}]
}], null, { hasUrlChanged: [{
type: Output
}], hasLongUrlChanged: [{
type: Output
}], onInput: [{
type: HostListener,
args: [':input']
}] }); })();
/*
* Public API Surface of cauca-input-validators
*/
/**
* Generated bundle index. Do not edit.
*/
export { PhoneNumberDetectorDirective, SmsAvoidCharactersDirective, SmsSegmentCalculatorDirective, UrlDetectorDirective };
//# sourceMappingURL=cauca-911-input-validators.mjs.map