Skip to content

Instantly share code, notes, and snippets.

View hontas's full-sized avatar

Pontus Lundin hontas

  • Stockholm, Sweden
View GitHub Profile
@hontas
hontas / treeMatrix
Created March 9, 2014 06:54
Recursively walk through tree-structure
// node { id: [Number], nodeTypeId: [String] }
// edge { fromNodeId: [Number], toNodeId: [Number], fromPort: [String] }
var decisionTree = {
nodes: [],
edges: [],
getTreeMatrix: function() {
var start = this.nodes.findBy('nodeTypeId', 'StartNode'),
self = this;
@hontas
hontas / MutationObserver
Created April 4, 2014 21:46
A simple DOM-change handler using MutationObserver
// MutationObserver
(function(){
var pluginName = "domChangeHandler";
var defaults = {};
var DOMChangeHandler = function(HTMLElement, options) {
this.options = options || {};
this.handler = new MutationsObserver(changeEventHandler);
this.handler.observe(HTMLElement);
@hontas
hontas / bootstrapButtonComponent.js
Created May 28, 2014 22:17
Ember Bootstrap Button Component
(function() {
"use strict";
var layout =
"{{yield}}" +
"{{currentText}}" +
"<span {{bind-attr class='iconClass loadingWhen:hide'}}></span>" +
"<span {{bind-attr class=':glyphicon :glyphicon-refresh loadingWhen:glyphicon-spin:hide'}}></span>";
App.BootstrapButtonComponent = Ember.Component.extend({
@hontas
hontas / validateYears.js
Created June 9, 2014 06:44
Good example of bad method naming
validateYears: function(Class, prop) {
var input = Ember.get(Class, prop),
regex = /\D*(\d*)\.?(\d{2}).*/,
result;
if (input.length > 0) {
this.set('errorMsg', null);
if (input.match(regex)) {
result = input.replace(regex, "$1,$2").replace(/[,]/g, '.');
@hontas
hontas / uniq.js
Last active August 29, 2015 14:07
Uniq implementation using Array.prototype.reduce
/* more functional version using concat */
function uniq(array) {
return array.reduce(function(result, currentElement) {
if (result.indexOf(currentElement) < 0) {
return results.concat([currentElement]);
}
return result;
}, []);
}
@hontas
hontas / README.md
Last active August 29, 2015 14:21
Base project README

Project name

optional tagline

Short description of what it is and what it does. Possibly even what it does not.

Local setup

  • Minimum possible steps
  • developer need take to start developing
@hontas
hontas / gist:3937887
Created October 23, 2012 09:33
JavaScript: jQuery PubSub
(function($){
var o = $( {} );
$.each({
on: 'subscribe',
trigger: 'publish',
off: 'unsubscribe'
}, function( key, api ) {
$[api] = function() {
@hontas
hontas / gist:3937905
Created October 23, 2012 09:37
JavaScript: PubSub
// Works in modern browsers + IE9, but Modernizr has a polyfill baked in for function.bind.
// Hat tip Paul Irish
var o = $( {} );
$.sub = o.on.bind(o);
$.unsub = o.off.bind(o);
$.pub = o.trigger.bind(o);
@hontas
hontas / minimalist-classes.js
Created October 23, 2012 10:46 — forked from BrendanEich/minimalist-classes.js
less minimalism, richer leather
// A response to jashkenas's fine proposal for minimalist JavaScript classes.
// Harmony always stipulated classes as sugar, so indeed we are keeping current
// JavaScript prototype semantics, and classes would only add a syntactic form
// that can desugar to ES5. This is mostly the same assumption that Jeremy
// chose, but I've stipulated ES5 and used a few accepted ES.next extensions.
// Where I part company is on reusing the object literal. It is not the syntax
// most classy programmers expect, coming from other languages. It has annoying
// and alien overhead, namely colons and commas. For JS community members who
@hontas
hontas / server.js
Created January 26, 2014 02:18
Express server for fakeEnd
var express = require('express'),
fs = require('fs'),
app = express(),
port = 8080;
function getInt (num) {
return parseInt(num, 10);
}
function readJSONFile (path, callback) {