Skip to content

Instantly share code, notes, and snippets.

View JoshOldenburg's full-sized avatar

Josh Oldenburg JoshOldenburg

View GitHub Profile
@JoshOldenburg
JoshOldenburg / JOComparisonResult.m
Created July 1, 2014 14:44
Objective-C simplified NSComparisonResult
typedef NS_ENUM(NSInteger, JODateComparisonResult) {JODateBefore = -1L, JODateSame, JODateAfter};
typedef NS_ENUM(NSInteger, JONumericalComparisonResult) {JONumericallySmaller = -1L, JONumericallyEqual, JONumericallyGreater};
@JoshOldenburg
JoshOldenburg / _.findKey.js
Created April 20, 2014 02:03
Underscore.js mixin which mimics _.find, except returning the key instead of the value
_.mixin({
findKey: function(obj, predicate, context) {
var result = null;
_.some(obj, function(value, index, list) {
if (predicate.call(context, value, index, list)) {
result = index;
return true;
}
});
return result;
@JoshOldenburg
JoshOldenburg / JOAssertEqualsString.h
Created June 22, 2013 14:12
JOAssertEqualsString for SenTestKit is like STAssertEquals but for NSString's.
#define JOAssertEqualsString(a1, a2, description, ...) \
do { \
@try {\
if (![a1 isKindOfClass:[NSString class]] || ![a2 isKindOfClass:[NSString class]]) { \
[self failWithException:([NSException failureInFile:[NSString stringWithUTF8String:__FILE__] \
atLine:__LINE__ \
withDescription:@"%@", [@"Type mismatch -- " stringByAppendingString:STComposeString(description, ##__VA_ARGS__)]])]; \
} \
else { \
if (![a1 isEqualToString:a2]) { \
@JoshOldenburg
JoshOldenburg / minToStr-spaces.js
Created April 30, 2013 00:40
Minutes to pretty formatted string
function formatPlural(val, singular, plural) {
if (val == 1) return val + '' + singular;
return val + '' + plural;
}
function minToStr(time) {
if (time >= (60 * 24 * 7)) {
var weeks = Math.floor(time / (60 * 24 * 7));
var remaining = Math.floor(time % (60 * 24 * 7));
return formatPlural(weeks, ' week', ' weeks') + ((remaining > 0) ? (', ' + minToStr(remaining)) : '');
@JoshOldenburg
JoshOldenburg / secondsToString.js
Created April 29, 2013 22:53
Turn amount of seconds into a pretty formatted string
function formatPlural(val, singular, plural) {
if (val == 1) return val + singular;
return val + plural;
}
function secondsToString(seconds) {
var time = Math.round(seconds / 60);
if (time >= (60 * 24)) {
var days = Math.floor(time / (24 * 60));
@JoshOldenburg
JoshOldenburg / array_merge.js
Created April 12, 2013 00:25
phpjs array_merge, formatted w/ tabs
function array_merge() {
// https://github.com/kvz/phpjs/blob/master/functions/array/array_merge.js
var args = Array.prototype.slice.call(arguments);
var argl = args.length;
var arg;
var retObj = {};
var k = '';
var argil = 0;
var j = 0;
var i = 0;