Skip to content

Instantly share code, notes, and snippets.

View esquinas's full-sized avatar

Enrique Esquinas esquinas

  • Asturias, Spain
View GitHub Profile
@esquinas
esquinas / hybrid-example.rb
Created July 20, 2020 12:23
Best-of-both-worlds example (DI + hard-coded constants) from this article: https://www.rubypigeon.com/posts/dependency_injection_containers_vs_hardcoded_constants/
# Usage:
# - To load default dependencies: `register_user = RegisterUser.build`
# - To inject own dependencies (for testing or reuse):
# ```rb
# register_admin = RegisterUser.new(validator: AdminValidator.new, repo: AdminRepository.new)
# ```
class RegisterUser
attr_reader :validator, :repo
def self.build
@esquinas
esquinas / el_quijote_utf8.txt
Last active April 17, 2020 18:08 — forked from jsdario/el_quijote.txt
El Quijote en texto plano, UTF-8 y CRLF (final de línea de Windows)
This file has been truncated, but you can view the full file.
DON QUIJOTE DE LA MANCHA
Miguel de Cervantes Saavedra
PRIMERA PARTE
CAPÍTULO 1: Que trata de la condición y ejercicio del famoso hidalgo D. Quijote de la Mancha
En un lugar de la Mancha, de cuyo nombre no quiero acordarme, no ha mucho tiempo que vivía un hidalgo de los de lanza en astillero, adarga antigua, rocín flaco y galgo corredor. Una olla de algo más vaca que carnero, salpicón las más noches, duelos y quebrantos los sábados, lentejas los viernes, algún palomino de añadidura los domingos, consumían las tres partes de su hacienda. El resto della concluían sayo de velarte, calzas de velludo para las fiestas con sus pantuflos de lo mismo, los días de entre semana se honraba con su vellori de lo más fino. Tenía en su casa una ama que pasaba de los cuarenta, y una sobrina que no llegaba a los veinte, y un mozo de campo y plaza, que así ensillaba el rocín como tomaba la podadera. Frisaba la edad de nuestro hidalgo con los cincuenta años, era de complexión recia, seco de carnes, enjuto de rostro; gran madruga
@esquinas
esquinas / are_of_the_same_type.rb
Last active February 18, 2020 15:54
Little helper function that returns true if every member of an array is of the same type as the rest.
def are_of_the_same_type?(array)
array.each_with_index { |item, index|
return false unless item.is_a?(array[index - 1].class)
}
true
end
@esquinas
esquinas / airport_to_timezone_converter.rb
Last active January 17, 2020 09:04
Quick & dirty module to convert an Airport's IATA code into an IANA Timezone. Data source is the timezones.csv file in @mj1856's gist https://gist.github.com/mj1856/6d219c48697c550c2476 (Thanks, Matt Johnson-Pint)
#encoding: utf-8
# Usage example:
# --------------
#
# airport_code = 'TFN' # Tenerife Norte Airport in the Canary Islands.
# canary_islands_tz = AirportToTimezoneConverter.from_iata(airport_code)
# puts canary_islands_tz # => "Atlantic/Canary"
#
# winter_time = '2020-01-01 12:00:00'
@esquinas
esquinas / djb2e-hash.js
Created September 3, 2019 17:37
Quick & simple non-cryptographic 32bit hash function for non-security applications like generating colors from a string or slug. Based on the djb2a algorithm but + UTF16 rotation, + reversal, + XORing a random number.
// Usage:
// djb2eHash( 'Hello World1' ); # => 1171219298
// djb2eHash( 'Hello World2' ); # => 449666786
// djb2eHash( 'Hello World3' ); # => 201925602
// djb2eHash( 'Hello World4' ); # => 3033001186
function djb2eHash(string, randomNumber = 0xcde7) {
// UTF16-rotate a character.
function rotU16(charCode) {
return 0x10000 - charCode;
}
@esquinas
esquinas / prettify-json-from-chromes-console.js
Created August 2, 2019 15:50
Used to prettify (make readable) JSON data returned by the WP JSON REST API, for example. Usage: copy & paste into your Chrome's console (in Firefox is built-in).
document.querySelector('pre').textContent = JSON.stringify(JSON.parse(document.querySelector('pre').textContent), null, 4);
@esquinas
esquinas / describe.js
Last active July 15, 2019 15:47
A JS debugging tool like console.log() but a little amplifyed. Copy/paste it in your browser console and use it like "describe(something)"
function describe(x) {
function trueType(obj) {
return (Number.isNaN(obj) && 'NaN') || (Object.prototype.toString.call(obj).slice(8, -1));
}
let description = `DESCRIPTION:
------------
trueType: ${trueType(x)},
typeOf: ${typeof x},
boolean: ${!!x},
number: ${+x},
@esquinas
esquinas / quick-SI-APA-EUR-number-formatting.js
Created January 12, 2019 19:24
Quick SI/APA euro currency formatting in functional JavaScript.
const intlSpanishFormatter = Intl.NumberFormat('es-ES', { style: 'currency', currency: 'EUR'});
const PreventGroupingOfFourIntegerDigits = function ({type, value }, idx, parts) {
(idx === (parts.length - 7) && value.length < 2 && (parts[idx + 1].value = ''));
return value;
};
intlSpanishFormatter.formatToParts(1234.56).map(PreventGroupingOfFourIntegerDigits).join('');
@esquinas
esquinas / NAV-posts-pagination.php
Last active October 23, 2018 10:03
WP pagination partial (TODO: add theme classes)
<?php if ( get_previous_posts_link() || get_next_posts_link() ) : ?>
<div class="">
<nav class="">
<ul>
<li><?php posts_nav_link( '</li><li>' ); ?></li>
</ul>
</nav>
</div>