Skip to content

Instantly share code, notes, and snippets.

View christophemarois's full-sized avatar

Christophe Marois christophemarois

  • Pathway Medical
  • Montreal
View GitHub Profile
@christophemarois
christophemarois / get_indefinite_article.rb
Created December 17, 2015 18:43
Programatically determine whether to use the “a” or “an” articles
def get_indefinite_article (noun_phrase)
chars = 'aedhilmnorsx'.split('')
regexp1 = /(?!FJO|[HLMNS]Y.|RY[EO]|SQU|(F[LR]?|[HL]|MN?|N|RH?|S[CHKLMNPTVW]?|X(YL)?)[AEIOU])[FHLMNRSX][A-Z]/
regexp2 = [/^e[uw]/, /^onc?e\b/, /^uni([^nmd]|mo)/, /^u[bcfhjkqrst][aeiou]/]
regexp3 = /^U[NK][AIEO]/
regexp4 = /^y(b[lor]|cl[ea]|fere|gg|p[ios]|rou|tt)/
m = /\w+/.match noun_phrase
return "an" unless m
@christophemarois
christophemarois / mkdirpSync.js
Created December 29, 2015 21:10
Create folder or use existing
function mkdirpSync (dirpath) {
var fs = require('fs');
var parts = dirpath.split(path.sep);
for( var i = 1; i <= parts.length; i++ ) {
try {
fs.mkdirSync(path.join.apply(null, parts.slice(0, i)));
} catch(e) {
if ( e.code != 'EEXIST' ) throw e;
}
}
@christophemarois
christophemarois / urlInfo.js
Last active January 8, 2016 20:30
Get url info in the DOM
function urlInfo (url) {
var props = 'hash host hostname href origin password pathname port protocol username search';
if (!window.urlInfoAnchorElement)
window.urlInfoAnchorElement = document.createElement('a');
urlInfoAnchorElement.href = url;
return props.split(' ').reduce(function (m, v, i) {
@christophemarois
christophemarois / multilineStr.js
Created January 13, 2016 04:15
pre-ES6 multiline strings
m = function (func) {
return func.toString()
.replace(/^[^{]+{\s*\/\*\s*/m, "")
.replace(/\s*\*\/\s*}$/m, "");
};
var str = m(function(){/*
everything in here
is a multiline string
it is a secret.
@christophemarois
christophemarois / hybrid-singletons.js
Created January 15, 2016 18:10
Removes the need for 'new' to instantiate a singleton in javascript
/* Hybrid singletons. removes the need for 'new' */
/* Really more an experiment than anything */
function Person (name, age) {
var parentFn = this[arguments.callee.name];
if (parentFn && !(this instanceof parentFn))
return new (Function.prototype.bind.apply(
parentFn, [this].concat(Array.from(arguments))));
@christophemarois
christophemarois / throttle.js
Created January 15, 2016 22:42
Simplest throttle function possible
function throttle (fn, limit) {
var wait = false;
return function () {
if (wait) return;
fn.call(); wait = true;
setTimeout(function () { wait = false; }, limit);
}
};
@christophemarois
christophemarois / crazytext.js
Created January 17, 2016 06:11
Make text lose it on hover
$(function(){
var speed = 80, times = 3;
$(document).on('mouseover', '[data-shuffle]', function (e) {
var $this = $(this);
if (!$this.data('text'))
$this.data('text', $this.text());
@christophemarois
christophemarois / instagram-scraper.rb
Created January 20, 2016 17:22
Downloader for the instagram-screen-scrape command-line utility
#!/usr/bin/env ruby
require 'json'
photos = JSON.parse(`instagram-screen-scrape -u #{ARGV[0]}`).each { |p|
system("curl -LO #{p['image']}")
}
@christophemarois
christophemarois / jumpytext.js
Created January 21, 2016 15:18
links that can't hold their shit together
$(function(){
var speed = 80, times = 3;
$(document).on('mouseover', '[data-shuffle]', function (e) {
var $this = $(this);
if (!$this.data('text'))
$this.data('text', $this.text());
@christophemarois
christophemarois / es6.js
Last active August 30, 2016 18:05
In-progress playground for ES6 features
/* Async-await, stage-3 proposal in babel, so coming very soon */
// Explanation: inside an `async function funcName (){}`, `await` token
// waits in a non-io-blocking way for a promise to resolve before continuing
// the seemingly synchronous control flow
(async () => {
const delay = ms => new Promise(res => setTimeout(res, ms));