Skip to content

Instantly share code, notes, and snippets.

@a-s-o
a-s-o / example-usage.js
Last active August 29, 2015 14:10
grid component for mithril (panels flow vertically, are absolutely positioned within the containing element and follow specified order)
import Grid from './grid.js';
var grid = new Grid();
m.module(document.getElementById("grid-container"), {
controller: function () {
grid.init();
// Generate a random number of panels (with random
// number of child tiles to create variations in height)
@a-s-o
a-s-o / collection.js
Last active August 29, 2015 14:11
Unique Sorted Set with binary search
class Collection {
constructor (opts = {}) {
this.list = [];
this.length = 0;
var isFn = $.is.function;
if (opts.key) {
var keyType = $.object.type(opts.key);
@a-s-o
a-s-o / stats.js
Created April 25, 2015 00:56
Basic functions for stats
function sum (arr) {
return arr.reduce((x, i) => x + i, 0);
}
function mean (arr) {
return sum(arr) / arr.length;
}
function variance (arr) {
let m = mean(arr);
@a-s-o
a-s-o / kefir.model.js
Last active August 29, 2015 14:22
Kefir model
/////////////
// Factory //
/////////////
Kefir.model = function (store, tx) {
if (typeof tx !== 'function') tx = x => x;
const getset = function kefir$model (update) {
if (arguments.length > 0) {
@a-s-o
a-s-o / action-validator.js
Created August 4, 2015 15:36
Validation system for cerebral actions using tcomb
function checkValue (msg, value, type) {
const check = t.validate(value, type);
if (!check.isValid()) {
console.error(msg + ' ' + check.firstError().message);
}
return value;
}
// This method accepts an object and returns a function that
@a-s-o
a-s-o / cerebral-action.js
Last active August 29, 2015 14:26
cerebral action draft implementation
// Action definition signature
const Definition = t.struct({
fn: t.Func,
displayName: t.maybe(t.Str),
inputs: t.maybe(t.Obj),
exits: t.maybe(t.Obj),
sync: t.maybe(t.Bool)
});
cerebral.action = function cerebral$action (action) {
@a-s-o
a-s-o / component.js
Last active August 29, 2015 14:27
Lifecycle hooks for mithril components
m.createComponent = function createClass (displayName, opts) {
const hooks = _.defaults(opts, hookDefaults); // Apply some defaults (excluded)
const methods = _.methods(_.omit(opts, hookNames)); // Get instance methods
const propTypes = createValidator(hooks.propTypes, displayName); // Create a prop type validator (excluded)
const Component = {
controller () {
const initialState = hooks.getInitialState() || {};
const ctrl = {
@a-s-o
a-s-o / index.js
Last active October 3, 2015 14:45
requirebin sketch
const t = require('tcomb');
const Person = t.struct({ name: t.Str, money: t.Num }, 'Person');
Person.prototype.pay = function (amount) {
return Person.update(this, {
money: { $set: this.money + t.Num(amount) }
});
};
@a-s-o
a-s-o / def.js
Last active May 12, 2016 00:56
Testing syntax for a validation/type-system
/**
* Internal helpers
*/
// Predicates can return true/false or they can return an error message.
// true, null, undefined and empty string are considered a valid result
function isValid (result) {
return result === true || result === null || result === void 0 || result === '';
}
@a-s-o
a-s-o / webpack.config.js
Created August 18, 2016 21:51
Move lodash chunks into a specified entry file if it is used in more than one module
new webpack.optimize.CommonsChunkPlugin({
name: 'someEntryFile',
minChunks (module, count) {
return module.resource && module.resource.indexOf('lodash') !== -1 && count > 1;
}
});