Skip to content

Instantly share code, notes, and snippets.

View jaf7's full-sized avatar

Anthony Fields jaf7

View GitHub Profile
@jaf7
jaf7 / copyToClipboard.html
Created March 13, 2021 03:41 — forked from lgarron/copyToClipboard.html
Simple `navigator.clipboard.writeText()` polyfill.
<script>
// A minimal polyfill for `navigator.clipboard.writeText()` that works most of the time in most modern browsers.
// Note that:
// - In Edge, this may call `resolve()` even if copying failed.
// - In Safari, this may fail if there is nothing selected on the page.
// See https://github.com/lgarron/clipboard-polyfill for a more robust solution.
// License: public domain
function writeText(str) {
return new Promise(function(resolve, reject) {
var success = false;
@jaf7
jaf7 / libmysql.py
Created February 26, 2021 16:54 — forked from larry1001/libmysql.py
mysql wrapper in python
import pymysql
import os.path
import sys
from configparser import ConfigParser
from pymysql import (escape_sequence, cursors)
sys.path.append(os.path.dirname(__file__))
config = ConfigParser()
@jaf7
jaf7 / analyze.js
Created August 5, 2020 15:18 — forked from romanonthego/analyze.js
create-react-app bundle-analyzer
process.env.NODE_ENV = 'production';
const webpack = require('webpack');
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin;
const webpackConfigProd = require('react-scripts/config/webpack.config')('production');
// this one is optional, just for better feedback on build
const chalk = require('chalk');
const ProgressBarPlugin = require('progress-bar-webpack-plugin');
const green = text => {
@jaf7
jaf7 / Spinner.js
Created July 23, 2020 18:15 — forked from adrianmcli/Spinner.js
Dead simple loading spinner with just CSS in styled-components.
import styled, { keyframes } from "styled-components";
const rotate360 = keyframes`
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
@jaf7
jaf7 / Spinner.js
Created July 23, 2020 18:15 — forked from knowbody/Spinner.js
Spinner - using styled-components
import React from 'react';
import styled from 'styled-components';
const Spinner = () => (
<StyledSpinner viewBox="0 0 50 50">
<circle
className="path"
cx="25"
cy="25"
r="20"
let UserContext = React.createContext();
class App extends React.Component {
state = {
user: null,
setUser: user => {
this.setState({ user });
}
};
@jaf7
jaf7 / error-handling-with-fetch.md
Created April 11, 2020 18:11 — forked from odewahn/error-handling-with-fetch.md
Processing errors with Fetch API

I really liked @tjvantoll article Handling Failed HTTP Responses With fetch(). The one thing I found annoying with it, though, is that response.statusText always returns the generic error message associated with the error code. Most APIs, however, will generally return some kind of useful, more human friendly message in the body.

Here's a modification that will capture this message. The key is that rather than throwing an error, you just throw the response and then process it in the catch block to extract the message in the body:

fetch("/api/foo")
  .then( response => {
    if (!response.ok) { throw response }
    return response.json()  //we only get here if there is no error
 })
@jaf7
jaf7 / time_dict_merge.py
Created March 23, 2020 01:06 — forked from treyhunner/time_dict_merge.py
Test performance of different dictionary merging functions in Python
"""
Results:
multiple_update: 57 ms
copy_and_update: 46 ms
dict_constructor: 56 ms
kwargs_hack: 45 ms
dict_comprehension: 45 ms
concatenate_items: 166 ms
union_items: 163 ms
@jaf7
jaf7 / import.py
Created January 2, 2020 18:30 — forked from miceno/import.py
Import management command for django-import-export
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
import mimetypes
import argparse
from django.utils.encoding import force_text
from django.utils.translation import ugettext as _
from django.core.management.base import BaseCommand
from django.db import transaction
@jaf7
jaf7 / async-thread.js
Created December 18, 2019 00:25 — forked from sergiodxa/async-thread.js
Use WebWorkers and promises to run sync heavy functions in a worker (process) and get the result in a promise
function asyncThread(fn, ...args) {
if (!window.Worker) throw Promise.reject(
new ReferenceError(`WebWorkers aren't available.`)
);
const fnWorker = `
self.onmessage = function(message) {
(${fn.toString()})
.apply(null, message.data)
.then(result => self.postMessage(result));