Skip to content

Instantly share code, notes, and snippets.

View mattlockyer's full-sized avatar
💭
🥳

Matt Lockyer mattlockyer

💭
🥳
View GitHub Profile
@mattlockyer
mattlockyer / email-or-phone-validation.js
Created June 11, 2019 15:23
Validate input as email or phone, with errors
let contact = 'test', data, error;
const first = contact.substring(0, 1)
if (first === '+' || first === '(' || !isNaN(first)) {
//phone
console.log('testing phone')
const phone = contact.replace(/[+|-|(|)|.| ]/g, "").toLowerCase()
if (phone === "" || isNaN(phone)) {
error = "Please enter a proper phone number.";
} else if (phone.length < 10) {
error = "Your phone number is not long enough. Please include the Area Code.";
@mattlockyer
mattlockyer / index.html
Last active June 18, 2019 23:47
Facebook Login for Web with Using Go Server.Validate token from web client POST request. FB API called with GET request. Reponse to client in JSON
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Facebook Login Example</title>
<meta name="description" content="Facebook Login Example">
<meta name="author" content="Matt Lockyer">
</head>
@mattlockyer
mattlockyer / index.html
Last active June 19, 2019 00:01
Facebook Login for Web with Using Go Server. Token returned from FB Login button on client. Client makes POST request. FB API called with GET request. Token Validated. Reponsd to client with error / JSON.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Facebook Login Example</title>
<meta name="description" content="Facebook Login Example">
<meta name="author" content="Matt Lockyer">
</head>
@mattlockyer
mattlockyer / index.html
Created June 19, 2019 00:05
YouTube Search with Go / GoLang Server. Client sends url query param "q" to server. Server searches YouTube API for videos matching q. Server decodes, does "anything", then responds to client with JSON.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>YouTube Search Example</title>
<meta name="description" content="YouTube Search Example">
<meta name="author" content="Matt Lockyer">
</head>
@mattlockyer
mattlockyer / conditional-json.js
Created June 19, 2019 15:25
Using spread syntax to conditionally add JSON key, value to object. Fetch example using POST and Authorization.
export const POST = (auth) => ({
method: "POST", // *GET, POST, PUT, DELETE, etc.
mode: "cors", // no-cors, cors, *same-origin
headers: {
"Content-Type": "application/json",
...(auth ? { "Authorization": auth } : {})
},
})
@mattlockyer
mattlockyer / genfunc.js
Created June 30, 2019 15:21
Generate functions that are based on a similar structure by name - e.g. set redux state of UI components
//example functions to set redux state of UI components
const {setDrawerState, setDialogState, setPopoverState} = ['Drawer', 'Dialog', 'Popover'].map((name) => ({
['set' + name + 'State']: (state) => async (dispatch, getState) => {
const stateName = name.toLowerCase() + 'State'
const currentState = getState().appReducer[stateName]
dispatch({ type: 'UPDATE_UI_STATE', [stateName]: { ...currentState, ...state } })
}
})).reduce((a, c) => ({...a, ...c}))
export { setDrawerState, setDialogState, setPopoverState }
@mattlockyer
mattlockyer / base64.js
Created July 10, 2019 03:22
base 64 conversions using file reader and fetch
//FROM HERE: https://stackoverflow.com/a/54123275/1060487
// base64 to buffer
function base64ToBufferAsync(base64) {
var dataUrl = "data:application/octet-binary;base64," + base64;
fetch(dataUrl)
.then(res => res.arrayBuffer())
.then(buffer => {
console.log("base64 to buffer: " + new Uint8Array(buffer));
@mattlockyer
mattlockyer / ga_cfw.js
Last active September 30, 2020 20:31
Google Analytics for Cloudflare Workers
/********************************
* GA data
********************************/
let data = {
v: 1,
}
/********************************
* Initializes GA data
* @param {string} tid your tracking id for GA
* @param {object} req the request object from event.request
@mattlockyer
mattlockyer / jwt.js
Created November 2, 2019 04:01
JWT Token Module for Cloudflare Workers / Browser
/********************************
* Module for generating and verifying JWT tokens
* Designed to work on Cloudflare Workers
* Should work in the browser or any env that supports Web Crypto API
********************************/
/********************************
* Key secret is a random Uint8Array of length 64
* See below for instructions on generating random values / keys
********************************/
@mattlockyer
mattlockyer / tx-curl.sh
Created December 6, 2019 19:20
Harmony Transaction Log Curl Script (testnet)
SHARD0=https://api.s0.b.hmny.io
SHARD1=https://api.s1.b.hmny.io
SHARD2=https://api.s2.b.hmny.io
#your params
SHARD=SHARD1
#example is HRC20 mint and transfer
TXID=0x039d2f87e6bdb81220e5a7490dc783ea835443f57f4e12d16d90dd0b3aa1f5af
#curl
curl -X POST $SHARD1 -H 'Accept: */*' -H 'Accept-Encoding: gzip, deflate' -H 'Cache-Control: no-cache' -H 'Connection: keep-alive' -H 'Content-Length: 162' -H 'Content-Type: application/json' -H 'Host: api.s1.b.hmny.io' -H 'Postman-Token: d5415117-657a-49f9-9100-a5b7ebc70daf,cc2f3cb9-2d10-408c-a003-d6e0822ec985' -H 'User-Agent: PostmanRuntime/7.19.0' -H 'cache-control: no-cache' -d '{
"jsonrpc":"2.0",