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 / Object.prototype.on.js
Created October 15, 2011 16:25
Transform any sort of object into Event emitter/dispatcher
(function () {
// Andrea Giammarchi - Mit Style License
// note: it's not production ready
function dispatch(callback) {
callback.call(this.r, this.e);
}
function add(callback) {
this.handlers.indexOf(callback) < 0 &&
@WebReflection
WebReflection / Object.prototype.define.html
Created October 15, 2011 12:25
100% code coverage for Object.prototype.define test
<!doctype html>
<html>
<head>
<title>wru</title>
<!-- https://github.com/WebReflection/wru -->
<script src="define.js"></script>
<script>function wru(wru){var assert=wru.assert,async=wru.async;
// enojy your tests!
@WebReflection
WebReflection / Object.prototype.define.js
Created October 15, 2011 11:01
A simplified and intuitive way to define objects properties
(function (Object) {
/**
* @name Object.prototype.define
* @description A simplified and intuitive way to define objects properties.
* @requires Object.defineProperty, Array.prototype.forEach (native or shimmed)
* @author Andrea Giammarchi (@WebReflection)
* @license Mit Style
*/
@WebReflection
WebReflection / properties.js
Created October 5, 2011 15:46
could be used by an IDE to inspect objects runtime
// could be used to inspect objects runtime
// e.g. with IDE for suggestion
// requires a modern JS engine in the IDE
var properties = function (
getNames, // alias Object.getOwnPropertyNames
getDescriptor,// alias Object.getOwnPropertyDescriptor
has, // alias {}.hasOwnProperty
toString // alias Function.prototype.toString
) {
// (C) WebReflection - Mit Style License
@WebReflection
WebReflection / module.js
Created October 5, 2011 08:30
implicit require
var module = (function create(
namespace, handler, module, Proxy
) {
// ------------------------------------
// (C) WebReflection - MitStyle License
// ------------------------------------
// @dependency npm install node-proxy
// ------------------------------------
// var sys = module.sys;
// sys.print("it works!");
@WebReflection
WebReflection / document.retrieveSelector.js
Created August 27, 2011 09:28
retrieve a CSS selector out of a DOM node
document.retrieveSelector = (function (filter) {
// (C) WebReflection - Mit Style License
function elementsOnly(element) {
return element.nodeType == 1;
}
function createSelector(element, documentElement, path, first) {
var
@WebReflection
WebReflection / fake-script.js
Created August 24, 2011 19:03
hot to mock script requests
document.createElement = function (
createElement, // the native one
createResponse // the function "in charge"
) {
return function (nodeName) {
var result, src;
// if we are creating a script
if (/^script$/i.test(nodeName)) {
// result will be a place holder
result = createElement.call(
@WebReflection
WebReflection / track.js
Created August 23, 2011 16:45
how to track old libraries / namespaces
function track(object) {
// how to track old libraries, by Andrea Giammarchi
// var report = track(genericObject);
// ... run whatever you run usually ...
// console.log(report);
function createGetterSetterIfNecessary(key) {
return hasOwnProperty.call(result, key = key.slice(1)) ?
result[key]
@WebReflection
WebReflection / Object.prototype.in.js
Created August 20, 2011 08:32
implementing "in" operator as generic object method
(function(Object, String){
// (C) WebReflection - Mit Style License
// @about implementing "in" operator as generic object method
// returns primitive value or object itself
function toValue(obj) {
switch (true) {
case isInstanceOf(obj, Boolean):
case isInstanceOf(obj, Number):