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 /
Created Nov 17, 2018
Magic eight ball bash script. Put this in your $PATH and never make a decision without it.
#! /bin/bash
number=$(echo $((1 + RANDOM % 20)))
case $number in
1) msg="It is certain.";;
2) msg="It is decidedly so.";;
3) msg="Without a doubt.";;
4) msg="Yes - definitely.";;
ccnokes /
Last active Oct 26, 2018
Generate a data URI for a file
data_uri() {
if [[ "$#" -ne 1 ]] || [[ ! -f $1 ]]; then
echo "ERROR! Usage: ${FUNCNAME[0]} <file name>"
return 1
mime=$(file -b --mime-type "$1")
data=$(base64 "$1")
echo "data:$mime;base64,$data"
ccnokes / parse-bash-brace-expansion.js
Created Oct 4, 2018
Parse basic bash brace expansions. Demo: Just because. I should learn how to write a compiler/interpreter thing for real.
View parse-bash-brace-expansion.js
// Test it...
/* test-a-test test-b-test test-c-test */
function expandBraces(str) {
let { preamble, expressionList, postscript } = parse(str);
return expressionList
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:
View cache-based-async-storage.js
// Working example:
class Storage {
constructor(key = location.origin + '/cache-key-v1') {
this.KEY = key;
get(id) {
.then(cache => cache.match(id))
ccnokes /
Created Aug 13, 2018
Plays Rick Astley's "Never Gonna Give You Up" in Spotify
osascript <<EOD
tell application "Spotify"
play track "spotify:track:4uLU6hMCjMI75M1A2tKUQC"
end tell
ccnokes / index.html
Created Aug 10, 2018
Electron Fiddle Gist
View index.html
<!DOCTYPE html>
<meta charset="UTF-8">
<title>Hello World!</title>
<h1>Hello World!</h1>
<!-- All of the Node.js APIs are available in this renderer process. -->
We are using Node.js <script>document.write(process.versions.node)</script>,
ccnokes /
Last active Jul 9, 2019
Bash script to grep for unused dependencies in a node.js project
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 / infinite-load-generator.js
Last active Jul 12, 2018
Generator function for infinite loading things
View infinite-load-generator.js
// See demo here:
/* 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);
ccnokes /
Last active May 18, 2019
Set your wallpaper to the current Astronomy Picture of the Day (APOD)
# get the URL of the current Astronomy Picture of the Day (APOD)
apod_url=$(curl -s | jq -r '.hdurl')
# get just the image name from the URL
filepath=$(basename "$apod_url")
# Now get the image and save it
curl -s -o "$filepath" "$apod_url"
# Use AppleScript to set it as the wallpaper
ccnokes / task-queue.js
Last active Jun 28, 2018
Task queue with configurable concurrency and priority. Demo:
View task-queue.js
// see demo:
class Deferred {
constructor() {
this.promise = new Promise((resolve, reject) => {
this.resolve = resolve;
this.reject = reject;
this.then = this.promise.then.bind(this.promise);
this.catch = this.promise.catch.bind(this.promise);
You can’t perform that action at this time.