Skip to content

Commit 09fe5eb

Browse files
authored
Merge pull request #9 from jasonlam604/develop
Release 0.3.0
2 parents 4aaf3fc + c1c75f9 commit 09fe5eb

File tree

8 files changed

+261
-0
lines changed

8 files changed

+261
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package = "stringizer-lua"
2+
version = "0.3.0-0"
3+
source = {
4+
url = "git://github.com/jasonlam604/StringizerLua.git"
5+
}
6+
description = {
7+
summary = "Lua String manipulation made easy!",
8+
detailed = [[Handy little string library for string manipulation and checks]],
9+
homepage = "https://github.com/jasonlam604/StringizerLua",
10+
license = "MIT"
11+
}
12+
dependencies = {
13+
"lua >= 5.1"
14+
}
15+
build = {
16+
type = "builtin",
17+
modules = {
18+
requests = "src/stringizer.lua"
19+
}
20+
}

spec/pad_spec.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require "busted.runner"()
2+
3+
local Stringizer = require "stringizer"
4+
5+
describe("Stringizer", function()
6+
describe("Pad Test", function()
7+
8+
it("when left pad 'FizzBuzz' by 15 with - it should be '------Fizz Buzz'", function()
9+
assert.equal(Stringizer.pad_left("Fizz Buzz", 15, "-"),"------Fizz Buzz")
10+
end)
11+
12+
it("when right pad 'FizzBuzz' by 15 with - it should be 'Fizz Buzz------'", function()
13+
assert.equal(Stringizer.pad_right("Fizz Buzz", 15, "-"),"Fizz Buzz------")
14+
end)
15+
16+
it("should encode ȘŦŗÍñĝìzĕŕߓ to yJjFpsWXw43DscSdw6x6xJXFld+T", function()
17+
assert.equal(Stringizer.pad("FizzBuzz", 15, "-"),"---FizzBuzz----")
18+
end)
19+
20+
end)
21+
end)

spec/trim_spec.lua

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
require "busted.runner"()
2+
3+
local Stringizer = require "stringizer"
4+
5+
describe("Stringizer", function()
6+
describe("Test Trim", function()
7+
8+
it("should trim left of ' Fizz Buzz ' will result in 'Fizz Buzz '", function()
9+
assert.equal(Stringizer.trim_left(" Fizz Buzz "),"Fizz Buzz ")
10+
end)
11+
12+
13+
it("should trim right of ' Fizz Buzz ' will result in ' Fizz Buzz'", function()
14+
assert.equal(Stringizer.trim_right(" Fizz Buzz ")," Fizz Buzz")
15+
end)
16+
17+
it("should trim left and right of ' Fizz Buzz ' will result in 'Fizz Buzz'", function()
18+
assert.equal(Stringizer.trim(" Fizz Buzz "),"Fizz Buzz")
19+
end)
20+
21+
end)
22+
end)

