Skip to content

Instantly share code, notes, and snippets.

@cmstead
cmstead / xhr-request.js
Last active November 17, 2015 01:40
XHR Request Module (non-browserify-export, no guarantees)
var xhr = (function () {
'use strict';
function buildParamPair (requestData, paramList, key) {
paramList.push(key + '=' + requestData[key].toString());
return paramList;
}
function buildGetParams (requestData) {
return Object.keys(requestData).reduce(buildParamPair.bind(null, requestData)).join('&');
@cmstead
cmstead / express-static-files.js
Created December 13, 2015 19:45
Express serving static files from a directory
// Serving static files in a named directory, served from the root path
app.use(express.static('path/to/static/files')); // This will pick up deep references within named directory
// http://localhost:3000/js/foo.js <- foo.js lives in path/to/static/files/js/foo.js
// Serving files from a virtual directory
app.use('/blar', express.static('path/to/static/files'));
@cmstead
cmstead / grunt-file-globbing.json
Created December 13, 2015 20:00
Grunt file object a handy reference for performing file globbing in Grunt
{
"src": "path/to/source",
"defaultEncoding": "utf8",
"preserveBOM": false,
"dest": "path/to/destionation/",
"ext": ".extensions.must.contain.dots",
"cwd": "start/in/this/directory/",
"expand": {
"cwd": true,
"match": ["file globbing patterns"],
@cmstead
cmstead / Vector.js
Last active February 24, 2016 04:57
Vector type for Javascript -- built for discussion on types and functions in Javascript
(function () {
'use strict';
function Vector() {
Vector.attachPoints(this, arguments);
}
// Inherted behaviors
Vector.prototype = {
'use strict';
var routeFunctions = {
delete: {},
get: {},
post: {},
put: {}
};
function routeStub(method, path, action) {
function simpleMerge (baseObj, extensionObj){
return Object.keys(extensionObj).reduce(function (baseObj, propertyKey) {
baseObj[propertyKey] = extensionObj[propertyKey];
return baseObj;
}, baseObj);
}
@cmstead
cmstead / stats-api.js
Created February 5, 2017 21:11
Simple stats API example
var statsApi = (function () {
'use strict';
var square = pow(2);
var squareRoot = pow(0.5);
function getStandardDeviation (values) {
var xBar = getMean(values);
var computedValues = values.map(subtractMeanAndSquare(xBar));
return squareRoot(getMean(computedValues));
@cmstead
cmstead / view-helper.js
Created February 5, 2017 21:12
Simple helper for Stats API example code
var viewHelper = (function () {
'use strict';
function parseNumberValues (valueStr) {
return valueStr.trim().split(/[\s\,]+/).map(parseFloat);
}
return {
parseNumberValues: parseNumberValues
};
@cmstead
cmstead / stats-computer.html
Last active February 7, 2017 06:47
User view for simple stats API example
<html>
<body>
<input type="text" value="" name="sample" id="sample" />
<button id="compute">Compute Mean and Standard Deviation</button>
<h4>Mean:</h4>
<p id="mean">0</p>
<h4>Standard Deviation:</h4>
@cmstead
cmstead / simple-ui-test-runner.js
Last active February 7, 2017 06:45
Simple UI test runner for stats example program
var testRunner = (function () {
'use strict';
function isMatch (expectedValue, actualValue) {
var equalMatch = expectedValue === actualValue;
var NaNMatch = Number.isNaN(expectedValue) && Number.isNaN(actualValue);
return equalMatch || NaNMatch;
}