Lua String Manipulation Library
Table of Contents
StringizerLua is a string library is general port of Stringizer for PHP, a utility string library for Lua
PSR Compliance and Code Quality:
- Follows LuaRocks Style Guide
- Semver Versioning
Unstable in development as May 15th, 2022
Release 0.1.0
// Coming soon using LuaRocks
- May 21, 2022 Added tri, trim_left, trim_right, url_encode, url_decode, pad, pad_left, pad_right
- May 16, 2022 Remove the use of instantiating Stringizer instead string manipulation is exposed via static functions only. Added split
- May 15, 2022 Initial Release 0.1.0
The StringizerLua is licensed under the MIT license
is_string | is_empty | is_blank | base64_encode | base64_decode |
between | starts_with | ends_with | chop_left | chop_right |
split | trim | trim_left | trim_right | |
url_encode | url_decode | pad | pad_left | pad_right |
This retrieves the original given string value prior to any manipulation
Usage
print(Stringizer.is_string(42))
Output
false
Usage
print(Stringizer.is_string("valid-string"))
Output
true
Check is string empty including no whitespace
Usage (has whitespace)
print(Stringizer.is_empty(" "))
Output
false
Usage (no whitespace)
print(Stringizer.is_empty(""))
Output
true
Usage (nil)
print(Stringizer.is_empty(nil))
Output
true
Check is string blank contains whitespace
Usage (has whitespace)
print(Stringizer.is_blank(" "))
Output
true
Usage (has no whitespace)
print(Stringizer.is_blank(" "))
Output
true
Usage (has as non whitespace characters)
print(Stringizer.is_blank("test"))
Output
false
Base64 Encode given string
Usage (has whitespace)
print(Stringizer.base64_encode("ȘŦŗÍñĝìzĕŕ"))
Output
yJjFpsWXw43DscSdw6x6xJXFld+T
Base64 Decode given string
Usage (has whitespace)
print(Stringizer.base64_decode("yJjFpsWXw43DscSdw6x6xJXFld+T"))
Output
ȘŦŗÍñĝìzĕŕ
Extracts the a string from a larger string where both left and right side are identified
Usage
print(Stringizer.between("<div>ȘŦŗÍñĝìzĕŕ</div>", "<div>","</div>"))
Output
ȘŦŗÍñĝìzĕŕ
Checks if string begins with the supplied prefix
Usage
print(Stringizer.starts_with("Fizz Buzz", "Fizz"))
Output
true
Checks if string ends with the supplied suffix
Usage
print(Stringizer.ends_with("Fizz Buzz", "Buzz"))
Output
true
Modify string by dropping off the supplied prefix
Usage
print(Stringizer.chop_left("Fizz Buzz", "Fizz "))
Output
Buzz
Modify string by dropping off the supplied suffix
Usage
print(Stringizer.chop_left("Fizz Buzz", " Buzz"))
Output
Fizz
Split string into a list of words, split based on the given delimiter
Usage
local values = Stringizer.split("Fizz:Buzz:stringizer",":")
values of type list will contain 3 values: Fizz, Buzz, stringizer
Trim whitespace from string on both left and righ sides
Usage
print(Stringizer.trim(" Fizz Buzz ")
Output
Fizz Buzz
Trim whitespace from left side of string
Usage
print(Stringizer.trim(" Fizz Buzz")
Output
Fizz Buzz
Trim whitespace from right side of string
Usage
print(Stringizer.trim("Fizz Buzz ")
Output
Fizz Buzz
URL Encode string
Usage
print(Stringizer.url_encode("!@#$%^&*()-=_+[]\\{}|;':\",./<>?`~")
Output
%21%40%23%24%25%5E%26%2A%28%29-%3D_%2B%5B%5D%5C%7B%7D%7C%3B%27%3A%22%2C.%2F%3C%3E%3F%60~
URL Decode string
Usage
print(Stringizer.url_encode("%21%40%23%24%25%5E%26%2A%28%29-%3D_%2B%5B%5D%5C%7B%7D%7C%3B%27%3A%22%2C.%2F%3C%3E%3F%60~")
Output
!@#$%^&*()-=_+[]\\{}|;':\",./<>?`~
Pad Left side of string, pad_left(value, pad_number, value_append)
Usage
print(Stringizer.trim_left(" Fizz Buzz "))
Output
------Fizz Buzz
Pad Right side of string, pad_right(value, pad_number, value_append)
Usage
print(Stringizer.pad_right("Fizz Buzz", 15, "-"))
Output
Fizz Buzz------
Pad Left & Right side of string, pad(value, pad_number, value_append)
Usage
print(Stringizer.trim(" Fizz Buzz "))
Output
---FizzBuzz----
All tests are written using busted.
Install busted
:
$ luarocks install busted
Run All Tests:
$ busted --coverage ./spec
Code coverage is done using luacov.
Install luacov
:
$ luarocks install luacov
Run All Tests and check code coverage:
$ busted --coverage ./spec
$ luacov luacov.stats.out
$ cat luacov.report.out
You use the run_coverage bash script, friendly reminder you may need to make it executuable first
chomd +x run_coverage