Skip to content

Instantly share code, notes, and snippets.

View wkronemeijer's full-sized avatar
📡

Wessel Kronemeijer wkronemeijer

📡
  • 18:34 (UTC +01:00)
View GitHub Profile
@wkronemeijer
wkronemeijer / getKeyValue.js
Last active June 18, 2016 17:53
key value paths for javascript
function getKeyValue(object, path, default_value){
"use strict"
if (typeof object === 'undefined') {
return default_value;
}
let prop_cont_pair = path.split('.');
let prop = prop_cont_pair[0];
let cont = prop_cont_pair.slice(1).join('.');
@wkronemeijer
wkronemeijer / setKeyValue.js
Last active June 18, 2016 17:53
set key value paths for javascript
function setKeyValue(object, path, value) {
var prop_cont_pair = path.split('.');
var prop = prop_cont_pair[0];
var cont = prop_cont_pair.slice(1).join('.');
if (cont === '') {
object[prop] = value;
} else {
if(typeof object[prop] === 'undefined') {
object[prop] = {};
@wkronemeijer
wkronemeijer / Array#contains
Created October 17, 2013 16:21
Array utility
Array.prototype.contains = function(element) {
return this.indexOf(element) !== -1;
};
@wkronemeijer
wkronemeijer / class inspector replacements
Created October 17, 2013 19:23
jQuery replacement functions
function hasClass(element, class_name) {
return element.classList.contains(class_name);
}
function addClass(element, class_name) {
if (!hasClass(element, class_name)) {
element.classList.add(class_name)
};
}
@wkronemeijer
wkronemeijer / KeyboardController
Last active December 26, 2015 00:09
Key timer function for reliable input, for use in games and similiar. Originally not mine, but this version uses `addEventListener`.
function KeyboardController(keys, repeat) {
var timers = {};
document.addEventListener('keydown', function(event) {
var key = event.keyCode;
if (!(key in keys))
return true;
if (!(key in timers)) {
timers[key] = null;
keys[key](event);
@wkronemeijer
wkronemeijer / augment
Last active December 26, 2015 01:39
Replace function with a new function, whilst still calling the original function.
function augment(obj, function_name, new_function) {
var original = obj[function_name];
obj[function_name] = function () {
new_function.bind(this, original).apply(this, arguments);
original.bind(this).apply(this, arguments);
};
}
@wkronemeijer
wkronemeijer / Array#remove
Created October 23, 2013 15:20
Removes all instances of a element in an array
Array.prototype.remove = function (element) {
while (this.contains(element)) {
this.splice(this.indexOf(element), 1);
}
}
@wkronemeijer
wkronemeijer / assert
Created October 23, 2013 15:20
assertions for debugging activities
window.assert = function assert(condition) {
if (!condition) {
debugger;
};
};
@wkronemeijer
wkronemeijer / call_super
Created October 23, 2013 15:22
super for any object with a prototype chain; usage: call_super(this, 'method')('hello', 'world')
window.call_super = function call_super(obj, name) {
var super_class = Object.getPrototypeOf(Object.getPrototypeOf(obj));
return (function () {
super_class[name].apply(obj, arguments);
})
}
@wkronemeijer
wkronemeijer / __module
Created November 2, 2013 16:51
simple augmentation module system for javascript. usage:
function __module(base, name, func) {
if (typeof base[name] === 'undefined') {
base[name] = {};
}
func(base[name]);
}
//usage:
// __module(window.MyModule, 'namespace', function (exports) {
// exports.sayHello = function () {