Skip to content

Instantly share code, notes, and snippets.

View eduardorgv's full-sized avatar
馃彔
Working from home

Eduardo Rodr铆guez eduardorgv

馃彔
Working from home
View GitHub Profile
@eduardorgv
eduardorgv / vite-testing-config.md
Last active February 8, 2023 15:41 — forked from Klerith/vite-testing-config.md
Vite + Jest + React Testing Library - Configuraciones a seguir

Instalaci贸n y configuracion de Jest + React Testing Library

En proyectos de React + Vite

  1. Instalaciones:
npm i --dev jest babel-jest @babel/preset-env @babel/preset-react 
npm i --dev @testing-library/react @types/jest jest-environment-jsdom
  1. Opcional: Si usamos Fetch API en el proyecto:
@eduardorgv
eduardorgv / dateRangeValidators.ts
Created December 21, 2022 17:11
Toma el valor de dos campos y valida que el rango de las fechas seleccionadas sea igual, de no ser, asigna un error a los campos en el formulario reactivo de Angular. Esta funci贸n se llama en las validaciones as铆ncronas del formulario.
dateRangeValidator(field_1: string, field_2: string) {
return (formGroup: AbstractControl): ValidationErrors | null => {
let fromDate = formGroup.get(field_1)?.value;
let toDate = formGroup.get(field_2)?.value;
if(fromDate && toDate) {
const newFromDate = new Date(fromDate);
const newToDate = new Date(toDate);
@eduardorgv
eduardorgv / containsURL.ts
Last active December 21, 2022 17:06
Valida si en un texto contiene una URL, esta funci贸n se coloca en las validaciones as铆ncronas de un formulario reactivo de angular. De contener una URL, asigna un error al campo recibido en los par谩metros.
containsURL(field: string) {
return (formGroup: AbstractControl): ValidationErrors | null => {
const text = formGroup.get(field)?.value;
const match = text.match(this.urlPattern);
if(text && match && match[0]) {
formGroup.get(field)?.setErrors({includesURL: true});
return { includesURL: true };
}else if(!text) {
formGroup.get(field)?.setErrors({required: true});
@eduardorgv
eduardorgv / hostListener.ts
Last active December 21, 2022 17:06
Change property value by window resize on Typescript
export class AppComponent {
value = 7;
@HostListener("window:resize", []) updateValue() {
// lg (for laptops and desktops - screens equal to or greater than 1200px wide)
// md (for small laptops - screens equal to or greater than 992px wide)
// sm (for tablets - screens equal to or greater than 768px wide)
// xs (for phones - screens less than 768px wide)
if (window.innerWidth >= 1200) {
@eduardorgv
eduardorgv / validations.ts
Created June 21, 2022 21:12 — forked from Klerith/validations.ts
Validar email
export const isValidEmail = (email: string): boolean => {
const match = String(email)
.toLowerCase()
.match(
/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
);