Skip to content

Commit 08ed123

Browse files
committed
first commit
1 parent 37f23bd commit 08ed123

9 files changed

+402
-6
lines changed

.gitignore

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ xcuserdata/
1919

2020
## Other
2121
*.moved-aside
22-
*.xccheckout
23-
*.xcscmblueprint
22+
*.xcuserstate
2423

2524
## Obj-C/Swift specific
2625
*.hmap
@@ -35,9 +34,10 @@ playground.xcworkspace
3534
# Swift Package Manager
3635
#
3736
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
38-
# Packages/
39-
# Package.pins
37+
Packages/
4038
.build/
39+
*.xcodeproj
40+
Package.pins
4141

4242
# CocoaPods
4343
#
@@ -59,7 +59,7 @@ Carthage/Build
5959
# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the
6060
# screenshots whenever they are needed.
6161
# For more information about the recommended setup visit:
62-
# https://docs.fastlane.tools/best-practices/source-control/#source-control
62+
# https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md
6363

6464
fastlane/report.xml
6565
fastlane/Preview.html

CODE_OF_CONDUCT.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Contributor Covenant Code of Conduct
2+
3+
## Our Pledge
4+
5+
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
6+
7+
## Our Standards
8+
9+
Examples of behavior that contributes to creating a positive environment include:
10+
11+
* Using welcoming and inclusive language
12+
* Being respectful of differing viewpoints and experiences
13+
* Gracefully accepting constructive criticism
14+
* Focusing on what is best for the community
15+
* Showing empathy towards other community members
16+
17+
Examples of unacceptable behavior by participants include:
18+
19+
* The use of sexualized language or imagery and unwelcome sexual attention or advances
20+
* Trolling, insulting/derogatory comments, and personal or political attacks
21+
* Public or private harassment
22+
* Publishing others' private information, such as a physical or electronic address, without explicit permission
23+
* Other conduct which could reasonably be considered inappropriate in a professional setting
24+
25+
## Our Responsibilities
26+
27+
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
28+
29+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
30+
31+
## Scope
32+
33+
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
34+
35+
## Enforcement
36+
37+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at [[email protected]](mailto:[email protected]). All complaints will be reviewed and investigated and will result in a response that is deemed necessary and appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
38+
39+
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
40+
41+
## Attribution
42+
43+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
44+
45+
[homepage]: http://contributor-covenant.org
46+
[version]: http://contributor-covenant.org/version/1/4/

