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 / 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 Oct 1, 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 / 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 / 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
{
@jonasraoni
jonasraoni / valid-utf-8-regexp.txt
Created Dec 6, 2020
Regular expression to find valid UTF-8 characters (useful for filtering bad input which might break some applications)
View valid-utf-8-regexp.txt
[\x00-\x7F] # ASCII
|[\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
|\xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
|\xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
|\xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
|[\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
|\xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
@jonasraoni
jonasraoni / convert.js
Created Oct 29, 2020
Convert boolean to positive (1) negative (-1)
View convert.js
const condition = true;
// I don't know why, but adding an if here makes me sad since I started programming :L
condition ? 1 : -1;
// Alternative way 1
(-1) ** condition;
// Alternative way 2
2 * condition - 1;
@jonasraoni
jonasraoni / Lazy.php
Created Jun 10, 2020
Class to retrieve values lazily from a callback
View Lazy.php
<?php
//+ Jonas Raoni Soares Silva
//@ http://raoni.org
class Lazy {
private $_cache;
private $_handler;
public function __construct(callable $handler)
{
@jonasraoni
jonasraoni / regexp.js
Created Jun 1, 2020
RegExp techniques to split
View regexp.js
//Split by nothing/position
'USDEUR'.split(/(?=\w{3}$)/); //["USD", "EUR"]
//Split without consuming/swallowing
'2020-10-10'.split(/(-)/); //["2020", "-", "10", "-", "10"]