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 / promote.js
Created July 18, 2013 03:57
The most obtrusive and dark trick to promote objects inheritance without actually loosing the current one if not at the deepest level. It does not pollute the `Object.prototype`
var promote = function(getPrototypeOf, setPrototypeOf){
// (C) The Evilness
return function(self, Class){
var
promote = typeof Class == 'function' ?
Class.prototype : getPrototypeOf(Class),
current = self,
previous,
next
;
@WebReflection
WebReflection / find-or-fallback.js
Created July 17, 2013 06:19
a very simple and cross platform way to grab some experimental function
function findOrFallback(where, what, fallback) {
for(var
vendors = ['', 'webkit', 'moz', 'ms', 'o'],
first = what.charAt(0),
others = first.toUpperCase(),
suffix = what.slice(1),
i = 0, length = vendors.length,
current;
i < length; i++
) {
@WebReflection
WebReflection / umd.js
Created July 15, 2013 18:34
UMD does not really have to be transpiled per each module ... or does it?
function UMD(name, dependencies, factory) {
// AMD
if ('function' === typeof define && define.AMD) {
define(name, dependencies, factory);
}
// node.js
else if('object' === typeof exports) {
module.exports = factory.apply(
null,
(dependencies || []).map(require)
@WebReflection
WebReflection / WeakMap.js
Last active December 30, 2022 13:52
An alternative, less memory greedy, more robust WeakMap polyfill.
var WeakMap = WeakMap || (function (Object) {
/*! (C) Andrea Giammarchi - Mit Style License */
var
dP = Object.defineProperty,
hOP = Object.hasOwnProperty,
WeakMapPrototype = WeakMap.prototype,
i = 0
;
@WebReflection
WebReflection / gist:5675201
Last active December 17, 2015 21:29
knows if the browser supports already a font family
// example: if(!isFontSupported('Batang')) { ... }
// example: if(!isFontSupported('"Andale Mono"')) { ... }
var isFontSupported = (function(document){
// WebReflection - WTFPL License
var
defaultFamily = getComputedStyle(
document.documentElement, null
).getPropertyValue('font-family'),
defaultDataURL = '',
str = ['0123456789', 'abcdefghijklmnopqrstuvwxyz', defaultDataURL],
@WebReflection
WebReflection / Object.setPrototypeOf.js
Last active February 24, 2018 04:13
Object.setPrototypeOf(O, proto) full/complete polyfill with boolean flag for partial implementation.
/*jslint devel: true, indent: 2 */
// 15.2.3.2
if (!Object.setPrototypeOf) {
Object.setPrototypeOf = (function(Object, magic) {
'use strict';
var set;
function checkArgs(O, proto) {
if (typeof O !== 'object' || O === null) {
throw new TypeError('can not set prototype on a non-object');
}
@WebReflection
WebReflection / gist:5450375
Created April 24, 2013 07:45
Allwinner A10 sd card formatter
#!/bin/bash
# Usage ./a10.sh /dev/sdx hwpack bindfolder
hwpack_update_only=0
BOOT_SIZE=64
TEMP="${TMPDIR:-/tmp}/.a10.$$"
HWPACKDIR="$TEMP"
ROOTFSDIR="$TEMP/rootfs"
@WebReflection
WebReflection / gist:5379375
Created April 13, 2013 17:52
just a gist
alert('all your gists are belong to us');
@WebReflection
WebReflection / __proto__.js
Last active August 18, 2016 16:41
just __proto__ and all shenanigans it can bring to any environment
// all IE < 11 browsers:
// there is no __proto__
if (!('__proto__' in {})) {
console.log('you gonna have hard time');
// we need extra logic to be able to work
// as meant in IE too. IE9 ain't disappearing
// any time soon in both desktop and mobile
// neither will IE10
}
// all Mobile WebKit browsers:
@WebReflection
WebReflection / gist:5341317
Last active December 15, 2015 23:38
On JS interfaces and a possible `Object.prototype.implements()` method
Object.defineProperty(
Object.prototype,
'implements',
{
enumerable: false,
configurable: true,
writable: true,
value: function (target) {
return Object.getOwnPropertyNames(target).every(function(p){
var current = this,