Skip to content

Instantly share code, notes, and snippets.

View dexygen-gistio's full-sized avatar

dexygen-gistio

View GitHub Profile
@dexygen-gistio
dexygen-gistio / mr-resigs-deaf-ears.md
Last active December 23, 2015 13:59
mr resig's deaf ears

require.js: you're doing it wrong:

you, including Mr Burke himself
whose half hearted global pollution
witholds usability and strands,
glaringly, the obvious modules,
and dependencies;

you, including guru wannabes
and Crockford-ian anti-with-ites,

@dexygen-gistio
dexygen-gistio / if-else-switch-death-knell.md
Last active December 22, 2015 01:59
The death knell for if/else/switch

Turning once more to the outstanding underscore.js library -- see also http://gist.io/6376616 -- it turns out that with concerted effort you can do away with if/else statements. Consider for instance the following interface composed of (pun intended) _.compose and _.wrap

_.compose(submitForm, _.wrap(unlessFormInvalid, showFormErrors))();

If you can get past the noisiness of the actual invocation of underscore functions, and perhaps an aversion to Perl and/or it's 'unless' keyword, the above statement would be hard-pressed to better describe it's intentions, and furthermore lends itself to exclaiming: "Look Ma, No if/else!" However, I personally don't like the aforementioned noise, particularly if it were to be coupled with any necessary 'this.' prefixes, not to mention such an a

@dexygen-gistio
dexygen-gistio / filter-objects-with-underscorejs.md
Last active April 2, 2020 19:24
Filtering objects with underscore.js

underscore.js is a wonderful tool. Unfortunately there are some things you might think it would support, that it either doesn't, or that are difficult to achieve.

Filtering objects is one of these things. Filtering arrays is easy as using _.filter, but not so objects.

Considering the supreme hack which is the following, one might seriously consider using _.mixin to directly extend underscore.js itself and hide some of the implementation details. Nevertheless, here are those details in all their gory glory:

var compactObj = _.pick.apply({}, [fullObj].concat(_.filter(_.keys(fullObj), function(K) {
    return fullObj[K];
})));