Skip to content

Instantly share code, notes, and snippets.

View ccnokes's full-sized avatar

Cameron Nokes ccnokes

View GitHub Profile
@ccnokes
ccnokes / parse-bash-brace-expansion.js
Created October 4, 2018 04:34
Parse basic bash brace expansions. Demo: https://jsfiddle.net/ccnokes/kdqm5o7f/. Just because. I should learn how to write a compiler/interpreter thing for real.
// Test it...
console.log(
expandBraces('test-{a,b,c}-test')
);
/* test-a-test test-b-test test-c-test */
function expandBraces(str) {
let { preamble, expressionList, postscript } = parse(str);
return expressionList
const { Observable } = require('rxjs/Observable');
require('rxjs/add/operator/filter');
require('rxjs/add/operator/switchMap');
require('rxjs/add/operator/take');
require('rxjs/add/operator/toPromise');
const axios = require('axios');
const online$ = createOnline$();
//only make the network request when we're online
@ccnokes
ccnokes / data_uri.sh
Last active October 26, 2018 05:07
Generate a data URI for a file
data_uri() {
if [[ "$#" -ne 1 ]] || [[ ! -f $1 ]]; then
echo "ERROR! Usage: ${FUNCNAME[0]} <file name>"
return 1
fi
mime=$(file -b --mime-type "$1")
data=$(base64 "$1")
echo "data:$mime;base64,$data"
}
@ccnokes
ccnokes / cache-based-async-storage.js
Last active October 26, 2018 22:14
Example of how you could use the Cache API to create a generic async key/value store. Something like this could be used over localStorage because it allows for more space and is available to workers. Working example: https://jsfiddle.net/ccnokes/790xaw53/
// Working example: https://jsfiddle.net/ccnokes/790xaw53/
class Storage {
constructor(key = location.origin + '/cache-key-v1') {
this.KEY = key;
}
get(id) {
return caches.open(this.KEY)
.then(cache => cache.match(id))
@ccnokes
ccnokes / magic-eight-ball.sh
Created November 17, 2018 19:58
Magic eight ball bash script. Put this in your $PATH and never make a decision without it.
#! /bin/bash
number=$(echo $((1 + RANDOM % 20)))
msg=""
case $number in
1) msg="It is certain.";;
2) msg="It is decidedly so.";;
3) msg="Without a doubt.";;
4) msg="Yes - definitely.";;
@ccnokes
ccnokes / index.html
Created December 3, 2018 18:10
Electron Fiddle Gist
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
<meta http-equiv="Content-Security-Policy" content="default-src *; script-src 'self' 'unsafe-inline';">
</head>
<body>
<h1>Hello World!</h1>
<!-- All of the Node.js APIs are available in this renderer process. -->
@ccnokes
ccnokes / SafeSetState.ts
Last active January 3, 2019 21:44
mixin class that extends your component and makes `setState` "safe". See https://codesandbox.io/s/6438ymvk8z
import * as React from 'react';
// see https://github.com/Microsoft/TypeScript/pull/13743 for what the typing in here's all about
type Constructor<T> = new (...args: any[]) => T;
const isMounted_Symbol = Symbol('isMounted');
/**
* This is for when you're calling setState in uncancellable async callbacks.
* NOTE: Doing this can mask memory leaks so be careful
@ccnokes
ccnokes / batch-async-tasks.js
Last active January 15, 2019 09:51
Chunk async tasks into batches and process in parallel
// dummy async task
function asyncTask(n) {
return new Promise(resolve => setTimeout(() => resolve(n), 500));
}
// takes a flat array and returns a nested one, eg. chunkArray([1,2,3], 1) ==> [[1],[2],[3]]
function chunkArray(arr, chunkSize) {
return arr.reduce((aggr, item) => {
let lastArr = aggr[aggr.length - 1];
if (lastArr.length < chunkSize) {
@ccnokes
ccnokes / middleware.js
Created January 22, 2019 21:07
Really basic express-like middleware implementation
class Middleware {
private middlewares = [];
push(...middlewares) {
this.middlewares.push.apply(this.middlewares, middlewares);
}
run(...args) {
let i = 0;
const next = () => {
if (i < this.middlewares.length) {
@ccnokes
ccnokes / withProps.tsx
Created January 25, 2019 22:02
withProps HOC. For when you want to inject arbitrary stuff into a component
import * as React from 'react';
export default function withProps<InjectProps, Props = {}>(Component: React.ComponentType<Props & InjectProps>, props: InjectProps): React.ComponentClass<Pick<Props, Exclude<keyof Props, keyof InjectProps>>> {
return class extends React.Component<Props> {
static displayName = `withProps(${Component.displayName || Component.name})`;
static WrappedComponent = Component;
render() {
return <Component {...this.props} {...props} />
}
}