Skip to content

Instantly share code, notes, and snippets.

View frostney's full-sized avatar
🐢

Johannes Stein frostney

🐢
View GitHub Profile
@frostney
frostney / gist:4219529
Created December 5, 2012 21:11
deepEquals in CoffeeScript
deepEquals = (objA, objB) ->
# Simplest of all checks
return true if (objA is objB)
# Both parameters need to be objects
return false if (typeof objA isnt 'object') or (typeof objB isnt 'object')
# Special case for Date objects
if (objA instanceof Date) or (objB instanceof Date)
return false if ((objA instanceof Date) isnt (objB instanceof Date)) or (objA.getTime isnt objB.getTime)
@frostney
frostney / check.coffee
Last active October 13, 2015 20:48
Chainable type checks in CoffeeScript (Method chaining with type checks) Also allows to wrap type checks into an object, which is a safer option than a switch-case-typeof check AMD definition included :)
do (root = exports ? this) ->
'use strict'
root.check = (variable, checkObject) ->
stringedVar = {}.toString.call variable
typeName = stringedVar.slice(8, stringedVar.length - 1).toLowerCase()
checkType = (typeString, cb, inverse) ->
if inverse
@frostney
frostney / app.coffee
Created December 14, 2012 18:33
First draft of the MilkSugar web framework based on jQuery and Handlebars. This is not fully functional neither complete.
((window, MilkSugar, $) ->
"use strict"
# Rebind method for JSHint
check = window.check
class MilkSugar.App
constructor: (assets = ['image', 'view']) ->
if assets?
@frostney
frostney / gist:4373262
Created December 25, 2012 13:35
Simple namespacing mechanism in CoffeeScript
namespace = (name, func) ->
if name.indexOf('.') > 0
nameArray = name.split('.')
else
nameArray = [name]
context = window
for i in nameArray
context = context[i] or= {}
@frostney
frostney / gist:4402499
Created December 28, 2012 22:22
Rudimentary Entity-Component-model in CoffeeScript
class Entity
componentList = {}
constructor: (@name = @constructor.name) ->
get: (componentName) ->
if componentName
componentList[componentName]
else
Object.keys componentList
@frostney
frostney / gist:4447330
Created January 3, 2013 21:13
Alternative approach to converting CSON (CoffeeScript Object Notation) to JSON (JavaScript Object Notation) which does not require outer curly brackets to be present in the CSON.
fs = require 'fs'
path = require 'path'
coffeeScript = require 'coffee-script'
convertCSON = (csonObject) ->
return unless csonObject?
completeObject =
"csonObject = {
#{csonObject}
@frostney
frostney / gist:4461762
Last active December 10, 2015 16:38
Simple StringReader class in CoffeeScript which can be used to extract specific tokens from a bigger string. Inspired by http://brandoncodes.wordpress.com/2012/04/07/add-some-syntactical-sugar-to-your-coffeescript/
class StringReader
buffer = ""
constructor: (@input) ->
@reset()
readUntil: (character, omitCharacter = false) ->
loop
break if @currentIndex is @input.length
@frostney
frostney / examples.coffee
Created January 20, 2013 16:07
A bit more advanced Entity-Component-Model (based on https://gist.github.com/4402499) which takes advantage of the dynamicness of JavaScript by adding the prototype and property functions from components to the entity itself.
# Example 1
# Adding properteies to Component instances
console.log '--- Example 1 ---'
comp1 = new Component('comp1')
comp1.test = -> console.log 'Comp1'
comp2 = new Component('comp2')
comp2.test = -> console.log 'Comp2'
myEntity = new Entity('myEntity')
@frostney
frostney / gist:4735272
Created February 7, 2013 23:39
Rudimentary CSS parser written in about 100 lines of CoffeeScript
class StringReader
buffer = ""
constructor: (@input) ->
@reset()
readUntil: (character, omitCharacter = false, notFoundFn) ->
loop
if @end()
notFoundFn(buffer) if notFoundFn
@frostney
frostney / gist:4746769
Last active December 12, 2015 08:49
StringBuilder class in CoffeeScript
do (root = if exports? and module.exports? then module.exports else @) ->
class root.StringReader
constructor: (@input) ->
@buffer = ""
@reset()
readUntil: (character, omitCharacter = false, notFoundFn) ->
loop