Skip to content

Instantly share code, notes, and snippets.

@maxchuquimia
Created August 30, 2019 05:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxchuquimia/e4f349003acc1bf4c4730a86b503bbe4 to your computer and use it in GitHub Desktop.
Save maxchuquimia/e4f349003acc1bf4c4730a86b503bbe4 to your computer and use it in GitHub Desktop.
An old script of mine that tries to convert a string into Slack flag codes
// DEVELOPER_DIR=/Applications/Xcode9-beta.app/Contents/Developer/ swift text-to-flags.swift
import Foundation
let input = CommandLine.arguments[1].uppercased()
if input.rangeOfCharacter(from: CharacterSet(charactersIn: "ABCDEFGHIJKLMNOPQRSTUVWXYZ").inverted) != nil {
fatalError("unsupported character found")
}
func request(_ r: URLRequest) -> [String] {
var shouldExit = false
var s: [String] = []
let task = URLSession.shared.dataTask(with: r) { (data, _, error) in
defer {
shouldExit = true
}
s = (try! JSONSerialization.jsonObject(with: data!, options: [])) as! [String]
}
task.resume()
while !shouldExit { }
return s
}
let r = URLRequest(url: URL(string: "https://wrapapi.com/use/maxchuquimia/texttoflags/flags/0.0.1?wrapAPIKey=EhmzOcggZNsYoZgl120hmf8zbUDph8u8")!)
let result = request(r) + [""]
var inputLetters = Array(input).map({ String($0) })
var output = ""
while inputLetters.count > 0 {
let a = inputLetters.removeFirst()
if a.isEmpty {
break
}
var b = ""
if inputLetters.count > 0 {
b = inputLetters.removeFirst()
}
if result.contains("\(a)\(b)") {
// Raw flag
output = output + ":flag-\(a.lowercased())\(b.lowercased()):"
} else if result.contains("\(a)\(a)") && result.contains("\(b)\(b)") {
output = output + ":flag-\(a.lowercased())\(a.lowercased()):"
if b.characters.count > 0 {
// If b has no characters then we are at the end of the input string
output = output + ":flag-\(b.lowercased())\(b.lowercased()):"
}
} else if result.contains("\(a)\(a)") {
// Flag with both first characters, need to repeat with b
output = output + ":flag-\(a.lowercased())\(a.lowercased()):"
inputLetters.insert(b, at: 0)
} else {
var didFind = false
for panic in ["A", "E", "I", "O", "U", "M", "N", "L"] {
if result.contains("\(a)\(panic)") {
didFind = true
output = output + ":flag-\(a.lowercased())\(panic.lowercased()):"
break
}
}
if !didFind {
output = output + a
}
inputLetters.insert(b, at: 0)
}
}
print("\(input) -> \(output)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment