Skip to content

Instantly share code, notes, and snippets.

View mdix's full-sized avatar
🇩🇰

Marc Dix mdix

🇩🇰
View GitHub Profile
@mdix
mdix / getBaseClass.js
Last active October 16, 2019 11:23
Determine base class (last level before builtin)
function getBaseClass(MaybeClass) {
if (typeof MaybeClass !== 'function') {
return null;
}
const Proto = Reflect.getPrototypeOf(MaybeClass);
return !Proto.name
? MaybeClass
: getBaseClass(Proto);
@mdix
mdix / mixin.js
Created August 25, 2016 12:21
Mixin that doesn't know anything about the object it's mixed into. Check http://disq.us/p/1ba1awd for discussion
var Person = {
init: function(name) {
this.name = name;
},
doSomethingAndUseSpeak: function(text) {
this.speak({name: this.name, text})
}
};
var canSpeak = {
@mdix
mdix / xhr.js
Created November 15, 2016 14:56
XHR no connection wrapper
var req = new XMLHttpRequest();
req.open("post", 'https://jquery.com', true);
req.onreadystatechange = function receiveResponse() {
console.log('readyState: ', this.readyState);
console.log('status: ', this.status);
console.log('response: ', this.response.length);
if (this.readyState == 4) {
if (this.status === 0 && this.response.length === 0) {
return console.log('NO CON')
@mdix
mdix / mym.manageprofile.js
Created November 16, 2013 19:54
declare multiple vars with one var statment
var initialized = false,
user = {
'data': [
{}
]
},
dataUrl = '',
updateUrl = '',
requests = [];
// 9: object-literals - basics
// To do: make all tests pass, leave the assert lines unchanged!
describe('The object literal allows for new shorthands', () => {
const x = 1;
const y = 2;
describe('with variables', () => {
it('the short version for `{x: x}` is {x}', () => {
@mdix
mdix / email.txt
Last active June 12, 2016 11:18
E-Mail from Chad Parry
That looks great. Sorry it took me forever to take a look at it.
One pedantic thing that I would change is that I would aim for immutability whenever possible.
For example, ApplicationScope.setArgs (https://github.com/mdix/diy-di-typescript/blob/master/class/ApplicationScope.ts#L4)
wouldn't be necessary if instead the ApplicationScope took the args in the constructor.
That way, you are guaranteed that everything is initialized correctly throughout the entire program.
Another pedantic thing I would watch out for is to purge your scope class of any logic.
That includes array access, (https://github.com/mdix/diy-di-typescript/blob/master/class/Injector.ts#L17).
Even though "getArgs()[0]" looks harmless, it's the beginning of an ArgumentParser class, and so that's where that logic usually belongs.
Otherwise, more and more logic will creep in to your scope object.None of that logic will be testable.
On the other hand, keeping the logic in an ArgumentParser class means you can test the corner cases, such a
@mdix
mdix / someTest.js
Created December 18, 2014 12:35
jasmine data provider
define(['lib/validator/rule.notEmpty'], function(_ruleNotEmpty_) {
describe('rule.notEmpty', function() {
var ruleNotEmpty;
beforeEach(function() {
ruleNotEmpty = new _ruleNotEmpty_();
});
describe('its checkField function', function() {
// just wrap an it in a loop (if you have jquery you can use each)
@mdix
mdix / Gruntfile
Created January 28, 2014 10:11
grunt -vvvv dalek:phantom
/* globals module*/
module.exports = function(grunt) {
    grunt.initConfig({
dalek: {
chrome: {
options: {
browser: ['chrome']
},
src: 'src/spec/**/*chrome*.js'
},
@mdix
mdix / UserHandler.js
Created November 19, 2013 07:16
Two ways to update
db.user.findOne({'userid': '1'}, function(err, doc) {
db.user.update({'_id': doc._id}, {$set: {'mails.0.isRead': 'true'}}, function(err2, doc2) {
// doc2 is true / false
// doc has a reference to the document (so it will show the update)
});
});
db.user.findAndModify(
{
'query': {'userid': '1'},
@mdix
mdix / mym.manageprofile.js
Created November 16, 2013 19:58
newline after fct. declaration
var one = function() {
};
var two = function() {
};