Skip to content

Instantly share code, notes, and snippets.

@brandonpapworth
brandonpapworth / counter1.ts
Created February 1, 2023 21:30
Make a function that returns a number, starting with 1, increasing that number by 1 with each call of the function
// just using the module as the closure
let count = 1;
export function counter() {
return count++;
}
@brandonpapworth
brandonpapworth / README.md
Last active January 31, 2022 09:03
[WIP] TypeScript: Creating "Enums" from Tuple of String Literals

Creating Enums from Tuple of String Literals

Description

It is as it sounds: this function will create something similar to an enum (in this case, an object with a null prototype) from a tuple of string literals.

Why?

Even though native enums have some convenient benefits, they can get a little "wordy" when you wish them to work like holders of string constants.

@brandonpapworth
brandonpapworth / README.md
Last active March 8, 2021 14:20
Simple "what's-my-ip" service (think ipify) via Lambda@Edge

Simple "what's-my-ip" service

via Lambda@Edge

Usage

This will return the IPv4 or IPv6 address of the client making the GET request to the domain you specified when making the CloudFront distribution. If you add an Accept header with application/json present, it will return an json-parseable object like { "ip": "127.0.0.1" }, but if not specified or text/plain appears before application/json, it will default to text/plain and just return the IP address like 127.0.0.1 (following standard handling of Accept headers).

Shitty Directions

Keybase proof

I hereby claim:

  • I am brandonpapworth on github.
  • I am brandonpapworth (https://keybase.io/brandonpapworth) on keybase.
  • I have a public key ASAM9NUHKV50RBstYCEF7Tk2MKzG-8kqshOVgd0LqjN2mQo

To claim this, I am signing this object:

var _toString = Object.prototype.toString;
var _getPrototypeOf = Object.getPrototypeOf;
function isPlainObject(value) {
switch(value) {
default:
if (_toString.call(value) === '[object Object]') {
break;
}
case null:
const complaints = Complaints.find({status: 'pending'});
// An object with keys of <Complaint.objectId> and values of {complaint<Complaint>, count<Number>}
// Will dedupe complaints as well as track the number of times they appear.
const complaintStore = complaints.reduce((complaintStore, complaint) => {
if (complaintStore[complaint.objectId]) {
// Already encountered this complaint, so just increment the counter
complaintStore[complaint.objectId].count += 1;
} else {
// First time seeing this complaint, so initialize the container and store it
@brandonpapworth
brandonpapworth / README.md
Created June 3, 2020 13:57
Project: Temp Management System for AC Unit
@brandonpapworth
brandonpapworth / README.md
Last active February 13, 2022 20:55
pycharm optimizations
@brandonpapworth
brandonpapworth / action-type.class.js
Created April 4, 2018 11:13
Using class-based action types instead of strings in Reduxsauce
'use strict';
const __ACTION_TYPE_NAME__ = Symbol('ActionType[__ACTION_TYPE_NAME__]');
const actionTypeSingletonStore = new Map();
export class ActionType {
constructor(actionTypeName, ignoreActionTypeSingletonStore = false) {
if (ignoreActionTypeSingletonStore === false) {
@brandonpapworth
brandonpapworth / introspection-query-2.graphql
Created February 7, 2018 22:31
introspection-query-2.graphql
query IntrospectionQuery {
__schema {
queryType { name }
mutationType { name }
subscriptionType { name }
types {
...FullType
}
directives {
name