Skip to content

Instantly share code, notes, and snippets.

View rvighne's full-sized avatar

Rohit Vighne rvighne

View GitHub Profile
@rvighne
rvighne / isWeekday.js
Created July 23, 2014 22:39
Checks whether a Date object represents a weekday, where a weekday is any of the days Monday to Friday.
Object.defineProperty(Date.prototype, 'isWeekday', {
value: function(UTC) {
var day = (UTC ? this.getUTCDay : this.getDay)();
if (day === 0 || day === 6)
return false;
else
return true;
},
writable: true, configurable: true
@rvighne
rvighne / deepFreeze.js
Last active August 29, 2015 14:04
Deep-freeze an object (freeze the object and all objects inside it). See the native Object.freeze, which only does a shallow freeze. It is safe against circular structures.
Object.deepFreeze = function deepFreeze(obj) {
this.freeze(obj)
for (var key in obj) {
var prop = obj[key]
if (obj.hasOwnProperty(key) && prop instanceof this && !this.isFrozen(prop))
this.deepFreeze(prop)
}
return obj
@rvighne
rvighne / favicanvas.js
Created August 3, 2014 19:26
Exposes a global canvas context that renders to the page's favicon. Works in all browsers supporting Canvas 2D.
/*
The MIT License (MIT)
Copyright (c) 2014 Rohit Vighne
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
@rvighne
rvighne / hasWebGL.js
Created August 4, 2014 20:18
Foolproof test whether the browser supports WebGL and is ready-to-use.
function hasWebGL () {
try {
return !!(WebGLRenderingContext && document.createElement('canvas').getContext('webgl'))
} catch (e) {
if (!(e instanceof TypeError || e instanceof ReferenceError))
throw e
}
}
@rvighne
rvighne / shaderUtils.js
Created August 5, 2014 21:21
WebGL shader program creation made easy.
/*
The MIT License (MIT)
Copyright (c) 2014 Rohit Vighne
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
@rvighne
rvighne / microop.js
Created August 18, 2014 05:49
Micro-framework that makes multiple inheritance easy. It works best using single inheritance, otherwise there will be a slight performance and stability cost. That is because it simulates MI using mixins.
/*
The MIT License (MIT)
Copyright (c) 2014 Rohit Vighne
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
@rvighne
rvighne / sumDigits.js
Last active August 29, 2015 14:05
Sum of the digits of a number. Fastest possible according to http://jsperf.com/sum-of-the-digits-of-a-number
function sumDigits(n) {
var sum = 0;
while (n !== 0) {
sum += n % 10;
n = Math.floor(n / 10);
}
return sum;
}
@rvighne
rvighne / miller-rabin.js
Created November 12, 2014 00:41
Miller-Rabin probabillistic primality test. You can trade off speed for more accuracy by raising the k argument.
function probablyPrime(n, k) {
// Always prime
if (n === 2 || n === 3)
return true;
// Never prime
if (n % 2 === 0 || n < 2)
return false;
// Write (n - 1) as 2^s * d
@rvighne
rvighne / RightTriangles.hpp
Created June 17, 2016 22:01
Given a perimeter, this function returns a vector of all the integer-sided right triangles that can be formed that have that perimeter. Inspired by https://projecteuler.net/problem=75
#pragma once
#include <cmath>
#include <vector>
struct Triangle {
int a, b, c;
};
inline int gcd(int a, int b)
@rvighne
rvighne / treeIterator.js
Created July 21, 2016 21:46
Wraps the TreeWalker interface into a generator function, for simpler and more idiomatic usage.
/*
SYNTAX:
Generator treeIterator(Node root, int whatToShow, bool function filter(Node))
- root: The parent node. It is not returned.
- whatToShow: Bitmask of the NodeFilter.SHOW_* static constants. Represents the types of nodes included.
- filter: Predicate returning whether the node given as the first argument should be included in the iteration.
Instead of a boolean, it may return a NodeFilter.FILTER_* static constant.
A return value of `true` is implicitly converted to `NodeFilter.FILTER_ACCEPT`,
and `false` becomes `NodeFilter.FILTER_REJECT`