1
1
mirror of https://github.com/bitgapp/eqMac.git synced 2024-12-12 06:43:07 +03:00
eqMac/native/app/Source/Extensions/StringExtensions.swift
2020-04-04 19:15:38 +01:00

57 lines
1.4 KiB
Swift

//
// StringExtensions.swift
// eqMac
//
// Created by Roman Kisil on 26/02/2018.
// Copyright © 2018 Roman Kisil. All rights reserved.
//
import Foundation
extension String {
func toJSON() -> Any? {
guard let data = self.data(using: .utf8, allowLossyConversion: false) else { return nil }
return try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
}
func toDictionary() -> [String: AnyObject]? {
if let data = self.data(using: .utf8) {
do {
return try JSONSerialization.jsonObject(with: data, options: []) as? [String: AnyObject]
} catch {
Console.log(error.localizedDescription)
}
}
return nil
}
var byteArray: [UInt8] {
return Array(self.utf8)
}
}
extension String: Error {}
extension String: LocalizedError {
public var errorDescription: String? { return self }
}
extension String {
func capitalizingFirstLetter() -> String {
return prefix(1).uppercased() + dropFirst()
}
var camelCasedString: String {
return self.components(separatedBy: " ")
.enumerated()
.map { (index, component) in
var result = component.lowercased()
if (index != 0) {
result = result.capitalizingFirstLetter()
}
return result
}
.joined()
}
}