Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
Shet yer Shtatus

Jason Sebring sebringj

💭
Shet yer Shtatus
View GitHub Profile
@sebringj
sebringj / App.tsx
Last active Feb 5, 2022
DumbContextFactory: Enables easy React Context creation with optional browser storage or custom storage options
View App.tsx
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 Aug 14, 2021
Firebase Functions Middleware Approach
View README.md

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
This is a really simple way to do global state in react
View SimpleStore.ts
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 Apr 9, 2021
finds first repeated value and returns index
View findRepeatedIndex.js
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 Apr 9, 2021
My sliding window algorithm
View sliding-window.js
const contains = (str, t) => {
for (const c of t) {
if (!str.includes(c)) {
return false;
}
}
return true;
}
const findWindows = (size, s, t) => {
View versionToNumber.js
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 Jan 17, 2020
JSON to text in JavaScript
View textFromJson.js
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 Nov 9, 2017
React Native Fetch alternative with abort method attached to returned promise and timeout support
View retch.js
'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 Jul 21, 2017
flattens nested list of arrays of integers
View flattenArray.js
// 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])
@sebringj
sebringj / getKeyList.js
Last active Jun 21, 2017
JavaScript getKeyList
View getKeyList.js
/*
use like so:
let keyList = getKeyList("a", "b", "c")
console.log(keyList[0]) // "a"
console.log(keyList.index["a"]) // 0
*/
function getKeyList() {
let arr = [].slice.call(arguments);