Skip to content

Instantly share code, notes, and snippets.

View vinicius5581's full-sized avatar
:octocat:
Chilling

Vinicius Santana vinicius5581

:octocat:
Chilling
View GitHub Profile
console.log("🥪🍅🍍🍑🍓😛😭😬💩");
const debounce = (fn, time) => {
let timerId = null
return (...args) => {
if (timerId) {
clearTimeout(timeId)
}
timeId = setTimeout(() => {
fn(...args)
}, time)
}
// Class
class Emitter {
constructor (events = []) {
this.events = new Map(events)
}
on (name, cb) {
this.events.set(name, [...(this.events.get(name) || []), cb])
return () => this.events.set(name, this.events.get(name).filter(fn => fn !== cb))
class Thing {
constructor(isLiving){
this.isLiving = isLiving;
}
doSomething() {
console.log('doing some')
}
}
const p1 = {
name: 'p1',
sayHi: function(){console.log('hi')}
}
function Person(name) {
this.name = name;
this.sayHi = function(){console.log('hi')};
}
@vinicius5581
vinicius5581 / arrays.js
Last active October 19, 2019 20:18
Array basics
const frutas = ['Maçã', 'Banana'];
const fruits = ["Maçã", "Manga", "Uva", "Laranja", "Maçã", "Manga", "Banana", "Banana"]
console.log('adciona no comeco - unshift')
console.log(frutas.unshift('Laranja'))
console.log(frutas)
console.log('remove no comeco - shift')
console.log(frutas.shift())
console.log(frutas)
@vinicius5581
vinicius5581 / getNeighboors.js
Created December 17, 2019 08:35
Find matrix point neighbors
const getNeighboors = (matrix, row,col) => {
let isTopBorder = row === 0;
let isRightBorder = col === matrix[row].length - 1;
let isBottomBorder = row === matrix.length - 1;
let isLeftBorder = col === 0;
let top = isTopBorder ? row : row - 1;
let left = isLeftBorder ? col : col - 1;
let bottom = isBottomBorder ? row : row + 1;
@vinicius5581
vinicius5581 / reverseString.js
Last active January 9, 2020 18:09
Reverser string with a reverse for loop
function reverseString(str) {
// Step 1. Create an empty string that will host the new created string
var newString = "";
// Step 2. Create the FOR loop
/* The starting point of the loop will be (str.length - 1) which corresponds to the
last character of the string, "o"
As long as i is greater than or equals 0, the loop will go on
We decrement i after each iteration */
for (var i = str.length - 1; i >= 0; i--) {
@vinicius5581
vinicius5581 / custom-with-callback.js
Last active April 13, 2020 04:01
Array.prototype
const numbersArray = [1, 2, 3, 4];
const logTimesTwo = num => console.log(num * 2);
const customForEach = (arr, cb) => {
for (let i = 0; i < arr.length; i++) {
cb(arr[i]);
}
}
@vinicius5581
vinicius5581 / concessionaria.sql
Created April 26, 2020 03:23
Banco de dados simplificado de uma concessionaria de carros fictícia com fins únicos de servir de exemplo para o blog post sobre usando SQL com MySQL.
DROP DATABASE IF EXISTS `concessionaria`;
CREATE DATABASE `concessionaria` ;
USE `concessionaria`;
CREATE TABLE `carros`(
`id_do_carro` int(11) NOT NULL AUTO_INCREMENT,
`nome` varchar(50) NOT NULL,
`cor` varchar(50) NOT NULL,
`quantidade_em_estoque` int(11) NOT NULL,
`preco` decimal(8,2) NOT NULL,