Skip to content

Instantly share code, notes, and snippets.

View shamasis's full-sized avatar

Shamasis Bhattacharya shamasis

View GitHub Profile
@shamasis
shamasis / describe-it.min.js
Last active May 4, 2018 02:58
mocha/jasmine compatible test framework for postman test scripts (in less than 1KB minified)
(typeof tests!=='object')&&(tests={});var
it=((it=function(k,v){it.d.push(k);it.t[it.d]=1;it.b.forEach(it.c);try{v()}catch(e){it.t[it.d]=0;setTimeout&&setTimeout(function(){throw e;})}it.a.forEach(it.c);it.d.pop()}),
it.a=[],it.b=[],it.c=function(x){x()},it.d=[],it.d.toString=function(){return this.join(' ')},
it.t=tests,it.x=function(v){this.v=v},it.xp=it.x.prototype,it.xp.toBe=function(x){(this.v!==x)&&it._()},
it.xp.toNotBe=function(x){(this.v===x)&&it._()},it.xp.toEql=function(x){(this.v!=x)&&it._()},
it.xp.toNotEql=function(x){(this.v==x)&&it._()},it.xp.toBeOk=function(){!this.v&&it._()},
it.xp.toNotBeOk=function(){this.v&&it._()},it),describe=function(k,v){it.d.push(k);v();it.d.pop()},
expect=function(v){return new it.x(v)},beforeEach=it.b.push.bind(it.b),afterEach=it.a.push.bind(it.a);
@shamasis
shamasis / benchmark-unique-string-set-vs-obj.benchmark.js
Created January 7, 2018 10:11
Benchmark ES6 Set vs Object to filter unique strings from an array
const
_ = require('lodash'),
Benchmark = require('benchmark'),
suite = new Benchmark.Suite,
result = [],
seedSize = 10000,
seedData = Array(seedSize).fill().map(() => String(Math.round(Math.random() * seedSize)));
suite
@shamasis
shamasis / tail-slack.sh
Last active December 28, 2017 06:09
Tail a file and output to slack
#!/bin/bash
tail -n0 -F "$1" | while read LINE; do
(echo "$LINE" | grep -e "$3") && curl -X POST --silent --data-urlencode \
"payload={\"text\": \"$(echo $LINE | sed "s/\"/'/g")\"}" "$2";
done
@shamasis
shamasis / fake_nsp_package.js
Last active April 5, 2016 15:54
Creates a fake package.json for sending to NSP (adds ability to exclude packages using `exclusions: []` in .nsprc)
#!/usr/bin/env node
const
resolve = require('path').resolve,
loadJSON = function (file) {
return JSON.parse(require('fs').readFileSync(file).toString());
},
dependencySources = ['dependencies', 'devDependencies', 'optionalDependencies', 'peerDependencies',
@shamasis
shamasis / bunde_dependencies.js
Last active March 28, 2016 18:52
Converts package.json dependencies to bundled dependencies
#!/usr/bin/env node
/**
* Read a package.json file and then replace all dependencies and
* optionally dev dependencies into bundled dependencies.
*/
var p = '',
writeFile = require('fs').writeFile,
resolve = require('path').resolve;
@shamasis
shamasis / check-unix-directory.js
Last active December 30, 2015 08:39
Function to test whether a path has any hidden folder in it or whether a file is hidden or not using regular expressions and not using the file-system. I find it particularly useful to validate a path that is user-provided and does not actually exist on disk. For unix based systems only.
/**
* Tests whether a path is a directory or possibly a file reference.
*
* @param {string} path
* @returns {boolean}
*/
isUnixDirectory: function (path) {
return (/(^\.+$)|(\/$)/).test(path);
}
@shamasis
shamasis / directed-graph.js
Last active December 29, 2015 18:09
Directed graph data structure
var Graph = function () {
this.vertices = {};
this.edges = [];
this.length = 0;
};
Graph.Vertex = function (name, value) {
this.name = name;
this.edges = [];
this.value = value;
@shamasis
shamasis / check-unix-hidden-path.js
Last active December 29, 2015 17:29
Check whether a path has hidden directory or whether a file/directory is hidden.http://stackoverflow.com/questions/8905680/nodejs-check-for-hidden-files/
/**
* Checks whether a path starts with or contains a hidden file or a folder.
*
* @param {string} source - The path of the file that needs to be validated.
* returns {boolean} - `true` if the source is blacklisted and otherwise `false`.
*/
var isUnixHiddenPath = function (path) {
return (/(\/|^)\.[^\/\.]/g).test(path);
};
@shamasis
shamasis / act-usage
Created August 5, 2014 18:48
Get ACT Broadband usage information using Unix Terminal
echo -n "ACT Data Usage Info:";
# Fetch Url, locate line with quota and remove HTML tags
curl --silent --url "http://portal.acttv.in/index.php/mypackage" | grep 'Quota' | sed -e 's/<[^>]*>//g' -e 's/\&nbsp;/\ /g';
@shamasis
shamasis / SlackBootstrap.js
Last active August 29, 2015 21:13
SlackBootstrap for SailsJS
/**
* Bootstrap module for Slack notification on server startup
*
* Expects: config/slack.js
* - enabled
* - organisation
* - key
* - messages.spawn
* - payload
*/