spec/url_spec.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require "busted.runner"()
2+
3+
local Stringizer = require "stringizer"
4+
5+
describe("Stringizer", function()
6+
describe("Url Test", function()
7+
8+
it("should encode !@#$%^&*()-=_+[]\\{}|;':\",./<>?`~", function()
9+
assert.equal(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~")
10+
end)
11+
12+
it("should decode %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~", function()
13+
assert.equal(Stringizer.url_decode("%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~"),"!@#$%^&*()-=_+[]\\{}|;':\",./<>?`~")
14+
end)
15+
16+
end)
17+
end)

src/encoders/url.lua

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
------
2+
-- Url encoder and decoder
3+
--- @module encoders.url
4+
local Url = {}
5+
6+
function Url.new()
7+
local self = {}
8+
9+
-----
10+
-- Encode given string
11+
-- @return encoded value
12+
function self.encode(value)
13+
if (value) then
14+
value = string.gsub (value, "\n", "\r\n")
15+
value = string.gsub (value, "([^%w %-%_%.%~])",
16+
function (c) return string.format ("%%%02X", string.byte(c)) end)
17+
value = string.gsub (value, " ", "+")
18+
end
19+
return value
20+
end
21+
22+
-----
23+
-- Decode encoded string
24+
-- @return string
25+
function self.decode(value)
26+
value = string.gsub (value, "+", " ")
27+
value = string.gsub (value, "%%(%x%x)",
28+
function(h) return string.char(tonumber(h,16)) end)
29+
value = string.gsub (value, "\r\n", "\n")
30+
return value
31+
end
32+
33+
return self
34+
end
35+
36+
return Url

src/stringizer.lua

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
-----
22
-- Modules
33
local Base64 = require ("encoders.base64")
4+
local Url = require ("encoders.url")
5+
46
local Container = require ("checkers.container")
7+
58
local Between = require ("transformers.between")
69
local Chop = require ("transformers.chop")
10+
local Pad = require ("transformers.pad")
711
local Parts = require ("transformers.parts")
12+
local Trim = require ("transformers.trim")
813

914
------
1015
-- Create the table for the class definition
@@ -116,4 +121,60 @@ function Stringizer.split(value,delimiter)
116121
return (Parts.new()).split(value, delimiter)
117122
end
118123

124+
-----
125+
-- Remove whitespace on left side of string
126+
-- @return string
127+
function Stringizer.trim_left(value)
128+
return (Trim.new()).left(value)
129+
end
130+
131+
-----
132+
-- Remove whitespace on right side of string
133+
-- @return string
134+
function Stringizer.trim_right(value)
135+
return (Trim.new()).right(value)
136+
end
137+
138+
-----
139+
-- Remove whitespace on left and right side of string
140+
-- @return string
141+
function Stringizer.trim(value)
142+
return (Trim.new()).both(value)
143+
end
144+
145+
-----
146+
-- Encode URL
147+
-- @return string
148+
function Stringizer.url_encode(value)
149+
return (Url.new()).encode(value)
150+
end
151+
152+
-----
153+
-- Decode URL
154+
-- @return string
155+
function Stringizer.url_decode(value)
156+
return (Url.new()).decode(value)
157+
end
158+
159+
-----
160+
-- Pad Left
161+
-- @return string
162+
function Stringizer.pad_left(value, pad_number, value_append)
163+
return (Pad.new()).left(value, pad_number, value_append)
164+
end
165+
166+
-----
167+
-- Pad Right
168+
-- @return string
169+
function Stringizer.pad_right(value, pad_number, value_append)
170+
return (Pad.new()).right(value, pad_number, value_append)
171+
end
172+
173+
-----
174+
-- Pad Both
175+
-- @return string
176+
function Stringizer.pad(value, pad_number, value_append)
177+
return (Pad.new()).both(value, pad_number, value_append)
178+
end
179+
119180
return Stringizer

src/transformers/pad.lua

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
------
2+
-- Pad - Add indicated string padding to value left, right or both sides
3+
--- @module transformers.between
4+
local Pad = {}
5+
6+
function Pad.new()
7+
local self = {}
8+
9+
-----
10+
-- Left - Pad left of value the given char and given number of times
11+
-- @return string
12+
function self.left(s, l, c)
13+
local res = string.rep(c or ' ', l - #s) .. s
14+
return res, res ~= s
15+
end
16+
17+
-----
18+
-- Right - Pad right of value the given char and given number of times
19+
-- @return string
20+
function self.right(s, l, c)
21+
local res = s .. string.rep(c or ' ', l - #s)
22+
return res, res ~= s
23+
end
24+
25+
-----
26+
-- Both - Pad right & left of value the given char and given number of times
27+
-- @return string
28+
function self.both(s, l, c)
29+
if( l > string.len(s)) then
30+
c = c or ' '
31+
local pad_len = l - string.len(s)
32+
local target_len = pad_len / 2
33+
local repeat_len = string.len(c)
34+
local repeat_times = math.ceil(target_len / repeat_len)
35+
local repeated_string = string.rep(c, math.max(0, repeat_times))
36+
local before = string.sub(repeated_string, 0, math.floor(target_len))
37+
local after = string.sub(repeated_string, 0, math.ceil(target_len))
38+
return before .. s .. after
39+
else
40+
return s
41+
end
42+
end
43+
44+
return self
45+
end
46+
47+
return Pad

src/transformers/trim.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
------
2+
-- Trim - Trim whitespace from both sides, left side or right side
3+
--- @module transformers.chop
4+
local Trim = {}
5+
6+
function Trim.new()
7+
local self = {}
8+
9+
-----
10+
-- Remove whitespace left of string
11+
-- @return string
12+
function self.left(value)
13+
return value:match'^%s*(.*)'
14+
end
15+
16+
-----
17+
-- Remove whitespace right of string
18+
-- @return string
19+
function self.right(value)
20+
local n = #value
21+
while n > 0 and value:find("^%s", n) do
22+
n = n - 1
23+
end
24+
return value:sub(1, n)
25+
end
26+
27+
-----
28+
-- Remove whitespace both left and right of string
29+
-- @return string
30+
function self.both(value)
31+
return value:match'^()%s*$' and '' or value:match'^%s*(.*%S)'
32+
end
33+
34+
return self
35+
end
36+
37+
return Trim

0 commit comments

Comments
 (0)