Skip to content

Instantly share code, notes, and snippets.

View scwood's full-sized avatar

Spencer Wood scwood

View GitHub Profile
@scwood
scwood / smallpwd.js
Created May 2, 2017 20:48
Get abbreviated pwd
var os = require('os');
var home = os.homedir();
var cwd = process.cwd();
if (cwd.substring(0, home.length) === home) {
cwd = cwd.replace(home, '~')
}
var items = cwd.split('/');
var isAbsolute;
@scwood
scwood / find.sh
Last active June 2, 2017 18:38
Using find to only get files in nested directories n levels deep
# So here's my directory structure
# .
# ├── a
# │   └── test.sh
# ├── b
# │   └── test.sh
# └── test.sh
find . -name *.sh
@scwood
scwood / autoexec.cfg
Created June 6, 2017 20:02
CS:GO config
// general
fps_max 0
closeonbuy 1
gameinstructor_enable 0
bind p "exec autoexec"
// net graph
net_graph 1
net_graphpos 2
function promisify(fn) {
return (...args) => {
return new Promise((resolve, reject) => {
fn(...args, (error, ...result) => {
error ? reject(error) : resolve(...result);
})
})
}
}
@scwood
scwood / gcloud-express-middleware.js
Last active September 28, 2017 20:25
How to call express middleware in a Google Cloud Function
function cors(req, res, next) {
console.log('in cors middleware:');
req.cors = 'enabled';
console.log(req);
next();
}
function logger(req, res, next) {
console.log('in logger middleware:');
req.logged = true;
@scwood
scwood / progress.js
Last active July 19, 2017 19:47
Progress bar in Node
const readline = require('readline')
function drawProgress(current, total, promptLength = 25) {
const percentComplete = current / total
const visualPercent = Math.round(percentComplete * 100)
const inProgressLength = Math.round(promptLength * percentComplete)
const filledBar = rightPad('='.repeat(inProgressLength), ' ', promptLength)
readline.cursorTo(process.stdout, 0)
process.stdout.write(`[${filledBar}] ${visualPercent}%`)
}
@scwood
scwood / attemptWithRetry.js
Last active October 4, 2018 05:42
Exponential backoff retry logic for a function that returns a Promise
/**
* Retries a function that returns a promise a given number of times and backs
* off each attempt exponentially.
*
* @param {Function} promiseGenerator The function to retry.
* @param {number} [attempts=5] Number of attempts to make.
* @param {number} [delay=1000] Initial delay between attempts in ms.
* @return {Promise}
*/
function attemptWithRetry(promiseGenerator, attempts = 5, delay = 100) {
@scwood
scwood / clearConsole.js
Last active January 13, 2018 00:27
Clear console in JavaScript
function clearConsole() {
return process.stdout.write('\033c');
}
// run this at before app.listen() to clear console, great with tools like nodemon
// Edit: or you could use the builtin console.clear() lol
@scwood
scwood / largestRemainderRound.js
Last active December 14, 2021 14:07
Largest remainder round
/**
* largestRemainderRound will round each number in an array to the nearest
* integer but make sure that the the sum of all the numbers still equals
* desiredTotal. Uses largest remainder method. Returns numbers in order they
* came.
*
* @param {number[]} numbers - numbers to round
* @param {number} desiredTotal - total that sum of the return list must equal
* @return {number[]} the list of rounded numbers
* @example
@scwood
scwood / column.bash
Created October 3, 2017 15:02
Append output to column of file
touch output.csv # file with the end result
for i in {1..10}; do # replace with your "for every file loop"
sh ./program.sh | tr ' ' '\n' > tempfile1 # program.sh is your software, we pipe it into tr to split it into a column
paste -d ',' tempfile1 output.csv > tempfile2 # paste the column with a delimeter to a tempfile
mv tempfile2 output.csv # clean up the tempfiles on every iteration
rm tempfile1
done