Created
September 27, 2014 19:15
-
-
Save jdivock/668e5d0f40e03e86986a to your computer and use it in GitHub Desktop.
date fuckery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.