Skip to content

Instantly share code, notes, and snippets.

View bahmutov's full-sized avatar
🍺
on a roll

Gleb Bahmutov bahmutov

🍺
on a roll
View GitHub Profile
@bahmutov
bahmutov / qunit-promises.js
Created June 7, 2013 12:14
Testing promises using QUnit
QUnit.extend( QUnit, {
expectSuccess: function (promise, message) {
if (!promise) {
QUnit.push(false, 'undefined promise', 'promise object', 'missing a promise');
QUnit.start();
} else {
promise.then(function onSuccess(successMessage) {
QUnit.push(true, null, null, successMessage);
QUnit.start();
}, function onFailure(errorMessage) {
@bahmutov
bahmutov / q-timeout.js
Created June 18, 2014 14:48
Add q-timeout to promises with either a value or callback
/**
Example: timeout with message, just like Q.promise.timeout
foo()
.timeout(500, 'promise timed out!')
.then(function () {
console.log('foo has been resolved!');
})
.fail(function (msg) {
console.log('failed with value:', msg);
@bahmutov
bahmutov / gist:80369a9870889cd58a15
Created June 21, 2014 02:00
You cannot set prototype of an object created by Object.create(null)
// __proto__ DOES NOT WORK
var foo = Object.create(null);
foo.__proto__ = {
bar: 'bar'
};
foo; // { [__proto__]: { bar: 'bar' } }
foo.bar; // undefined
// __proto__ works
var foo = {};
@bahmutov
bahmutov / gist:17c31f834509c0f0fd12
Created July 31, 2014 03:27
Unique items from Array by calling a method
var _ = require('lodash');
var fp = require('functional-pipeline');
var items = [{
toString: function () {
return 'foo';
}
}, {
toString: function () {
return 'bar';
@bahmutov
bahmutov / load-angular-from-node.js
Created September 26, 2014 03:23
Using Angular under Node
var benv = require('benv');
benv.setup(function () {
benv.expose({
$: benv.require('./bower_components/zepto/zepto.js', 'Zepto'),
angular: benv.require('./bower_components/angular/angular.js', 'angular')
});
console.log('loaded angular', angular.version);
console.log('loaded zepto');
$('body').html('<body>\n' +
@bahmutov
bahmutov / gist:64a0dd821dfb97d97066
Created October 22, 2014 02:02
Module pattern using anonymous constructor function expression
var calc = new function () {
this.add = function add(a, b) { return a + b; };
this.sub = function sub(a, b) {
return this.add(a, -b);
};
};
console.log(calc.add(2, 3)); // 5
console.log(calc.sub(2, 3)); // -1
var subtract = calc.sub;
console.log(subtract(2, 4)); // exception: this.add is undefined
@bahmutov
bahmutov / bio.md
Last active January 28, 2020 01:35
Performance measurement using Chrome DevTools code snippets

Gleb Bahmutov is JavaScript ninja, image processing expert and software quality fanatic. After receiving a PhD in computer science from Purdue University, Gleb worked on laser scanners, 3D reconstruction, and panorama-based virtual tours at EveryScape. Later Gleb switched to writing browser data visualization software at MathWorks. After a year, Gleb went back to the startup environment and developed software quality analysis tools at uTest (now Applause). Today Gleb is developing real-time financial analysis tools at Kensho. He blogs about software development topics at http://bahmutov.calepin.co/ and links his projects at http://glebbahmutov.com/. You can follow him and his work @bahmutov

@bahmutov
bahmutov / insert.bash
Created December 16, 2014 18:53
Adds file into old commit (and all commits after that)
file_path=bower_components/angular-bindonce/bindonce.js
before_commit=$(git rev-parse --verify 14f0b9d)
file_blob=$(git hash-object -w "$file_path")
echo "before commit $before_commit"
echo "file blog $file_blob"
git filter-branch \
--index-filter '
@bahmutov
bahmutov / ServiceWorker.js
Created January 18, 2015 02:02
Change downloaded javascript using ServiceWorker
/*
Using ServiceWorker, every request (except from iframes) can be intercepted and the javascript
could be changed before returning the result. Here is an example that if "*foo.js" is
requested, downloads it, but then returns 'console.log("hi there");'
Related: https://github.com/bahmutov/service-turtle/issues/7
*/
var allowJavaScriptFromAnywhere = {
'Access-Control-Allow-Origin': '*',
'Content-Type': 'text/javascript; charset=utf-8'
@bahmutov
bahmutov / server-after.js
Last active August 7, 2017 17:34
Remove boilerplate when connecting promise-returning middleware to Express
// use point-free callbacks
// http://glebbahmutov.com/blog/point-free-programming-is-not-pointless/
var middleware = require('./middleware');
app.get('example/uri', function (req, res, next) {
middleware.first(req, res)
.then(next)
.catch(res.json)
.done();
}, function (req, res, next) {
middleware.second(req, res)