Skip to content

Instantly share code, notes, and snippets.

View jonasraoni's full-sized avatar
☠️
Do what you want cause a pirate is free! You are a pirate!

Jonas Raoni Soares da Silva jonasraoni

☠️
Do what you want cause a pirate is free! You are a pirate!
View GitHub Profile
@jonasraoni
jonasraoni / update.sql
Last active February 24, 2019 11:28
Fill the gaps in SQL Server
/*
There is a table in database. This table contains unduplicated natural numbers. There may be gaps in the sequence of natural numbers in the table. You need to output missing numbers.
Table of natural numbers: declare @values as table ([number] int not null).
Test data: insert into @values([number]) values (1), (2), (3), (5), (9).
Result: declare @missing as table ([left] int not null, [right] int not null).
*/
DECLARE @values AS TABLE ([number] INT NOT NULL);
INSERT INTO @values([number]) VALUES (1), (2), (3), (5), (9);
DECLARE @missing AS TABLE ([left] INT NOT NULL, [right] INT NOT NULL)
@jonasraoni
jonasraoni / number-to-ordinal.js
Created February 24, 2019 11:41
Number to ordinal (English)
function numberToOrdinal(n) {
const
number = Math.abs(n) || 0,
units = number % 10,
tens = ~~(number % 100 / 10),
suffix = new Map([
[1, 'st'],
[2, 'nd'],
[3, 'rd']
]);
@jonasraoni
jonasraoni / twin-strings.cs
Last active February 24, 2019 11:58
Twin Strings
/*
Twin Strings
Two strings, a and b, are said to be twins only if they can be made equivalent by performing some number of operations on one or both strings. There are two possible operations:
SwapEven: Swap a character at an even-numbered index with a character at another even-numbered index.
SwapOdd: Swap a character at an odd-numbered index with a character at another odd-numbered index.
For example, a = "abcd" and b = "cdab" are twins because we can make them equivalent by performing operations. Alternatively, a = "abcd" and b = "bcda" are not twins (operations do not move characters between odd and even indices), and neither are a = "abc" and b = "ab" (no amount of operations will insert a 'c' into string b).
Complete the twins function in the provided code. It has two parameters:
@jonasraoni
jonasraoni / backup.js
Last active February 27, 2019 13:03
Creates a full backup of all SQL Server, PostgreSQL and MySQL databases in JScript/JavaScript.
Backup = {
shell: WScript.CreateObject("WScript.Shell"),
base: "C:/BACKUP/",
username: "USERNAME",
hostname: "HOST",
password: "PASSWORD",
type: {
postgreSQL: function(){
var shell = Backup.shell;
var path = "c:/program files/postgresql/9.6/bin/";
@jonasraoni
jonasraoni / max-decimal-places.js
Created March 30, 2019 19:38
Given a list of numeric arguments, retrieves the longest amount of decimal places, useful to format :)
//+ Jonas Raoni Soares Silva
//@ http://raoni.org
export default (...values) => Math.max(...values.map(value =>
(value = (value + '').split(/[.,]/)).length > 1 && value.pop().length
));
//+ Jonas Raoni Soares Silva
//@ http://raoni.org
export default function debounce (action, delay) {
let handle;
return function (...args) {
if (handle) {
clearTimeout(handle);
handle = null;
}
@jonasraoni
jonasraoni / server-time.vue
Created March 30, 2019 19:41
Vue plugin to retrieve the server time
//+ Jonas Raoni Soares Silva
//@ http://raoni.org
export default class ServerTime {
constructor ({http = new Error('http parameter is required'), url = new Error('url is required'), thresholdDelay = 300, autoSynchronizeProbes = 0}) {
Object.assign(this, {
http,
url,
thresholdDelay,
best: null
@jonasraoni
jonasraoni / hub.js
Created March 30, 2019 19:43
Class to handle SignalR subscriptions
//+ Jonas Raoni Soares Silva
//@ http://raoni.org
const signalR = require('@aspnet/signalR');
export default class Hub {
constructor (url, autoReloadTimeout = 1000) {
Object.assign(this, {
url,
autoReloadTimeout,
@jonasraoni
jonasraoni / regexp-quote.js
Created April 2, 2019 19:11
Regexp match quotes
/(["'`])(?:\\.|(?!\1).)*?\1/
@jonasraoni
jonasraoni / list-to-tree.js
Created July 5, 2019 13:34
Performatic list to tree using one loop and map
//+ Jonas Raoni Soares Silva
//@ http://raoni.org
function tree(items) {
const tree = [];
const map = new Map;
for (const item of items) {
const placeholder = map.get(item.id);
item.children = placeholder ? placeholder.children : [];
map.set(item.id, item);