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 / 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
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
// https://gist.github.com/WebReflection/8924001
var
MINIMUM_SCROLL_DISTANCE = 8,
ONE_EIGHTY_PI = 180 / Math.PI,
atan2 = Math.atan2,
round = Math.round,
sqrt = Math.sqrt,
@WebReflection
WebReflection / NamedStorage.md
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
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
var
// 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
WebReflection / JS2.md
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'];
  all.push(object);
  Object.getOwnPropertyNames(object).forEach(function(name) {
    list.push(name);
    if (/^[A-Z]/.test(name) && object[name] instanceof Object) {
 later.push([object, name]);
@WebReflection
WebReflection / node.md
Created March 18, 2014 20:41
node environment

global

decodeURI
Float64Array
URIError
clearTimeout
setInterval
RangeError
escape
RegExp
@WebReflection
WebReflection / examples.md
Last active August 29, 2015 13:57
some example about define-strict-properties - https://github.com/WebReflection/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
WebReflection / handleEvent.md
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)

Update

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

@WebReflection
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){
var
create = Object.create,
set = Object.setPrototypeOf || function(o, p) {
o.__proto__ = p;
return o;
@WebReflection
WebReflection / ToPropertyDescriptor.md
Last active August 29, 2015 14:01
ToPropertyDescriptor ( Obj )

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

ToPropertyDescriptor

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