From fb64932134afd76f9567584d2282990eb1fd1b39 Mon Sep 17 00:00:00 2001 From: rxi Date: Tue, 4 Mar 2014 12:18:50 +0000 Subject: [PATCH] Added support for numerical keys to lume.format() --- README.md | 7 ++++--- lume.lua | 4 +++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 3b8c06f..b7f319b 100644 --- a/README.md +++ b/README.md @@ -213,10 +213,11 @@ lume.trim(" Hello ") -- Returns "Hello" ### lume.format(str [, vars]) Returns a formatted string. The values of keys in the table `vars` can be -inserted into the string by using the form `"{key}"` in `str`. +inserted into the string by using the form `"{key}"` in `str`; numerical keys +can also be used. ```lua -lume.format("Hello {a}, I hope {a} is {b}.", {a = "world", b = "well"}) --- Returns "Hello world, I hope world is well." +lume.format("{b} hi {a}", {a = "mark", b = "Oh"}) -- Returns "Oh hi mark" +lume.format("Hello {1}!", {"world"}) -- Returns "Hello world!" ``` ### lume.trace(...) diff --git a/lume.lua b/lume.lua index d26ec7b..a8313b9 100644 --- a/lume.lua +++ b/lume.lua @@ -232,7 +232,9 @@ end function lume.format(str, vars) if not vars then return str end - local f = function(x) return tostring(vars[x] or "{" .. x .. "}") end + local f = function(x) + return tostring(vars[x] or vars[tonumber(x)] or "{" .. x .. "}") + end return (str:gsub("{(.-)}", f)) end