Skip to content

Instantly share code, notes, and snippets.

View harrytwright's full-sized avatar

Harry Wright harrytwright

  • Halifax, United Kingdom
View GitHub Profile
@harrytwright
harrytwright / Predicatable.swift
Last active April 30, 2022 13:01
Swift4 KeyPath Predicate
import Foundation
/// Predicatable is the protocol that all predicting objects conform.
public protocol Predicatable: CustomStringConvertible {
/// Returns a Boolean value indicating whether the specified object matches the conditions specified by the predicate.
///
/// - Parameter object: The object against which to evaluate the predicate.
/// - Returns: `true` if object matches the conditions specified by the predicate, otherwise `false`.
func evaluate(with object: Any?) -> Bool
import Foundation
internal protocol _AnyCodableBox {
var _base: Any { get }
var objectIdentifier: ObjectIdentifier { get }
}
extension AnyCodable: Codable {
public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
if let intValue = try? container.decode(Int.self) {
self._box = _ConcreteCodableBox<Int>(intValue)
} else if let doubleValue = try? container.decode(Double.self) {
self._box = _ConcreteCodableBox<Double>(doubleValue)
} else if let floatValue = try? container.decode(Float.self) {
self._box = _ConcreteCodableBox<Float>(floatValue)
import UIKit
@objc extension UIView {
@nonobjc public func addConstraintsWithFormat(_ format: String, views: UIView...) {
var viewsDictionary = [String: UIView]()
for (index, view) in views.enumerated() {
let key = "v\(index)"
viewsDictionary[key] = view
view.translatesAutoresizingMaskIntoConstraints = false
@harrytwright
harrytwright / Address.js
Created October 24, 2018 09:29
Change array value
let AddressSchema = mongoose.Schema({
formattedAddress: String,
components: [
{
longName: String,
shortName: String,
types: [String]
}
]
});
@harrytwright
harrytwright / promise-proto.js
Created September 24, 2019 10:32
Promise.allSettled polyfill
if (!Promise.allSettled) {
// Polyfill
Promise.prototype.allSettled = function(values) {
if (this == null) {
throw new TypeError('this is null or not defined');
}
return this.all(values.map(reflect));
};
@harrytwright
harrytwright / mongoose.discrimination.test.js
Last active November 21, 2019 17:38
This is how to use discrimination and virtual prototypes, for future reference
'use strict';
var chai = require('chai')
var mongoose = require('mongoose');
var Schema = mongoose.Schema;
const BaseSchema = new Schema({
name: { type: String, required: true }
}, { collection: 'products', discriminatorKey: 'type' });
const os = require('os')
const fs = require('fs')
const path = require('path')
const { exec } = require('child_process')
const tmpdir = os.tmpdir()
const directory = path.join(tmpdir, './docker-hook')
const avatar = "https://www.docker.com/sites/default/files/d8/2019-07/Moby-logo.png"
@harrytwright
harrytwright / get.swift
Last active May 16, 2022 16:01
Very basic implementaion of `lodash.get`. Not meant for production, just a proof
protocol KeyValuePathing {
subscript(keyPathWithString keyPath: String) -> Any? { get }
}
extension Array: KeyValuePathing {
subscript(keyPathWithString keyPath: String) -> Any? {
get {
var parts = keyPath.components(separatedBy: ".")
let initialKey = parts.removeFirst()