Initial commit

This commit is contained in:
Gustavo Cordova Avila
2024-03-25 16:32:35 -07:00
commit b02bd3aaff
6 changed files with 81 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
# Ignore the binary
snake_case

9
snake_case.nimble Normal file
View 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
View 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
View File

@@ -0,0 +1 @@
switch("path", "$projectDir/../src")

View 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"

View 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"