Skip to content

Instantly share code, notes, and snippets.

View shamasis's full-sized avatar

Shamasis Bhattacharya shamasis

View GitHub Profile
@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 / 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-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 / 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 / event-layer-coordinates.js
Created June 7, 2014 20:40
Cross browser layerX and layerY in JavaScript
/**
* Get layerX and layerY of an event across all browsers without
* using the deprecated layerX of webkit.
* It stores `targetX` and `targetY` in the event, to act like `layerY`
* and `layerY` respectively.
*/
getElementPosition = (function () {
var body = window.document.body || window.document.documentElement;
return function (event) {
@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 / 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 / alloc-assoc-elastic-ip.sh
Created December 21, 2014 09:41
Create and allocate AWS Elastic IP at one go using AWS CLI. Replace `$1` with your Instance Id or simply execute this file as a script and pass the Instance Id as the first argument to the script.
aws ec2 associate-address --instance-id $1 --public-ip $(aws ec2 allocate-address --output text --query 'PublicIp')
@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 = '-',