Skip to content

Instantly share code, notes, and snippets.

💭
Copying and pasting from StackOverflow

Cameron Nokes ccnokes

💭
Copying and pasting from StackOverflow
Block or report user

Report or block ccnokes

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@ccnokes
ccnokes / withProps.tsx
Created Jan 25, 2019
withProps HOC. For when you want to inject arbitrary stuff into a component
View withProps.tsx
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} />
}
}
@ccnokes
ccnokes / middleware.js
Created Jan 22, 2019
Really basic express-like middleware implementation
View middleware.js
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 / index.html
Created Dec 3, 2018
Electron Fiddle Gist
View index.html
<!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 / magic-eight-ball.sh
Created Nov 17, 2018
Magic eight ball bash script. Put this in your $PATH and never make a decision without it.
View magic-eight-ball.sh
#! /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 / data_uri.sh
Last active Oct 26, 2018
Generate a data URI for a file
View data_uri.sh
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 / parse-bash-brace-expansion.js
Created Oct 4, 2018
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.
View parse-bash-brace-expansion.js
// 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
@ccnokes
ccnokes / cache-based-async-storage.js
Last active Oct 26, 2018
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/
View cache-based-async-storage.js
// 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 / never_gonna_give_you_up.sh
Created Aug 13, 2018
Plays Rick Astley's "Never Gonna Give You Up" in Spotify
View never_gonna_give_you_up.sh
#!/bin/bash
osascript <<EOD
tell application "Spotify"
activate
play track "spotify:track:4uLU6hMCjMI75M1A2tKUQC"
play
end tell
EOD
@ccnokes
ccnokes / grep-for-dependencies.sh
Last active Jul 29, 2019
Bash script to grep for unused dependencies in a node.js project
View grep-for-dependencies.sh
set -e
# function to grep for a dependency
grep_dep() {
# params: $1 = the string to grep for, $2 = directory to grep in
# [1]
grep --include="*.js" --exclude-dir="node_modules" -R --color -n "require\(.*$1.*\)" "$2"
# if grep returns 0 results, it has an exit code of 1. No results means dependency is not in use
@ccnokes
ccnokes / infinite-load-generator.js
Last active Jul 12, 2018
Generator function for infinite loading things
View infinite-load-generator.js
// See demo here: https://jsfiddle.net/ccnokes/xfu3yzce/
/* fake request for remote data */
function request(offset = 0) {
return new Promise((res) => {
let data = new Array(5).fill(offset).map((n, i) => n + i);
setTimeout(() => res({ total: 20, data }), 1000);
});
}
You can’t perform that action at this time.