|
1 | | -local indentStr = " " |
2 | | - |
3 | | -local function prettyPrint(t, indent) |
4 | | - indent = indent or 1 |
5 | | - local outputBuffer = { |
6 | | - "{\n", |
7 | | - } |
8 | | - |
9 | | - for key, value in pairs(t) do |
10 | | - local strKey = tostring(key) |
11 | | - |
12 | | - table.insert(outputBuffer, indentStr:rep(indent + 1)) |
13 | | - table.insert(outputBuffer, strKey) |
14 | | - table.insert(outputBuffer, " = ") |
15 | | - |
16 | | - if typeof(value) == "table" then |
17 | | - table.insert(outputBuffer, prettyPrint(value, indent + 1)) |
18 | | - table.insert(outputBuffer, "\n") |
19 | | - else |
20 | | - table.insert(outputBuffer, tostring(value)) |
21 | | - table.insert(outputBuffer, "; (") |
22 | | - table.insert(outputBuffer, typeof(value)) |
23 | | - table.insert(outputBuffer, ")\n") |
| 1 | +local indent = " " |
| 2 | + |
| 3 | +local function prettyPrint(value, indentLevel) |
| 4 | + indentLevel = indentLevel or 0 |
| 5 | + local output = {} |
| 6 | + |
| 7 | + if typeof(value) == "table" then |
| 8 | + table.insert(output, "{\n") |
| 9 | + |
| 10 | + for key, value in pairs(value) do |
| 11 | + table.insert(output, indent:rep(indentLevel + 1)) |
| 12 | + table.insert(output, tostring(key)) |
| 13 | + table.insert(output, " = ") |
| 14 | + |
| 15 | + table.insert(output, prettyPrint(value, indentLevel + 1)) |
| 16 | + table.insert(output, "\n") |
24 | 17 | end |
25 | | - end |
26 | 18 |
|
27 | | - table.insert(outputBuffer, indentStr:rep(indent)) |
28 | | - table.insert(outputBuffer, "}") |
| 19 | + table.insert(output, indent:rep(indentLevel)) |
| 20 | + table.insert(output, "}") |
| 21 | + elseif typeof(value) == "string" then |
| 22 | + table.insert(output, string.format("%q", value)) |
| 23 | + table.insert(output, " (string)") |
| 24 | + else |
| 25 | + table.insert(output, tostring(value)) |
| 26 | + table.insert(output, " (") |
| 27 | + table.insert(output, typeof(value)) |
| 28 | + table.insert(output, ")") |
| 29 | + end |
29 | 30 |
|
30 | | - return table.concat(outputBuffer, "") |
| 31 | + return table.concat(output, "") |
31 | 32 | end |
32 | 33 |
|
33 | | -local function loggerMiddleware(outputFunction) |
34 | | - outputFunction = outputFunction or print |
| 34 | +-- We want to be able to override outputFunction in tests, so the shape of this |
| 35 | +-- module is kind of unconventional. |
| 36 | +-- |
| 37 | +-- We fix it this weird shape in init.lua. |
| 38 | +local loggerMiddleware = { |
| 39 | + outputFunction = print, |
| 40 | +} |
35 | 41 |
|
36 | | - return function(next) |
37 | | - return function(store, action) |
38 | | - local result = next(store, action) |
| 42 | +function loggerMiddleware.middleware(next) |
| 43 | + return function(store, action) |
| 44 | + local result = next(store, action) |
39 | 45 |
|
40 | | - outputFunction(("Action dispatched: %s\nState changed to: %s"):format( |
41 | | - prettyPrint(action), |
42 | | - prettyPrint(store:getState())) |
43 | | - ) |
| 46 | + loggerMiddleware.outputFunction(("Action dispatched: %s\nState changed to: %s"):format( |
| 47 | + prettyPrint(action), |
| 48 | + prettyPrint(store:getState()) |
| 49 | + )) |
44 | 50 |
|
45 | | - return result |
46 | | - end |
| 51 | + return result |
47 | 52 | end |
48 | 53 | end |
49 | 54 |
|
|
0 commit comments