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 / 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>
@esquinas
esquinas / my_uuid_codes.rb
Last active August 14, 2018 14:54
Quick, dirty but typographically resilient UUID codes. Hex digits A, B, C, D, E and F are replaced by letters that cannot be mistaken by any digit or any other letter. The choosen letters are E, J, N, P, V and X.
# Takes 3 optional params: an int code_length, a string "separator" and a look-up "table" (hash).
def my_uuid_code(
code_length = 24,
separator: '-',
table: { "0" => '0', "1" => '1', "2" => '2', "3" => '3', "4" => '4', "5" => '5',
"6" => '6', "7" => '7', "8" => '8', "9" => '9',
"A" => 'E', "B" => 'J', "C" => 'N', "D" => 'P', "E" => 'V', "F" => 'X' })
posibilities = 16**code_length
@esquinas
esquinas / birthday_collision.rb
Created August 14, 2018 14:23
Calculate percentage probability of birthday collisions (Birthday paradox) in Ruby
def prob_of_birthday_collision(people = 23.0, days = 365.25)
e = Math::E
k = people.to_f()
n = days.to_f()
return 100.0 * (1.0 - e**((-k * (k - 1.0)) / (2.0 * n)))
end