Skip to content

Instantly share code, notes, and snippets.

View tomsoderlund's full-sized avatar
🤔
Learning AI/ML!

Tom Söderlund tomsoderlund

🤔
Learning AI/ML!
View GitHub Profile
@tomsoderlund
tomsoderlund / superwatcher-jquery.js
Last active February 19, 2017 08:54
SuperWatcher in JavaScript: watches a function/value, triggers a callback when changed
// ----- SuperWatcher JQuery extension -----
var watcher = new SuperWatcher();
// Dynamically modify a CSS property when function result changes
$.fn.dynamicCss = function (prop, func) {
// Currying the $.css method
watcher.add(func.bind(this), this.css.bind(this, prop), prop);
// For function chaining
return this;
@tomsoderlund
tomsoderlund / convert_evernote_html_to_markdown.sh
Last active June 3, 2017 04:24
Convert Evernote notes in HTML to Markdown
#!/usr/bin/env
# Convert Evernote notes in HTML to Markdown
# requires reverse_markdown (https://github.com/xijo/reverse_markdown)
# Preserve timestamp from Evernote
function get_timestamp {
reverse_markdown "$1" | \
sed -n '/<meta name="created" content="/p' | \
sed -e 's/<meta name="created" content="//g' | \
@tomsoderlund
tomsoderlund / namedColors.json
Created November 3, 2019 17:08
All named HTML colors with RGB and HSL values
[
{
"name": "indianred",
"hexValue": "#cd5c5c",
"rgbValues": [205, 92, 92],
"hslValues": [0, 0.531, 0.582]
},
{
"name": "lightcoral",
"hexValue": "#f08080",
@tomsoderlund
tomsoderlund / parallelPool.js
Created November 26, 2019 14:43
A resource pool e.g. for projects. Inspired by https://www.npmjs.com/package/generic-pool
/**
* parallelPool module
* @description A resource pool e.g. for projects. Inspired by https://www.npmjs.com/package/generic-pool
* @module parallelPool
* @author Tom Söderlund
*/
// Private functions
/* const projectPool = new ParallelPool({}) */
@tomsoderlund
tomsoderlund / killapps.sh
Last active January 24, 2021 17:09
macOS bash script to close all non-essential apps – like “Close all browser tabs” but for apps
#!/bin/sh
# Example: kill all non-development apps:
# . killapps.sh dev
# Example: kill all non-office apps in a soft way:
# . killapps.sh office soft
# Strip leading and trailing white space (new line inclusive).
trim () {
[[ "$1" =~ ^[[:space:]]*(.*[^[:space:]])[[:space:]]*$ ]]
@tomsoderlund
tomsoderlund / ScrollingNavigation.js
Created February 11, 2021 12:26
Scrolling 3D navigation
import React, { createContext, useContext, useState, useEffect, useRef } from 'react'
const ScrollingNavigation = ({ children, maxX, maxY = 2000, startX = 0, startY = 0, scrollSpeed = 2.0 }) => {
const scrollingRef = useRef()
return (
<ScrollContextProvider
startX={startX}
startY={startY}
scrollSpeed={scrollSpeed}
scrollingRef={scrollingRef}
@tomsoderlund
tomsoderlund / ExampleComponent.js
Last active May 13, 2021 01:50
Shared state using React Hooks and Context, to allow sharing of state between multiple components or hooks
import React, { useContext } from 'react'
import { UserContext, UserContextProvider } from './UserContext'
export default (props) => {
const [user, setUser] = useContext(UserContext)
return (
<UserContextProvider user={null}>
<div>User name: {user && user.name}</div>
</UserContextProvider>
@tomsoderlund
tomsoderlund / useItem.js
Created February 9, 2022 11:23
React Context and State rolled into one
/*
Usage:
import { ItemContextProvider } from 'hooks/useItem'
<ItemContextProvider
item={item}
>
<ComponentThatUsesItem />
@tomsoderlund
tomsoderlund / Map.js
Created June 22, 2020 09:50
Using fitBounds in ReactMapGL to center points on map
import React, { useState } from 'react'
import ReactMapGL, { Marker, WebMercatorViewport } from 'react-map-gl'
const applyToArray = (func, array) => func.apply(Math, array)
const getBoundsForPoints = (points) => {
// Calculate corner values of bounds
const pointsLong = points.map(point => point.coordinates._long)
const pointsLat = points.map(point => point.coordinates._lat)
const cornersLongLat = [
@tomsoderlund
tomsoderlund / useRouterQueryState.js
Last active August 15, 2023 21:03
Like useState hook, but saves current state in browser search bar (Next.js router query)
// import useRouterQueryState from '../hooks/useRouterQueryState'
// const [myState, setMyState] = useRouterQueryState(propertyName, defaultValue)
import { useState, useEffect } from 'react'
import { useRouter } from 'next/router'
function useRouterQueryState (key, defaultValue) {
const router = useRouter()
// Get initial state from router query or default value