Skip to content

Instantly share code, notes, and snippets.

@jdivock
Created September 27, 2014 19:15
Show Gist options
  • Save jdivock/668e5d0f40e03e86986a to your computer and use it in GitHub Desktop.
Save jdivock/668e5d0f40e03e86986a to your computer and use it in GitHub Desktop.
date fuckery
var times = { "Schedule": {
"2014-09-27T00:00:00": [
16
],
"2014-09-28T00:00:00": [
8,
9,
10,
11,
14
]
}
};
_.map(times.Schedule, function(val, key) {
return _.map(val, function(hour, date){
var d = new Date(key);
return {
label: hour > 12 ? [hour - 12, '00 PM'].join(':') : [hour, '00 AM'].join(':'),
value: new Date(d.setHours(d.getHours() + hour)).toISOString()
}
});
});
@ethagnawl
Copy link

Here's my first stab at refactoring this. The only real differences are that I've given names to various operations and I'm using _.partial for redundant function calls with the same leading argument.

Pulling things apart and giving them names may seem like overkill, but I'd argue that it helps readability, the different pieces become easier to test and if there's a performance bottleneck (unlikely in this scenario...), it'd be trivial to use _.memoize to cache return values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment