Skip to content

Instantly share code, notes, and snippets.

View wuhhh's full-sized avatar

Huw Roberts wuhhh

View GitHub Profile
function parseVideo(url) {
// - Supported YouTube URL formats:
// - http://www.youtube.com/watch?v=My2FRPA3Gf8
// - http://youtu.be/My2FRPA3Gf8
// - https://youtube.googleapis.com/v/My2FRPA3Gf8
// - Supported Vimeo URL formats:
// - http://vimeo.com/25451551
// - http://player.vimeo.com/video/25451551
// - Also supports relative URLs:
// - //player.vimeo.com/video/25451551
@wuhhh
wuhhh / generics.ts
Created February 21, 2019 11:17
Reminder on TypeScript's Generics syntax
/**
* The generic function loggingIdentity takes a type parameter T, and
* an argument arg which is an array of Ts, and returns an array of Ts.
*/
function loggingIdentity<T>(arg: T[]): T[] {
console.log(arg.length); // Array has a .length, so no more error
return arg;
}
@wuhhh
wuhhh / wp_rewrite_content_type.php
Created February 19, 2019 15:21
Rewrite a WordPress URL and set its content-type. Use case: Displaying dynamic PDF files which follow a custom URL pattern.
<?php
/**
* Hook into WP init and add custom rewrite rule(s)
*/
add_action('init', 'wuhhh_add_rewrites'), 1, 0);
function wuhhh_add_rewrites() {
// Add a rewrite rule according to your requirements
add_rewrite_rule(
@wuhhh
wuhhh / map.js
Created February 15, 2018 16:35 — forked from AugustMiller/map.js
Extension of the native Number class to map a number in one range to a number in another.
Number.prototype.map = function ( in_min , in_max , out_min , out_max ) {
return ( this - in_min ) * ( out_max - out_min ) / ( in_max - in_min ) + out_min;
}