Skip to content

Instantly share code, notes, and snippets.

View vpellegrino's full-sized avatar
👨‍🎤
License MIT - do anything with the code, but don't blame me if it does not work.

Valentino Pellegrino vpellegrino

👨‍🎤
License MIT - do anything with the code, but don't blame me if it does not work.
View GitHub Profile
@vpellegrino
vpellegrino / 1-CalendarItem.js
Created July 21, 2022 14:18 — forked from getify/1-CalendarItem.js
an illustration (non-trivial example) of many newer JS class features
// abstract class, not intended to be instantiated directly
class CalendarItem {
static #UNSET = Symbol("unset")
static #isUnset(v) {
return v === this.#UNSET;
}
static {
for (let [idx,msg] of [
"ID is already set.",
"ID is unset.",
@vpellegrino
vpellegrino / walkInCartesia.js
Created June 28, 2022 14:39
The city provides its citizens with a Walk Generating App on their phones -- every time you press the button it sends you an array of one-letter strings representing directions to walk (eg. ['n', 's', 'w', 'e']). You always walk only a single block for each letter (direction) and you know it takes you one minute to traverse one city block, so cr…
/**
* production code
*/
const backFromWalkInTime = (steps) => {
const stepsUp = steps.filter((step) => step === "n").length;
const stepsDown = steps.filter((step) => step === "s").length;
const stepsLeft = steps.filter((step) => step === "e").length;
const stepsRight = steps.filter((step) => step === "w").length;
@vpellegrino
vpellegrino / occurrences.js
Last active June 28, 2022 14:38
Task: Write some code that, giving an array of strings, returns the number of occurrences where at least a char is repeated.
/**
* production code
*/
const _countOccurrences = charMap => {
let occ = 0;
for (let char in charMap) {
if (charMap[char] >= 2) {
occ++;
}
@vpellegrino
vpellegrino / useContextMenuHandler.ts
Last active May 4, 2023 14:50
Collection of useful React Hooks to be used by a React component
import { RefObject, useCallback, useEffect, useRef, useState } from "react";
export function useContextMenuHandler(): {
contextMenuRef: RefObject<HTMLDivElement>;
contextMenuXPos: string;
contextMenuYPos: string;
contextMenuVisibility: boolean;
setContextMenuVisibility: (value: ((prevState: boolean) => boolean) | boolean) => void;
} {
const wrapperRef = useRef<HTMLDivElement>(null);
@vpellegrino
vpellegrino / flatten.js
Last active December 29, 2019 02:52
Task: write some code that will flatten an array of arbitrarily nested arrays of integers into a flat array of integers
/**
* production code
*/
const flatten = arr => safeArray(arr).reduce(flatReducer, []);
const safeArray = arr => Array.isArray(arr) ? arr : [];
const flatReducer = (res, elem) => {
if (Array.isArray(elem)) {