Skip to content

Instantly share code, notes, and snippets.

Avatar
💭
🥳

Matt Lockyer mattlockyer

💭
🥳
View GitHub Profile
@mattlockyer
mattlockyer / console.js
Created Oct 23, 2020
Quick Modal for Developer Console
View console.js
modal = document.createElement('div')
modal.innerHTML = '<span id="close-modal">x</span>'
style = modal.style
modal.style.zIndex = 1000
modal.style.width = modal.style.height = '90%'
modal.style.background = 'white'
modal.style.position = 'absolute'
modal.style.top = modal.style.left = '5%'
modal.style.padding = '16px'
document.body.appendChild(modal)
@mattlockyer
mattlockyer / sane-localStorage.js
Created Sep 26, 2020
Sane localStorage wrappers for JSON
View sane-localStorage.js
const get = (k, d = {}) => JSON.parse(localStorage.getItem(k) || JSON.stringify(d))
const set = (k, v) => localStorage.setItem(k, JSON.stringify(v))
const del = (k) => localStorage.removeItem(k)
// e.g.
const balances = get('balances', [])
// outputs [] if never used
@mattlockyer
mattlockyer / check-hash.js
Created Sep 21, 2020
Check a NEAR contract hash using near-api-js
View check-hash.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 Sep 27, 2020
React useContext, state, dispatch with thunk for namespaced state management
View App.js
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
Javascript Mutex to prevent re-entry from multiple method calls. Protect methods that modify app, localStorage or IndexedDB.
View app.js
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
Docker notes if having trouble starting, manual, killing, etc...
View docker.txt
# 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
Keyboard shortcuts for vs code
View keybindings.json
[
{
"key": "ctrl+]",
"command": "workbench.action.terminal.focusNext"
},
{
"key": "ctrl+[",
"command": "workbench.action.terminal.focusPrevious"
},
{
@mattlockyer
mattlockyer / contraints.sql
Created May 11, 2020
Postgres - get constraints from table
View contraints.sql
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 Apr 30, 2020
Rename all files of a type in a folder with a sequential number scheme
View rename.sh
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
View ebay-scrape.js
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)
You can’t perform that action at this time.