Skip to content

Instantly share code, notes, and snippets.

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 / cloudSettings
Last active Sep 16, 2021
Visual Studio Code Settings Sync Gist
View cloudSettings
{"lastUpload":"2021-09-16T16:59:40.313Z","extensionVersion":"v3.4.3"}
@jonasraoni
jonasraoni / vue-numeric-directive.js
Last active Aug 5, 2021
Numeric directive for Vue, v-decimal and v-integer, with unsigned modifier
View vue-numeric-directive.js
//+ Jonas Raoni Soares Silva
//@ http://raoni.org
export default class NumericDirective {
constructor (input, binding) {
Object.assign(this, { input, binding });
input.addEventListener('keydown', this);
input.addEventListener('change', this);
}
@jonasraoni
jonasraoni / convert-latin1-utf8.sql
Created Jul 21, 2021
Detect and convert bad encoded UTF-8 data in MySQL
View convert-latin1-utf8.sql
-- Convert
SELECT CONVERT(CAST(CONVERT('São Paulo' USING latin1) AS BINARY) USING utf8) -- São Paulo
-- Our lovely friends celebrate ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö \o/
-- Poor man's bad encoding detection (in my case there was LATIN1 data mixed with UTF8 in the database and I just wanted to find when things started to get mixed up)
-- When trying to convert, normally the amount of characters decrease...
SELECT field
FROM table
WHERE LENGTH(CONVERT(CAST(CONVERT(field USING latin1) AS BINARY) USING utf8)) < LENGTH(field)
@jonasraoni
jonasraoni / remove-trailing-zeros.cs
Created Jun 28, 2021
Remove trailing zeros from a decimal value (C#)
View remove-trailing-zeros.cs
var value = 0.1230000000000000M;
Console.WriteLine(value);
Console.WriteLine(value / 1.0000000000000000000000000000M);
@jonasraoni
jonasraoni / localized-compare.js
Created Mar 15, 2021
Compare texts using another locale in JavaScript
View localized-compare.js
const comparer = new Intl.Collator('ru', { numeric: true }).compare;
console.log(comparer('ф', 'в')); //returns -1, 0, 1
@jonasraoni
jonasraoni / tootip-on-overflow-directive.js
Last active Mar 8, 2021
Vue directive: Tooltip on overflow for hidden text
View tootip-on-overflow-directive.js
export default class tooltipOnOverflowDirective {
static install (Vue) {
Vue.directive('tooltip-on-overflow', this.directive);
}
static directive = {
bind (el) {
for (const event of ['mouseover', 'mouseout']) {
el.addEventListener(event, TooltipOnOverflowDirective[event]);
}
@jonasraoni
jonasraoni / Flattener.cs
Created Jul 14, 2018
Flatten Array in C#
View Flattener.cs
using System;
using System.Collections;
using System.Collections.Generic;
/// <summary>
/// Extends the Array with the Flatten method
/// </summary>
public static class Flattener {
/// <summary>
/// Given a N-dimensional array, flattens it into a new one-dimensional array without modifying the elements' order
@jonasraoni
jonasraoni / format-number.js
Created Jan 12, 2021
Format number Vue directive
View format-number.js
export default {
install: Vue => Vue.filter('format-number', (value, { thousand = ' ', decimal = '.', decimals = null, maxDecimals = decimals, minDecimals = decimals, normalize = true, roundToEven = true } = {}) => {
value = normalize ? normalizeNumber(value) : `${value != null ? value : ''}`;
let pieces = value.split('.');
if (!pieces[0].length) {
return;
}
if (minDecimals > 0) {
pieces[1] = (pieces[1] = pieces[1] || '').padEnd(minDecimals, '0');
@jonasraoni
jonasraoni / chained-sum.js
Last active Jan 3, 2021
A chained sum function in JavaScript
View chained-sum.js
//+ Jonas Raoni Soares Silva
//@ http://raoni.org
// Iteration 3: manually minified xD
const sum = (...args) => ((v, r = sum.bind(this, v)) => (r[Symbol.toPrimitive] = () => v, r))(args.reduce((a, b) => a + b, 0));
// Iteration 2: supports sum(1,2,3)
const sum = (...args) => {
const operation = (...args) => args.reduce((a, b) => a + b, 0);
let current = operation(...args);
@jonasraoni
jonasraoni / UrlBuilder.cs
Created Dec 23, 2020
Simple URL builder for C#
View UrlBuilder.cs
//+ Jonas Raoni Soares Silva
//@ http://raoni.org
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
namespace JonasRaoni
{