Skip to content

Instantly share code, notes, and snippets.

View frostney's full-sized avatar
🐢

Johannes Stein frostney

🐢
View GitHub Profile
@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: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:4963462
Created February 15, 2013 20:54
Bind function one-liner
bind = (func, context) -> -> func.apply(context, arguments)
@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:5016824
Created February 22, 2013 21:53
Proof-of-concept for an AMD (Asynchronous module pattern) written in CoffeeScript, inspired by the way how ImpactJS handles modules
do (window = @) ->
cache = {}
window.module = (name) ->
deps = []
requires = (dependencies) ->
deps = if Array.isArray dependencies then dependencies else [dependencies]
{requires, defines}
@frostney
frostney / definition.coffee
Created February 22, 2013 22:23
Different approach for an AMD which doesn't have method chaining and looks more ideomatic in a CoffeeScript environment
do ->
cache = {}
window.module = (name, defines) ->
return undefined unless name and defines
if typeof defines is 'function' then define = defines else {define, require, context} = defines
require = [] unless require?
context = @ unless context?
@frostney
frostney / gist:5076576
Last active December 14, 2015 11:08
Different approach to properties in CoffeeScript class (CoffeeScript 1.5.0 compatible) Property names are not written as strings
Function::property = (prop) ->
for key, value of prop
Object.defineProperty @prototype, key, value
Function::staticProperty = (prop) ->
for key, value of prop
Object.defineProperty @, key, value
class MyClass
privateVar = 5
@frostney
frostney / classhelper.coffee
Last active December 14, 2015 11:19
Properties in CoffeeScript without modifying the Function prototype, plus it's also compatible to Codo (https://github.com/netzpirat/codo).
do (root = exports ? this) ->
'use check'
root.ClassHelper = (object) ->
objPrototype = object::
methods =
property: (prop) ->
for key, value of prop
propObject =
@frostney
frostney / gist:5126442
Created March 10, 2013 00:13
Small utility library
do (root = exports ? window.eos or= {}) ->
root.each = (object, callback) ->
if Array.isArray object
callback key, value for value, key in object
else
callback key, value for own key, value of object
null
root.isEmptyObject = (object) -> Object.keys(object).length is 0
@frostney
frostney / gist:5427786
Last active February 11, 2024 22:25
RequestAnimationFrame and CancelAnimationFrame as AMD modules
define('vendors', function() {
return ['ms', 'moz', 'webkit', 'o'];
});
define('requestAnimationFrame', ['root', 'vendors'], function(root, vendors) {
// frameRate is only used if requestAnimationFrame is not available
var frameRate = 60;
var requestAnimationFrame = root.requestAnimationFrame;