Skip to content

Instantly share code, notes, and snippets.

/*
* The MIT License (MIT)
*
* Copyright (c) 2014 Matthieu Harlé
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
func timeAgoSinceDate(date:NSDate, numericDates:Bool) -> String {
let calendar = NSCalendar.currentCalendar()
let unitFlags = NSCalendarUnit.CalendarUnitMinute | NSCalendarUnit.CalendarUnitHour | NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitWeekOfYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitSecond
let now = NSDate()
let earliest = now.earlierDate(date)
let latest = (earliest == now) ? date : now
let components:NSDateComponents = calendar.components(unitFlags, fromDate: earliest, toDate: latest, options: nil)
if (components.year >= 2) {
return "\(components.year) years ago"
@barbietunnie
barbietunnie / strip-html-with-js
Last active August 29, 2015 14:19
Strip HTML using Javascript
myString.replace(/<(?:.|\n)*?>/gm, '');
//
// This is the template PFQueryTableViewController subclass file. Use it to customize your own subclass.
//
#import <UIKit/UIKit.h>
#import "Parse/Parse.h"
@interface MyPFQueryTableViewController : PFQueryTableViewController
@end
/* Delete orphaned wordpress posts */
DELETE pm
FROM wp_postmeta pm
LEFT JOIN wp_posts wp ON wp.ID = pm.post_id
WHERE wp.ID IS NULL
import Foundation
public func profiling<R>(label: String, @noescape _ block: () -> R) -> R {
NSLog("*** %@...", label)
let start = NSDate()
defer {
let end = NSDate()
NSLog("*** %@ took %5.3g seconds", label, end.timeIntervalSinceDate(start))
}
return block()
extension Array {
func first() -> Element? {
if isEmpty {
return nil
}
return self[0]
}
func last() -> Element? {
//
// dispatch_cancelable_block.h
// sebastienthiebaud.us
//
// Created by Sebastien Thiebaud on 4/9/14.
// Copyright (c) 2014 Sebastien Thiebaud. All rights reserved.
//
typedef void(^dispatch_cancelable_block_t)(BOOL cancel);
/**
* Delay the block specified by delay seconds
*
* With help from http://stackoverflow.com/questions/24034544/dispatch-after-gcd-in-swift/24318861#24318861
*/
func delay(delay:Double, closure:()->()) {
dispatch_after(
dispatch_time(
DISPATCH_TIME_NOW,
Int64(delay * Double(NSEC_PER_SEC))
typealias dispatch_cancelable_closure = (cancel : Bool) -> Void
/**
* Delay the execution of the block by time seconds
*
* With help from Waam at http://stackoverflow.com/questions/24034544/dispatch-after-gcd-in-swift/24318861#24318861
*/
func delay(time:NSTimeInterval, closure:()->Void) -> dispatch_cancelable_closure? {