CONTRIBUTING.md

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Contributing to string-case-swift
2+
3+
First off, thanks for taking the time to contribute! :tada::+1:
4+
5+
The following is a set of guidelines for contributing to Digipolitan and its packages, which are hosted in the [Digipolitan Organization](https://github.com/digipolitan) on GitHub.
6+
These are just guidelines, not rules. Use your best judgment, and feel free to propose changes to this document in a pull request.
7+
8+
#### Table Of Contents
9+
10+
[What should I know before I get started?](#what-should-i-know-before-i-get-started)
11+
* [Code of Conduct](#code-of-conduct)
12+
13+
[How Can I Contribute?](#how-can-i-contribute)
14+
* [Reporting Bugs](#reporting-bugs)
15+
16+
## What should I know before I get started?
17+
18+
### Code of Conduct
19+
20+
This project adheres to the Contributor Covenant [code of conduct](CODE_OF_CONDUCT.md).
21+
By participating, you are expected to uphold this code.
22+
Please report unacceptable behavior to [[email protected]](mailto:[email protected]).
23+
24+
### Reporting Bugs
25+
26+
This section guides you through submitting a bug report for Digipolitan. Following these guidelines helps maintainers and the community understand your report :pencil:, reproduce the behavior :computer: :computer:, and find related reports :mag_right:.
27+
28+
Before creating bug reports, please check [this list](#before-submitting-a-bug-report) as you might find out that you don't need to create one. When you are creating a bug report, please [include as many details as possible](#how-do-i-submit-a-good-bug-report). If you'd like, you can use [this template](#template-for-submitting-bug-reports) to structure the information.
29+
30+
#### Before Submitting A Bug Report
31+
32+
* **Perform a [search](https://github.com/issues?q=+is%3Aissue+user%3Adigipolitan)** to see if the problem has already been reported. If it has, add a comment to the existing issue instead of opening a new one.
33+
34+
#### How Do I Submit A (Good) Bug Report?
35+
36+
Bugs are tracked as [GitHub issues](https://guides.github.com/features/issues/). After you've determined
37+
38+
Explain the problem and include additional details to help maintainers reproduce the problem:
39+
40+
* **Use a clear and descriptive title** for the issue to identify the problem.
41+
* **Describe the exact steps which reproduce the problem** in as many details as possible.
42+
* **Provide specific examples to demonstrate the steps**. Include links to files or GitHub projects, or copy/pasteable snippets, which you use in those examples. If you're providing snippets in the issue, use [Markdown code blocks](https://help.github.com/articles/markdown-basics/#multiple-lines).
43+
* **Describe the behavior you observed after following the steps** and point out what exactly is the problem with that behavior.
44+
* **Explain which behavior you expected to see instead and why.**
45+
* **Include screenshots and animated GIFs** which show you following the described steps and clearly demonstrate the problem.
46+
* **If you're reporting that string-case-swift crashed**, include a crash report with a stack trace from the operating system. On iOS, the crash report is available in Xcode, on macOS, the crash report will be available in `Console.app` under "Diagnostic and usage information" > "User diagnostic reports". Include the crash report in the issue in a [code block](https://help.github.com/articles/markdown-basics/#multiple-lines), a [file attachment](https://help.github.com/articles/file-attachments-on-issues-and-pull-requests/), or put it in a [gist](https://gist.github.com/) and provide link to that gist.
47+
* **If the problem is related to performance**, include a CPU profile capture and a screenshot with your report.
48+
* **If the problem wasn't triggered by a specific action**, describe what you were doing before the problem happened and share more information using the guidelines below.
49+
50+
Provide more context by answering these questions:
51+
52+
* **Did the problem start happening recently** (e.g. after updating to a new version of string-case-swift) or was this always a problem?
53+
* If the problem started happening recently, **can you reproduce the problem in an older version of string-case-swift ?** What's the most recent version in which the problem doesn't happen? You can download older versions of string-case-swift from [the releases page](https://github.com/digipolitan/string-case-swift/releases).
54+
* **Can you reliably reproduce the issue?** If not, provide details about how often the problem happens and under which conditions it normally happens.
55+
56+
Include details about your configuration and environment:
57+
58+
* **Which version of string-case-swift are you using?
59+
* **What's the name and version of the macOS or iOS you're using**?
60+
61+
#### Template For Submitting Bug Reports
62+
63+
[Short description of problem here]
64+
65+
**Reproduction Steps:**
66+
67+
1. [First Step]
68+
2. [Second Step]
69+
3. [Other Steps...]
70+
71+
**Expected behavior:**
72+
73+
[Describe expected behavior here]
74+
75+
**Observed behavior:**
76+
77+
[Describe observed behavior here]
78+
79+
**Screenshots and GIFs**
80+
81+
![Screenshots and GIFs which follow reproduction steps to demonstrate the problem](url)
82+
83+
**string-case-swift version:** [Enter string-case-swift version here]
84+
**OS and version:** [Enter macOS / iOS / linux name and version here]
85+
86+
**Installed packages:**
87+
88+
[List of installed packages here]
89+
90+
**Additional information:**
91+
92+
* Problem started happening recently, didn't happen in an older version of string-case-swift: [Yes/No]
93+
* Problem can be reliably reproduced, doesn't happen randomly: [Yes/No]
94+
* Problem happens with all files and projects, not only some files or projects: [Yes/No]

Package.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// swift-tools-version:4.0
2+
// The swift-tools-version declares the minimum version of Swift required to build this package.
3+
4+
5+
import PackageDescription
6+
7+
let package = Package(
8+
name: "StringCase",
9+
products: [
10+
// Products define the executables and libraries produced by a package, and make them visible to other packages.
11+
.library(
12+
name: "StringCase",
13+
targets: ["StringCase"]),
14+
],
15+
dependencies: [
16+
// Dependencies declare other packages that this package depends on.
17+
],
18+
targets: [
19+
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
20+
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
21+
.target(
22+
name: "StringCase",
23+
dependencies: []),
24+
.testTarget(
25+
name: "StringCaseTests",
26+
dependencies: ["StringCase"]),
27+
]
28+
)

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
# string-case-swift
1+
# string-case-swift

Sources/StringCase/StringCase.swift

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
//
2+
// StringCase.swift
3+
// StringCase
4+
//
5+
// Created by Benoit BRIATTE on 26/06/2017.
6+
//
7+
8+
import Foundation
9+
10+
public extension String {
11+
12+
public enum CaseFormat {
13+
14+
public enum SnakeCase {
15+
case lower
16+
case upper
17+
case capitalized
18+
}
19+
20+
public enum CamelCase {
21+
case `default`
22+
case capitalized
23+
}
24+
}
25+
26+
public func snakeCased(_ format: CaseFormat.SnakeCase = .lower) -> String {
27+
let alphanumerics = CharacterSet.alphanumerics
28+
let uppercaseLetters = CharacterSet.uppercaseLetters
29+
let lowercaseLetters = CharacterSet.lowercaseLetters
30+
var underscore = false
31+
var previousCase = 0 // 0 none, 1 upper, 2 lower, 3 other
32+
var currentCase = 0 // 0 none, 1 upper, 2 lower, 3 other
33+
var letterInWord = 0
34+
var res = ""
35+
for ch in self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) {
36+
for scalar in ch.unicodeScalars {
37+
if alphanumerics.contains(scalar) {
38+
if uppercaseLetters.contains(scalar) {
39+
currentCase = 1
40+
} else if lowercaseLetters.contains(scalar) {
41+
currentCase = 2
42+
} else {
43+
currentCase = 0
44+
}
45+
var str = String(scalar)
46+
var caseChange = underscore
47+
if !caseChange && letterInWord > 0 {
48+
if currentCase != previousCase {
49+
if previousCase == 1 {
50+
if letterInWord > 1 {
51+
caseChange = true
52+
}
53+
} else {
54+
caseChange = true
55+
}
56+
}
57+
}
58+
if caseChange {
59+
res += "_"
60+
letterInWord = 0
61+
}
62+
letterInWord += 1
63+
if format == .capitalized {
64+
if currentCase != 1 {
65+
if caseChange || previousCase == 0 {
66+
str = str.uppercased()
67+
}
68+
} else if !caseChange {
69+
str = str.lowercased()
70+
}
71+
} else if format == .upper && currentCase != 1{
72+
str = str.uppercased()
73+
} else if format == .lower && currentCase != 2 {
74+
str = str.lowercased()
75+
}
76+
res += str
77+
underscore = false;
78+
previousCase = currentCase;
79+
} else {
80+
underscore = true
81+
}
82+
}
83+
}
84+
return res
85+
}
86+
87+
public func camelCased(_ format: CaseFormat.CamelCase = .default) -> String {
88+
var uppercase = format == .capitalized;
89+
var res = ""
90+
let alphanumerics = CharacterSet.alphanumerics
91+
let lowercaseLetters = CharacterSet.lowercaseLetters
92+
for ch in self.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines) {
93+
for scalar in ch.unicodeScalars {
94+
if alphanumerics.contains(scalar) {
95+
var str = String(scalar)
96+
if uppercase {
97+
if lowercaseLetters.contains(scalar) {
98+
str = str.uppercased()
99+
}
100+
uppercase = false
101+
} else if res.count == 0 {
102+
str = str.lowercased()
103+
}
104+
res += str
105+
} else {
106+
uppercase = true
107+
}
108+
}
109+
}
110+
return res
111+
}
112+
}
113+

Tests/LinuxMain.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import XCTest
2+
@testable import StringCaseTests
3+
4+
XCTMain([
5+
testCase(StringCaseTests.allTests),
6+
])
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import XCTest
2+
@testable import StringCase
3+
4+
class StringCamelCaseTests: XCTestCase {
5+
func testCamelCase() {
6+
XCTAssertEqual("i love swift".camelCased(), "iLoveSwift")
7+
}
8+
9+
func testCamelCaseTrim() {
10+
XCTAssertEqual(" i love swift ".camelCased(), "iLoveSwift")
11+
}
12+
13+
func testCamelCaseTrimAndUpperLetter() {
14+
XCTAssertEqual(" I Love Swift ".camelCased(), "iLoveSwift")
15+
}
16+
17+
func testCapitalizeCamelCase() {
18+
XCTAssertEqual("i love swift".camelCased(.capitalized), "ILoveSwift")
19+
}
20+
21+
func testCapitalizeCamelCaseTrim() {
22+
XCTAssertEqual(" i love swift ".camelCased(.capitalized), "ILoveSwift")
23+
}
24+
25+
func testCapitalizeCamelCaseTrimAndUpperLetter() {
26+
XCTAssertEqual(" I Love Swift ".camelCased(.capitalized), "ILoveSwift")
27+
}
28+
29+
static var allTests: [(String, (StringCamelCaseTests) -> () -> Void)] = [
30+
("testCamelCase", testCamelCase),
31+
("testCamelCaseTrim", testCamelCaseTrim),
32+
("testCamelCaseTrimAndUpperLetter", testCamelCaseTrimAndUpperLetter),
33+
("testCapitalizeCamelCase", testCapitalizeCamelCase),
34+
("testCapitalizeCamelCaseTrim", testCapitalizeCamelCaseTrim),
35+
("testCapitalizeCamelCaseTrimAndUpperLetter", testCapitalizeCamelCaseTrimAndUpperLetter)
36+
]
37+
}

0 commit comments

Comments
 (0)