Skip to content

Instantly share code, notes, and snippets.

View dmitrythaler's full-sized avatar

Dmitry Thaler dmitrythaler

View GitHub Profile
/** *********************************
* return the contents of the array "arr" divided into "n" equally sized arrays
*
* @param {number[]} arr - input array
* @param {integer} n - number of resulting arrays
*/
const groupArrayElements = (arr, n) => {
if (n === 1) {
return [arr]
@dmitrythaler
dmitrythaler / JSON.replacer.js
Created March 27, 2017 12:49
replacer function for the JSON.stringify() with depth and duplicates control
const replacer = function( depth = Number.MAX_SAFE_INTEGER ) {
let objects, stack, keys;
return function(key, value) {
// very first iteration
if (key === '') {
keys = ['root'];
objects = [{keys: 'root', value: value}];
stack = [];
return value;
}
@dmitrythaler
dmitrythaler / mysql-check.js
Created November 20, 2016 21:51
node.js waits until MySQL is up before init connection, promise based
const promise = require('bluebird');
const net = require('net');
const config = require('someConfigTool');
// ---------------------------------
// most probably MySQL server will start together with ours(this one)
// so we have to wait until it up
let tryOnce_ = (opts) => {
return new promise((resolve, reject) => {
let client = new net.Socket();
/**
* combine objects
*
* gets arrays with possible @param values, like
* [{a:1},{a:2}], [{b:'one'}, {b:'two'}], [{c:4,d:5},{c:7,d:9}]
* @returns all param values combinations
* [{ a: 1, b: 'one', c: 4, d: 5 },
* { a: 2, b: 'one', c: 4, d: 5 },
* { a: 1, b: 'two', c: 4, d: 5 },
* { a: 2, b: 'two', c: 4, d: 5 },
@dmitrythaler
dmitrythaler / simple.glob.resolve.js
Last active July 3, 2016 12:11
nodejs, simple glob resolve, no dependencies
var fs = require( 'fs' );
var path = require( 'path' );
// path/path/*.html → array of files
var resolve_glob = function( glob ) {
if ( glob.indexOf( '*' ) !== -1 || glob.indexOf( '?' ) !== -1 ) {
var re = new RegExp( '^' + path.basename( glob ).replace( /\./g, '\.' ).replace( /\*/g, '.*' ).replace( /\?/g, '.' ) + '$' ),
dir = path.normalize( path.dirname( glob ) + '/' );
return fs.readdirSync( dir ).filter( function( f ) {
[user]
name = dmitry_t
email = dmitry@mainlywrenches.co
[core]
autocrlf = input
safecrlf = true # it can be your headache ..
whitespace = -indent-with-non-tab,-trailing-space,-space-before-tab,cr-at-eol
[alias]
@dmitrythaler
dmitrythaler / keybase.md
Created August 8, 2015 13:32
Keybase proof

Keybase proof

I hereby claim:

  • I am googgah on github.
  • I am googgah (https://keybase.io/googgah) on keybase.
  • I have a public key whose fingerprint is 1775 4DD5 3247 CFE2 E2CD F74C EF03 EC1A 466A 90F7

To claim this, I am signing this object:

@dmitrythaler
dmitrythaler / pre-receive
Last active January 11, 2016 16:52
pre-receive gist hook, shared repository
#!/bin/bash
# this pre-receive hook returns error when you are trying to push your silly code to the PRODUCTION branch not being member of repo administrators group
REPOADMINGROUP='repo-admin'
GROUPS_=`groups`
PRODUCTIONBRANCH='master'
while read oldsha newsha REFNAME; do
@dmitrythaler
dmitrythaler / post-receive
Last active August 29, 2015 14:06
post-receive git hook, shared repository
#!/bin/bash
# 2 points assumed
# * web server and repository are/can-be placed on different boxes
# * one can login to repository box without manual password entering - rsa keys are keychained by ssh-agent
PRODUCTIONBRANCH='master'
DEVBRANCH='dev'
PRODUCTIONPATH='/var/www/path/to/prod/server'
DEVPATH='/var/www/path/to/dev/server'