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 / Array.prototype.search.js
Created May 16, 2012 10:14
A way to use some returning the very first matched object
// @license WTFPL
Array.prototype.search = function (some) {
return function search(cb, ctx) {
var result = {f:cb, c:ctx, r:result};
this.some(some, result);
return result.r;
};
}(function some(v, i, self) {
if (this.f.call(this.c, v, i, self)) {
this.r = v;
@WebReflection
WebReflection / Queue.js
Created June 1, 2012 08:30
All you need to manage queues
function Queue(q) {"use strict";
// (C) WebReflection - Mit Style License
var
next = function next() {
return (callback = q.shift()) ? !!callback(q) || !0 : !1;
},
callback
;
(q.wait = function wait(condition, delay) {
condition || callback && q.unshift(callback);
@WebReflection
WebReflection / process.nextTick.js
Created June 19, 2012 10:57
process.nextTick(callback) for browsers too
!function (window) {"use strict";
// by WebReflection - WTFPL License
var
prefixes = "r webkitR mozR msR oR".split(" "),
process = "process",
nextTick = "nextTick",
i = 0,
p = window[process] || (window[process] = {})
;
while (!p[nextTick] && i < prefixes.length)
@WebReflection
WebReflection / Object.extra.js
Created August 16, 2012 20:42
Object.getPropertyDescriptor and Object.getPropertyNames
!function(Object, getPropertyDescriptor, getPropertyNames){
// (C) WebReflection - Mit Style License
if (!(getPropertyDescriptor in Object)) {
var getOwnPropertyDescriptor = Object.getOwnPropertyDescriptor;
Object[getPropertyDescriptor] = function getPropertyDescriptor(o, name) {
var proto = o, descriptor;
while (proto && !(
descriptor = getOwnPropertyDescriptor(proto, name))
) proto = proto.__proto__;
return descriptor;
@WebReflection
WebReflection / Function.prototype.asImplicitCall.js
Created November 15, 2012 01:47
asImplicitCall() to avoid usage of `fn.call()` everywhere
/*! (C) WebReflection - Mit Style License */
(function(FunctionPrototype){
var
bind = FunctionPrototype.bind || function () {
var callback = this;
return function bind() {
return callback.apply(this, arguments);
};
},
call = FunctionPrototype.call
@WebReflection
WebReflection / gist:4163041
Created November 28, 2012 18:22
Cross ECMAScript way to know if an object is an Arguments one. IE5+ and every other old or modern engine should simply work.
var isArguments = function(toString){
// (C) WebReflection - Mit Style License
var compare = toString.call(arguments);
return ~compare.indexOf("Arguments") ?
function isArguments(object) {
return toString.call(object) == compare;
} :
(compare = toString.call([])) &&
function isArguments(object) {
try {
function loop(e) {
if (e.type == 'mousedown') loop.move = true;
if (e.type == 'mouseup') loop.move = false;
if (loop.move) move(e);
}
$('#box').mousedown(loop);
$(window).mousemove(loop)
.mouseup(loop);
@WebReflection
WebReflection / gist:4331070
Created December 18, 2012 19:23
another example of yield possibilities
var loop = coroutine(function loop(e) {
// could be outer scope moving variable too
// so that woul dbe shared within generators
loop.moving = 0;
while (yield e) {
if (e.type == 'mousedown') {
loop.moving++;
while (e = yield) {
if (e.type == 'mousemove')
move(e);
@WebReflection
WebReflection / inherit.js
Last active October 16, 2018 16:26
a meaningful shim limited to the first argument only of `Object.create()`, able to create empty dictionaries too such `inherit(null)` even in older browsers, down to IE5, without `__proto__` and/or without ES5 `Object.create()` support.
/*!(C) WebReflection *//** @license Mit Style */
// inspired by https://gist.github.com/4395291
this.inherit || (this.inherit = function(create){
if (!create) {
if ({__proto__:null} instanceof Object) {
for (var
Null = function Null() {},
doc = document,
html = doc.documentElement,
iframe = html.insertBefore(
@WebReflection
WebReflection / forIn.js
Created December 29, 2012 06:24
an Array#forEach like function to loop over object enumerable properties as for/in does. If you don't need to support IE < 9 you probably don't need this function too ;)
this.forIn = function (){
var
propertyIsEnumerable = "propertyIsEnumerable",
toString = "toString",
OK = {toString:1}[propertyIsEnumerable](toString),
keys = OK || [
propertyIsEnumerable,
toString,
"hasOwnProperty",
"isPrototypeOf",