Skip to content

Instantly share code, notes, and snippets.

View WebReflection's full-sized avatar
🎯
Focusing

Andrea Giammarchi WebReflection

🎯
Focusing
View GitHub Profile
@WebReflection
WebReflection / Function.prototype.notifier.js
Created November 7, 2011 18:08
A Function.prototype method handy to monitor "functions lifecycle"
Function.prototype.notifier = (function () {"use strict";
// (C) WebReflection - Mit Style License
function create(callback) {
function notifier() {
var args = [].slice.call(arguments), output;
if (fire(notifier, "before", callback, this, args, null)) {
try {
output = callback.apply(this, args);
} catch(e) {
fire(notifier, "error", callback, this, args, e);
@WebReflection
WebReflection / Function.prototype.notifier.html
Created November 8, 2011 22:33
Full test coverage for Function.prototype.notifier.js
<!doctype html>
<html>
<head>
<title>Function.prototype.notifier</title>
<!-- https://github.com/WebReflection/wru -->
<script src="callback.js"></script>
<script>function wru(wru){var assert=wru.assert,async=wru.async;
// enojy your tests!
@WebReflection
WebReflection / ienewfeatures_usethem.js
Created November 29, 2011 20:11
Some thoughts on MS code used "to impress" with modern demos ...
// I know it's for demo purpose from MS:
// http://ie.microsoft.com/testdrive/HTML5/TypedArrays/js/binaryReader.js
BinaryReader.prototype = {
readUint8: function () { var result = this.dataView.getUint8(this.streamPosition, true); this._movePointerTo(this.streamPosition + 1); return result; },
readInt8: function () { var result = this.dataView.getInt8(this.streamPosition, true); this._movePointerTo(this.streamPosition + 1); return result; },
readUint16: function () { var result = this.dataView.getUint16(this.streamPosition, true); this._movePointerTo(this.streamPosition + 2); return result; },
readInt16: function () { var result = this.dataView.getInt16(this.streamPosition, true); this._movePointerTo(this.streamPosition + 2); return result; },
readUint32: function () { var result = this.dataView.getUint32(this.streamPosition, true); this._movePointerTo(this.streamPosition + 4); return result; },
readInt32: function () { var result = this.dataView.getInt32(this.s
@WebReflection
WebReflection / Object.extra.js
Created November 30, 2011 19:26
Array extras applied to Objects
(function (Object) {
// (C) WebReflection - Mit Style License
// @wtf? Array extras applied to Objects
function OPmap(key, i, arr) {
this.n[key] = invoke(this, key);
}
function OPfilter(key, i, arr) {
invoke(this, key) && (this.n[key] = this.s[key]);
}
function define(key, callback) {
@WebReflection
WebReflection / Object.prototype.boundTo.js
Created January 6, 2012 10:16
proposal to bind once object as callback context pairs
!function (Object) {
// (C) WebReflection - Mit Style License
var // private scope shortcuts
BOUND_TO = "boundTo", // or maybe "asContextOf" ?
// ad-hoc partial private shims for "not there yet" ES5 browsers
indexOf = [].indexOf || function indexOf(value) {
@WebReflection
WebReflection / WeakMap.test.html
Created January 6, 2012 18:51
100% Test Coverage for WeakMap.js
<!doctype html>
<html>
<head>
<title>WeakMap</title>
<!-- https://github.com/WebReflection/wru -->
<script src="WeakMap.js">/*
https://gist.github.com/1571878
*/</script>
<script>function wru(wru){var assert=wru.assert,async=wru.async;
@WebReflection
WebReflection / mutations.js
Created January 13, 2012 07:09
DOM MutationEvent Feature Detection
var mutations = (function (document) {
// (C) WebReflection - Mit Style License
var
type = [
"DOMSubtreeModified",
"DOMNodeInserted",
"DOMNodeRemoved",
"DOMNodeRemovedFromDocument",
"DOMNodeInsertedIntoDocument",
"DOMAttrModified",
@WebReflection
WebReflection / JSON.stringify
Created January 30, 2012 21:09
Fix incorrect behavior of \u2028 and \u2029 characters in JSON.stringify
// @see http://www.thespanner.co.uk/2011/07/25/the-json-specification-is-now-wrong/
// fix it via RegExp and feature detection
if (JSON.stringify("\u2028") == '"\u2028"') {
JSON.stringify = function (stringify) {
function place(m) {
return "\\u202" + (m == "\u2028" ? "8" : "9");
}
var re = /\u2028|\u2029/g;
return function fixed(data) {
return stringify(data).replace(re, place);
@WebReflection
WebReflection / each.js
Created April 3, 2012 19:28
all you need to `each(obj, cb [,context])`
var each = function (Array) {"use strict";
/*! all you need to `each(obj, cb [,context])`
* @author WebReflection
* @license WTFPL - http://en.wikipedia.org/wiki/WTFPL
* @gist https://gist.github.com/2294934
*/
var
toString = {}.toString,
hasOwnProperty = {}.hasOwnProperty,
array = toString.call([]),
@WebReflection
WebReflection / whatever.js
Last active October 3, 2015 00:57
random stuff
function ifClickedElsewhere(el, callback) {
// add prioritized 'click' listener
document.addEventListener(
'click',
function onClick(e) {
var parentNode = e.target;
// until we reach the current node or no node at all
while (parentNode && parentNode !== el) {
// grab the parent
parentNode = parentNode.parentNode;