Skip to content

Instantly share code, notes, and snippets.

View sebringj's full-sized avatar
💭
Shet yer Shtatus

Jason Sebring sebringj

💭
Shet yer Shtatus
View GitHub Profile
@sebringj
sebringj / GuidShortener.ts
Created April 19, 2024 20:52
GuidShortener
class GuidShortener {
public static guidToBase95(guid: string): string {
const bigInt = this.hexToBigInt(guid);
let base95String = this.bigIntToBase95(bigInt);
// Pad the result to ensure it's exactly 20 characters long
while (base95String.length < 20) {
base95String = ' ' + base95String; // Using space to pad for simplicity
}
@sebringj
sebringj / App.tsx
Last active February 5, 2022 18:22
DumbContextFactory: Enables easy React Context creation with optional browser storage or custom storage options
import { FC } from "react";
import { UserContextProvider, useUserContext } from "./UserContext";
const Component1: FC = () => {
const { user } = useUserContext();
return user.loaded ? (
<h1>
Hello {user.firstName} {user.lastName}
</h1>
@sebringj
sebringj / README.md
Last active August 14, 2021 18:38
Firebase Functions Middleware Approach

Firebase Functions Middleware Approach

Initially, tried using the express-cors way but ended up causing the firebase functions https to be overwritten for some reason or another and had to rebuild everything and not do that. Instead, tried my own middleware that mimics how express works in how you call it.

I'm not trying to reuse existing middleware from express at this time but might be worth it at some point.

Use like so:

import {onRequest} from './middleware';
import {cors} from './cors';
@sebringj
sebringj / SimpleStore.ts
Last active May 8, 2021 00:13
This is a really simple way to do global state in react
import EventEmitter from 'eventemitter3'
export enum SimpleStoreEvents {
change = 'change'
}
export class SimpleStore<T> {
constructor(storeName: string, state: T) {
this._state = state
this._storeName = storeName
@sebringj
sebringj / findRepeatedIndex.js
Last active April 9, 2021 18:32
finds first repeated value and returns index
const findRepeatedIndex = arr => [...arr].findIndex(function(item) {
this.lookup = this.lookup || {};
return this.lookup[item] && 1 || (this.lookup[item] = 1) && 0;
});
@sebringj
sebringj / sliding-window.js
Last active April 9, 2021 19:10
My sliding window algorithm
const contains = (str, t) => {
for (const c of t) {
if (!str.includes(c)) {
return false;
}
}
return true;
}
const findWindows = (size, s, t) => {
function versionToNumber(version) {
return version
.split('.')
.reverse()
.map((v, i) => parseInt(v, 10) * Math.pow(1000, i))
.reduce((a, v) => a + v);
}
@sebringj
sebringj / textFromJson.js
Last active January 17, 2020 21:10
JSON to text in JavaScript
function textFromJson(json) {
if (json === null || json === undefined) {
return '';
}
if (!Array.isArray(json) && !Object.getPrototypeOf(json).isPrototypeOf(Object)) {
return '' + json;
}
const obj = {};
for (const key of Object.keys(json)) {
obj[key] = textFromJson(json[key]);
@sebringj
sebringj / retch.js
Created November 9, 2017 15:27
React Native Fetch alternative with abort method attached to returned promise and timeout support
'use strict';
var self = this || global;
// Polyfill from https://github.com/github/fetch/blob/v1.1.1/fetch.js#L8-L21
var support = {
searchParams: 'URLSearchParams' in self,
iterable: 'Symbol' in self && 'iterator' in Symbol,
blob: 'FileReader' in self && 'Blob' in self && (function() {
try {
@sebringj
sebringj / flattenArray.js
Last active July 21, 2017 15:46
flattens nested list of arrays of integers
// VIEW ON JSFIDDLE -> https://jsfiddle.net/v5scgqao/3/
/*
flattens array without mutation
*/
function flattenArray(arr) {
let arr2 = []
function flatten(a) {
for (let i = 0; i < a.length; i++) {
if (Array.isArray(a[i]))
flatten(a[i])