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 / 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, () => {
@bahmutov
bahmutov / ImageMagick.md
Last active May 14, 2016 19:14
My common ImageMagick commands

ImageMagick commands

Image information

identify <image>

Resize an image

@bahmutov
bahmutov / Advanced Rx.md
Created December 8, 2016 04:05
List of advanced Rx.js topics
  • Marble diagrams are awesome and show the streams really well. When you cannot fit them due to space constraints - time to refactor
  • Testing Rx.js code is still hard and requires controlling timing of events. See https://glebbahmutov.com/blog/testing-reactive-code/
  • Reactive is all about controlling sequence of events. A simple operation might be take

In its first form it just takes N items s.take(2) which creates second stream with first 2 events from s and the completes. But what happens if you don't know how many items to take? Maybe there is a condition, a simple function that operates on the event data and/or global state that tells when to stop taking items. In this case takeWhile is useful s.takeWhile(x =&gt; x &lt; 10)

@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 / tmux.md
Last active March 31, 2017 15:47
tmux basics

install

Install on Mac using brew install tmux and start with tmux

main commands

See help (list of commands) with Control-b ? key combination. Leave help view by pressing q key. From now on use Control-b (aka C-b) to start all tmux commands (C-b is called command prefix and can be changed, see tmux.config options).

@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)
@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;