Skip to content

Instantly share code, notes, and snippets.

View frostney's full-sized avatar
🐢

Johannes Stein frostney

🐢
View GitHub Profile
@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 / 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 / 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 / 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 / collection.coffee
Created December 2, 2012 17:01
Collection class in Coffeescript including proxies (e.g. adding function to when a key is changed like triggering a redraw of the UI).
class Collection
data =
collection: {}
state: {}
proxies:
handler: {}
keyHandler: {}
contructor: (content) ->
if content
@frostney
frostney / gist:4171169
Created November 29, 2012 19:07
jQuery-like extend in Coffeescript
extend = (target, objects...) ->
for obj in objects
for key, value of obj
target[key] = value
target
# One object
a = extend {}, {test: 5}
alert JSON.stringify(a)
@frostney
frostney / gist:4165353
Created November 28, 2012 22:56
Walking through folder recursively async (node.js): includes option to filter elements, hidden dot files and filenames with trailing underscore. (walkFiles function taken from http://stackoverflow.com/questions/7041638/walking-a-directory-with-node-js)
var path = require('path');
var fs = require('fs');
var es6shim = require('es6-shim');
/**
* dir: path to the directory to explore
* action(file, stat): called on each file or until an error occurs. file: path to the file. stat: stat of the file (retrived by fs.stat)
* done(err): called one time when the process is complete. err is undifined is everything was ok. the error that stopped the process otherwise
*/
var walkFiles = function(dir, action, done) {
@frostney
frostney / gist:4144794
Last active October 13, 2015 05:17
Properties in CoffeeScript classes: Not completely my code, but I found it extremely useful
Function::property = (prop, desc) ->
Object.defineProperty @prototype, prop, desc
Function::staticProperty = (prop, desc) ->
Object.defineProperty @, prop, desc
class MyClass
privateVar = 5
testVar = 10
@frostney
frostney / gist:4144754
Created November 25, 2012 18:43
My .gitignore for delphi/freepascal projects
# Compiled source #
###################
*.com
*.class
*.dll
*.exe
*.o
*.ppu
*.dcu
*.so
@frostney
frostney / gist:4144734
Created November 25, 2012 18:39
My .gitignore for web projects
# Packages #
############
# it's better to unpack these files and commit the raw source
# git has its own built in compression methods
*.7z
*.dmg
*.gz
*.iso
*.jar
*.rar