Skip to content

Commit 7abb672

Browse files
committed
Doc: update 'utils' documentation; Utils: reafactored;
1 parent 32598a0 commit 7abb672

File tree

3 files changed

+214
-62
lines changed

3 files changed

+214
-62
lines changed

doc/api/utils.markdown

Lines changed: 75 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,86 @@
1-
# utils
1+
# Utils
22

3-
These functions are in the module `'util'`. Use `require('util')` to access
3+
These functions and properties are in the module `'utils'`. Use `require('utils')` to access
44
them.
55

6-
## functions
6+
---
7+
8+
## Properties
9+
Use the color in the output. This Property used `dump`, `prettyPrint` and `debug` functions.
10+
11+
### useColors
12+
* value : boolean
13+
* default : true
14+
15+
local utils = require('utils')
16+
utils.useColors = false
17+
...
18+
utils.prettyPrint('hello world')
19+
20+
If you would not like to color output, set `false` to this property.
21+
22+
---
23+
24+
## Functions
725

826
### bind(fn, self, [...])
27+
Creates a new function that, when called, has its `self` keyword set to the provided value, with a given sequence of arguments preceding any provided when the new function was called.
28+
29+
#### parameters
30+
* fn : a target function (required)
31+
* self : a target object (required)
32+
* ... : arguments of `fn` function (optional)
33+
34+
#### returns
35+
Return a bound function.
36+
37+
local say = function (self, x, y, z)
38+
print(self.message, x, y, z)
39+
return self.message, x, y, z
40+
end
41+
42+
local fn = utils.bind(say, { message = 'hello' })
43+
print(fn(1, 2, 3)) -- 'hello', 1, 2, 3
44+
945

