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 December 18, 2020 16:20
Using Custom CLI Parameters in Node

Using Custom CLI Parameters in Node

In the following TypeScript example, we're expecting two possible CLI parameters, --build and --port. We prepare a paramMap variable that has default values for all parameters, which are overwritten for each parameter that is manually passed in via the CLI.

See further down for a TypeScript example without comments as well as a JavaScript example.

// create an enum that contains each of the params
enum Params {
  BUILD = '--build',
@rpivo
rpivo / index.md
Last active December 24, 2020 04:32
JSON & Serialization

JSON & Serialization

Looking at the JSON documentation, JSON doesn't support a lot of JavaScript values for the reason that there's no way to translate these values to other languages, and therefore the values are not serializable.

Not being serializable means that a value can't be converted into a string and then later parsed from the string with 100% certainty of what the value should represent in the new context with which the value is being parsed.

For example, if a JavaScript function should be serialized within a JSON string, and if this JSON string is passed into a Python context, Python won't have any idea what to do with this function since it uses JavaScript language features and syntax that aren't available in Python.

Below is an object that we're stringifying and then parsing with JSON. We can get a sense of what is and isn't serializable in JavaScript this way.

@rpivo
rpivo / index.md
Last active December 23, 2020 16:38
BigInt in JavaScript

BigInt in JavaScript

BigInt is a relatively new JavaScript data type introduced in 2020.

JavaScript's original Number primitive can only represent numbers up to 253 - 1.

We can log this maximum value with Number.MAX_SAFE_INTEGER:

console.log(Number.MAX_SAFE_INTEGER)
// 9007199254740991
@rpivo
rpivo / index.md
Last active December 28, 2020 01:39
Point-Free Style in JavaScript

Point-Free Style in JavaScript

Point-free style is a really cool mechanic that can help establish good practices in regards to functional programming.

const arr = [1,2,3]

arr.forEach(console.log)
@rpivo
rpivo / index.md
Last active January 8, 2021 17:02
Killing a Process on a Specific Port on Windows

Killing a Process on a Specific Port on Windows

This gist is based on this Stack Overflow answer.

For the below commands, imagine we're running a process on port 3000, and we want to kill it.

To kill a process on a specific port on Windows, first run:

netstat -ano | findstr :3000
@rpivo
rpivo / index.md
Last active January 8, 2021 17:27
Installing Pip on Mac From the Terminal
@rpivo
rpivo / index.md
Created January 9, 2021 12:51
Checking if a Substring Exists in a String in Python

Checking if a Substring Exists in a String in Python

allowed = 'abc'
word = 'cat'
for char in word:
  if char in allowed: print(char) # prints 'c', 'a',
  if char not in allowed: print(char) # prints 't'
@rpivo
rpivo / index.md
Last active January 10, 2021 14:09
Using Map in Python

Using Map in Python

Python's map function works similarly to JavaScript's map. To use it, you must supply as a first argument a callback or lambda function that will perform work on each item in a given iterable. For its second argument, you pass in the iterable itself.

def increment(n: int) -> int:
  return n + 1

l = [1,2,3]
@rpivo
rpivo / index.md
Last active January 12, 2021 15:12
Python String Constants

Python String Constants

You can import string constants from string.

  • ascii_lowercase ‘abcdefghijklmnopqrstuvwxyz’
  • ascii_uppercase ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’
  • ascii_letters ‘ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz’
  • digits ‘0123456789’
  • hexdigits ‘0123456789abcdefABCDEF’
  • octdigits ‘01234567’