Skip to content

Instantly share code, notes, and snippets.

View NV's full-sized avatar

Nikita Vasilyev NV

View GitHub Profile
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) {
/**
* Example:
* 'hello {{username}}'.between('{{','}}') === 'username'
*/
String.prototype.between_regexp = function between(begin, end) {
var regexp = new RegExp(begin + '(.+)' + end);
return this.match(regexp)[1];
}
@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)
}
}
@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 / greedy?.rb
Created January 1, 2010 05:17
Regex, why so greedy?
'wrath, greed, sloth, pride, lust, envy, and gluttony'.match(/greed.+/)
#<MatchData "greed, sloth, pride, lust, envy, and gluttony">
'wrath, greed, sloth, pride, lust, envy, and gluttony'.match(/greed.+?/)
#<MatchData "greed,">
@NV
NV / README.md
Created December 29, 2009 19:57
Simple JS preprocessor with /*> file.js */
@NV
NV / ligatweet.user.js
Created December 1, 2009 00:38
Script for tweet shortening using unicode ligatures and other compound symbols
// ==UserScript==
// @name ligatweet
// @namespace http://leaverou.me/demos/ligatweet/
// @description Script for tweet shortening using unicode ligatures and other compound symbols
// @include htt*://twitter.com/*
// @author Lea Verou (UserJS by Nikita Vasilyev)
// @version 1.0
// @license Licensed under the MIT license http://www.opensource.org/licenses/mit-license.php
// ==/UserScript==
@NV
NV / function_comments.js
Created November 23, 2009 10:54
IE, Opera, Chrome doesn't remove comments after function.toString() coercion
function f(){/* comment */}
alert(f+'');
// Opera, IE, Chrome: function f(){/* comment */}
// Firefox, Safari: function f(){}
//
// Note: in Firefox: f+'' === f.toSource()
@NV
NV / String.prototype.between.js
Created November 18, 2009 13:05
JS string.between method
/**
* Example:
* 'hello {{username}}'.between('{{','}}') === 'username'
*/
String.prototype.between_regexp = function between(begin, end) {
var regexp = new RegExp(begin + '(.+)' + end);
return this.match(regexp)[1];
}
@NV
NV / events.js
Created October 30, 2009 02:18
reinventing JS events API
// JS Events API sucks. addEventListener ugly. If I'll design this API from scratch, I've done this:
window.events // {}
window.events.load // []
window.events.load.length // 0
window.events.load.push(function(e){
alert('window loaded')
})
window.events.load.length // 1