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 / 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 / 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 / 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 / pouch-init.js
Created July 13, 2015 20:48
How to detect invalid IndexedDB (in Firefox incognito mode for example) when opening PouchDB?
// I am trying to open PouchDB normally, but this fails in Firefox Incognito mode
var db = new PouchDB('test');
// InvalidStateError
// I see the idb error callback being called if the open request fails
exports.idbError = function (callback) {
return function (event) {
var message = (event.target && event.target.error &&
event.target.error.name) || event.target;
callback(errors.error(errors.IDB_ERROR, message, event.type));
};
@bahmutov
bahmutov / change.js
Last active October 28, 2015 13:45
Mutate the parent closure named arguments via arguments from outside
// NOTE does not work in strict mode
function parent(a, b) {
console.log('in parent, a', a, 'b', b);
var parentArguments = arguments;
function child() {
console.log('in child, a', a, 'b', b);
console.log('is parentArguments === c.args?', parentArguments === c.args);
console.log('parentArguments', parentArguments);
return a + b;
@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 / README.md
Created February 12, 2016 02:51
Find local IP address

On Mac OSX

To find the local network (WIFI usually) address ipconfig getifaddr en0

ipconfig getifaddr en0

192.168.0.8