Skip to content

Instantly share code, notes, and snippets.

@sramam
sramam / starter.sh
Last active August 29, 2015 14:01
A mature bash file starter
#!/bin/bash
# allow color coding the console output.
# simple, but effective way to getting visual oomph to the screen
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
white='\033[0;37m'
end_color='\033[0;37m'
@sramam
sramam / gist:0c1c272a3ac43b2e7e5e
Last active August 29, 2015 14:04
npmls to determine package dependencies for a nodenev
# inspired by https://gist.github.com/nicholascloud/5372443
function npmls() {
npm ls --depth=0 | grep -v "^\/.*" | sed "s/└── //g" | sed "s/├── //g"
}
@sramam
sramam / gist:8a65efeb4d483471b5b9
Created September 22, 2014 18:50
highlighted source code from the command line
function pbhighlight() {
HTML=`highlight --inline-css -O html --style=tcsoft $@ | hexdump -ve '1/1 "%.2x"'`;
osascript -e "set the clipboard to «data HTML${HTML}»"
}
which can be used as `pbhighlight file.java` to copy highlighted source to your clipboard for pasting. May require installation of "highlight" if that's not included, "brew install highlight"
@sramam
sramam / rate-limit
Last active April 20, 2017 16:48
node/js rate limiter
/**
* A simple illustration of how to use the npm `limiter` package to create a rate limiting wrapped api call.
*/
const TokenBucket = require('limiter').TokenBucket;
function makeActualApiCall (args) {
console.log(`makeActualApiCall invoked @${new Date()} - in ms: ${Date.now()}`);
}
@sramam
sramam / gist:3d7543fb7444f2f95ec05a2abc4770ef
Created November 20, 2017 15:20
nyc/istanbul coverage report
v8.8.1
5.4.2
data-check@0.0.0-development /Users/sramam/trial/data-check
├─┬ @types/chalk@2.2.0
│ └── chalk@2.3.0 deduped
├── @types/circular-json@0.4.0
├── @types/node@8.0.53
├── @types/semver@5.4.0
├─┬ ajv@5.3.0
│ ├── co@4.6.0
@sramam
sramam / gist:f47e418299465f08ba5954e115a2822e
Last active January 27, 2018 16:07
creating files with specific dates in the past - for testing file cleanup logic. (node.js)
const fs = require('fs');
const dir = '/tmp';
const now = Date.now();
const tmp = Date.now();
const dates = {
_31days: new Date(now - 31 * 24 * 60 * 60 * 1000),
_30days: new Date(now - 30 * 24 * 60 * 60 * 1000),
_29days: new Date(now - 29 * 24 * 60 * 60 * 1000),
_28days: new Date(now - 28 * 24 * 60 * 60 * 1000),
@sramam
sramam / async_generators.ts
Created February 21, 2018 02:52
understanding async generators
import * as delay from 'delay';
(<any>Symbol).asyncIterator = Symbol.asyncIterator || Symbol.for("Symbol.asyncIterator");
async function* g() {
yield 1;
await delay(100);
yield* [2, 3];
yield* (async function *() {
/**
script adapted from https://www.youtube.com/watch?v=pXUsW6VRQak
*/
function myFunction() {
var sheetName = 'Sheet1';
var formId = '1LnmQRJX0l586EvNeXYNLujO2vqhqB-Grtfe1J5zfFWI';
var title = 'Form Title here';
var description =
'Form description here.' +
'This should really be moved to a cell in the sheet.' +
@sramam
sramam / gist:ccd2f221a12b55a38e4fe47a40ccc7fc
Created March 17, 2018 00:48
Map & Reduce promises with concurrency throttling - an example
const pReduce = require('p-reduce');
const pMap = require('p-map');
const delay = require('delay');
// const batches = [['a'], ['b', 'c'], ['d', 'e', 'f', 'g'], ['h'], ['i'], ['j', 'k']];
const batches = [['a', 'b', 'c', 'd', 'e', 'f', 'g'], ['h'], ['i'], ['j', 'k']];
const step = 2000;
const max = 5000;
@sramam
sramam / bank-account.js
Created March 17, 2018 20:56
A simple bank-account FSM, implemented in javascript-state-machine
const StateMachine = require('javascript-state-machine');
const Account = StateMachine.factory({
init: 'open',
transitions: [
// open state
{name: 'deposit', from: 'open', to: 'open'},
{name: 'withdraw', from: 'open', to: 'open'},
{name: 'available', from: 'open', to: 'open'},