1046
### debug([...])
47+
A synchronous dump output function. Will block the process and output arguments immediately to `stderr`.
48+
49+
#### parameters
50+
* ... : some arguments (optional)
51+
52+
#### returns
53+
Return the `nil`.
54+
55+
utils.debug('hello world', 12, true)
56+
57+
If `useColors` is `false`, this function does not output the color codes.
58+
59+
60+
### dump(v, [depth])
61+
Return a string representation of value, which is useful for debugging.
62+
63+
#### parameters
64+
* o : a target value (required)
65+
* depth : an indent level (optional, default: 0)
66+
67+
#### returns
68+
Return a string of value. If `useColors` is `false`, Return a string does not include the color codes.
69+
70+
print(utils.dump(true))
71+
print(utils.dump({ hoge = 'hoge', foo = 1, bar = { a = 1, b = 2 } }, 2)
1172

12-
### dump(o, [depth])
1373

1474
### prettyPrint([...])
75+
A synchronous dump output function. Will block the process and output arguments immediately to `stdout`.
76+
77+
#### parameters
78+
* ... : some arguments (optional)
79+
80+
#### returns
81+
Return the `nil`.
82+
83+
utils.prettyPrint('hello world', 12, true)
84+
85+
If `useColors` is `false`, this function does not output the color codes.
1586

lib/lev/utils.lua

Lines changed: 21 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -41,71 +41,56 @@ local colors = {
4141
Bwhite = "1;37"
4242
}
4343

44-
if utils._useColors == nil then
45-
utils._useColors = true
44+
if utils.useColors == nil then
45+
utils.useColors = true
4646
end
4747

48-
function utils.color(color_name)
49-
if utils._useColors then
50-
return "\27[" .. (colors[color_name] or "0") .. "m"
51-
else
52-
return ""
53-
end
54-
end
55-
56-
function utils.colorize(color_name, string, reset_name)
57-
return utils.color(color_name) .. tostring(string) .. utils.color(reset_name)
48+
function color(color_name)
49+
return utils.useColors and ("\27[" .. (colors[color_name] or "0") .. "m") or ""
5850
end
5951

60-
local backslash, null, newline, carriage, tab, quote, quote2, obracket, cbracket
61-
62-
function utils.loadColors (n)
63-
if n ~= nil then utils._useColors = n end
64-
backslash = utils.colorize("Bgreen", "\\\\", "green")
65-
null = utils.colorize("Bgreen", "\\0", "green")
66-
newline = utils.colorize("Bgreen", "\\n", "green")
67-
carriage = utils.colorize("Bgreen", "\\r", "green")
68-
tab = utils.colorize("Bgreen", "\\t", "green")
69-
quote = utils.colorize("Bgreen", '"', "green")
70-
quote2 = utils.colorize("Bgreen", '"')
71-
obracket = utils.colorize("B", '[')
72-
cbracket = utils.colorize("B", ']')
52+
function colorize(color_name, string, reset_name)
53+
return color(color_name) .. tostring(string) .. color(reset_name)
7354
end
7455

75-
utils.loadColors ()
76-
7756
function utils.dump(o, depth)
7857
local t = type(o)
7958
if t == 'string' then
80-
return quote .. o:gsub("\\", backslash):gsub("%z", null):gsub("\n", newline):gsub("\r", carriage):gsub("\t", tab) .. quote2
59+
return colorize("Bgreen", '"', "green")
60+
.. o:gsub("\\", colorize("Bgreen", "\\\\", "green"))
61+
:gsub("%z", colorize("Bgreen", "\\0", "green"))
62+
:gsub("\n", colorize("Bgreen", "\\n", "green"))
63+
:gsub("\r", colorize("Bgreen", "\\r", "green"))
64+
:gsub("\t", colorize("Bgreen", "\\t", "green"))
65+
.. colorize("Bgreen", '"')
8166
end
8267
if t == 'nil' then
83-
return utils.colorize("Bblack", "nil")
68+
return colorize("Bblack", "nil")
8469
end
8570
if t == 'boolean' then
86-
return utils.colorize("yellow", tostring(o))
71+
return colorize("yellow", tostring(o))
8772
end
8873
if t == 'number' then
89-
return utils.colorize("blue", tostring(o))
74+
return colorize("blue", tostring(o))
9075
end
9176
if t == 'userdata' then
92-
return utils.colorize("magenta", tostring(o))
77+
return colorize("magenta", tostring(o))
9378
end
9479
if t == 'thread' then
95-
return utils.colorize("Bred", tostring(o))
80+
return colorize("Bred", tostring(o))
9681
end
9782
if t == 'function' then
98-
return utils.colorize("cyan", tostring(o))
83+
return colorize("cyan", tostring(o))
9984
end
10085
if t == 'cdata' then
101-
return utils.colorize("Bmagenta", tostring(o))
86+
return colorize("Bmagenta", tostring(o))
10287
end
10388
if t == 'table' then
10489
if type(depth) == 'nil' then
10590
depth = 0
10691
end
10792
if depth > 1 then
108-
return utils.colorize("yellow", tostring(o))
93+
return colorize("yellow", tostring(o))
10994
end
11095
local indent = (" "):rep(depth)
11196

@@ -213,25 +198,3 @@ end
213198

214199
return utils
215200

216-
--print("nil", dump(nil))
217-
218-
--print("number", dump(42))
219-
220-
--print("boolean", dump(true), dump(false))
221-
222-
--print("string", dump("\"Hello\""), dump("world\nwith\r\nnewlines\r\t\n"))
223-
224-
--print("funct", dump(print))
225-
226-
--print("table", dump({
227-
-- ["nil"] = nil,
228-
-- ["8"] = 8,
229-
-- ["number"] = 42,
230-
-- ["boolean"] = true,
231-
-- ["table"] = {age = 29, name="Tim"},
232-
-- ["string"] = "Another String",
233-
-- ["function"] = dump,
234-
-- ["thread"] = coroutine.create(dump),
235-
-- [print] = {{"deep"},{{"nesting"}},3,4,5},
236-
-- [{1,2,3}] = {4,5,6}
237-
--}))

tests/test-utils.lua

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
--[[
2+
3+
Copyright 2012 The lev Authors. All Rights Reserved.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS-IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
17+
--]]
18+
19+
local utils = require('utils')
20+
21+
local exports = {}
22+
23+
exports['lev.utils:\tdump: default'] = function (test)
24+
local dump = utils.dump
25+
test.ok(utils.useColors)
26+
27+
test.equal("\27[1;30mnil\27[0m", dump(nil)) -- nil
28+
test.equal('\27[1;32m"\27[0;32mh\27[1;32m\\\\\27[0;32me\27[1;32m\\n\27[0;32ml\27[1;32m\\r\27[0;32ml\27[1;32m\\t\27[0;32mowor\27[1;32m\\0\27[0;32mld\27[1;32m"\27[0m', dump('h\\e\nl\rl\towor\0ld')) -- string
29+
test.equal("\27[0;33mtrue\27[0m", dump(true)) -- boolean
30+
test.equal("\27[0;34m42\27[0m", dump(42)) -- number
31+
test.equal("\27[0;36m" .. tostring(print) .. "\27[0m", dump(print)) -- function
32+
33+
local thread = _coroutine.create(function () end)
34+
test.equal("\27[1;31m" .. tostring(thread) .. "\27[0m", dump(thread)) -- thread
35+
36+
-- table
37+
test.equal("{ }", dump({})) -- empty
38+
local inner_table = {}
39+
local ot = "{ a = \27[0;34m1\27[0m, b = { a = \27[0;34m2\27[0m, c = \27[0;33m" .. tostring(inner_table) .. "\27[0m } }"
40+
test.equal(ot, dump({ a = 1, b = { a = 2, c = inner_table } }))
41+
42+
-- TODO: dump userdata & cdata tests
43+
-- TODO: dump depth test
44+
45+
test.done()
46+
end
47+
48+
exports['lev.utils:\tdump: not use color'] = function (test)
49+
local dump = utils.dump
50+
utils.useColors = false
51+
52+
test.equal("nil", dump(nil)) -- nil
53+
test.equal('"h\\\\e\\nl\\rl\\towor\\0ld"', dump('h\\e\nl\rl\towor\0ld')) -- string
54+
test.equal("true", dump(true)) -- boolean
55+
test.equal("42", dump(42)) -- number
56+
test.equal(tostring(print), dump(print)) -- function
57+
58+
local thread = _coroutine.create(function () end)
59+
test.equal(tostring(thread), dump(thread)) -- thread
60+
61+
-- table
62+
test.equal("{ }", dump({})) -- empty
63+
local inner_table = {}
64+
local ot = "{ a = 1, b = { a = 2, c = " .. tostring(inner_table) .. " } }"
65+
test.equal(ot, dump({ a = 1, b = { a = 2, c = inner_table } }))
66+
67+
-- TODO: dump userdata & cdata tests
68+
-- TODO: dump depth test
69+
70+
test.done()
71+
end
72+
73+
exports['lev.utils:\tprint'] = function (test)
74+
test.not_throws(utils.print, 1)
75+
test.not_throws(utils.print, 1, 2)
76+
test.not_throws(utils.print, 1, 2, 'hello')
77+
78+
test.done()
79+
end
80+
81+
exports['lev.utils:\tprettyPrint'] = function (test)
82+
local thread = _coroutine.create(function () end)
83+
local io = require('io')
84+
test.not_throws(utils.prettyPrint, 1)
85+
test.not_throws(utils.prettyPrint, 1, true)
86+
test.not_throws(utils.prettyPrint, 1, true, 'hello', print, thread, {}, io)
87+
88+
test.done()
89+
end
90+
91+
exports['lev.utils:\tdebug'] = function (test)
92+
local thread = _coroutine.create(function () end)
93+
local io = require('io')
94+
test.not_throws(utils.debug, 1)
95+
test.not_throws(utils.debug, 1, true)
96+
test.not_throws(utils.debug, 1, true, 'hello', print, thread, {}, io)
97+
98+
test.done()
99+
end
100+
101+
exports['lev.utils:\tbind'] = function (test)
102+
local say = function (self, x, y, z)
103+
print(self.message, x, y, z)
104+
return self.message, x, y, z
105+
end
106+
107+
local fn = utils.bind(say, { message = 'hello' });
108+
test.ok(type(fn) == 'function')
109+
local msg, x, y, z = fn(1, 2, 3)
110+
test.equal('hello', msg)
111+
test.equal(1, x)
112+
test.equal(2, y)
113+
test.equal(3, z)
114+
115+
test.done()
116+
end
117+
118+
return exports

0 commit comments

Comments
 (0)