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 / 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 / index.html
Created Aug 10, 2018
Electron Fiddle Gist
View index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<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
ccnokes / grep-for-dependencies.sh
Last active Jul 9, 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);
});
}
@ccnokes
ccnokes / set-wallpaper-to-apod.sh
Last active May 18, 2019
Set your wallpaper to the current Astronomy Picture of the Day (APOD)
View set-wallpaper-to-apod.sh
# get the URL of the current Astronomy Picture of the Day (APOD)
apod_url=$(curl -s https://api.nasa.gov/planetary/apod?api_key=DEMO_KEY | 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
ccnokes / task-queue.js
Last active Jun 28, 2018
Task queue with configurable concurrency and priority. Demo: https://jsfiddle.net/ccnokes/6hb92mde/
View task-queue.js
// see demo: https://jsfiddle.net/ccnokes/6hb92mde/
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.