Skip to content

Instantly share code, notes, and snippets.

View lorenzoongithub's full-sized avatar

lorenzo puccetti lorenzoongithub

  • london, united kingdom
View GitHub Profile
load('http://olado.github.io/doT/doT.min.js');
// USAGE
//1. Compile template function
// var tempFn = doT.template("<h1>Here is a sample template {{=it.foo}}</h1>");
// 2. Use template function as many times as you like
// var resultText = tempFn({foo: 'with doT'});
// INTERPOLATION.
//
// https://github.com/janl/mustache.js
// Logic-less templates
//
load('https://cdn.jsdelivr.net/mustache.js/0.8.1/mustache.js');
view = {
title: "Joe",
calc: function () {
return 2 + 4;
}
//
// http://mathjs.org
// An extensive math library for JavaScript and Node.js
// here is a port of http://mathjs.org/examples/basic_usage.js.html
//
load('http://cdnjs.cloudflare.com/ajax/libs/mathjs/1.2.0/math.min.js')
function json(x) { return JSON.stringify(x); }
@lorenzoongithub
lorenzoongithub / how-to-check-if-variable-is-int.js
Created July 3, 2015 18:33
how to check if a variable is an integer in javascript
//
// four different ways to check if a variable is an integer
// from stackoverflow
// http://stackoverflow.com/questions/14636536/how-to-check-if-a-variable-is-an-integer-in-javascript
//
//Raw solution
function isInt1(value) {
return !isNaN(value) && parseInt(Number(value)) == value && !isNaN(parseInt(value, 10));
}
//
// http://Ramdajs.org - a functional library
//
load('https://cdnjs.cloudflare.com/ajax/libs/ramda/0.14.0/ramda.js');
incr = R.add(1);
x = incr(10);
if (x !== 11) throw '';
x = R.all(function(x) { return x%2==0; },[1,2,3,4]);
if (x !== false) throw '';
x = R.any(function(x) { return x%2==0; },[1,2,3,4]);
//
// http://xtype.js.org
// Elegant, highly efficient data validation for JavaScript
//
load('https://rawgit.com/lucono/xtypejs/master/xtype.js');
x = xtype.isSingleCharString('g')
if (x !== true) throw '';
x = xtype.isNonEmptyObject({foo: 'bar'})
if (x !== true) throw '';
@lorenzoongithub
lorenzoongithub / jade-lang.js
Last active August 29, 2015 14:24
jade-lang.js
//
// Basic Show n Tell for jade. See: http://jade-lang.com/
//
load('https://cdn.jsdelivr.net/jade/1.3.1/jade.min.js');
f = jade.compile('b bold');
s = f();
if (s != '<b>bold</b>') throw '';
//
// A compiler for the Mustache templating language
// http://twitter.github.com/hogan.js
//
load('https://cdnjs.cloudflare.com/ajax/libs/hogan.js/3.0.2/hogan.js');
data = {
screenName: "dhg"
};
//
// https://github.com/marak/Faker.js/ -
// Generate massive amounts of fake contextual data
//
load('https://cdnjs.cloudflare.com/ajax/libs/Faker/0.7.2/MinFaker.js');
from = new Date(1990, 1,1);
to = new Date(2000, 1,1);
date = Faker.Date.between(from, to);
if (date > from) throw ''
//
// neural networks in javascript
// https://github.com/harthur/brain
//
load('http://harthur.github.io/brain/brain-0.6.3.js');
net = new brain.NeuralNetwork();
net.train([{input: [0, 0], output: [0]},
{input: [0, 1], output: [1]},