Skip to content

Instantly share code, notes, and snippets.

View frostney's full-sized avatar
🐢

Johannes Stein frostney

🐢
View GitHub Profile
@frostney
frostney / mixins.coffee
Last active December 16, 2015 21:59
Mixins in CoffeeScript (does not override existing properties, but makes them into a function or an array)
mixin = (target, source...) ->
return unless target or source
for s in source
for key, value of s
unless Object.hasOwnProperty.call target, key
console.log key
console.log value
target[key] = value
else
@frostney
frostney / gist:5595008
Created May 16, 2013 20:52
ArrayList: Generic Java-/JavaScript-class for handling a list of values for FreePascal and Delphi
(**
* ArrayList.pas
* Provides a Java-/JavaScript-like generic class for handling a list of
* values
*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
@frostney
frostney / gist:5688215
Last active December 17, 2015 23:19
RequestAnimationFrame and CancelAnimationFrame as AMD modules (CoffeeScript edition)
do ->
vendors = ['ms', 'moz', 'webkit', 'o']
define 'requestAnimationFrame', ['root'], (root) ->
lastTime = 0
{requestAnimationFrame} = root
unless requestAnimationFrame
for x in vendors
requestAnimationFrame = root["#{x}RequestAnimationFrame"]
@frostney
frostney / gist:5784526
Last active December 18, 2015 12:39
Safe and fast alternative for deleting items from arrays and objects
deleteItem = (obj, item) ->
if Array.isArray obj
i for i, num in obj when num isnt item
else
newObject = {}
newObject[key] = obj[key] for key of obj when key isnt item
newObject
@frostney
frostney / amara.coffee
Last active December 18, 2015 15:58
Asynchronous module loader in about 50 lines of CoffeeScript. It's not a script loader though, all modules have to be in one file
do (root = if exports? then exports else @) ->
defined = []
modules = {}
evaluateModules = (ids, callback, after) ->
ids = [ids] if typeof deps is 'string'
depList = []
depLength = ids.length
@frostney
frostney / gist:6042112
Created July 19, 2013 20:30
EventMap in HaXe
package ;
/**
* ...
* @author Johannes Stein
*/
class EventMap
{
private var map: Map<String, Array<Dynamic -> Void>>;
@frostney
frostney / gist:6091445
Last active December 20, 2015 07:09
Trying to find a better alternative to setTimeout(fn, 0)
var nextTick = function(fn) {
var id = window.requestAnimationFrame(function() {
fn && fn();
window.cancelAnimationFrame(id);
});
};
@frostney
frostney / gist:6240668
Created August 15, 2013 13:05
Basic shim for getting performance.now() when now function is prefixed. (Prefixes everywhere :) )
var performance = window.performance;
performance.now = performance.now || (function() {
var vendors = ['ms', 'moz', 'webkit', 'o'];
var functionName = '';
for (var i = 0, j = vendors.length; i < j; i++) {
functionName = vendors[i] + 'Now';
if (performance[functionName]) {
return performance[functionName];
@frostney
frostney / gist:6245794
Last active December 21, 2015 03:58
Reusable mixins
do (root = @) ->
root.mixinList = {}
root.mixin = (target, name, params...) ->
root.mixin(target, n, params) for n in name if Array.isArray name
if typeof mixinList[name] is 'function'
mixinList[name].apply(target, params)
else
target[key] = value for key, value of mixinList[name] when not Object.hasOwnProperty.call target, key
@frostney
frostney / gist:6245988
Created August 15, 2013 23:40
Simple object traversion by dot notation
traverseObject = (o, key) ->
keyArray = if key.indexOf('.') > 0 then key.split('.') else [key]
for k in keyArray
return unless Object.hasOwnProperty.call o, k
o = o[k]
o