Skip to content

Instantly share code, notes, and snippets.

View Gummiees's full-sized avatar

Xavi Caro Gummiees

  • Balearic Islands, Spain
  • 03:15 (UTC 01:00)
View GitHub Profile
@Gummiees
Gummiees / was-last-page-current-domain.ts
Created February 2, 2022 12:25
Checks whether the page the user comes from was the current one or not. This does not work if the user came by typing the URL.
if(!document.referrer.includes(window.location.host)) {
this.analyticsSink.logFirstLoadEvent();
}
@Gummiees
Gummiees / TS-interfaces-problem.ts
Last active February 2, 2022 10:47
A TypeScript problem with interfaces
export interface ExampleInterface {
exampleMethod(result: string): void;
}
export class CorrectExampleClass implements ExampleInterface {
exampleMethod(result: string) {
console.log('This is okay :D');
}
}
@Gummiees
Gummiees / coolTypesStuff.ts
Created February 2, 2022 10:46
Cool stuff you can do with types
// Similar magic I want to achieve:
type Role = 'patient' | 'clinician' | 'admin';
// No need to declare one by one!
type capitalizedRole = Capitalize<Role>;
// The example goes as follows:
interface GenericEvent {
id: string;
view: string;
@Gummiees
Gummiees / new-array-length.ts
Last active January 28, 2022 14:36
Create a new array of defined length
// Create a new array of length X, that contains the index 1 to the length.
const x = 10;
const numbers = new Array(x).fill(null).map((_,i)=> i 1);
@Gummiees
Gummiees / angular-visibilitychange.ts
Created January 28, 2022 14:27
Detect with Angular when the user sends the site to the background/foreground
@HostListener('document:visibilitychange', ['$event'])
visibilitychange() {
if (document.visibilityState === 'hidden') {
console.log('hidden');
} else {
console.log('visible');
}
}
@Gummiees
Gummiees / angular-window-beforeunload.ts
Created January 28, 2022 14:26
Display default browser popup about not saved changes when leaving the site with Angular
/* When returning false, it provokes the browser to show the popup asking if they want to leave without saving. */
@HostListener('window:beforeunload', ['$event'])
beforeUnloadHander(event) {
console.log('window:beforeunload');
return false;
}
@Gummiees
Gummiees / angular-window-unload.ts
Created January 28, 2022 14:24
Detecting when the user navigates to another site with Angular
/* This method detects when the user is leaving without prompting anything. */
@HostListener('window:unload', ['$event'])
unloadHandler(event) {
console.log('window:unload');
}
@Gummiees
Gummiees / objectDefined.ts
Last active January 22, 2021 14:34
Checks if an object is has all the properties defined.
/**
* Checks if an object is has all the properties defined.
*
* Attention: It does not work recursively.
* @param obj Object to scan.
* @param type Object type.
* @returns Boolean, depending on if all the properties are defined or not.
*/
defined<T>(obj: T, type: new () => T): boolean {
if (!obj) {
@Gummiees
Gummiees / objectsEquals.ts
Created October 29, 2020 09:48
Compares two same type objects to check if their properties and values are equals
/**
* Compares two same type objects to check if their properties and values are equals.
*
* Attention: It is not recursively.
* @param original Original element to have as a base.
* @param compareTo element to compare to with the original one.
* @returns The result from comparing both.
*/
equals<T>(original: T, compareTo: T): boolean {
if (!original && !compareTo) {
@Gummiees
Gummiees / mapAll.ts
Created October 29, 2020 09:46
Duplicates the content from a source to a target, both being the same type
/**
*
* Duplica el contenido de un objeto sobre otro del mismo tipo.
*
* @param source Objeto a partir de cual obtener los valores
* @param destiny Objeto al cual asignar dichos valores
*/
mapAll<T>(source: T, destiny: T) {
const keys: any[] = Object.keys(source);
keys.forEach((key) => {