Skip to content

Commit

Permalink
Adding some helpful string extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik Sargent committed May 17, 2017
1 parent ac3de60 commit 078ff4a
Show file tree
Hide file tree
Showing 4 changed files with 159 additions and 4 deletions.
6 changes: 5 additions & 1 deletion Example/LKAPI.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
06B7E811FF9950DEA416F455 /* Pods_LKAPI_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B876C0A737409CA7D1E49274 /* Pods_LKAPI_Tests.framework */; };
607FACEC1AFB9204008FA782 /* APITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACEB1AFB9204008FA782 /* APITests.swift */; };
BF293BF11ECCDF2B00678711 /* StringTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BF293BF01ECCDF2B00678711 /* StringTests.swift */; };
BFFB61AC1D6B684600E7B5C3 /* ParsingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = BFFB61AB1D6B684600E7B5C3 /* ParsingTests.swift */; };
/* End PBXBuildFile section */

Expand All @@ -21,6 +22,7 @@
8642178C40AD8705BC647625 /* README.md */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = net.daringfireball.markdown; name = README.md; path = ../README.md; sourceTree = "<group>"; };
AF213812B10805C7C6D522F1 /* LICENSE */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text; name = LICENSE; path = ../LICENSE; sourceTree = "<group>"; };
B876C0A737409CA7D1E49274 /* Pods_LKAPI_Tests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_LKAPI_Tests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
BF293BF01ECCDF2B00678711 /* StringTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StringTests.swift; sourceTree = "<group>"; };
BFFB61AB1D6B684600E7B5C3 /* ParsingTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ParsingTests.swift; sourceTree = "<group>"; };
DBDD8842462905F99CB51A11 /* Pods-LKAPI_Tests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-LKAPI_Tests.release.xcconfig"; path = "Pods/Target Support Files/Pods-LKAPI_Tests/Pods-LKAPI_Tests.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -70,6 +72,7 @@
children = (
607FACEB1AFB9204008FA782 /* APITests.swift */,
BFFB61AB1D6B684600E7B5C3 /* ParsingTests.swift */,
BF293BF01ECCDF2B00678711 /* StringTests.swift */,
607FACE91AFB9204008FA782 /* Supporting Files */,
);
path = Tests;
Expand Down Expand Up @@ -196,7 +199,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
shellScript = "diff \"${PODS_ROOT}/../Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
shellScript = "diff \"${PODS_PODFILE_DIR_PATH}/Podfile.lock\" \"${PODS_ROOT}/Manifest.lock\" > /dev/null\nif [ $? != 0 ] ; then\n # print error to STDERR\n echo \"error: The sandbox is not in sync with the Podfile.lock. Run 'pod install' or update your CocoaPods installation.\" >&2\n exit 1\nfi\n";
showEnvVarsInLog = 0;
};
FC5DBFD43FF64C5BE7C072AF /* [CP] Embed Pods Frameworks */ = {
Expand All @@ -222,6 +225,7 @@
buildActionMask = 2147483647;
files = (
BFFB61AC1D6B684600E7B5C3 /* ParsingTests.swift in Sources */,
BF293BF11ECCDF2B00678711 /* StringTests.swift in Sources */,
607FACEC1AFB9204008FA782 /* APITests.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
6 changes: 3 additions & 3 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PODS:
- Alamofire (4.4.0)
- LKAPI (1.2.2):
- LKAPI (1.2.3):
- Alamofire

DEPENDENCIES:
Expand All @@ -13,8 +13,8 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
Alamofire: dc44b1600b800eb63da6a19039a0083d62a6a62d
LKAPI: bf6e5af601ad854c127412c3969f5803ef25157e
LKAPI: 1b44628c9a5678f4ae9eb80b36132d4fb647699a

PODFILE CHECKSUM: '0293954057b7f5af945b7e06279b1d86a8c755f1'

COCOAPODS: 1.2.0
COCOAPODS: 1.2.1
84 changes: 84 additions & 0 deletions Example/Tests/StringTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
//
// StringTests.swift
// LKAPI
//
// Created by Erik Sargent on 5/17/17.
// Copyright © 2017 CocoaPods. All rights reserved.
//

import XCTest


class StringTests: XCTestCase {
let camel = "thisIsWonderfulCamelCase"
let snakedCamel = "this_is_wonderful_camel_case"

let SuperCamel = "ThisIsWonderfulCamelCase"
let snakedSuperCamel = "this_is_wonderful_camel_case"

let snake = "this_is_horrible_snake_case"
let cameledSnake = "thisIsHorribleSnakeCase"

let capitalSnake = "This_Is_Horrible_Snake_Case"
let cameledCapitalSnake = "ThisIsHorribleSnakeCase"

let spaced = "this is what we are used to"
let blank = ""
let emoji = "😀_😃"
let cameledEmoji = "😀😃"

let kata = "カ_ヌ"
let cameledKata = "カヌ"

let accented = "résumé"

let date = "2017-04-03"
let dateTime = "2017-5-4T02:40:90"
let dateTimeZ = "2017-5-4T02:40:90Z"
let dateTimeZone = "2017-5-4T02:40:90-0700"
let dateGarbage = "2017-5-4stuff"
let dateTimeGarbage = "2017-5-4T02:40:90Stuff"


func testToCamel() {
XCTAssertEqual(camel.toCamel, camel)
XCTAssertEqual(SuperCamel.toCamel, SuperCamel)
XCTAssertEqual(snake.toCamel, cameledSnake)
XCTAssertEqual(capitalSnake.toCamel, cameledCapitalSnake)
XCTAssertEqual(spaced.toCamel, spaced)
XCTAssertEqual(blank.toCamel, blank)
XCTAssertEqual(emoji.toCamel, cameledEmoji)
XCTAssertEqual(kata.toCamel, cameledKata)
XCTAssertEqual(accented.toCamel, accented)
}

func testToSnake() {
XCTAssertEqual(camel.toSnake, snakedCamel)
XCTAssertEqual(SuperCamel.toSnake, snakedSuperCamel)
XCTAssertEqual(snake.toSnake, snake)
XCTAssertEqual(capitalSnake.toSnake, capitalSnake.lowercased())
XCTAssertEqual(spaced.toSnake, spaced)
XCTAssertEqual(blank.toSnake, blank)
XCTAssertEqual(emoji.toSnake, emoji)
XCTAssertEqual(kata.toSnake, kata)
XCTAssertEqual(accented.toSnake, accented)
}

func testIsDate() {
XCTAssertTrue(date.isDate)
XCTAssertFalse(dateTime.isDate)
XCTAssertFalse(dateTimeZ.isDate)
XCTAssertFalse(dateTimeZone.isDate)
XCTAssertFalse(dateGarbage.isDate)
XCTAssertFalse(dateTimeGarbage.isDate)
}

func testIsDateTime() {
XCTAssertFalse(date.isDateTime)
XCTAssertTrue(dateTime.isDateTime)
XCTAssertTrue(dateTimeZ.isDateTime)
XCTAssertTrue(dateTimeZone.isDateTime)
XCTAssertFalse(dateGarbage.isDateTime)
XCTAssertFalse(dateTimeGarbage.isDateTime)
}
}
67 changes: 67 additions & 0 deletions Pod/Classes/String.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
//
// String.swift
// Pods
//
// Created by Erik Sargent on 5/17/17.
//
//

import Foundation


extension String {
public var toCamel: String {
let comps = self.components(separatedBy: "_")
let notFirst = comps.dropFirst()

guard !comps.isEmpty else {
return ""
}

var value = comps.first!

notFirst.forEach { comp in
value += comp.capitalized
}

return value
}

public var toSnake: String {
var value = self

while true {
if let range = value.rangeOfCharacter(from: .uppercaseLetters) {
var letter = value.substring(with: range).lowercased()
if value.distance(from: value.startIndex, to: range.lowerBound) > 0 && !value.substring(with: value.index(before: range.lowerBound)..<range.upperBound).contains("_") {
letter = "_" + letter
}
value.replaceSubrange(range, with: letter)
} else {
break
}
}

return value
}

public var isDate: Bool {
guard let regex = try? NSRegularExpression(pattern: "^\\d{1,4}-\\d{1,2}-\\d{1,2}$", options: []) else {
return false
}

let matches = regex.matches(in: self, options: [], range: NSRange(location: 0, length: self.characters.count))

return matches.count > 0
}

public var isDateTime: Bool {
guard let regex = try? NSRegularExpression(pattern: "^\\d{1,4}-\\d{1,2}-\\d{1,2}T\\d\\d:\\d\\d:\\d\\d(Z|-\\d\\d\\d\\d)?$", options: []) else {
return false
}

let matches = regex.matches(in: self, options: [], range: NSRange(location: 0, length: self.characters.count))

return matches.count > 0
}
}

0 comments on commit 078ff4a

Please sign in to comment.