Skip to content

Instantly share code, notes, and snippets.

View mattlockyer's full-sized avatar
💭
🥳

Matt Lockyer mattlockyer

💭
🥳
View GitHub Profile
@mattlockyer
mattlockyer / sane-localStorage.js
Last active January 14, 2021 03:46
Sane localStorage wrappers for JSON
export const get = (k, d = {}) => {
let v = localStorage.getItem(k)
try {
return JSON.parse(v || JSON.stringify(d))
} catch (e) {
return v
}
}
export const set = (k, v) => localStorage.setItem(k, typeof v === 'string' ? v : JSON.stringify(v))
export const del = (k) => localStorage.removeItem(k)
@mattlockyer
mattlockyer / check-hash.js
Created September 21, 2020 00:31
Check a NEAR contract hash using near-api-js
import * as nearlib from 'near-api-js'
const contractBytes = await fetch('./contract.wasm').then((r) => r.arrayBuffer())
const hash = await crypto.subtle.digest('SHA-256', contractBytes)
const hash58 = nearlib.utils.serialize.base_encode(hash)
console.log(hash58)
@mattlockyer
mattlockyer / App.js
Last active September 27, 2020 23:53
React useContext, state, dispatch with thunk for namespaced state management
import React, { useContext, useEffect } from 'react';
import { store } from './state/store';
import { onMount } from './state/test';
const ExampleComponent = () => {
const { state, dispatch, update: updateStore } = useContext(store)
console.log(state)
const update = async () => {
// dispatch thunk wraps function with state, dispatch
const res = await dispatch(initNear())
@mattlockyer
mattlockyer / app.js
Last active May 26, 2020 15:52
Javascript Mutex to prevent re-entry from multiple method calls. Protect methods that modify app, localStorage or IndexedDB.
import { lock, unlock } from './mutex.js'
async function processCallsLikeQueue() {
// stop method calls here
await lock('processCallsLikeQueue()')
// ...
// code to protect from re-entry
// i.e. prevent multiple simultaneous calls to this part of the code
// ...
// let next method call in
@mattlockyer
mattlockyer / docker.txt
Last active May 21, 2020 20:09
Docker notes if having trouble starting, manual, killing, etc...
# start docker manually
sudo rm -rf /var/run/docker.pid; sudo dockerd &
# kill all docker ps
ps axf | grep docker | grep -v grep | awk '{print "kill -9 " $1}' | sudo sh
# restart as service
sudo systemctl restart docker
# build docker
@mattlockyer
mattlockyer / keybindings.json
Created May 13, 2020 00:57
Keyboard shortcuts for vs code
[
{
"key": "ctrl+]",
"command": "workbench.action.terminal.focusNext"
},
{
"key": "ctrl+[",
"command": "workbench.action.terminal.focusPrevious"
},
{
@mattlockyer
mattlockyer / contraints.sql
Created May 11, 2020 18:56
Postgres - get constraints from table
SELECT con.*
FROM pg_catalog.pg_constraint con
INNER JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid
INNER JOIN pg_catalog.pg_namespace nsp ON nsp.oid = connamespace
WHERE
nsp.nspname = '<schema name>'
AND rel.relname = '<table name>';
/* from: https://dba.stackexchange.com/a/214877/187757 */
@mattlockyer
mattlockyer / rename.sh
Created April 30, 2020 23:40
Rename all files of a type in a folder with a sequential number scheme
a=1
for i in *.jpg; do
new=$(printf "%04d.jpg" "$a") #04 pad to length of 4
mv -i -- "$i" "$new"
let a=a+1
done
# from https://stackoverflow.com/a/3211670/1060487
@mattlockyer
mattlockyer / ebay-scrape.js
Created April 9, 2020 22:18
Scrapes Ebay Prices
const cheerio = require('cheerio')
const request = require('request');
let query = process.env.QUERY
if (!query) {
console.log('Please define an ENV var for QUERY="..."')
return -1
}
query = encodeURIComponent(query)
@mattlockyer
mattlockyer / countries.json
Created February 24, 2020 17:58
Country Code to Country Name, Unicode and Flag Emoji
{
"AC": {
"unicode": "U+1F1E6 U+1F1E8",
"name": "Ascension Island",
"emoji": "🇦🇨"
},
"AD": {
"unicode": "U+1F1E6 U+1F1E9",
"name": "Andorra",
"emoji": "🇦🇩"