Skip to content

Instantly share code, notes, and snippets.

Avatar

Pontus Lundin hontas

View GitHub Profile
@hontas
hontas / minimalist-classes.js
Created Oct 23, 2012 — forked from BrendanEich/minimalist-classes.js
less minimalism, richer leather
View minimalist-classes.js
// 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 / gist:3937887
Created Oct 23, 2012
JavaScript: jQuery PubSub
View gist:3937887
(function($){
var o = $( {} );
$.each({
on: 'subscribe',
trigger: 'publish',
off: 'unsubscribe'
}, function( key, api ) {
$[api] = function() {
@hontas
hontas / gist:3937905
Created Oct 23, 2012
JavaScript: PubSub
View gist:3937905
// 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 / README.md
Last active Aug 29, 2015
Base project README
View README.md

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 / uniq.js
Last active Aug 29, 2015
Uniq implementation using Array.prototype.reduce
View uniq.js
/* 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 / validateYears.js
Created Jun 9, 2014
Good example of bad method naming
View validateYears.js
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 / bootstrapButtonComponent.js
Created May 28, 2014
Ember Bootstrap Button Component
View bootstrapButtonComponent.js
(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 / MutationObserver
Created Apr 4, 2014
A simple DOM-change handler using MutationObserver
View 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 / treeMatrix
Created Mar 9, 2014
Recursively walk through tree-structure
View treeMatrix
// 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;
You can’t perform that action at this time.