Skip to content

Instantly share code, notes, and snippets.

View AitorAlejandro's full-sized avatar

Aitor Alejandro Herrera AitorAlejandro

View GitHub Profile
@AitorAlejandro
AitorAlejandro / formatDate.js
Last active May 30, 2023 12:40
Format date
function formatDate(date = new Date(), format = "yyyy/mm/dd") {
const map = {
mm: date.getMonth() + 1,
dd: date.getDate(),
yyyy: date.getFullYear()
}
return format.replace(/mm|dd|yyyy/gi, matched => {
return map[matched].toString().padStart(2, '0')
})
@AitorAlejandro
AitorAlejandro / truncate.js
Last active May 30, 2023 12:08
Truncate strings
function truncate(str, length) {
return str.length > length
? `${str.substr(0, length)}...`
: str;
}
import { AfterViewInit, Directive, ElementRef, Input, Renderer2 } from '@angular/core';
@Directive({
selector: '[appReadMore]'
})
export class ReadMoreDirective implements AfterViewInit {
@Input() limit : number = 50; //to get the custom text limit
@Input() toggle : boolean = false; //true: 'Read Less' button will be present
txtSpan! : HTMLSpanElement;

Install Angular ESLint

ng add @angular-eslint/schematics

Install Prettier and Prettier-ESLint dependencies

npm i prettier prettier-eslint eslint-config-prettier eslint-plugin-prettier -D

ESLint configuration

Filename: .eslintrc.json

@AitorAlejandro
AitorAlejandro / mensaje-hablado.js
Last active September 7, 2022 12:40
Uso básico total de SpeechSynthesis
const synth = speechSynthesis;
const msg = new SpeechSynthesisUtterance();
msg.text = 'Hola amo';
// o directamente new SpeechSynthesisUtterance('Hola amo');
msg.lang = 'es-ES';
synth.speak(msg);
@AitorAlejandro
AitorAlejandro / queryStringToJSON.js
Created June 30, 2016 09:54
Convertir los parámetros de la query string en un objeto javascript
function queryStringToJSON(qs) {
var pares = qs.slice(1).split('&');
var resultado = {};
pares.forEach(function(par) {
par = par.split('=');
resultado[par[0]] = decodeURIComponent(par[1] || '');
});
return JSON.parse(JSON.stringify(resultado));
type ServiceConfigParams = "port" | "basePath" | "enableStripePayments";
const serviceConfigChecked: Record<ServiceConfigParams, string | number | boolean> = {
port: 3000,
basePath: "http://localhost",
enableStripePayments: false,
};
type Point2d = [x: number, y: number];
type Point3d = [x: number, y: number, z: number];
const point: Point2d = [1, 2]; // the IDE will help you showing which is x and y
@AitorAlejandro
AitorAlejandro / degToRad.ts
Created June 5, 2022 20:52
Degrees to Radius converter
const degToRad = (degree): number => (degree * Math.PI) / 180;
@AitorAlejandro
AitorAlejandro / computeFrequency.ts
Created June 5, 2022 20:51
Compute how is the frequency of each char in a string. Returns a Map.
function computeFrequency(input: string): Map<string, number> {
const freqTable = new Map();
for (let ch of input) {
if (!freqTable.has(ch)) {
freqTable.set(ch, 1);
} else {
freqTable.set(ch, freqTable.get(ch) + 1);
}
}