Initial commit
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
# Ignore the binary
|
||||||
|
snake_case
|
||||||
9
snake_case.nimble
Normal file
9
snake_case.nimble
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# Package
|
||||||
|
version = "1.0.0"
|
||||||
|
author = "Gustavo Córdova Avila"
|
||||||
|
description = "Convert strings to/from SNAKE_CASE"
|
||||||
|
license = "Apache-2.0"
|
||||||
|
srcDir = "src"
|
||||||
|
|
||||||
|
# Dependencies
|
||||||
|
requires "nim >= 2.0.0"
|
||||||
35
src/snake_case.nim
Normal file
35
src/snake_case.nim
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
## Convert strings to/from SNAKE_CASE
|
||||||
|
|
||||||
|
proc toSnakeCase(str: string): string =
|
||||||
|
## Convert a camelCase string to SNAKE_CASE
|
||||||
|
if str.len > 0:
|
||||||
|
var prv: char = '\0'
|
||||||
|
for ch in str:
|
||||||
|
if ch in {'A'..'Z'}:
|
||||||
|
if result.len > 0 and prv notin {'A'..'Z'}:
|
||||||
|
result.add '_'
|
||||||
|
result.add ch
|
||||||
|
elif ch in {'a'..'z'}:
|
||||||
|
result.add (ch.byte - 32).char
|
||||||
|
elif ch in {'0'..'9'}:
|
||||||
|
result.add ch
|
||||||
|
elif ch in {'-', '_', '.', '+'}:
|
||||||
|
if result.len > 0 and result[^1] != '_':
|
||||||
|
result.add '_'
|
||||||
|
|
||||||
|
proc fromSnakeCase(str: string): string =
|
||||||
|
## Convert from SNAKE_CASE to camelCase
|
||||||
|
var usc: bool = false
|
||||||
|
for ch in str:
|
||||||
|
if ch in {'A'..'Z'}:
|
||||||
|
if usc:
|
||||||
|
result.add ch
|
||||||
|
usc = false
|
||||||
|
else:
|
||||||
|
result.add (ch.byte + 32).char
|
||||||
|
elif ch in {'0'..'9', 'a'..'z'}:
|
||||||
|
result.add ch
|
||||||
|
elif ch in {'_', '-', '.', '+'}:
|
||||||
|
usc = true
|
||||||
|
|
||||||
|
# Fin.
|
||||||
1
tests/config.nims
Normal file
1
tests/config.nims
Normal file
@@ -0,0 +1 @@
|
|||||||
|
switch("path", "$projectDir/../src")
|
||||||
17
tests/test1_toSnakeCase.nim
Normal file
17
tests/test1_toSnakeCase.nim
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# This is just an example to get you started. You may wish to put all of your
|
||||||
|
# tests into a single file, or separate them into multiple `test1`, `test2`
|
||||||
|
# etc. files (better names are recommended, just make sure the name starts with
|
||||||
|
# the letter 't').
|
||||||
|
#
|
||||||
|
# To run these tests, simply execute `nimble test`.
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
import snake_case
|
||||||
|
|
||||||
|
test "toSnakeCase":
|
||||||
|
check "toSnakeCase".toSnakeCase() == "TO_SNAKE_CASE"
|
||||||
|
check "httpServerName".toSnakeCase() == "HTTP_SERVER_NAME"
|
||||||
|
check "portNum".toSnakeCase() == "PORT_NUM"
|
||||||
|
check "firstNameOnly".toSnakeCase() == "FIRST_NAME_ONLY"
|
||||||
|
check "rostr.token".toSnakeCase() == "ROSTR_TOKEN"
|
||||||
|
|
||||||
17
tests/test2_fromSnakeCase.nim
Normal file
17
tests/test2_fromSnakeCase.nim
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
# This is just an example to get you started. You may wish to put all of your
|
||||||
|
# tests into a single file, or separate them into multiple `test1`, `test2`
|
||||||
|
# etc. files (better names are recommended, just make sure the name starts with
|
||||||
|
# the letter 't').
|
||||||
|
#
|
||||||
|
# To run these tests, simply execute `nimble test`.
|
||||||
|
|
||||||
|
import unittest
|
||||||
|
import snake_case
|
||||||
|
|
||||||
|
test "fromSnakeCase":
|
||||||
|
check "TO_SNAKE_CASE".fromSnakeCase() == "toSnakeCase"
|
||||||
|
check "HTTP_SERVER_NAME".fromSnakeCase() == "httpServerName"
|
||||||
|
check "PORT_NUM".fromSnakeCase() == "portNum"
|
||||||
|
check "FIRST_NAME_ONLY".fromSnakeCase() == "firstNameOnly"
|
||||||
|
check "ROSTR_TOKEN".fromSnakeCase() == "rostrToken"
|
||||||
|
|
||||||
Reference in New Issue
Block a user