Skip to content

Instantly share code, notes, and snippets.

View jcampuza's full-sized avatar
🎯
This is new

Joseph Campuzano jcampuza

🎯
This is new
View GitHub Profile
@jcampuza
jcampuza / useGlobalState.ts
Last active November 29, 2021 17:55
Simple function that returns a global state hook.
import { useEffect, useState } from 'preact/hooks';
type StateSetter<T> = Partial<T> | ((currentState: T) => T);
export const createGlobalState = <T>(init: T) => {
const store = {
state: init,
get: (): Readonly<T> => store.state,
@jcampuza
jcampuza / csvToJson.js
Created April 19, 2021 22:37
Convert a simple CSV to JSON (No commas inserted in strings)
const csvToJson = (input) => {
const lines = input.split('\n');
const keys = lines[0].split(',');
const dataset = lines.slice(1);
const res = [];
for (const data of dataset) {
console.log(data, keys);
@jcampuza
jcampuza / first-person-controls.ts
Created April 10, 2021 18:03
FirstPersonControls for three.js
import { Camera, Euler } from 'three';
export class FirstPersonControls {
constructor(private camera: Camera, private domElement: HTMLElement) {
this.connect();
}
state = {
mousedown: false,
enabled: false,
@jcampuza
jcampuza / combineLatestObj.ts
Created April 5, 2021 13:49
Combine Latest that functions with a keyed object
const combineLatestObj = <T>(
obj: T,
): Observable<{ [K in keyof T]: ObservedValueOf<T[K]> }> => {
const keys = Object.keys(obj);
const values = Object.values(obj);
return combineLatest(values).pipe(
map(
(observedValues) =>
keys.reduce((result, key, i) => {
@jcampuza
jcampuza / observable-store.ts
Last active March 24, 2021 01:25
Simple observable store class to emulate flux-like state slice with rxjs.
import { BehaviorSubject } from 'rxjs';
import { distinctUntilChanged, map } from 'rxjs/operators';
import produce from 'immer';
type StateSetter<T> = Partial<T> | ((s: T) => T | void);
/**
* Simple abstraction to create an immutable like rxjs store.
*/
export abstract class ObservableStore<T = unknown> {