Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
Shet yer Shtatus

Jason Sebring sebringj

💭
Shet yer Shtatus
View GitHub Profile
@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);
@sebringj
sebringj / LookupList.js
Last active Jun 21, 2017
JavaScript LookupList
View LookupList.js
/*
use like so:
const lookupList = new LookupList({ a: 1 }, { b: 2 }, { c: 3 })
console.log(lookupList.valueByKey('a')) // 1
console.log(lookupList.keyByValue(1)) // "a"
console.log(lookupList.keysByValue(1)) // ["a"]
console.log(lookupList.valueByIndex(0)) // 1
console.log(lookupList.keyByIndex(0)) // "a"
@sebringj
sebringj / promiseMapAll.js
Last active May 3, 2017
maps Promise.all
View promiseMapAll.js
async function promiseMapAll(promiseMap) {
const keys = Object.keys(promiseMap);
const promises = keys.map(key => promiseMap[key]);
const promisesArr = await Promise.all(promises);
let objMapped = {};
keys.forEach((key, i) => {
objMapped[key] = promisesArr[i];
});
return objMapped;
}