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 / init.vim
Created March 23, 2022 11:35
config nvim
call plug#begin('~/.config/nvim/vim-plug')
" 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 / 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(() => {
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 / 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 = {};
@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 / 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 / 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 / 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 / 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 / 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)
}