Skip to content

Instantly share code, notes, and snippets.

View nelsonfncosta's full-sized avatar
:shipit:
burnit

Nélson Costa nelsonfncosta

:shipit:
burnit
View GitHub Profile

Keybase proof

I hereby claim:

  • I am nelsonfncosta on github.
  • I am nelson_costa (https://keybase.io/nelson_costa) on keybase.
  • I have a public key ASCr9rX-RNCh2Pw_V_vXunwBuOFpm-mdxcBqihAB0eznwQo

To claim this, I am signing this object:

@nelsonfncosta
nelsonfncosta / example.js
Created October 22, 2018 09:41
JS private variable
function Container(param) {
let secret = 3;
let that = this;
this.member = param;
function dec() {
if (secret > 0) {
secret -= 1;
return true;
} else {
@nelsonfncosta
nelsonfncosta / example.js
Created October 22, 2018 09:43
JS private variable
function Container(param) {
let secret = 3;
let that = this;
this.member = param;
function dec() {
if (secret > 0) {
secret -= 1;
return true;
} else {
@nelsonfncosta
nelsonfncosta / prepare-commit-msg
Created February 8, 2019 16:00
Commit prepare message
#!/bin/sh
#
# Automatically adds issue number (optional) and ticket number based on branch name or
# description to every commit message.
#
# Copy this file to .git/hooks under your local repository directory
# PS: If you're on linux you may need to change "sed -En" to "sed -rn"
#
# Example:
#
@nelsonfncosta
nelsonfncosta / numDuplicates.js
Created October 26, 2020 10:11
numDuplicates example
function logItem() {
console.log(Array.from(arguments).join("\t"))
}
function numDuplicates(name, price, weight) {
const uniqueItems = new Set()
name.forEach(
(item, index) => {
const itemPrice = price[index]
@nelsonfncosta
nelsonfncosta / read-file.js
Created March 23, 2021 19:06
READ FILE snippet
function readFile(file: File | null) {
const reader = new FileReader();
reader.addEventListener("load", (event) => {
const result = event.target.result;
console.log(result);
});
reader.addEventListener("progress", (event) => {
@nelsonfncosta
nelsonfncosta / closures-ex.js
Created April 20, 2021 16:20
Example of how closures can help with memory management
function findCustomerCity(name) {
const texasCustomers = ['John', 'Ludwig', 'Kate'];
const californiaCustomers = ['Wade', 'Lucie','Kylie'];
return texasCustomers.includes(name) ? 'Texas' :
californiaCustomers.includes(name) ? 'California' : 'Unknown';
};
// For every call, memory is unnecessarily re-allocated to the variables texasCustometrs and californiaCustomers .
@nelsonfncosta
nelsonfncosta / short-switch-ex.js
Created April 21, 2021 21:54
Shorthand example for switch statement
// Longhand
switch (data) {
case 'a':
test1();
break;
case 'b':
test2();
break;
@nelsonfncosta
nelsonfncosta / stream-to-string-ex.js
Created April 22, 2021 22:11
Example of converting a stream to a string
function streamToString (stream) {
const chunks = []
return new Promise((resolve, reject) => {
stream.on('data', (chunk) => chunks.push(Buffer.from(chunk)))
stream.on('error', (err) => reject(err))
stream.on('end', () => resolve(Buffer.concat(chunks).toString('utf8')))
})
}
@nelsonfncosta
nelsonfncosta / array-buffer-to-string-ex.js
Last active April 25, 2021 15:24
ArrayBuffer to String example
function arrayBufferToString (buffer, encoding = 'UTF-8', callback = console.log) {
const blob = new Blob([buffer], { type: 'text/plain' })
const reader = new FileReader()
reader.onload = evt => callback(evt.target.result)
reader.readAsText(blob, encoding)
}