Skip to content

Instantly share code, notes, and snippets.

View dmshvetsov's full-sized avatar
🪄

magic dima dmshvetsov

🪄
View GitHub Profile
@dmshvetsov
dmshvetsov / smalltalk_inspired_if_else_averse_in_ruby.rb
Created September 9, 2020 01:47
How to avoid if/else branch condition in Ruby. By Yehuda Katz https://yehudakatz.com/2009/10/04/emulating-smalltalks-conditionals-in-ruby/. Please don't do it in production code or your gems.
class Object
def if_true
yield
self
end
def if_false
self
end
end
@dmshvetsov
dmshvetsov / roman-to-arabic-converter.js
Last active November 14, 2020 07:49
Example for roman numeral kata
const ARABIC_ROMAN_MAP = [
["M", 1000],
["CM", 900],
["D", 500],
["CD", 400],
["C", 100],
["XC", 90],
["L", 50],
["XL", 40],
["X", 10],
const ARABIC_ROMAN_MAP = [
["M", 1000],
["CM", 900],
["D", 500],
["CD", 400],
["C", 100],
["XC", 90],
["L", 50],
["XL", 40],
["X", 10],
@dmshvetsov
dmshvetsov / insert-random-data-with-trailing-withespace.sql
Last active February 24, 2020 03:22
Create a million record with random strings, 10% of them will have hanging whitespace, 10% will have trailing whitespace
insert into
trailing_spaces (value)
select
case
when mod(indx, 9) = 0 then 'A' || (random() * 100)::text || ' '
when mod(indx, 10) = 0 then ' B' || (random() * 100)::text
else 'C' || (random() * 100)::text
end
from
generate_series(1, 1000000) as d (indx)
@dmshvetsov
dmshvetsov / nord-theme-indent-rainbow-vscode-settings.json
Last active February 10, 2020 09:43
Color settings for indent-rainbow + awesome Nord theme. 1. Install rainbow VSCode plugin 2. copy the snippet below to VSCode settings.json 3 Enjoy awesomeness
{
"indentRainbow.colors": [
"rgba(46,52,64,0.5)",
"rgba(59,66,82,0.5)",
"rgba(67,76,94,0.5)",
"rgba(76,86,106,0.5)",
"rgba(87,98,121,0.4)",
"rgba(97,110,136,0.3)",
"rgba(109,122,150,0.2)",
]
@dmshvetsov
dmshvetsov / mac-os-setup.sh
Last active June 24, 2022 07:45
DEPRECATED checkout my dotfiles/install script for the up to date version
# DEPRECATED checkout my dotfiles/install script for the up to date version
# https://github.com/dmshvetsov/dotfiles/blob/master/install
set -e
ssh -q [email protected] | true
if [ $? -eq 0 ]; then
echo "SSH to github OK"
else
echo "Cannot connect to github via SSH. Are you added your SSH key to your github account?"
@dmshvetsov
dmshvetsov / mongodb-ssl.sh
Created October 8, 2019 12:46 — forked from kevinadi/mongodb-ssl.sh
Script to create self-signed CA certificates, server certificates, and client certificates for testing MongoDB with SSL
#!/bin/sh
# Generate self signed root CA cert
openssl req -nodes -x509 -newkey rsa:2048 -keyout ca.key -out ca.crt -subj "/C=AU/ST=NSW/L=Sydney/O=MongoDB/OU=root/CN=`hostname -f`/[email protected]"
# Generate server cert to be signed
openssl req -nodes -newkey rsa:2048 -keyout server.key -out server.csr -subj "/C=AU/ST=NSW/L=Sydney/O=MongoDB/OU=server/CN=`hostname -f`/[email protected]"
# Sign the server cert
set -e
ssh -q [email protected] | true
if [ $? -eq 0 ]; then
echo "SSH to github OK"
else
echo "Cannot connect to github via SSH. Are you added your SSH key to your github account?"
exit
fi
@dmshvetsov
dmshvetsov / pg_create_to_locid_fn.sql
Last active September 2, 2019 01:05
PostgreSQL function to decode relay global IDs and extract local (postgres) ID
-- Usage:
-- select * from users where id = to_locid('VXNlcjozNg==');
create or replace function to_locid(globid text) returns int as $$
begin
return (split_part(encode(decode(globid, 'base64'), 'escape'), ':', 2))::int;
end;
$$ language plpgsql;
  1. look for stared repos in your Github account [TODO: link to API]
  2. Google it