Skip to content

Instantly share code, notes, and snippets.

View JH108's full-sized avatar
🕵️
Android, Kotlin, Jetpack Compose, Go, always learning!

Jesse Hill JH108

🕵️
Android, Kotlin, Jetpack Compose, Go, always learning!
View GitHub Profile
@JH108
JH108 / List.jsx
Created September 30, 2017 00:34
Difference between createClass and Component
var React = require('react');
var ListItem = require('./ListItem.jsx');
var ingredients = [{"id":1,"text":"ham"}, {"id":2,"text":"cheese"},{"id":3,"text":"Tomato"}];
class List extends React.Component {
render() {
const listItems = ingredients.map((item) => (<ListItem key={item.id} ingredient={item.text} />));
return (
<ul>{listItems}</ul>
@JH108
JH108 / getListeners
Last active April 9, 2019 16:39
Get all the event listeners on a page
[...document.querySelectorAll('*')]
.map((e) => ({
element: e,
listeners: Object.keys(getEventListeners(e))
.map((k) => ({
event: k,
listeners: getEventListeners(e)[k]
}))
}))
.filter((item) => item.listeners.length);
@JH108
JH108 / parseWindowSelection.js
Created April 20, 2018 19:23
Parse window.getSelection
const parseSelection = () => {
const s = window.getSelection();
const aText = s.anchorNode.textContent;
const eText = s.extentNode.textContent;
const aOffset = s.anchorOffset;
const eOffset = s.extentOffset;
const lineBreaks = '\n\n';
const selectedText = s.toString();
console.log(
@JH108
JH108 / CloseMenuFunction.js
Created June 6, 2018 04:54
Helper to close menus on a click outside of their boundaries
class CloseMenuFunctions {
// Need to implement a parallel for IE because getBoundingClientRect does not work the same as with the other browsers
constructor(componentRef, closeFunction, onCloseOptions) {
this.ref = componentRef || { getBoundingClientRect() { return { x: 0, y: 0, width: 0, height: 0 }; }, contains() { return true; } };
this.closeFunction = closeFunction;
this.onCloseOptions = onCloseOptions;
}
handleTouchend = (event) => {
if (event.changedTouches.length === 1) {
Object.keys(localStorage).filter(key => key.slice(0, 7) === 'lscache').forEach(key => localStorage.removeItem(key))
@JH108
JH108 / hexToRgba.js
Created April 4, 2019 19:08
Simple hex to rgba converter using bitwise operators.
// Repl.it that logs out several of the operations so you can experiment and see how the function works
// Link: https://repl.it/@JH108/Bitwise-fun
// If curious as to how it works see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Bitwise_Operators
function hexToRgbA(hex){
var c;
// Check to ensure that the string is a valid hex value
if(/^#([A-Fa-f0-9]{3}){1,2}$/.test(hex)){
// Remove the # from the value and split into an array
c= hex.substring(1).split('');
# Customizes the text displayed in the terminal to look like ~/code/projects (master). Git branches are shown.
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
low_pri() {
command="$@"
echo $command
nice -n 5 $command
}
text() {
@JH108
JH108 / jsperfLinks.txt
Created November 26, 2019 16:20
Links to performance tests on jsperf
@JH108
JH108 / promises.js
Created December 20, 2019 13:06
Implementation of promises - (inspired by Low Level Javascript)
const states = {
PENDING: "pending",
FULFILLED: "fulfilled",
REJECTED: "rejected"
};
const isThenable = maybePromise =>
maybePromise && typeof maybePromise.then === "function";
class CustomPromise {
@JH108
JH108 / leaderboard.js
Created January 9, 2020 23:09
Score a Leaderboard
function setupTournament(number, games) {
const scores = [];
const leaderboard = [];
// Set all points against and for to 0
for (var i = 0; i < number; i++) {
scores[i] = {}
scores[i].for = 0;
scores[i].against = 0;
scores[i].key = i;