Skip to content

Instantly share code, notes, and snippets.

View deepCopy.js
const copy = item => {
return !item ? item
: Array.isArray(item) ? deepArrayCopy(item)
: Object.prototype.toString.call(item).includes('Date') ? new Date(item.getTime())
: typeof item === 'object' ? deepCopy(item)
: item
}
const deepCopy = obj => {
const objKeys = Object.keys(obj)
@jpenney1
jpenney1 / pureTrampoline.js
Last active Mar 29, 2019
Custom efficient tail end optimization for recursive functions in JavaScript utilizing the trampoline pattern
View pureTrampoline.js
const trampoline = fn => (...args) => {
let res = fn(...args)
while(typeof res === 'function'){
res = res()
}
return res
}
/*
@jpenney1
jpenney1 / pureBounce.js
Created Mar 29, 2019
Run this code in an html script with an index element in the body, type anything into the input and see an alert after two seconds of inactivity
View pureBounce.js
const debounce = (callback, wait) => {
let timeout = null
return (...args) => {
const next = () => callback(...args)
clearTimeout(timeout)
timeout = setTimeout(next, wait)
}
}
const inputEl = document.getElementsByTagName('input')[0]
View Generate React Component
var ARGS = process.argv.slice(2),
COMPONENT_NAME = ARGS[0],
COMPONENT_IS_SYSTEM = ARGS[1] == 'true',
JSX_SUUBPATH = (COMPONENT_IS_SYSTEM) ? 'systems' : 'components',
COMPONENT_DIR = `./${JSX_SUUBPATH}/${COMPONENT_NAME}`,
fs = require('fs')
if(!COMPONENT_NAME) {
@jpenney1
jpenney1 / gamepad snippet
Created May 9, 2018
with this snippet, you can add an event listener to any webpage which will log the button pressed to the console, along with its entire availbale buttons array and index at which it was found. Anyone know how if there's a naive way to determine which button in the array is which?
View gamepad snippet
var gameLoop = function(res) {
var gamepad = navigator.getGamepads();
navigator.getGamepads()[0].buttons.forEach(function(button, i, buttons) {
if(button.pressed) console.log('pressed: ', buttons.slice(),' i: ', i, buttons[i])
});
requestAnimationFrame(gameLoop)
}
window.addEventListener('gamepadconnected', gameLoop)
// src: https://developer.mozilla.org/en-US/docs/Web/API/Gamepad_API/Using_the_Gamepad_API
@jpenney1
jpenney1 / Deep copies Matter... why?
Created Apr 16, 2018
Checkout out how the deep clone changes does not change the reference to the object in numsRef[4]. Uncomment line 16, and see how the assignment copy assigns even references, allowing for the original objects to be mutated through the 'copies'.
View Deep copies Matter... why?
let numsRef = {
1: 1,
2: 2,
3: 4,
4: { 1: 'hi' }
};
let deepObj = {
numbers: numsRef
};
@jpenney1
jpenney1 / Flux Architecture Base (Vanilla JS)
Created Apr 16, 2018
Vanilla Javascript flux implementation. Easy to understand and copy into your own project, modifying and extending as necessary.
View Flux Architecture Base (Vanilla JS)
const createStore = (reducer, initialState) => {
const subscribers = [];
let currentState = initialState;
return {
getState: () => currentState,
subscribe: fn => {
subscribers.push(fn);
fn(currentState);
},
View generate2dBoard
const generate2dBoard = (cols, rows) => {
let board = generate2dArray(cols, rows);
let roomSpecs = {
heightMin: 5,
heightMax: 6,
widthMin: 5,
widthMax: 6,
paddingMin: 1,
paddingMax: 4
};
View Member Assignment
var Oldsmobile = function(year, color) {
// private members:
var allowed = ['red', 'black', 'tan'];
var defaultColor = 'black';
// manipulate instance initialization by accessing constructor methods:
Oldsmobile.addAllowed(allowed, 'blue');
// privleged members:
this.color = allowed.find(_color => _color === color) ? color : defaultColor;
You can’t perform that action at this time.