Skip to content

Instantly share code, notes, and snippets.

View rpivo's full-sized avatar

Ryan Pivovar rpivo

  • New York City, NY
View GitHub Profile
@rpivo
rpivo / index.md
Last active April 29, 2021 14:46
Creating a JSON Walker Class

Creating a JSON Walker Class

class JsonWalker {
  constructor(content) {
    this.content = content;
  }

  get firstChild() {
 return new JsonWalker(this.content.children[0]);
@rpivo
rpivo / index.md
Last active May 20, 2021 22:31
Implementing a Hot-Reloading Server

Implementing a Hot-Reloading Server

This native Node hot-reloading server implementation uses the EventSource Web API to unidirectionally send pings from the server to the client whenever the client should refresh the page. The server will send a ping whenever the source code is rebuilt.

On starting the server, the browser will automatically open at http://localhost:8000.


First, you can add a serve script to package.json. This will run Server.js inside a util folder.

@rpivo
rpivo / index.md
Created May 20, 2021 22:55
Adding a Clean Script in Node

Adding a Clean Script in Node

import fs from "fs";
import path from "path";

const cwd = process.cwd();

const assetsFolder = "assets";
const distFolder = "dist";
@rpivo
rpivo / index.md
Last active May 22, 2021 18:01
Traverse All Subsets in an Array

Traverse All Subsets in an Array

In the below example, we create a function that's responsible for looping through an array. At each traversal, if we've reached the end of the array, we return x. Otherwise, we call the function again with i incremented, and with x added with the value of the array at index i.

function sum(arr, i = 0, x = 0) {
  return i === arr.length
    ? x
    : sum(arr, i + 1, x + arr[i]);
}
@rpivo
rpivo / index.md
Last active May 25, 2021 12:12
Getting the Number of Subsets Within a Set

Getting the Number of Subsets Within a Set

The number of subsets within a set will always be 2 ^ N, where N is the length of the set.

const a = [1, 2, 3];

console.log(2 ** 3); // 8

Below is the **power set** of the set `a`, or a representation of all the subsets with the set `a`.
@rpivo
rpivo / index.md
Last active June 15, 2021 12:26
Create a MySQL Table Only if It Doesn't Already Exist

Create a MySQL Table Only if It Doesn't Already Exist

The following statement creates a MySQL table Products only if it doesn't yet exist. If the table does already exist, then the statement will do nothing.

CREATE TABLE IF NOT EXISTS Products (
  product_id int,
  low_fats ENUM('Y', 'N'),
  recyclable ENUM('Y', 'N')
);
@rpivo
rpivo / index.md
Last active June 15, 2021 12:34
Declaring Enum Types in MySQL

Declaring Enum Types in MySQL

The following statement creates a table Products that contains three columns: product_id, low_fats, and recyclable.

While product_id is of type int, both low_fats and recyclable are enums that can only be one of the declared values within the ENUM() clause ('Y' or 'N', in this case).

CREATE TABLE Products (
  product_id int,
 low_fats ENUM('Y', 'N'),
@rpivo
rpivo / index.md
Last active June 15, 2021 12:45
Completely Emptying a MySQL Table With TRUNCATE TABLE

Completely Emptying a MySQL Table With TRUNCATE TABLE

The following statement will create a new table Products and insert values into it.

CREATE TABLE IF NOT EXISTS Products (
  product_id int,
  low_fats ENUM('Y', 'N'),
  recyclable ENUM('Y', 'N')
);
@rpivo
rpivo / index.md
Last active June 17, 2021 12:02
Using Order By to Sort MySQL Table Results by Ascending or Descending Order

Using Order By to Sort MySQL Table Results by Ascending or Descending Order

The following statement will create a new table Products and insert values into it.

CREATE TABLE IF NOT EXISTS Products (
  product_id int,
  low_fats ENUM('Y', 'N'),
  recyclable ENUM('Y', 'N')
);
@rpivo
rpivo / index.md
Last active June 19, 2021 13:33
Checking the Length of a String in a MySQL Where Clause

Checking the Length of a String in a MySQL Where Clause

Let's say we have a SQL schema like this:

Create table If Not Exists Tweets(tweet_id int, content varchar(50));
Truncate table Tweets;
insert into Tweets (tweet_id, content) values ('1', 'Vote for Biden');
insert into Tweets (tweet_id, content) values ('2', 'Let us make America great again!');