Skip to content

Instantly share code, notes, and snippets.

View NV's full-sized avatar

Nikita Vasilyev NV

View GitHub Profile
@NV
NV / many_args.js
Created January 3, 2010 12:52
call() and apply() on steroids
Function.prototype.callMany = function (context) {
var result = [];
for (var i=1; i<arguments.length; i++) {
result.push( this.call(context, arguments[i]) );
}
return result;
}
function hi (name) {
return 'Hi '+ name + '!';
@NV
NV / fibonacci.js
Created January 6, 2010 00:43
Caching fibonacci function
function fib(n) {
if (n==0) {
return 0
} else if (n==1) {
return 1
} else {
return fib[n] = fib[n] || fib(n-2) + fib(n-1)
}
}
/**
* Example:
* 'hello {{username}}'.between('{{','}}') === 'username'
*/
String.prototype.between_regexp = function between(begin, end) {
var regexp = new RegExp(begin + '(.+)' + end);
return this.match(regexp)[1];
}
function count(x, i) {
var i = i || 1;
console.log(i);
if (i < x) count(x, i+1);
}
count(5) // 1, 2, 3, 4, 5
function count_with_depth_limit (x, start) {
num = 1;
(function () {
alert(num || 2); // 1, Obviosly
})();
(function () {
var num = num || 2;
alert(num); // 2!
})();
function Markdown(value) {
this.value = value || '';
this.length = this.value.length;
};
Markdown.prototype = new String;
// Instance methods
Markdown.prototype.toString = Markdown.prototype.valueOf = function(){return this.value};
Markdown.prototype.toHTML = function(){return Markdown.decode(this)};
@NV
NV / README.md
Created January 22, 2010 08:29 — forked from ucnv/README.md

This script is also available at .

@NV
NV / Array.reduce.js
Created January 30, 2010 23:27
String argument for Array reduce function
var A_reduce = [].reduce;
Array.prototype.reduce = function reduce (callback, initialValue) {
var operator = callback;
return typeof operator == 'string' && /^[ +*/%-><!&|^?,.]/.test(operator)
? eval(this.join(operator))
: A_reduce.apply(this, arguments)
};
[1,2,3,4].reduce('+') // 10
[1,2,3,4].reduce('*') // 24
/
([a-zA-Z][-+.a-zA-Z\d]*): (?# 1: scheme)
(?:
((?:[-_.!~*'()a-zA-Z\d;?:@&=+$,]|%[a-fA-F\d]{2})(?:[-_.!~*'()a-zA-Z\d;\/?:@&=+$,\[\]]|%[a-fA-F\d]{2})*) (?# 2: opaque)
|
(?:(?:
\/\/(?:
(?:(?:((?:[-_.!~*'()a-zA-Z\d;:&=+$,]|%[a-fA-F\d]{2})*)@)? (?# 3: userinfo)
(?:((?:(?:(?:[a-zA-Z\d](?:[-a-zA-Z\d]*[a-zA-Z\d])?)\.)*(?:[a-zA-Z](?:[-a-zA-Z\d]*[a-zA-Z\d])?)\.?|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}|\[(?:(?:[a-fA-F\d]{1,4}:)*(?:[a-fA-F\d]{1,4}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})|(?:(?:[a-fA-F\d]{1,4}:)*[a-fA-F\d]{1,4})?::(?:(?:[a-fA-F\d]{1,4}:)*(?:[a-fA-F\d]{1,4}|\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}))?)\]))(?::(\d*))?))?(?# 4: host, 5: port)
|
@NV
NV / opera.document.all.js
Created February 22, 2010 19:43
Opera document.all WTF
>>> typeof document.all
"undefined"
>>> document.all
[object HTMLCollection]
>>> typeof document.all[0]
"object"