beautify_js

This commit is contained in:
Ilya Kantor 2015-03-09 18:48:58 +03:00
parent 0febe4f5fd
commit 5c2f32e184
208 changed files with 3891 additions and 1474 deletions

View file

@ -9,11 +9,12 @@ describe("delay", function() {
it("вызывает функцию через указанный таймаут", function() {
var start = Date.now();
function f(x) {
assert.equal(Date.now() - start, 1000);
}
f = sinon.spy(f);
var f1000 = delay(f, 1000);
f1000("test");
this.clock.tick(2000);
@ -30,16 +31,16 @@ describe("delay", function() {
assert.equal(Date.now() - start, 1500);
}
};
user.sayHi = sinon.spy(user.sayHi);
var spy = user.sayHi;
user.sayHi = delay(user.sayHi, 1500);
user.sayHi("Привет", "Вася");
this.clock.tick(2000);
assert(spy.calledOnce, 'проверка calledOnce не сработала');
});
});

View file

@ -11,7 +11,9 @@ function debounce(f, ms) {
state = COOLDOWN;
setTimeout(function() { state = null }, ms);
setTimeout(function() {
state = null
}, ms);
}
}
}

View file

@ -2,23 +2,32 @@ describe("debounce", function() {
before(function() {
this.clock = sinon.useFakeTimers();
});
after(function() {
this.clock.restore();
});
it("вызывает функцию не чаще чем раз в ms миллисекунд", function() {
var log = '';
function f(a) { log += a; }
function f(a) {
log += a;
}
f = debounce(f, 1000);
f(1); // выполнится сразу же
f(2); // игнор
setTimeout(function() { f(3) }, 100); // игнор (рановато)
setTimeout(function() { f(4) }, 1100); // выполнится (таймаут прошёл)
setTimeout(function() { f(5) }, 1500); // игнор
setTimeout(function() {
f(3)
}, 100); // игнор (рановато)
setTimeout(function() {
f(4)
}, 1100); // выполнится (таймаут прошёл)
setTimeout(function() {
f(5)
}, 1500); // игнор
this.clock.tick(5000);
assert.equal(log, "14");
@ -26,13 +35,13 @@ describe("debounce", function() {
it("сохраняет контекст вызова", function() {
var obj = {
f: function() {
f: function() {
assert.equal(this, obj);
}
};
obj.f = debounce(obj.f, 1000);
obj.f("test");
});
});
});

View file

@ -1,29 +1,29 @@
function throttle(func, ms) {
var isThrottled = false,
savedArgs,
savedThis;
function wrapper() {
if (isThrottled) {
if (isThrottled) {
savedArgs = arguments;
savedThis = this;
return;
}
func.apply(this, arguments);
func.apply(this, arguments);
isThrottled = true;
setTimeout(function() {
isThrottled = false;
isThrottled = false;
if (savedArgs) {
wrapper.apply(savedThis, savedArgs);
savedArgs = savedThis = null;
}
}, ms);
}, ms);
}
return wrapper;
}
}

View file

@ -1,7 +1,10 @@
describe("throttle(f, 1000)", function() {
var f1000;
var log = "";
function f(a) { log += a; }
function f(a) {
log += a;
}
before(function() {
f1000 = throttle(f, 1000);
@ -19,9 +22,9 @@ describe("throttle(f, 1000)", function() {
// через 1000 мс запланирован вызов с последним аргументом
assert.equal(log, "1"); // пока что сработал только первый вызов
this.clock.tick(1000); // прошло 1000мс времени
assert.equal(log, "13"); // log==13, т.к. сработал вызов f1000(3)
assert.equal(log, "13"); // log==13, т.к. сработал вызов f1000(3)
});
it("тормозит третье срабатывание до 1000мс после второго", function() {
@ -31,7 +34,7 @@ describe("throttle(f, 1000)", function() {
f1000(5); // (тормозим, с последнего вызова прошло 200мс - менее 1000мс)
this.clock.tick(700);
f1000(6); // (тормозим, с последнего вызова прошло 900мс - менее 1000мс)
this.clock.tick(100); // сработал вызов с 6
assert.equal(log, "136");