Skip to content

Instantly share code, notes, and snippets.

View Alonso-Pablo's full-sized avatar
Writing code ...

Alonso Pablo Alonso-Pablo

Writing code ...
View GitHub Profile
@Alonso-Pablo
Alonso-Pablo / clean_code.md
Last active April 28, 2022 12:39 — forked from wojteklu/clean_code.md
Summary of 'Clean code' by Robert C. Martin

El código esta limpio si puede ser entendido fácilmente por todos en el equipo. Un código limpio puede ser leído y mejorado por un desarrollador que no sea su autor original. Con la comprensibilidad viene la legibilidad, la capacidad de cambio, la extensibilidad y la facilidad de mantenimiento.


Reglas generales

  1. Siga las convenciones estándar.
  2. Mantenlo simple y estúpido. Lo más simple siempre es mejor. Reduzca la complejidad tanto como sea posible.
  3. Regla de los boy scouts. Deje el campamento más limpio de lo que lo encontró.
  4. Siempre encuentre la causa raíz. Busque siempre la causa raíz de un problema.

Reglas de diseño

@Alonso-Pablo
Alonso-Pablo / addNewMethodInPrototype.js
Last active October 18, 2021 02:47
Add a method inside the prototype of Number or String
const aNumber = 13254
const aString = 'aa'
// We add a method in the prototype of Number and String
Number.prototype.sort = function() {
return Number(this.toString().split('').sort((a,b)=>a-b).join(''))
}
String.prototype.capitalize = function() {
return this.charAt(0).toUpperCase() + this.slice(1)
}
@Alonso-Pablo
Alonso-Pablo / init.vim
Last active October 19, 2021 19:56
vim initialization
call plug#begin('~/AppData/Local/nvim/plugins')
" Fold where saves plugins data
" syntax
Plug 'sheerun/vim-polyglot'
Plug 'styled-components/vim-styled-components', { 'branch': 'main' }
Plug 'norcalli/nvim-colorizer.lua'
" status bar
Plug 'maximbaz/lightline-ale'
@Alonso-Pablo
Alonso-Pablo / sortBy.js
Created October 21, 2021 05:15
Sort an object[] by keys
function sortBy(array, keys) {
let i = 0
return [...array].sort((a,b) => compareBy(a, b, keys, i))
}
function compareBy(a, b, keys, i) {
if (a[keys[i]] > b[keys[i]]) return 1
if (a[keys[i]] < b[keys[i]]) return -1
i++
if (!keys[i]) return 0
@Alonso-Pablo
Alonso-Pablo / for-in-for-of.js
Last active December 31, 2021 16:45
Diff. between 'For of' and 'For in'
const obj = { a: 1, b: 2, c: 3 };
const array = ['a', 'b', 'c']
const str = 'abc'
// FOR OF OBJECT
for (const property of obj) {
console.log(property);
}
// Output:
@Alonso-Pablo
Alonso-Pablo / hastTable.js
Last active December 31, 2021 16:37
Saves the characters that are repeated within the array in a unique index in a dictionary. If the character was repeated more than the times indicated by the second parameter of the function, it is deleted from the result.
function deleteNth(arr,n){
const result = []
const dict = {}
for (const num of arr) {
if (num in dict) {
if (dict[num] < n) {
result.push(num)
}
dict[num]++
} else {
@Alonso-Pablo
Alonso-Pablo / isObjEmpty.js
Created December 5, 2021 16:14
Check if the object is empty or not
// Objects:
const emptyObj = {}
const noEmptyObj = { foo: 'bar'}
// Option 1:
function isObjEmpty(obj) {
return (Object.keys(obj).length)
? 'No empty'
: 'Empty'
@Alonso-Pablo
Alonso-Pablo / deepCopy.js
Created February 23, 2022 23:13
Clone a object in JavaScript
function deepCopy(subject) {
let copySubject;
const subjectIsObject = isObject(subject);
const subjectIsArray = isArray(subject);
if (subjectIsArray) {
copySubject = [];
} else if (subjectIsObject) {
copySubject = {};
const httpStatus = {
// 1xx Informational
100: "Continue",
101: "Switching Protocols",
102: "Processing",
103: "Early Hints",
// 2xx Success
200: "Ok",
201: "Created",
202: "Accepted",
@Alonso-Pablo
Alonso-Pablo / useAsync.ts
Created March 12, 2022 21:49
Avoids errors if the request cannot be resolved because the React component was disassembled.
import { useEffect } from 'react'
import { AxiosResponse } from 'axios'
export const useAsync = (
asyncFn: () => Promise<AxiosResponse<any, any>>,
successFunction: Function,
returnFunction: Function,
dependencies: any[] = []
) => {
useEffect(() => {