Skip to content

Instantly share code, notes, and snippets.

View rmirabelli's full-sized avatar
🐢
Slowly putting things together.

Russell Mirabelli rmirabelli

🐢
Slowly putting things together.
View GitHub Profile
@rmirabelli
rmirabelli / swiftregexsample3.swift
Created August 11, 2022 19:52
Sample of swift regex DSL
let regex3 = Regex {
"a"
Capture {
OneOrMore(.word)
}
" "
}
let match3 = baseString.firstMatch(of: regex3)
print(match3?.1)
@rmirabelli
rmirabelli / swiftregexsample2.swift
Created August 11, 2022 19:51
A simple regex using compile-time checking
let regex2 = /a(?<remainder>.*?)\ /
let match2 = baseString.firstMatch(of: regex2)
print(match2?.remainder)
@rmirabelli
rmirabelli / swiftregexsnippet1.swift
Created August 11, 2022 19:49
Example one: Typical regex syntax
let baseString = "An anchor or an anchorman, take your pick"
let regex = try Regex("a(.*) ")
let match = baseString.firstMatch(of: regex)
print(match?.0)
@rmirabelli
rmirabelli / AzureAppConfigSwiftSample.swift
Created January 21, 2022 18:59
Implementation of AzureAppConfiguration using the AzureAppConfiguration-Swift package.
let endpoint = "https://OBTAINED-FROM-DASHBOARD.azconfig.io"
let secret = "PASTE-SECRET-HERE"
let credential = "PASTE-CREDENTIAL-HERE"
// Fetch and decode the configuration
let request = try AzureAppConfiguration.prepareRequest(endpoint: endpoint, secret: secret, credential: credential)
let (data, _) = try await URLSession.shared.data(for: request)
let dictionary = try AzureAppConfiguration.decodeResponse(data: data)
@rmirabelli
rmirabelli / AzureAppConfigurationSampleResponse.json
Created January 21, 2022 18:43
A sample response from the Azure App Configuration Service.
{
"items": [
{
"etag": "A3uJKGd9vCNHpUzaDAMPiAFrZUK",
"key": "AndroidRequiredVersion",
"label": null,
"content_type": "",
"value": "2.1",
"tags": {},
"locked": false,
@rmirabelli
rmirabelli / AzureAppConfiguration.js
Created January 19, 2022 18:38
Reference Postman implementation for accessing Azure App Configuration.
// Set the Date header to our Date as a UTC String.
const dateStr = new Date().toUTCString();
pm.request.headers.upsert({key:'Date', value: dateStr});
// Hash the request body using SHA256 and encode it as Base64
const hashedBodyStr = CryptoJS.SHA256(pm.request.body.raw).toString(CryptoJS.enc.Base64)
// And add that to the header x-ms-content-sha256
pm.request.headers.upsert({
key:'x-ms-content-sha256',
value: hashedBodyStr
@rmirabelli
rmirabelli / asynctests.swift
Created December 17, 2021 20:14
Unit tests in Swift for async/await
import XCTest
@testable import AsyncAwaitModule
class AsyncAwaitModuleTests: XCTestCase {
// note the complexity in this unit test
func testClosure() throws {
let expectation = expectation(description: "posts fetch")
PostsService().fetchPosts { result in
@rmirabelli
rmirabelli / asyncawaitsample.swift
Created December 17, 2021 20:03
A sample using completions, and then with async/await
import Foundation
import UIKit
enum PostError: Error {
case badURL
case badResponse
case noData
case decodingError
}
@rmirabelli
rmirabelli / pub_sub_sample.swift
Created December 17, 2021 19:54
A sample from Apple's Documentation for Pub/Sub in Comine
let sub = NotificationCenter.default
.publisher(for: NSControl.textDidChangeNotification, object: filterField)
.map( { ($0.object as! NSTextField).stringValue } )
.filter( { $0.unicodeScalars.allSatisfy({CharacterSet.alphanumerics.contains($0)}) } )
.debounce(for: .milliseconds(500), scheduler: RunLoop.main)
.receive(on: RunLoop.main)
.assign(to:\MyViewModel.filterString, on: myViewModel)
@rmirabelli
rmirabelli / completionAsynchronousSample.swift
Created December 15, 2021 16:55
A sample using a closure for asynchronous tasks
func useAsynchronousFetch() -> Void {
fetchData(value: 23) { result in
switch result {
case .success(let data):
transformData(data) { result in
switch result {
case .success(let viewModel):
displayModel(viewModel)
case .failure(let error):
displayError(error)