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 / 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 / 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');
require('rxjs/add/observable/fromEvent');
require('rxjs/add/operator/map');
require('rxjs/add/observable/merge');
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
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
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 / 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 / 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
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: https://codesandbox.io/s/v86lv7k89l
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
ccnokes / stash_to_patch.sh
Created Mar 28, 2019
Convert all stashes to patches in a git repository
View stash_to_patch.sh
#!/bin/bash
stash_count=$(git stash list | wc -l)
i=0
while [[ $i -lt $stash_count ]]; do
git stash show -p stash@{$i} > "stash-$i.patch"
i=$(( $i + 1 ))
done
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
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 https://developers.google.com/web/updates/2015/08/using-requestidlecallback). Sandbox: https://codesandbox.io/s/r45rv9lv1o
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.