Skip to content

Instantly share code, notes, and snippets.

var compose = function compose(f) {
var queue = f ? [f] : [];
var fn = function fn(g) {
if (arguments.length) {
queue.push(g);
return fn;
}
return function() {
var args = Array.prototype.slice.call(arguments);
queue.forEach(function(func) {
@bttmly
bttmly / curry-compose.js
Last active August 29, 2015 14:02
Curry and compose for functional programming.
// adapted from:
// http://javascriptweblog.wordpress.com/2010/04/05/curry-cooking-up-tastier-functions/
// http://javascriptweblog.wordpress.com/2010/04/14/compose-functions-as-building-blocks/
function curry( fn ) {
if ( arguments.length < 2 ) {
return fn;
}
var args = [].slice.call( arguments, 1 );
return function() {
var getPrototypeChainOf = function( obj ) {
var chain = [];
if ( isFunction(obj) ) {
obj = obj.prototype;
} else {
obj = obj.constructor.prototype;
}
do {
@bttmly
bttmly / compose.coffee
Last active August 29, 2015 14:01
Function to compose a class from other classes.
compose = do ->
each = ( obj, itr ) ->
list = if Array.isArray( obj ) then obj.map ( e, i ) -> i else Object.keys( obj )
i = 0
while i < list.length
itr( obj[ list[i] ], list[ i ], obj )
i += 1
obj
@bttmly
bttmly / collection-styles.coffee
Created March 28, 2014 02:54
Two ways of structuring Controls.js collection classes.
# uses dummy elements to listen for/dispatch events
class ControlCollectionA extends Array
constructor : ( arr ) ->
this.push( item ) for item in arr
this.el = document.createElement "dummy"
on : ( eventType, handler ) ->
thisEl = this.el
thisEl.addEventListener eventType, handler.bind( this ), false
@bttmly
bttmly / prototype-chain.js
Created March 27, 2014 21:28
Get the prototype chain of an object.
var prototypeChain = function( o ) {
prototypes = [];
while ( o = Object.getPrototypeOf( o ) ) {
prototypes.push( o )
}
return prototypes;
}
<form action="your-action.lang" method="POST">
<script
src="https://my.tok3n.com/v1/embed.js" class="your-class"
data-publicKey="dOcPsnXndcRw3QdIosnE0j"
data-userKey="Ngfl6Gn2q15g8mR28vi2no"
data-theme="default"
data-action-name="Login"
data-app-name="Demo Site">
</script>
<input type="submit" />
var Tok3n = require('tok3n').Tok3n,
tok3n = new Tok3n('yourPrivateKey',
userKey,
req.body.tok3nOtpField);
tok3n.verify(function(success, error_code) {
if (success) {
// Successful verification
}
else {
<%@ page import="net.tok3n.Tok3nImpl" %>
<%@ page import="net.tok3n.Tok3nResponse" %>
<%
tok3n.setPrivateKey("your_private_key");
Tok3nImpl tok3n = new Tok3nImpl();
String otp = request.getParameter("tok3n_otp_field");
Tok3nResponse tok3nResponse = tok3n.verify(user_key, otp);
if (tok3nResponse.isValid()) {
// Successful verification
# Gemfile: gem 'tok3n', :require => "tok3n/rails"
respond_to do |format|
if tok3n_verify(:private_key => 'your_private_key',
:user_key => @user_key,
:otp => 'tok3n_otp_field')
# Successful verification
else
# Failed verification