Skip to content

Instantly share code, notes, and snippets.

View GabrielModog's full-sized avatar
🤠

Gabriel Tavares GabrielModog

🤠
View GitHub Profile
@GabrielModog
GabrielModog / flatArray.js
Created June 9, 2021 03:01
method to flat up nested arrays
function flat(array){
return array.reduce((accumulator, current) => {
accumulator.push(...current)
return accumulator
}, [])
}
function Queue(){
const list = []
return {
add(item){
list.unshift(item)
return item
},
remove(){
return list.pop()
function map(array, transform){
let shaped = []
for(let element of array){
mapped.push(transform(element))
}
return shaped
}
@GabrielModog
GabrielModog / arrayToList.js
Created May 13, 2021 02:44
transform array into a list
function arrayToList(arr){
let list = {}
arr.forEach(item => {
const newValue = {
value: item,
rest: Object.entries(list).length ? null : list,
}
list = newValue
@GabrielModog
GabrielModog / formatColorAndSize.js
Created April 25, 2021 01:39
format color and size object when the info is an array of strings
function formatColorAndSize(list){
return list.reduce((accum, colorAndSize) => {
const [color, size] = colorAndSize.split('-')
accum[color] = accum[color] || {}
accum[color][size] = accum[color][size] || 0
accum[color][size] += 1
return accum
}, {})
}
@GabrielModog
GabrielModog / somedesignpatterntypescriptbasics.ts
Created March 13, 2021 05:11
somedesignpatterntypescriptbasics.ts
type Listener<EventType> = (evt: EventType) => void;
function createObserver<EventType>(): {
subscribe: (listener: Listener<EventType>) => () => void;
publish: (event: EventType) => void;
} {
let listeners: Listener<EventType>[] = [];
return {
subscribe: (listener: Listener<EventType>): (() => void) => {
const original =
`v=0
o=bulma bulma@capsule.corporation.com
s=
t=0
m=audio 123 rtp
a=128kbps
a=no-echo
a=filters
a=dragon-effect
function animalStrategy(name){
const types = {
dog: {
name: 'Dog',
race: 'Dog',
version: '1.3',
id: 'doggod',
noise: () => 'bark bark bark',
},
cat: {
@GabrielModog
GabrielModog / fixCurrencyToBRL__bugOn.js
Last active January 6, 2021 03:21
[incorret]fixes intl numbers to brl currency
const fixCurrencyToBRL = (num) => {
if (typeof num === "string") {
if (num.includes(",")){
const prepared = num.replace(/[.*]/g, '');
return Number(prepared.replace(/,/g, "."))
.toLocaleString("pt-br", { minimumFractionDigits: 2 });
}
const fixed = Number(
@GabrielModog
GabrielModog / fibonacci.js
Created December 17, 2020 07:22
recursive memoized fibonacci
const fibonacci = (num, memo = {}) => {
if(num in memo) return memo[num];
if(num <= 2) return 1;
memo[num] = fibonacci(num - 1, memo) + fibonacci(num - 2, memo)
return memo[num];
}