[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: jQuery like lua table query lib
- From: HyperHacker <hyperhacker@...>
- Date: Sun, 26 Dec 2010 20:19:45 -0700
2010/12/26 임창진(callin) <callin2@gmail.com>:
> 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)
>
It looks quite useful, but I think "~=" will be confusing for some,
because it means "does not equal" in Lua.
--
Sent from my toaster.