lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hi all lua lovers!

few weeks ago I need lib which behave like jQuery over lua table.
I search it but cannot find. then I coded it for me.
And I guess it may useful.
below is sample.
It uses Lpeg(0.9)
anybody can use/improve it freely. feedback is welcome.
--------------------------------

require "lquery"
require "base" -- this is optional for table pretty print

-- table for test
local root = {tag='a',
	{tag='c', 1,2,3},
	{tag='a',b='xxx',
		{tag='c',id='qq',age=33, class='a b c'},
		{tag='c',id='qq123',age=34, class='a b'},
	},
}

local lqroot = lquery.new(root)
assert( lqroot:get(1) == root , 'it must be equal')

--[[
	'=' 	:	equal
	'^='	:	startwith
	'$='	:	endWith
	'~='	:	contains



	'/a/c'  	===> '/[tag="a"]/[tag="c"]'
	'c' 		===> '*[tag="c"]'
	'#xxx'  	===> '*[id="xxx"]'
	'.xxx'		===> '*[class~="xxx"]'

]]


local fr = lqroot:find('c')

for t in fr:each() do
	print('t1',t)
end

print( fr:get(3))  --  {age=34,name=qq123,tag=c}

fr = lqroot:find('/a/c')

for t in fr:each() do
	print('t2',t)
end

fr = lqroot:find('/[tag="a"]/[tag="c"]')

for t in fr:each() do
	print('t3',t)
end

fr = lqroot:find('/a/a c')

for t in fr:each() do
	print('t4',t)
end

fr = lqroot:find('#qq')

for t in fr:each() do
	print('t5',t)
end

for t in lqroot:find('.b'):each() do
	print('t6',t)
end

fr = lqroot:find('.c')

for t in fr:each() do
	print('t7',t)
end


-----------------------------------


-- 
임창진(rim.chang.jin)

Attachment: lquery.lua
Description: Binary data