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 /
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 / rx-online-offline.js
Last active Jun 26, 2019
Online/offline event observable with RxJS
View rx-online-offline.js
const { Observable } = require('rxjs/Observable');
function createOnline$() {
//merge several events into one
return Observable.merge(
//use .map() to transform the returned Event type into a true/false value
Observable.fromEvent(window, 'offline').map(() => false),
ccnokes / rafScheduler.ts
Created Jun 18, 2019
requestAnimationFrame scheduler
View rafScheduler.ts
* A function for batching RAFs together
export default function RAFScheduler() {
let queue = [];
let rafId;
let scheduled = false;
const DURATION = 10;
return function scheduleRaf(cb: () => void) {
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 /
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 / axios-instance-config.js
Created Jul 6, 2017
Good default configuration for axios in node.js
View axios-instance-config.js
const axios = require('axios');
const http = require('http');
const https = require('https');
module.exports = axios.create({
//60 sec timeout
timeout: 60000,
//keepAlive pools and reuses TCP connections, so it's faster
httpAgent: new http.Agent({ keepAlive: true }),
ccnokes / Batch.ts
Last active Apr 2, 2019
A class that manages the logic of batching items together within a timeframe and then passing those items to a function. Sandbox:
View Batch.ts
class Deferred<Type> {
promise: Promise<Type>;
// TODO type these
resolve: any;
reject: any;
then: any;
catch: any;
constructor() {
this.promise = new Promise((resolve, reject) => {
ccnokes /
Created Mar 28, 2019
Convert all stashes to patches in a git repository
stash_count=$(git stash list | wc -l)
while [[ $i -lt $stash_count ]]; do
git stash show -p stash@{$i} > "stash-$i.patch"
i=$(( $i + 1 ))
View electron-remote-example.js
// This works in the either the main or renderer processes.
const { requireTaskPool } = require('electron-remote');
const work = requireTaskPool(require.resolve('./work'));
console.log('start work');
// `work` will get executed concurrently in separate processes
work().then(result => {
ccnokes / RICScheduler.ts
Last active Feb 23, 2019
RequestIdleCallback scheduler. Create an instance and use it globally to ensure that tasks are efficiently run in as few requestIdleCallbacks as possible (as described here Sandbox:
View RICScheduler.ts
class RICScheduler {
private queue: { task: () => void, id: number }[] = [];
private running = false;
private nextId = 0;
private ricId;
private id() {
return this.nextId++;
You can’t perform that action at this time.