Skip to content

Instantly share code, notes, and snippets.

View barcellos-pedro's full-sized avatar
🛠️
Working to become

Pedro Reis barcellos-pedro

🛠️
Working to become
View GitHub Profile

Hi there, I'm Pedro, and welcome! ✌🏽

👨‍💻 I'm a Software Engineer.

  • 🙂 Pronouns: He/him/his.
  • ☕ I enjoy working with Web technologies.
  • 💙 Consistently learning about programming concepts and techniques.
  • 📫 How to reach me: LinkedIn
  • 📑 Check-out some snippets: Gist
@barcellos-pedro
barcellos-pedro / clone.sh
Created July 19, 2023 02:00
Clone github repos based on txt file
#!/usr/bin/env bash
echo -e "\n[Cloning repos...]"
user=$1
count=0
while read repo
do
echo "Cloning $repo..."
@barcellos-pedro
barcellos-pedro / tagged-templates.js
Last active June 20, 2023 22:04
Template literals - Tagged templates
// MDN Docs
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
/**
* @param {string[]} value as array
* @param {any} val1
* @param {any} val2
*/
function templateExample(string, val1, val2) {
console.log('string\n', string);
@barcellos-pedro
barcellos-pedro / app.js
Created June 10, 2023 03:39
Server sent events (SSE)
const express = require("express");
const app = express();
app.use("/static", express.static("static"));
app.get("/", (req, res) => {
res.sendFile("views/index.html", { root: __dirname });
});
app.get("/events", async (req, res) => {
const { Worker, isMainThread } = require("node:worker_threads");
const worker = new Worker("./report.js", {
workerData: { message: "hello from app.js" },
});
console.log("Running worker: ", worker.threadId);
console.log("is main thread (app.js) => ", isMainThread);
worker.on("error", (err) =>
@barcellos-pedro
barcellos-pedro / app.js
Created May 1, 2023 01:34
Async generators example with ArrayBuffer
import { appendFile } from "node:fs/promises";
const url = "https://jsonplaceholder.typicode.com/posts";
let times = 5;
async function* getData() {
while (times > 0) {
times--;
const response = await fetch(url);
yield await response.arrayBuffer();
@barcellos-pedro
barcellos-pedro / app.js
Created April 26, 2023 17:03
JSON to CSV
const { writeFile, readfile } = require("node:fs/promises");
const isObject = (value) =› typeof value = "object";
const isArray = (value) = Boolean (value?. Length) ;
const fileName = "file.json"
(async () = {
const file = await readFile(fileName, { encoding: "utf-8"});
const data = JSON.parse (file);
let result = “”;
@barcellos-pedro
barcellos-pedro / array.js
Last active March 31, 2023 21:35
Array implementation in JS
class List {
constructor(size) {
this.buffer = new ArrayBuffer(size);
this.array = new Uint8Array(this.buffer);
}
push(value) {
this.array[this.array.length - 1] = value;
}
@barcellos-pedro
barcellos-pedro / range.js
Created February 5, 2023 16:09
JS range example
// standard function to loop through our range
function range(start = 0, end = 10, increment = 1) {
for (let i = start; i <= end; i += increment) {
console.log(i)
}
}
range() // 0, 1, 2...
range(2, 6, 2) // 2, 4, 6
@barcellos-pedro
barcellos-pedro / server.mjs
Last active January 21, 2023 21:56
Nodejs basic server
import { createServer } from "http";
const PORT = 3000;
createServer((request, response) => {
if (request.method !== "POST" || request.url !== "/stream") {
response.status = 404;
response.end("method or url not found");
return;
}