Skip to content

Instantly share code, notes, and snippets.

View Gummiees's full-sized avatar

Xavi Caro Gummiees

  • Balearic Islands, Spain
  • 03:20 (UTC +02:00)
View GitHub Profile
@Gummiees
Gummiees / Encryption.cs
Last active October 28, 2020 12:51
Secure encryption and password verification for C#
using System;
using System.Security.Cryptography;
public class Program
{
/**
* You can try this code at https://dotnetfiddle.net/ZkY1HV
*/
private const int SALT_LENGTH = 16;
@Gummiees
Gummiees / Codification.cs
Last active October 28, 2020 12:52
Encode and decode strings with C#
using System;
public class Program
{
/**
* You can this code at https://dotnetfiddle.net/uH4B69
*/
public static void Main()
{
string text = "Hello World!";
@Gummiees
Gummiees / randomColor.ts
Created October 29, 2020 09:30
Generate random color
public getRandomColor(): string {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
@Gummiees
Gummiees / downloadBlob.ts
Created October 29, 2020 09:31
Download Blob in TS
public downloadBlob(blob: Blob, documentName: string, extension: string)
{
const fileURL = URL.createObjectURL(blob);
const a: HTMLAnchorElement = document.createElement('a');
document.body.appendChild(a);
a.href = fileURL;
a.download = `${documentName}.${extension}`;
a.click();
}
@Gummiees
Gummiees / hexadecimalToRGBA.ts
Created October 29, 2020 09:35
Convert hexadecimal color to RGBA with transparency
/** Converts hexadecimal color to RGBA with transparency. */
public transparentize(color: string, opacity: number = 0.5): string {
return this.hexToRgbA(color, 1 - opacity);
}
/** Converts an hexadecimal color to RGBA */
public hexToRgbA(hex: string, opacity: number = 1): string {
let c: any;
if (/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)) {
c = hex.substring(1).split('');
@Gummiees
Gummiees / removeDateOffset.ts
Created October 29, 2020 09:37
Removes the date UTC offset with moment.js
/** Removes the current UTC offset. */
public removeUtcOffset(utcDate: any): Date {
let date: Date;
if (utcDate instanceof Date) {
date = utcDate;
} else {
date = moment(utcDate, this.localGlobalService.getDashDateMomentFormat()).toDate();
}
const noOffsetDate: Date = moment(date).utc().subtract(date.getTimezoneOffset(), 'm').toDate();
return noOffsetDate;
@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) => {
@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 / 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 / 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');
}