Skip to content

Instantly share code, notes, and snippets.

View frostney's full-sized avatar
🐢

Johannes Stein frostney

🐢
View GitHub Profile
@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 / 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: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 / 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: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;
@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 / 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: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 / 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: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}