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 / 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 / post.md
Last active November 29, 2017 20:44
Setting up git message validation in NPM project

Setting up Git message validation hook

Install pre-git for handling Git hooks and commitizen for helping format the commit messages

npm install pre-git commitizen cz-conventional-changelog --save-dev
@bahmutov
bahmutov / index.js
Last active April 30, 2018 12:15
Rendering graphics using Canvas and Path2D polyfill on node
// node-canvas
var Canvas = require('canvas')
console.log('Canvas is', Canvas)
var nodeCanvas = new Canvas(120, 120)
var ctx = nodeCanvas.getContext('2d')
// canvas-5-polyfill patches CanvasRenderingContext2D.prototype
// lets give it one
global.CanvasRenderingContext2D = {};
global.CanvasRenderingContext2D.prototype = ctx;
@bahmutov
bahmutov / Docker shell commands.sh
Last active February 9, 2024 07:55
A personal cheat sheet for running local Node project in a Docker container
# See list of docker virtual machines on the local box
$ docker-machine ls
NAME ACTIVE URL STATE URL SWARM DOCKER ERRORS
default * virtualbox Running tcp://192.168.99.100:2376 v1.9.1
# Note the host URL 192.168.99.100 - it will be used later!
# Build an image from current folder under given image name
$ docker build -t gleb/demo-app .
@bahmutov
bahmutov / README.md
Last active July 11, 2022 13:03
Trying Redis in Docker + as-a

Goal: try redis inside a Docker container

Run Redis Docker image

  1. Open docker terminal
  • displays docker is configured to use the default machine with IP 192.168.99.100
  1. Start redis:alpine prebuilt container from docker hub.
  • docker run --name redis -p 6379:6379 -d redis:alpine
  • the "alpine" image is very slim (5MB!)
  • the running container binds the internal port 6379 to the "outside" world port with same number
@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

@bahmutov
bahmutov / README.md
Last active October 4, 2023 08:35
Single command to run Node with local file access

Goal: run a simple NodeJS application inside a Docker container

Subgoal: give it access to local files.

docker run -it --rm --name example -v "$PWD":/usr/src/app -w /usr/src/app node:5-slim node index.js

output:

@bahmutov
bahmutov / index.js
Last active April 7, 2016 14:08
Is it pure?
// A: is this pure function?
function sum(a, b) { return a + b }
// B: is this a pure function?
const k = 2
function addK(a) { return a + k }
// C: is this a pure function?
function sub(a, b) { return sum(a, -b) }
// D: is this a pure function?
function sub(a, b, summer) { return summer(a, b) }
// E: is this a pure function?
@bahmutov
bahmutov / dynamic-spec.js
Last active April 7, 2016 19:45
Grid of individual tests
// if our data is more complicated and has to form things from items for example
// just pass around list of functions
const API = {
github: (username) => 'http://github.com/' + username,
photo: (username) => `http://github.com/${username}/avatar/`
}
const users = ['bahmutov', 'adity']
Object.keys(API).forEach((apiName) => {
const formApiUrl = API[apiName]
describe('api ' + apiName, () => {