Skip to content

Instantly share code, notes, and snippets.

View frostney's full-sized avatar
🐢

Johannes Stein frostney

🐢
View GitHub Profile
@frostney
frostney / gist:4973597
Last active December 13, 2015 20:58
ES6-like string functions (they are not really compatible - like not at all - to the official specification though)
do (String) ->
String::contains or= (searchString, position = 0) -> @toString().indexOf(searchString) >= position
String::startsWith or= (searchString, position = 0) -> @toString.toString().indexOf(searchString) >= position
String::endsWith or= (searchString, position = @length) -> @toString().lastIndexOf(searchString) is position - searchString.length
String::repeat or= (count) ->
newString = ''
newString += @toString() for [1..count]
newString
String::toArray or= -> @toString().split ''
String::reverse or= -> @toArray().reverse().join ''
@frostney
frostney / gist:4963462
Created February 15, 2013 20:54
Bind function one-liner
bind = (func, context) -> -> func.apply(context, arguments)
@frostney
frostney / gist:4963372
Created February 15, 2013 20:38
Converts an objects into a query string
toQueryString = (obj) ->
queryArray = []
for key, value of obj
queryArray.push "#{key}=#{value}"
queryArray.join('&')
@frostney
frostney / gist:4757449
Created February 11, 2013 20:44
Helper function, which either creates an object with empty functions from an object with primitive types or an array - or - converts an object with functions into an object with function and method chaining
chainedOp = (obj) ->
result = {}
resultFunc = (i) ->
if Array.isArray obj
(result[i] = -> @) for i in obj
else
for key, value of obj
if typeof value is 'function'
result[key] = (args... ) ->
@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
@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 / 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: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 / 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: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