Skip to content

Instantly share code, notes, and snippets.

View AitorAlejandro's full-sized avatar

Aitor Alejandro Herrera AitorAlejandro

View GitHub Profile
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 / truncate.js
Last active May 30, 2023 12:08
Truncate strings
function truncate(str, length) {
return str.length > length
? `${str.substr(0, length)}...`
: str;
}
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);
}
}
@AitorAlejandro
AitorAlejandro / Identifiable_example.ts
Created June 5, 2022 20:25
Identifiable example TS
interface Identifiable<T> {
id: T;
}
class Product implements Identifiable<string> {
id: string;
constructor(id: string) {
this.id = id;
}
}
@AitorAlejandro
AitorAlejandro / closure.js
Created June 3, 2022 12:14 — forked from henrik1/closure.js
Closure example
function counter() {
let count = 0;
function increment() {
return count += 1;
};
return increment;
}
@AitorAlejandro
AitorAlejandro / utilsCollection.js
Last active March 25, 2022 22:06
Collecition of utils
// Check if an element is focused
const hasFocus = (ele) => ele === document.activeElement;
// Get all siblings of an element
const siblings = (ele) => [].slice.call(ele.parentNode.children).filter((child) => child !== ele);
// Get selected text
const getSelectedText = () => window.getSelection().toString();
// Go back to the previous page