Skip to content

Instantly share code, notes, and snippets.

View obenjiro's full-sized avatar
🎯
Focusing

Alexey Okhrimenko obenjiro

🎯
Focusing
View GitHub Profile
@obenjiro
obenjiro / vanilla-namespace-1.js
Created October 11, 2012 21:12
Vanilla Namespace Pattern #1 (96 chars)
//namespace patter #1 - with comments
(function(window, lastObject, private_n){
private_n = function(namespace){
lastObject = lastObject[namespace] = lastObject[namespace] || {};
return private_n;
}
window.n = function(namespace){
lastObject = window;
return private_n(namespace)
}
@obenjiro
obenjiro / vanilla-namespace-2.js
Last active October 11, 2015 14:48
Namespace Pattern #2 (81 chars)
//namespace patter #2 - with comments
this.n = function(namespace, namespaceItem, lastObject, namespaceList) {
lastObject = this;
namespaceList = namespace.split('.');
while(namespaceItem = namespaceList.shift()) {
lastObject = lastObject[namespaceItem] = lastObject[namespaceItem]||{};
}
}
@obenjiro
obenjiro / observer-pattern.js
Created October 11, 2012 22:56
Observer Pattern (285 chars)
//observer pattern - with comments
(function() {
this.o_O = function(obj, eventList, i, item) {
eventList = [];
obj.subscribe = function(event,fn) {
eventList.push(event, fn);
}
obj.unsubscribe = function(fn) {
eventList.splice(eventList.indexOf(fn)-1, 2);
@obenjiro
obenjiro / smallest-validation.js
Created October 12, 2012 00:11
Smallest function argument validation
//smallest function argument validation + throwing an error if wrong type
//is string
var a = "";
a.charAt(0); //OK
a = 1;
a.charAt(0); //TypeError: Object 1 has no method 'charAt'
//is number
var a = 1;
@obenjiro
obenjiro / pub-sub.js
Created October 13, 2012 22:26
Smallest Pub\Sub Pattern (268 chars)
//pubsub pattern
(function (eventList, i, item) {
eventList = [];
this.pubsub = {
fire: function (event, a, b, c, d, e) {
for (i in eventList) {
if (eventList[i] == event) {
eventList[+i + 1](a, b, c, d, e);
}
}
@obenjiro
obenjiro / getElementByClassNamePoly.js
Last active October 12, 2015 08:08
Smallest getElementsByClassName polyfill (146 chars)
//A little bit buggy, but for me it's ok (work wrong if \t (tab) is used inside of className field)
function getElementsByClassPolyfill(className, array, space, index, item){
index = 0, space = " ", className = space + className + space, array = [];
while(item = document.all[index++]) {
if((space + item.className + space).indexOf(className)+1) {
array.push(item);
}
}
return array;
}
@obenjiro
obenjiro / no_W_no_D.js
Last active March 29, 2017 10:34
Prevent access to Document and Window objects
// http://jsbin.com/igonuh/1/edit
function Fx(){return function(){return -1;};}
function SafeThis(that){
if (that == window) {
return fakeWindow;
} else if (that == document) {
return fakeDocument;
} else {
return that;
@obenjiro
obenjiro / vanilla-namespace-3.js
Last active October 13, 2015 03:38
Namespace Pattern #3 (82 chars)
//namespace patter #3 - with comments
this.n = function(s, array, lastItem) {
if (array) n(s[array = lastItem.shift()] = s[array] || {}, array, lastItem);
else if (!lastItem) n(this, 1, s.split('.'));
}
//namespace patter #3 - obfuscated (82 chars)
this.n=function(a,b,c){b?n(a[b=c.shift()]=a[b]||{},b,c):c||n(this,1,a.split("."))}
//namespace patter #3 - usage
@obenjiro
obenjiro / function-builder.js
Last active October 13, 2015 14:28
Function builder - allows to save more space (74 chars)
//Function builder
var functionBuilder = function (args, body) {
var argsAsString = args.split(',').toString();
return eval('(function(' + argsAsString + '){' + body + '})');
}
//minifed (74 chars)
var f=function(a,b){return eval("(function("+""+a.split(",")+"){"+b+"})")}
//sample usage
@obenjiro
obenjiro / pseudolinq.js
Last active December 14, 2015 08:09
Simple version of .Net Linq
var a = [ 1, 2, 3, 2 ];
console.log(PseudoLinq(a).select(function(i){
return i + 1
}).toArray());
console.log(PseudoLinq(a).where(function(i){
return i > 1
}).select(function(i){
return i - 1