Skip to content

Instantly share code, notes, and snippets.

View koistya's full-sized avatar
🏠
Working from home

Konstantin Tarkus koistya

🏠
Working from home
View GitHub Profile
@koistya
koistya / loader.js
Created June 22, 2011 20:17
Closer
var loader = (function() {
var speed = 200,
index = 0,
timeout = null,
symbols = ['└', '├', '┌', '┬', '┐', '┤', '┘', '┴'];
function show() {
console.log(symbols[index]);
index = (index + 1) % symbols.length;
timeout = setTimeout(show, speed);
var endpoints = null;
function addEndpoint(url) {
var i;
if (endpoints == null) {
endpoints = getEndpoints();
}
url = url.trim();
if (url.charAt(url.length - 1) != '/') {
url += "/";
@koistya
koistya / swap.js
Created February 11, 2012 13:54
Swap two values in JavaScript without using a temp variable
var _swap = function(a, b) {
return "".concat(a, " = [", b, ", ", b, " = ", a, "][0]")
};
var a = 1, b = 2;
eval(_swap('a', 'b'));
@koistya
koistya / Unix2Dos.sh
Created February 11, 2012 17:58
Unix2Dos shell script for Git
# Finds all .css and .js files in specified folders and converts LF line endings to CRLF
dirs=( './Packages' './App.Web/Scripts' );
find "${dirs[@]}" -type f \( -name '*.js' -o -name '*.css' \) -exec dos2unix -D -v {} \;
@koistya
koistya / gist:1846562
Created February 16, 2012 17:20
Show v8 stack trace
var stack_holder = new Error;
function stackPrepare(e,stacks) {
var stack = stacks
for (var p in stack[0]) {
stack[p] = stack[0][p]
}
stack.find = function(fn) {
for (var i=stack.length;i--;) {
if (stack[i].getFunction() === fn) break;
@koistya
koistya / gist:1974320
Created March 4, 2012 18:42
ES5 Complete Object Clone
/**
* ES5 Complete Object Clone
*
* @param {Object} Object to clone
* @return {Object} Cloned object
*/
function (obj, tmp /* placeholder */) {
// import Object.*
with(Object)
// get property names
@koistya
koistya / gist:1974712
Created March 4, 2012 20:44
Convert an array-like object to a true array
/**
* A utility function to convert an array-like object (or suffix of it) to a true array.
*/
function (arr, start /* optional */) {
return Array.prototype.slice.call(arr, start || 0);
}
@koistya
koistya / gist:1975483
Created March 4, 2012 23:54
Partial application of a function
/**
* Returns a new function that invokes `func` in a specified context and
* with specified set of arguments.
* The arguments to this function serve as a template. Undefined values
* in the argument list are filled in with values from the inner set.
*
* Example: var f = function(x, y, z) { return x * (y - z); }
* partialApply(f, undefined, 2)(3, 4) // => -6: 3 * (2 - 4)
*
* @param func {Function} A function to invoke.
@koistya
koistya / gist:1975645
Created March 5, 2012 00:33
Named parameters application of a function
/**
* Applies a set of named arguments to `fn`.
*
* Example: var f = function(x, y, z) { return x * (y - z); };
* namedApply(f, {x: 1, y: 2, z: 3});
*
* @param fn {Function} A function to invoke.
* @param args {Object} A collection of named arguments.
*/
function namedApply(fn, args) {
@koistya
koistya / gist:2230969
Created March 28, 2012 22:06
Pick some random numbers
// Дано: Цикл, который перебирает все элементы массива данных. Размер массива может быть любым.
// В цикле можно совершать любые действия, но нельзя запускать этот цикл повторно.
// Найти: Сумму только пяти случайных элементов, которые можно получить этим циклом:
var i = 0, sum = 0;
while (i < sizeof(array))
{
sum += array[i];
i++;