Skip to content

Instantly share code, notes, and snippets.

View dmshvetsov's full-sized avatar
🪄

Dmitry Shvetsov dmshvetsov

🪄
View GitHub Profile
@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],
/*
//so far we've worked with some basic operators
Positive and negative operators (unary)
+
-
Arithmetic (binary)
+
-
/
*
@dmshvetsov
dmshvetsov / site_migration_check.sh
Last active September 22, 2020 02:05
Check if you miss any page when migrated a site from one domain to another, or from one tech to another. In this example dmitryshvetsov.com and localhost:8000 is used.
curl https://dmitryshvetsov.com/sitemap.xml | \
grep -e loc | \
sed 's:<loc>\(.*\)<\/loc>$:\1:g' | \
sed -e 's/^[[:space:]]*//' | \
sed 's/https:\/\/dmitryshvetsov\.com/http:\/\/localhost:8000/' | \
while read -r line; do open $line; done
@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 / worker_and_queue_perfect_pair.rb
Created February 5, 2019 05:33
Example of how we may use queue with threads to make efficient background worker.
class Worker
def self.start(num_threads:, queue_size:)
queue = SizedQueue.new(queue_size)
worker = new(num_threads: num_threads, queue: queue)
worker.spawn_threads
worker
end
def initialize(num_threads:, queue:)
@num_threads = num_threads
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)",
]
set -e
ssh -q git@github.com | 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 / 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`/emailAddress=kevinadi@mongodb.com"
# 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`/emailAddress=kevinadi@mongodb.com"
# Sign the server cert