Skip to content

Instantly share code, notes, and snippets.

View DigitalLeaves's full-sized avatar
👨‍💻
Focused on Your Company In Estonia now...

Ignacio Nieto DigitalLeaves

👨‍💻
Focused on Your Company In Estonia now...
View GitHub Profile
//
// String+NumericValue.swift
// Created by Ignacio Nieto Carvajal (https://digitalleaves.com)
//
import Foundation
extension String {
func toFailableBool() -> Bool? {
switch self.lowercased() {
@DigitalLeaves
DigitalLeaves / common elements in swift sequences
Last active March 6, 2017 18:16
A convenient function to get all common elements from two given sequences.
func commonElementsInSequences <T, U> (_ a: T, _ b: U) -> [T.Iterator.Element] where T: Sequence, U: Sequence, T.Iterator.Element: Equatable, T.Iterator.Element == U.Iterator.Element {
var returnArray:[T.Iterator.Element] = []
for aItem in a {
for bItem in b {
if aItem == bItem {
returnArray.append(aItem)
}
}
}
return returnArray
@DigitalLeaves
DigitalLeaves / genericNumber.swift
Last active March 6, 2017 18:28
A Generic number that allows us to perform generic operations on Int/Double/Float, etc, and a sequence extensions like "add 1"
public protocol GenericNumber : Comparable, ExpressibleByFloatLiteral, ExpressibleByIntegerLiteral, SignedNumber {
var value :Double { set get }
init(_ value: Double)
}
public func +<T: GenericNumber> (lhs: T, rhs: T) -> T { return T(lhs.value + rhs.value) }
public func -<T: GenericNumber> (lhs: T, rhs: T) -> T { return T(lhs.value - rhs.value) }
public func +<T: GenericNumber> (lhs: T, rhs: Double) -> T { return T(round(lhs.value + rhs)) }
public func -<T: GenericNumber> (lhs: T, rhs: Double) -> T { return T(lhs.value - rhs) }
extension GenericNumber {
@DigitalLeaves
DigitalLeaves / quickRelativeDate.swift
Created March 14, 2017 16:33
A quick and dirty way to get an approximate, relative date from a date.
import Foundation
extension Date {
func quickRelativeTime() -> String {
let then = self.timeIntervalSince1970
let now = Date().timeIntervalSince1970
let seconds = now - then
if seconds < 0 { return "Sometime in the future..." }
/*********** database.js ******************/
var mongoose = require('mongoose')
mongoose.Promise = global.Promise
mongoose.set('useFindAndModify', false)
// connection
exports.connection = null
// schemas and entities
@DigitalLeaves
DigitalLeaves / mappingExtensionsToFA.js
Last active May 23, 2022 16:47
Mapping file extensions to font awesome classes for easy UI display
const fontAwesomeIconClasses = {
// Media
"png": "fa-file-image-o",
"jpg": "fa-file-image-o",
"jpeg": "fa-file-image-o",
"gif": "fa-file-image-o",
"mp3": "fa-file-audio-o",
"mpg": "fa-file-video-o",
"mpeg": "fa-file-video-o",
"mp4": "fa-file-video-o",