From 27f881444a01bb6884dd87ddf3f152c54d431f97 Mon Sep 17 00:00:00 2001 From: bakpakin Date: Sat, 5 Mar 2016 16:06:56 -0500 Subject: [PATCH] Fix string filter generator. --- spec/tiny_spec.lua | 22 ++++++++++++++++++++++ tiny.lua | 9 +++++---- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/spec/tiny_spec.lua b/spec/tiny_spec.lua index c007b87..c5ced28 100644 --- a/spec/tiny_spec.lua +++ b/spec/tiny_spec.lua @@ -111,6 +111,28 @@ describe('tiny-ecs:', function() end) + it("Can use string filters", function() + + local f = tiny.filter('a|b|c') + + assert.truthy(f(nil, { + a = true, b = true, c = true + })) + assert.truthy(f(nil, { + a = true + })) + assert.truthy(f(nil, { + b = true + })) + assert.truthy(f(nil, { + c = true + })) + assert.falsy(f(nil, { + x = true, y = true, z = true + })) + + end) + end) describe('World:', function() diff --git a/tiny.lua b/tiny.lua index 1b35974..7b16fc0 100644 --- a/tiny.lua +++ b/tiny.lua @@ -92,6 +92,7 @@ local filterJoin local filterBuildString do + local loadstring = loadstring or load local function getchr(c) return "\\" .. c:byte() @@ -145,12 +146,12 @@ do return table.concat(accum) end - print(buildPart'a|b|(c&d)') - function filterBuildString(str) - local source = ('return function(_, e) return %s end end'):format(buildPart(str)) + local source = ("return function(_, e) return %s end"):format(buildPart(str)) local loader, err = loadstring(source) - if err then error(err) end + if err then + error(err) + end return loader() end