Skip to content

Instantly share code, notes, and snippets.

View frostney's full-sized avatar
🐢

Johannes Stein frostney

🐢
View GitHub Profile
@frostney
frostney / tilemap.js
Created February 8, 2014 15:04
Simple TileMap
define('tilemap', function() {
var TileMap = (function() {
var TileMap = function(width, height) {
if (width == null) {
width = {
min: 0,
max: 4
};
@frostney
frostney / gist:9197604
Created February 24, 2014 21:31
Observable class draft, not complete what I want yet, just a little experiment
class Observable
constructor: (data) ->
@changes = []
Object.defineProperty @, 'data',
get: -> data
set: (value) ->
data = value
i() for i in @changes
value
@frostney
frostney / gist:10783806
Last active August 29, 2015 13:59
Prototyping GameObject-/Behavior-Relationship
class Behavior
constructor: (descriptor) ->
mixedice [@, @::], new EventMap()
@type = 'Behavior'
@name = "#{@type}-#{Date.now()}"
@children = {}
@parent = null
@frostney
frostney / gist:b179a80bfea5d19f50e7
Created May 7, 2014 21:56
Prototyping GameObjects with fluent APIs and tagged behaviors in a group
class GameObject
constructor: ->
@x = 0
@y = 0
@behaviors = {}
andObject = @
@move = (x, y) =>
@x += x
@frostney
frostney / gist:644da01f725b8c72f3a2
Created August 21, 2014 08:55
Normalizing a path
var normalize = function(path, delimiter) {
if (delimiter == null) {
delimiter = '/';
}
var pathArr = path.split(delimiter);
var newPath = [];
for (var i = 0, j = pathArr.length; i < j; i++) {
(function(item) {
@frostney
frostney / gist:ca41937e0eb86fb4a833
Created December 1, 2014 23:07
Requiring global properties like CommonJS modules
(function() {
var modules = {};
var require = function(name) {
var globals = Object.keys(window);
if (Object.keys(modules).length !== globals.length) {
globals.forEach(function(item) {
if (!Object.hasOwnProperty.call(modules, item)) {
modules[item] = window[item];
@frostney
frostney / gist:e2acfbd98d4a4e9c1024
Created February 15, 2015 00:27
propertyValue: Gets a value from deep/nested objects
var propertyValue = function(obj, key, separator = '.') {
var keyArray = key.split(separator);
var result = obj;
for (var i = 0, j = keyArray.length; i < j; i++) {
(function(name) {
result = result[name];
})(keyArray[i]);
}
@frostney
frostney / gist:9cdc735cc1e26487328e
Created February 23, 2015 21:52
Minimal CommonJS loader
(function(root) {
var modules = {};
var cache = {};
root.require = function(name) {
if (!Object.hasOwnProperty.call(cache, name)) {
var module = {
id: name,
};
@frostney
frostney / mixin.js
Created April 7, 2015 10:15
React mixins with decorators
var ReactMixin = function(...mixins) {
return function(Component) {
mixins.forEach(function(mixin) {
Object.keys(mixin).forEach(function(methodName) {
const methodValue = mixin[methodName];
if (methodName === 'statics') {
Object.keys(mixin.statics).forEach(function(staticProp) {
const staticValue = mixin.statics[staticProp];
@frostney
frostney / gist:78c9f6ceb70da39f4107
Last active August 29, 2015 14:18
Event Emitter experiment (<160 characters)
// Originally, I wanted to a simple Node.js Event Emitter-style object in less than 150 characters, but it just wouldn't fit
/*
Couple of thoughts
* The name Emitter shouldn't be changed
* `var` is missing, so it's directly bound to `window` and we're saving characters (Yeah for using a JS bad practice to our advantage)
* While `e` as the property isn't really amazing, it's just there to use as little characters as possible
* Obviously, there is no checking if the events really exist
* With ES6, method shorthands and arrow functions it would be considerably less than 150 characters