Skip to content

Instantly share code, notes, and snippets.

View shamasis's full-sized avatar

Shamasis Bhattacharya shamasis

View GitHub Profile
@shamasis
shamasis / ServerStatsBootstrap.js
Created August 26, 2015 22:44
SailsJS bootstrap module to log package version
/**
* ServerStatsBootstrap.js
* Bootstrap module to log server version from package file
*/
module.exports = function (done) {
require('fs').readFile('package.json', function (err, data) {
// exit if error in reading file
if (err) {
sails.log.error('Unable to read package. %s', err);
return done();
@shamasis
shamasis / bootstrap.js
Last active August 26, 2015 22:47
Modular Bootstrap for SailsJS
/**
* Bootstrap
* (sails.config.bootstrap)
*
* An asynchronous bootstrap function that runs before your
* Sails app gets lifted.
*/
module.exports.bootstrap = function (callback) {
// load all modules *Bootstrap.js in bootstrap directory
// and execute async
@shamasis
shamasis / event-coordinate.js
Last active August 29, 2015 14:02
Cross-browser pageX and pageY of events
/**
* This function ensures that the event object always has pageX and pageY irrespective of the
* browser. Older IE browsers do not support pageXY.
*
* @param {MouseEvent} event
* @returns {MouseEvent} This function updates the events `pageX` and `pageY` properties when
* they're missing and also returns the same event for the sexy programming styles.
*/
var getEventCoordinate = (function () {
var PAGEX = 'pageX',
@shamasis
shamasis / paste-as-text.js
Created October 7, 2014 18:00
Library function to procure text form of clipboard data
var getClipboardDataAsText = (function () {
if (window.clipboardData && window.clipboardData.getData) { // Browsers with global handler
return function () {
return window.clipboardData.getData('Text') || E;
};
}
// For event specific paste supported browsers
return function (referenceEvent) {
return e.clipboardData && e.clipboardData.getData && e.clipboardData.getData('text/plain') || E;
};
@shamasis
shamasis / isarray.js
Last active August 29, 2015 14:08
Best JavaScript Array Detection Technique
/**
* Check whether an object is Array or not
* @type Boolean
* @param {object} subject is the variable that is
* tested for Array identity check
*/
var isArray = (function () {
// Use browser's own `isArray` when available
if (Array.isArray) {
return Array.isArray;
@shamasis
shamasis / guid-builder.js
Last active August 29, 2015 14:13
UUID (GUID) generation
/**
* Returns unique GUID on every call as per pseudo-number RFC4122 standards.
*
* @type {function}
* @returns {string}
*/
module.exports = (function() {
var E = '',
H = '-',
@shamasis
shamasis / quickargs.js
Last active August 29, 2015 14:17
Quick parsing of CLI arguments
/**
* @example
* var args = require('./quickargs.js')(process.argv);
*/
module.exports = function (argv) {
var args = {}, // object to store all key-value argument extraction
lastarg; // args are split by space, so we keep a track of last key detected
argv && argv.slice && argv.slice(2).forEach(function (item) {
lastarg = /^-/.test(item) ? item.slice(1) : (lastarg && (args[lastarg] = item), undefined);
@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
*/
@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 / 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);
};