Skip to content

Instantly share code, notes, and snippets.

@michaelficarra
michaelficarra / pseudo-privates.es5.js
Last active Oct 12, 2015
pseudo-private methods using a capabilities-based approach
View pseudo-privates.es5.js
var ClassName = (function() {
var PRIVATE = {};
function ClassName(state) {
this.state = state;
}
ClassName.prototype.publicMethod = function publicMethod() {
this.privateMethod(PRIVATE, "...");
};
@michaelficarra
michaelficarra / create-function-with-given-arity.js
Last active Aug 28, 2015
creating a function with a given name and arity in ES5 vs. ES2015
View create-function-with-given-arity.js
var apply = Function.prototype.call.bind(Function.prototype.apply);
function createFunctionES5(name, arity, behaviour) {
var params = Array.apply(null, Array(arity)).map(function (x, p) { return "p" + p; }).join(",");
var code = "return function " + name + "(" + params + ") { return apply(f, this, arguments); }";
return Function("apply", "f", code)(apply, behaviour);
}
var define = Object.defineProperty.bind(Object);
@michaelficarra
michaelficarra / simple-templating.js
Created Aug 11, 2015
simple templating system built with ES2015 tagged templates
View simple-templating.js
function escapeUsing(escaper) {
return function(literalParts, ...interpolatedParts) {
let s = literalParts[0];
for (let [interpolatedPart, literalPart] of zip(interpolatedParts, literalParts.slice(1))) {
s += escaper(interpolatedPart) + literalPart;
}
return s;
}
}
View countdown-timer.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Countdown Timer</title>
<style>
body, html {
display: flex;
align-items: center;
justify-content: center;
View Main.hs
{-# LANGUAGE PolyKinds, KindSignatures, MultiParamTypeClasses #-}
module Main where
class Category (arr :: k -> k -> *) where
id :: arr t t
(.) :: arr b c -> arr a b -> arr a c
instance Category (->) where
id x = x
View es6-validations.md
  • ArrayBinding

    • restElement must be BindingIdentifier or null except when ArrayBinding is part of an AssignmentPattern
  • BindingIdentifier

    • name must match the ES6 IdentifierName lexical grammar except when name is "*default*" and BindingIdentifier is the name of a ClassDeclaration or FunctionDeclaration which is the body of an ExportDefault
    • name must not be a reserved word
    • name must not be a future reserved word in strict mode
  • (12.1.1 and 12.14.5.1 and 14.1.2 and 14.4.1) name must not be a restricted word in strict mode

@michaelficarra
michaelficarra / phantom-rows.purs
Created Feb 25, 2015
phantom types using rows in PureScript; needs DataKinds to achieve full awesome
View phantom-rows.purs
module Main where
--data Language = English | Spanish
--data Censored = Censored | NotCensored
--data Encoding = Plain | EncodingA | EncodingB
data English
data Spanish
data Censored
@michaelficarra
michaelficarra / set.prototype.map.js
Last active Aug 29, 2015
ES6 Set.prototype.map issue
View set.prototype.map.js
var set = new Set([0, 1]),
f = function(x) { return 1 / x; },
g = function(x) { return x ? -0 : x; };
set.map(g); // Set{0}
set.map(g).map(f); // Set{1/0}
set.map(function(x){ return f(g(x)); }); // Set{1/0, -1/0}
View monads.js
function Container(val) {
this.val = val
}
// Functor's `fmap` in Haskell
Container.prototype.map = function(f) {
return new Container(f(this.val));
};
// Monad's `>>=` (pronounced bind) in Haskell
View textnode-innerhtml.js
/**
* Proof of concept ESLint rule for warning about potential
* XSS vulnerabilities caused by mixing innerHTML/text node
* property access.
*
* More here: http://benv.ca/2012/10/2/you-are-probably-misusing-DOM-text-methods/
*/
'use strict';
var WARNING_MSG =