Skip to content

Instantly share code, notes, and snippets.

@heroqu
heroqu / int32ToBytes.js
Created August 25, 2017 10:53
function to convert javascript integer to an array of 4 bytes
function int32ToBytes (int) {
return [
int & 0xff,
(int >> 8) & 0xff,
(int >> 16) & 0xff,
(int >> 24) & 0xff
]
}
// 3 (base 10) = 00000000000000000000000000000011 (base 2)
@heroqu
heroqu / custom_error.js
Created July 11, 2016 10:34
Making custom error type in Javascript
// Making custom error types
// Taken from the comment at https://blog.getify.com/howto-custom-error-types-in-javascript/
//
function makeErrorType() {
var it = function (message) {
if (!(this instanceof it)) {
return new it(message);
}
this.message = message;
this.stack = new Error(message).stack;
@heroqu
heroqu / es6_fibo.js
Last active July 9, 2016 11:12
Yet another ES6 Fibonacci generator
'use strict'
function* fibo() {
let [a,b] = [0,1];
while (true) {
yield b;
[a, b] = [b, a + b];
}
}
@heroqu
heroqu / flat_numeric_arr.js
Last active June 7, 2016 16:28
flatten an array of arbitrarily nested arrays of integers into a flat array of integers
'use strict'
/**
* flatten an array of arbitrarily nested arrays of integers into a flat array of integers
*/
function flat_numeric_arr(x, res) {
res = res || [];
if (typeof x === 'number') {
res.push(x);
} else if (Array.isArray(x)) {
@heroqu
heroqu / today.js
Created May 16, 2016 09:57
Get today date without time part (beginning of the current day)
function today() {
return new Date((new Date()).setHours(0, 0, 0, 0));
}
@heroqu
heroqu / add_months.js
Created May 16, 2016 09:51
Add / subtract number of months from given date (original date in param stays immutable).
function addMonths(date, months) {
var d = new Date(date.getTime());
var m = d.getMonth();
var y = d.getFullYear();
var ym = y * 12 + m;
ym = ym + months;
m = ym % 12;
y = (ym - m) / 12;
d.setYear(y);
d.setMonth(m);
@heroqu
heroqu / kill_node.sh
Last active April 11, 2016 09:27
Bash function (for OSX) to kill all running node processes, except for Atom editor
# kill all running node processes, except for Atom editor
function kinod {
read -r -p "Delete all node processes, except for Atom? [y/N] " response
case $response in
[yY][eE][sS]|[yY])
kill -9 `ps aux | grep node | grep -v grep | grep -v 'Atom.app' | awk '{print $2}'`
;;
*)
echo -- cancel --
;;
@heroqu
heroqu / permutations.rb
Created February 27, 2016 19:27
Print out all unique permutations of first N natural numbers
def permutations(n)
_p n-1, (1..n).to_a, ''
end
def _p(n, arr, res)
if n == -1
puts res
return
end
(0..n).each do |m|
@heroqu
heroqu / vbga_install.sh
Created February 11, 2016 02:10
VirtualBoxGuestAddition install/upgrade for Mac OSX
#! /bin/bash
# VirtualBoxGuestAddition install/upgrade for Mac OSX
#
# VirtualBox should be installed prior to running this script
#
if [ "$EUID" -ne 0 ]; then
echo "Please run as root"
exit
fi
@heroqu
heroqu / github_repo_bulk_delete.sh
Created January 6, 2016 18:03
Bash script for bulk deletion of repos under your github account
#! /bin/bash
## Based on and inspired by
# https://medium.com/@icanhazedit/clean-up-unused-github-rpositories-c2549294ee45
## Vars
# 1. Your github user name
USERNAME=john