Skip to content

Instantly share code, notes, and snippets.

View WebReflection's full-sized avatar

Andrea Giammarchi WebReflection

View GitHub Profile
WebReflection / scale.fix.js
Last active August 29, 2015 13:55
a better version of github pages file included in all templates since it's currently leaking variables and creating a disturbing function declaration for all browsers.
if (/iP(?:od|hone)/.test(navigator.userAgent)) {
// deletable at least, i.e.
// if (window.gestureStart && dontWantIt) {
// document.removeEventListener("gesturestart", gestureStart);
// delete window.gestureStart;
// }
this.gestureStart = (function(metas, i){
function gestureStart() {
// address .length once per loop
for (i = metas.length; i--;) {
WebReflection / ScrollHandler.js
Last active August 29, 2015 13:56
provides basics to handle touch actions on scrollable content
var ScrollHandler = (function(Math, UA){
// (C) 2014 - Andrea Giammarchi
ONE_EIGHTY_PI = 180 / Math.PI,
atan2 = Math.atan2,
round = Math.round,
sqrt = Math.sqrt,
WebReflection /
Last active August 29, 2015 13:56
DOM NamedStorage Interface Proposal with polyfills for ES3, ES5, and ES6

DOM NamedStorage Interface Proposal

Multiple scripts and multiple libraries have usually a common need of the W3C Web Storage, either the local or the session one.

However, it is extremely easy and very hard to prevent a not so infrequent storage.clear() call, able to remove any sort of data and for any kind of lbirary in the same realm.

A Possible Solution

This proposal aim is to harmonize multiple libraries preserving same interface but enriching the constructor.

Please note that current WebIDL is almost identical to the one already recommended by W3C.

WebReflection / createEmitter.js
Created March 5, 2014 23:39
Simplified EventEmitter like objects
function createEmitter() {
// not ideal when many simple emitters are needed
// returns object similar to node.js EventEmitter API
// not compatible with DOM nodes, only JS controllers
// on, off, emit as immutable and non enumerable
// all methods returns the implicitly bound emitter
emitter = Object.create(null, {
on: {value: function (type, handler) {
var tmp = get(type);
WebReflection /
Last active August 29, 2015 13:57
used to generate an overview of a generic JavaScript environment

Generates a Markdown friendly output similar to the one showed here.

(function show(object, name, all) {
  var f, k, later = [], list = ['### ' + name, '```javascript'];
  Object.getOwnPropertyNames(object).forEach(function(name) {
    if (/^[A-Z]/.test(name) && object[name] instanceof Object) {
 later.push([object, name]);
WebReflection /
Created March 18, 2014 20:41
node environment


WebReflection /
Last active August 29, 2015 13:57
some example about define-strict-properties -

All code assumes define strict properties in included upfront.

However, being the proposal fullyt backward compatible, you can tests these snipets by copy and paste but no type will be guarded.

Point2D Class

function Point2D(x, y) {
  this.x = x;
  this.y = y;
WebReflection /
Last active August 29, 2015 13:57
because handleEvent is the most powerful way to deal with DOM events or states (and not only DOM)


This snippet is now improved and officially an npm module called dom-handler

WebReflection / O.js
Last active August 29, 2015 13:57
if the only reason you like __proto__ is its literal integration, just use this and make the web better today. No credits needed
// ES5 dirty perf oriented tweet size
var O=function(O,s){s=O.setPrototypeOf||function(o,p){o.__proto__=p;return o};return function(p,o){return o?s(o,p):p||s({},p)}}(Object);
// ES5 dirty performance way
var O = function(Object){
create = Object.create,
set = Object.setPrototypeOf || function(o, p) {
o.__proto__ = p;
return o;
WebReflection /
Last active August 29, 2015 14:01
ToPropertyDescriptor ( Obj )

proposal identical to current ES5-6 one except [[HasOwnProperty]] is used instead


When the abstract operation ToPropertyDescriptor is called with object Obj, the following steps are taken:

  1. ReturnIfAbrupt(Obj).
  2. If Type(Obj) is not Object throw a TypeError exception.
  3. Let desc be a new Property Descriptor that initially has no fields.
  4. If HasOwnProperty(Obj, "enumerable") is true, then
    1. Let enum be Get(Obj, "enumerable").
  5. [Retur