Otter
Packs
Otter
Upstream Contract & Behavioral Tests
text-utils Pack
Essential string and text formatting utilities: repeatText, padLeft, padRight, normalizeSpaces, slugify, toUpper, toLower, isEqualIgnoreCase, isNonEmpty, replaceText, containsText, startsWithText, endsWithText, splitText, and joinText.
Schema v2 Operation Contract (15 operations)
Scoping Prefix:_tu_
Schema v2 Pack exposing 15 structurally AST-verified Otter 1.0.1 operations with isolated prefix '_tu_'.
| Operation Signature | Returns | Side Effects | Error Mode |
|---|---|---|---|
repeatText(src: text, timesCount: number) |
text |
none | returns-fallback |
padLeft(text: text, targetLen: number, padChar: text) |
text |
none | returns-fallback |
padRight(text: text, targetLen: number, padChar: text) |
text |
none | returns-fallback |
normalizeSpaces(text: text) |
text |
none | returns-fallback |
slugify(text: text) |
text |
none | returns-fallback |
toUpper(text: text) |
text |
none | returns-fallback |
toLower(text: text) |
text |
none | returns-fallback |
isEqualIgnoreCase(left: text, right: text) |
boolean |
none | returns-false |
isNonEmpty(text: any) |
boolean |
none | returns-false |
replaceText(source: text, findStr: text, replaceStr: text) |
text |
none | returns-fallback |
containsText(source: text, searchStr: text) |
boolean |
none | returns-false |
startsWithText(text: text, prefix: text) |
boolean |
none | returns-false |
endsWithText(text: text, suffix: text) |
boolean |
none | returns-false |
splitText(text: text, separator: text) |
list |
none | returns-fallback |
joinText(parts: list, separator: text) |
text |
none | returns-fallback |
Source Code (4 files)
# text-utils - Otter Reusable String Library
# Provides repeatText, padLeft, padRight, slugify, toUpper, toLower,
# isEqualIgnoreCase, isNonEmpty, replaceText, containsText, startsWithText,
# endsWithText, splitText, joinText, and normalizeSpaces
# Internal variables use '_tu_' prefix to prevent collisions with caller/outer variables.
to repeatText src timesCount
if timesCount is at most 0
return ""
.
_tu_result is ""
count from 1 to timesCount as _tu_idx
_tu_result is _tu_result plus src
.
return _tu_result
.
to padLeft text targetLen padChar
_tu_curLen is length of text
if _tu_curLen is at least targetLen
return text
.
_tu_needed is targetLen minus _tu_curLen
_tu_padding is repeatText padChar _tu_needed
return _tu_padding plus text
.
to padRight text targetLen padChar
_tu_curLen is length of text
if _tu_curLen is at least targetLen
return text
.
_tu_needed is targetLen minus _tu_curLen
_tu_padding is repeatText padChar _tu_needed
return text plus _tu_padding
.
to normalizeSpaces text
split text by " " into _tu_parts
_tu_valid are empty
for each _tu_p in _tu_parts
if length of _tu_p is greater than 0
add _tu_p to _tu_valid
.
.
join _tu_valid with " " into _tu_trimmed
return _tu_trimmed
.
to slugify text
_tu_clean is lowercase of text
replace "." with "" in _tu_clean
replace "," with "" in _tu_clean
replace "!" with "" in _tu_clean
replace "?" with "" in _tu_clean
replace ":" with "" in _tu_clean
replace ";" with "" in _tu_clean
replace "(" with "" in _tu_clean
replace ")" with "" in _tu_clean
replace "[" with "" in _tu_clean
replace "]" with "" in _tu_clean
replace "{" with "" in _tu_clean
replace "}" with "" in _tu_clean
replace "_" with " " in _tu_clean
replace "/" with " " in _tu_clean
replace "\\" with " " in _tu_clean
replace "-" with " " in _tu_clean
_tu_norm is normalizeSpaces _tu_clean
replace " " with "-" in _tu_norm
return _tu_norm
.
to toUpper text
return uppercase of text
.
to toLower text
return lowercase of text
.
to isEqualIgnoreCase left right
_tu_lLeft is lowercase of left
_tu_lRight is lowercase of right
if _tu_lLeft is _tu_lRight
return true
.
return false
.
to isNonEmpty text
if text is gone
return false
.
if length of text is 0
return false
.
return true
.
to replaceText source findStr replaceStr
_tu_buf is source
replace findStr with replaceStr in _tu_buf
return _tu_buf
.
to containsText source searchStr
if source contains searchStr
return true
.
return false
.
to startsWithText text prefix
if text starts with prefix
return true
.
return false
.
to endsWithText text suffix
if text ends with suffix
return true
.
return false
.
to splitText text separator
split text by separator into _tu_parts
return _tu_parts
.
to joinText parts separator
join parts with separator into _tu_result
return _tu_result
.
{
"schemaVersion": 2,
"name": "text-utils",
"publisher": "otter-official",
"version": "1.0.1",
"kind": "pack",
"description": "Essential string and text formatting utilities: repeatText, padLeft, padRight, normalizeSpaces, slugify, toUpper, toLower, isEqualIgnoreCase, isNonEmpty, replaceText, containsText, startsWithText, endsWithText, splitText, and joinText.",
"entry": "src/main.ot",
"license": "MIT",
"requiresOtter": ">=1.0.1",
"platforms": [
"console",
"desktop",
"web"
],
"variablePrefix": "_tu_",
"provides": {
"capabilities": [
"text.formatting",
"text.manipulation",
"text.search"
],
"operations": [
{
"name": "repeatText",
"description": "Repeats a source string timesCount times.",
"parameters": [
{ "name": "src", "type": "text" },
{ "name": "timesCount", "type": "number" }
],
"returns": { "type": "text" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "padLeft",
"description": "Pads text on the left with padChar until reaching targetLen.",
"parameters": [
{ "name": "text", "type": "text" },
{ "name": "targetLen", "type": "number" },
{ "name": "padChar", "type": "text" }
],
"returns": { "type": "text" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "padRight",
"description": "Pads text on the right with padChar until reaching targetLen.",
"parameters": [
{ "name": "text", "type": "text" },
{ "name": "targetLen", "type": "number" },
{ "name": "padChar", "type": "text" }
],
"returns": { "type": "text" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "normalizeSpaces",
"description": "Collapses multiple consecutive whitespace characters and trims boundaries.",
"parameters": [
{ "name": "text", "type": "text" }
],
"returns": { "type": "text" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "slugify",
"description": "Converts string to lowercase ASCII kebab-case URL slug.",
"parameters": [
{ "name": "text", "type": "text" }
],
"returns": { "type": "text" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "toUpper",
"description": "Converts string characters to uppercase.",
"parameters": [
{ "name": "text", "type": "text" }
],
"returns": { "type": "text" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "toLower",
"description": "Converts string characters to lowercase.",
"parameters": [
{ "name": "text", "type": "text" }
],
"returns": { "type": "text" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "isEqualIgnoreCase",
"description": "Performs case-insensitive equality comparison between two strings.",
"parameters": [
{ "name": "left", "type": "text" },
{ "name": "right", "type": "text" }
],
"returns": { "type": "boolean" },
"sideEffects": ["none"],
"errorMode": "returns-false"
},
{
"name": "isNonEmpty",
"description": "Checks whether text is defined and contains at least one character.",
"parameters": [
{ "name": "text", "type": "any" }
],
"returns": { "type": "boolean" },
"sideEffects": ["none"],
"errorMode": "returns-false"
},
{
"name": "replaceText",
"description": "Replaces all occurrences of findStr with replaceStr in source.",
"parameters": [
{ "name": "source", "type": "text" },
{ "name": "findStr", "type": "text" },
{ "name": "replaceStr", "type": "text" }
],
"returns": { "type": "text" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "containsText",
"description": "Checks whether source string contains searchStr substring.",
"parameters": [
{ "name": "source", "type": "text" },
{ "name": "searchStr", "type": "text" }
],
"returns": { "type": "boolean" },
"sideEffects": ["none"],
"errorMode": "returns-false"
},
{
"name": "startsWithText",
"description": "Checks whether text begins with prefix substring.",
"parameters": [
{ "name": "text", "type": "text" },
{ "name": "prefix", "type": "text" }
],
"returns": { "type": "boolean" },
"sideEffects": ["none"],
"errorMode": "returns-false"
},
{
"name": "endsWithText",
"description": "Checks whether text finishes with suffix substring.",
"parameters": [
{ "name": "text", "type": "text" },
{ "name": "suffix", "type": "text" }
],
"returns": { "type": "boolean" },
"sideEffects": ["none"],
"errorMode": "returns-false"
},
{
"name": "splitText",
"description": "Splits text by separator into a list of substrings.",
"parameters": [
{ "name": "text", "type": "text" },
{ "name": "separator", "type": "text" }
],
"returns": { "type": "list" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
},
{
"name": "joinText",
"description": "Joins list elements into a single string separated by separator.",
"parameters": [
{ "name": "parts", "type": "list" },
{ "name": "separator", "type": "text" }
],
"returns": { "type": "text" },
"sideEffects": ["none"],
"errorMode": "returns-fallback"
}
]
},
"requires": {
"capabilities": [],
"packages": {}
},
"tags": [
"text",
"string",
"slugify",
"padding",
"repeat",
"format"
],
"examples": [
"examples/demo.ot"
]
}
# Tests for text-utils (Strict Behavioral Assertions)
use "../src/main.ot"
say "Running text-utils behavioral tests..."
# 1. RepeatText
r1 is repeatText "*" 4
if r1 is not "****"
fail with "repeatText failed: expected ****"
.
rZero is repeatText "*" 0
if rZero is not ""
fail with "repeatText at 0 failed: expected empty string"
.
# 2. PadLeft
p1 is padLeft "item" 7 "x"
if p1 is not "xxxitem"
fail with "padLeft shorter string failed: expected xxxitem"
.
if length of p1 is not 7
fail with "padLeft length failed: expected 7"
.
p2 is padLeft "12345" 3 "0"
if p2 is not "12345"
fail with "padLeft longer string failed: expected 12345"
.
# 3. PadRight
p3 is padRight "Hi" 5 "!"
if p3 is not "Hi!!!"
fail with "padRight failed: expected Hi!!!"
.
# 4. Slugify
s1 is slugify "Hello World Project"
if s1 is not "hello-world-project"
fail with "slugify spaces failed: expected hello-world-project"
.
s2 is slugify "user_auth_token"
if s2 is not "user-auth-token"
fail with "slugify underscores failed: expected user-auth-token"
.
s3 is slugify "Otter 1.0 Reusable Library... Release 2026!"
if s3 is not "otter-10-reusable-library-release-2026"
fail with "slugify punctuation stripping failed"
.
# 5. Case conversion (toUpper, toLower)
up is toUpper "hello otter"
if up is not "HELLO OTTER"
fail with "toUpper failed: expected HELLO OTTER"
.
low is toLower "HELLO OTTER"
if low is not "hello otter"
fail with "toLower failed: expected hello otter"
.
# 6. Case-insensitive equality
eq1 is isEqualIgnoreCase "OtterLang" "otterlang"
if eq1 is not true
fail with "isEqualIgnoreCase failed for matching text"
.
eq2 is isEqualIgnoreCase "OtterLang" "csharp"
if eq2 is not false
fail with "isEqualIgnoreCase failed for non-matching text"
.
# 7. isNonEmpty validation
if isNonEmpty "" is true
fail with "isNonEmpty failed: empty string reported as non-empty"
.
if isNonEmpty "valid" is false
fail with "isNonEmpty failed: valid string reported as empty"
.
# 8. replaceText
rep is replaceText "foo bar baz" "bar" "qux"
if rep is not "foo qux baz"
fail with "replaceText failed: expected foo qux baz"
.
# 9. containsText, startsWithText, endsWithText
cCheck is containsText "The quick brown fox" "brown"
if cCheck is not true
fail with "containsText failed: expected true"
.
swCheck is startsWithText "HelloWorld" "Hello"
if swCheck is not true
fail with "startsWithText failed: expected true"
.
ewCheck is endsWithText "HelloWorld" "World"
if ewCheck is not true
fail with "endsWithText failed: expected true"
.
# 10. splitText and joinText
parts is splitText "alpha,beta,gamma" ","
if length of parts is not 3
fail with "splitText failed: expected 3 items"
.
rejoined is joinText parts " - "
if rejoined is not "alpha - beta - gamma"
fail with "joinText failed: expected alpha - beta - gamma"
.
# 11. normalizeSpaces
norm is normalizeSpaces "hello world otter"
if norm is not "hello world otter"
fail with "normalizeSpaces failed: expected single spaces"
.
normTrim is normalizeSpaces " leading and trailing spaces "
if normTrim is not "leading and trailing spaces"
fail with "normalizeSpaces failed: expected trimmed leading and trailing spaces"
.
say "All text-utils behavioral tests passed successfully!"
# text-utils Demo
use "../src/main.ot"
say "=== text-utils Demo ==="
say "Repeat 5 times:" repeatText "*=" 5
say "Pad left number:" padLeft "42" 6 "0"
say "Pad right tag:" padRight "INFO" 8 "."
say "Slugify title:" slugify "Otter 1.0 Reusable Library Release"
say "Uppercase:" toUpper "make it loud"
say "Lowercase:" toLower "KEEP IT CALM"
say "Case-insensitive equal:" isEqualIgnoreCase "Release" "RELEASE"
say "Normalize spaces:" normalizeSpaces "Clean extra spacing"
tags is splitText "parser,lexer,runtime,cli" ","
say "Split count:" length of tags
say "Rejoined with arrow:" joinText tags " -> "
Installation & Usage
otter pack install otter-official/text-utils
otter pack verify --contracts
use ".otter/packages/otter-official/text-utils/1.0.1/src/main.ot" # Inspect exported operation contracts: otter pack contract otter-official/text-utils