Skip to content

Instantly share code, notes, and snippets.

View francescozanoni's full-sized avatar

Francesco Zanoni francescozanoni

View GitHub Profile
@francescozanoni
francescozanoni / only_unique.js
Created March 27, 2020 14:32
[JS] Make unique the items of an array
/**
* Callback that makes unique the items of an array.
*
* Example usage:
* var myArray = ['a', 1, 'a'];
* var myUniqueArray = myArray.filter(onlyUnique);
*
* @param arrayItem one of the items of the array
* @param {number} arrayIndex
* @param {Array} arrayToMakeUnique
@francescozanoni
francescozanoni / get_raw_http_request.php
Last active November 3, 2019 13:59
[PHP] Get raw HTTP request
<?php
/**
* Get raw HTTP request.
*
* Inspired by:
* - https://gist.github.com/magnetikonline/650e30e485c0f91f2f40
* - https://www.php.net/manual/en/function.getallheaders.php#104307
*
* @return string
@francescozanoni
francescozanoni / iban_check_digits_calculator.php
Last active April 17, 2019 08:38
[PHP] IBAN check digits calculator (WITHOUT bcmath EXTENSION)
<?php
/**
* Calculate IBAN check digits (WITHOUT bcmath EXTENSION).
*
* Inspired by https://github.com/arhs/iban.js/blob/master/iban.js
*
* @param string $countryCode ISO 3166 2-letter country code, e.g. "FR"
* @param string $bban IBAN without country code and without check digits, e.g. "20041010050500013M02606"
*
* @return string e.g. "14"
@francescozanoni
francescozanoni / iban_check_digits_calculator.js
Last active March 18, 2019 08:00
[JS] IBAN check digits calculator
/**
* Calculate IBAN check digits.
*
* Inspired by https://github.com/arhs/iban.js/blob/master/iban.js
*
* @param string countryCode ISO 3166 2-letter country code, e.g. "FR"
* @param string bban IBAN without country code and without check digits, e.g. "20041010050500013M02606"
*
* @return string e.g. "14"
*/
@francescozanoni
francescozanoni / print_r_for_comments.md
Last active October 1, 2018 08:25
[PHP] print_r() function enhanced to output variables ready to be used in comments
function print_r_for_comments($variable, $return = false)
{

    $output = trim(print_r($variable, true));

    // Pull open round bracket to previous line
    $output = preg_replace('/\r?\n *\(/', ' (', $output);

    // Remove empty lines
@francescozanoni
francescozanoni / italian_bban_checksum_php.md
Last active August 14, 2018 05:36
[PHP] Compute Italy/San Marino's BBAN checksum (CIN - Control Internal Number)

A BBAN example is X0542811101000000123456, whereas

  • X is the checksum (CIN)
  • 05428 is the bank code (ABI)
  • 11101 is the branch code (CAB)
  • 000000123456 is the account number

The only official source describing the checksum computation algorithm, found so far, is http://www.cnb.cz/cs/platebni_styk/iban/download/TR201.pdf , at page 81.

/**

Code:

var year = new Date().getFullYear();
var months = ['01', '02', '03', '04', '05','06', '07', '08', '09', '10', '11', '12'];
var timezones = moment.tz.names();
var dates = months.map(function (month) {
    return year + '-'+ month +'-01';
});

var output = {};
@francescozanoni
francescozanoni / get_timezone_offsets_php.md
Last active May 16, 2018 09:44
[PHP] Get the offset list of a timezone, optionally defining the year

Function code:

/**
 * Get all the time offsets of a timezone.
 *
 * @param string $timezoneIdentifier
 * @param bool $withDayLightSavingTime
 * @return array
 */
function getTimeZoneOffsets(string $timezoneIdentifier, bool $withDayLightSavingTime = true) : array