Skip to content

Instantly share code, notes, and snippets.

@DigiTec
DigiTec / ClickThrough.js
Created September 22, 2013 21:30
In IE 11 every major browser now supports pointer-events, but until IE 11 gains wide adoption there will still be a large base of users who can't benefit from UI's which utilize this feature for "click through" UI's. In the meantime I figured I'd generate a list of click through mechanisms that vary based on properties used, nesting capabilities…
// Utilize visibility. This will cause layout to be invalid and have to be recomputed once the element is made visible again.
function _clickThroughVisNested(evt) {
var localStyle = evt.target.style;
var oldVisibility = localStyle.display;
localStyle.visibility = "hidden";
var target = document.elementFromPoint(evt.pageX, evt.pageY);
// Specific to my implementation. I want to only click through on certain types of elements.
if (target.classList.contains("achievement")) {
@DigiTec
DigiTec / RangeBasedForOnEnums.cxx
Created December 24, 2013 03:40
So I was thinking about range based for loops and I saw some partial code for one that worked on an enum. This adds more generic behavior to that enum and allows self selection of the start/end of the enumeration and ensures that the range you are using is increasing from first to last. Since this is pretty much a working piece of code and there…
// RangeBasedFor.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
template <typename T, T first = T::First, T last = T::Last>
class Enum
{
static_assert(first <= last, "foo");
public:
@DigiTec
DigiTec / PascalsMath.cpp
Created August 25, 2014 02:46
Computation for pascal's triangle values by calculating along the row so I don't lose it in the future. This is susceptible to round-off errors because we multiply before a divide when the triangle gets very large.
// Assumes row/col are 1 indexed
int PascalsMath(int row, int col)
{
// Enable mirroring about the central pivot of the row
col = min(col, row - col + 1);
int value = 1;
int numerator = row - 1;
int denominator = 1;
@DigiTec
DigiTec / ChampagneTower.cpp
Last active August 29, 2015 14:05
This uses similar features to Pascals Triangle to solve the champagne tower problem. Since I found two variants of the problem, I've solved both. The first variant gives the row and the column so no decomposition is necessary for the inputs. The second variant assigns cup numbers starting from 1 and assigning from top to bottom left to right as …
double ChampagneTower(double water, double capacity, int row, int col);
double ChampagneTower(double water, double capacity, int cup)
{
// The cup id is from left to right. We want the row and column in the triangular
// array which is basically the triangular root and our remainder.
int triangular_root = ceil((sqrt(8*cup+1)-1)/2);
int column = cup - ((triangular_root*(triangular_root-1))/2);
return ChampagneTower(water, capacity, triangular_root, column);
}
@DigiTec
DigiTec / BitOps.cpp
Created August 25, 2014 06:51
A bunch of bit ops where you need some "magic numbers" now logged in my gists for posterity.
bool PowerOf2(unsigned long value)
{
return (value != 0) && !(value & (value-1));
}
int BitCount(unsigned long value)
{
value = (value & 0x55555555) + ((value & 0xAAAAAAAA) >> 1);
value = (value & 0x33333333) + ((value & 0xCCCCCCCC) >> 2);
value = (value & 0x0F0F0F0F) + ((value & 0xF0F0F0F0) >> 4);
@DigiTec
DigiTec / FilterEventTargets.js
Last active August 29, 2015 14:07
This enumerates the window to find types that inherit from EventTarget.
var eventTargetObjects = Object.getOwnPropertyNames(window).filter(function prop(name) {
// This block of code identifies constructors with high confidence using casing, enumerability and prototype property presence
return (name[0] === name[0].toUpperCase() && !this.propertyIsEnumerable(name) && this[name].prototype !== undefined);
}, window).filter(function prop(name) {
// This block of code determines if a constructor is like an EventTarget
var currentPrototype = this[name].prototype;
while (currentPrototype !== null) {
if (currentPrototype === EventTarget.prototype) {
return true;
}
@DigiTec
DigiTec / config
Created December 2, 2014 08:01
Node.js filtering symbol server to avoid unnecessary symbol queries when debugging on slow connections.
var config = {
forward_server: 'msdl.microsoft.com',
forward_path: '/download/symbols',
forward_port: 80,
allow_list: [
/\/ntdll.pdb\//
],
};
module.exports = config;
@DigiTec
DigiTec / scraper.js
Last active March 14, 2016 08:11
Personal sample of using Node.js for processing web page contents using request and cheerio
var request = require('request');
var cheerio = require('cheerio');
var urls = [
'https://developer.android.com/reference/com/google/android/gms/location/Geofence.html',
'http://developer.android.com/reference/com/google/android/gms/maps/model/LatLng.html'
];
urls.forEach(function (elem) {
request({
@DigiTec
DigiTec / WebIDLLexer.pegjs
Last active August 29, 2015 14:13
A complete WebIDLLexer (without tests) which is compliant with the lexer rules for WebIDL Second Edition available here http://heycam.github.io/webidl/
{
function token() {
this.value = '';
this.type = 'error';
}
// Static methods on token
Object.defineProperties(token, {
createOther: {
value: function createOther(val) {
@DigiTec
DigiTec / img_src_override.js
Created February 1, 2015 00:27
ES 5 Configurable Properties
// http://jsfiddle.net/s4ex1dtj/
"use strict";
var myImage = new Image();
Object.defineProperty(myImage, "src", {
get: function () { return 'foo'; },
set: function (val) { alert(val); } });
console.log(myImage.src); // Logs value 'foo'
myImage.src = "new"; // alerts value 'new'
// newImage does not have a redefined setter since our previous property