Skip to content

Instantly share code, notes, and snippets.

View devm33's full-sized avatar

Devraj Mehta devm33

View GitHub Profile
@devm33
devm33 / rand_string.node.js
Last active March 3, 2022 09:00
Generate Random Hex String in NodeJS
function rand_string(n) {
if (n <= 0) {
return '';
}
var rs = '';
try {
rs = crypto.randomBytes(Math.ceil(n/2)).toString('hex').slice(0,n);
/* note: could do this non-blocking, but still might fail */
}
catch(ex) {
@devm33
devm33 / date_local.js
Created March 9, 2014 09:27
Puts the local time date in a format the W3 is happy with. Disclaimer, I was not made any happier by writing this.
var zeroPad = function(str) {
return ('0'+ str).slice(-2);
};
var dateLocal = function() {
var d = new Date();
var s = d.getFullYear() + '-';
s += zeroPad(d.getMonth()) + '-';
s += zeroPad(d.getDate()) + 'T';
s += zeroPad(d.getHours()) + ':';
s += zeroPad(d.getMinutes()) + ':';
@devm33
devm33 / backup_home.sh
Last active December 19, 2020 04:11
Bash script to backup home directories using rsync
#!/usr/bin/env bash
# Script to backup the home directory to an external hardrive mounted at /media/backup
# Uses rsync, dpkg, and mail (for error logging)
SRC='/home/*'
DEST='/media/backup'
RSYNC_OPTS='-haAXuv --delete'
EXCLUDE='*/.cache/* */.thumbnails/* */.config/google-* lost+found .gvfs'
EXCLUDE="$(echo $EXCLUDE | sed 's/\(\S\+\)/ --exclude \1/g')"
@devm33
devm33 / shell_well.sh
Last active October 10, 2015 22:12
Doing things well in shell
# Display summarized (s) human-readable (both h) sizes of sub-directories sorted descending
# - remove / to include files in that folder
# - remove r to sort ascending by size
du -sh */ | sort -hr
# Remove broken symlinks (leverages zsh)
rm -- *(-@D)
@devm33
devm33 / local.js
Last active August 29, 2015 13:58
Quickly serve a static folder using express
/* Set up a local testing server to serve a static folder */
var express = require("express");
express().use(express.static(__dirname)).listen(5000, function(){
console.log('http://0.0.0.0:5000 listening...');
});
/*
* robotMaze.js
*
* The blue key is inside a labyrinth, and extracting
* it will not be easy.
*
* It's a good thing that you're a AI expert, or
* we would have to leave empty-handed.
*/
@devm33
devm33 / reverse_polish.js
Created May 14, 2014 04:31
Reverse Polish Notation Calculator
function calc(expr) {
return expr.split(/\s+/).reduce(function(stack, current){
var a, b, c;
if(/[+\-*\/]/.test(current)){
b = stack.pop();
a = stack.pop();
switch(current) {
case '+': c = a + b; break;
case '-': c = a - b; break;
case '*': c = a * b; break;
@devm33
devm33 / exmachina.py
Created August 31, 2015 04:42
code displayed in ex machina
def sieve(n):
x = [1]*n
x[1] = 0
for i in range(2, n/2):
j = 2*i
while j<n:
x[j] = 0
j = j+i
return x
@devm33
devm33 / dynAttrs.js
Created September 13, 2015 16:55
angular directive to add/remove attributes like ngClass
angular.module('dynAttrs', [])
.directive('dynAttrs', () => {
return {
link: function(scope, element, attrs) {
scope.$watch(attrs.dynAttrs, (dynAttrs) => {
_.each(dynAttrs, (val, attr) => {
if(val) {
element.attr(attr, val);
} else {
element.removeAttr(attr);

Keybase proof

I hereby claim:

  • I am devm33 on github.
  • I am devm33 (https://keybase.io/devm33) on keybase.
  • I have a public key whose fingerprint is FB74 6ADC B899 CB2B E4C3 A712 E385 2F90 A94F A38B

To claim this, I am signing this object: