Skip to content

Instantly share code, notes, and snippets.

View megawac's full-sized avatar

Graeme Yeates megawac

  • Clearpath Robotics
  • Waterloo, Ontario
View GitHub Profile
/**
* Assign a value to the delimited key in the given object. The inverse of `_.lookup`
*
* @example
*
* var myObj = {};
*
* _.assign(myObj, 'foo.bar', 'baz'); // myObj = { foo: { bar: 'baz' }}
*
* @param {Object} obj the object to assign to
@megawac
megawac / css-calc-polyfill.js
Last active December 22, 2015 20:18
Mootools css calc polyfill. Only for relatively simple cases. Usage: util.calc($$('div')[8], 'width', '70% - 40px'); This will yield a cross browser calc. It uses native if it should be supported. Only supporting a few units for now.
//http://caniuse.com/#feat=calc
//union bool str (-webkit-calc, -moz-calc, calc)
//alternatively see last version where i did a version check based on can i use
document.addEvent("domready", function() {//based off https://gist.github.com/Rob-ot/3399053
Browser.Features.calc = false;//union bool str (-webkit-calc, -moz-calc, calc)
["","-webkit-","-moz-","-o-"].some(function(prefix) {
var $el = new Element('div', {
styles: {
width: prefix + "calc(5px)"
}
@megawac
megawac / jsbin.uhoVibU.html
Created December 31, 2013 02:51
childList and subtree tests
<!DOCTYPE html>
<html>
<head>
<link href="//code.jquery.com/qunit/qunit-git.css" rel="stylesheet" type="text/css" />
<script src="//cdn.jsdelivr.net/es5.shim/2.1.0/es5-shim.min.js"></script>
<script src="//code.jquery.com/jquery.min.js"></script>
<script src="//cdn.jsdelivr.net/curl.js/0.7.5/jquery/curl.js"></script>
<script src="//code.jquery.com/qunit/qunit-git.js"></script>
<script src="//www.broofa.com/Tools/JSLitmus/JSLitmus.js"></script>
@megawac
megawac / gist:8201012
Last active January 1, 2016 20:59
Saving the state of an element and its children.

Problem

A problem I ran into implementing my MutationObserver shimmed interface is being able to save the state of an elements children for later use. Naively I started by just using $oldnode = $node.cloneNode(true) which would give me an atomic node at a previous state. The problem with using cloneNode is when it comes time to compare. Any children in $oldnode will be !== to the same child in $node.

I addressed this issue by assigning a unique id to all elements and checking if the unique id of an old node is the same as from an earlier state. This is ugly and noone wants me adding my own unique id property on their elements.

var counter = 0;
var getId = function($ele) {
 var id = $ele.nodeType === 3 ? $ele.nodeValue ://text node id is the text content
@megawac
megawac / gist:8355978
Last active January 2, 2016 20:19
IE textNode throws error

So far I've tested this in

IE 10 win8
IE 9 win7
IE 8 emulated by IE9 win7

Consider the following:

var x = document.createElement('div');
@megawac
megawac / Log-.md
Last active August 29, 2015 13:56 — forked from bgrins/Log-.md

Javascript log Function

Every time I start a new project, I want to pull in a log function that allows the same functionality as the console.log, including the full functionality of the Console API.

There are a lot of ways to do this, but many are lacking. A common problem with wrapper functions is that the line number that shows up next to the log is the line number of the log function itself, not where log was invoked. There are also times where the arguments get logged in a way that isn't quite the same as the native function.

This is an attempt to once and for all document the function that I pull in to new projects. There are two different options:

  • The full version: Inspired by the plugin in HTML5 Boilerplate. Use this if you are writing an application and want to create a window.log function. Additionally,
<!DOCTYPE html>
<html>
<head>
<link href="//cdn.jsdelivr.net/qunit/1.14.0/qunit.css" rel="stylesheet" type="text/css" />
<script src="//cdn.jsdelivr.net/qunit/1.14.0/qunit.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
</head>
<body>
<div id="qunit"></div>
function sortedIndex(array, value, iterator, pivot) {
var low = 0,
high = array.length,
target = iterator(value);
while (low - pivot < high) {
var mid = (((low + high) >>> 1) + pivot) % array.length;
if (iterator(array[mid]) < target) low = mid + 1;
else high = mid;
}
return low;
@megawac
megawac / ci.sh
Created February 7, 2015 18:20
marionette-travis script
#!/bin/bash
npm install -g grunt-cli
npm install
npm dedupe
if [[ -n "$UNDERSCORE" ]]
then
npm install underscore@"$UNDERSCORE"
fi
@megawac
megawac / .travis.yml
Created May 5, 2015 19:48
NIM travis.yml file
language: C
compiler:
- gcc
before_install:
# Install nim
- git clone -b master git://github.com/Araq/Nim.git --depth 1
- cd Nim
- git clone -b master --depth 1 git://github.com/nim-lang/csources
- cd csources && sh build.sh
- cd ..