listag
v0.8.4
Published
List Manager for Array Operations & Query by Tag
Downloads
31
Readme
LISTAG
List Manager supports Array Operation & Query by Tags.
FEATURES
- Javascript Array like
can be used like a normal array with[]
,length
,forEach
,map
,reduce
functions. - Tags Associated with Each Item
can be queried by tags, return a array with items that has the specified tags. - Event Support
listag will emitadd
anddel
event which can be listened on. - TypeScript Support
with Type Definition File included.
WHY LISTAG
When we have some of hash values, we put them into a array:
const List = [
{ name: 'tom', value: 1 }
{ name: 'bob', value: 2 }
{ name: 'bob', value: 3 }
]
If we want to get the value out whose name is 'tom', we have to use filter:
const filteredList = List.filter(v => {
return v.name === 'tom'
})
Listag is a convinience way to do this, with more powerful enhancements.
const lt = new Listag()
lt.add(1, {
name: 'tom'
})
lt.add([2, 3], {
name: 'bob'
})
lt.get({
name: 'bob'
})
.forEach(v => {
console.log('value of name bob: %s', v)
})
EXAMPLE
Create listag
let lt = new Listtag(1, {
color: 'blue'
})
let lt2 = new Listtag([2, 3], {
color: 'green',
owner: 'tom',
})
Add item(s) to a listag
lt.add(4, {
color: 'red'
})
lt.add([5, 6], {
color: 'yellow'
, owner: 'mike'
})
Del item(s) from a listag
lt.del(4)
ltDel = lt.get({
color: 'red'
})
lt.del(ltDel)
Get item(s) from a listag
let blueLt = lt.get({
color: 'blue'
})
blueLt.forEach(i => {
console.log('items with color blue: %s', i)
})
get
can filter the result by exclude
some tag value, by add a minus sign -
before the value:
let notBlueLt = lt.get({
color: '-blue'
})
Array Operation
console.log('lt length: %s', lt.length)
console.log('lt[0] = %s', lt[0])
lt.forEach(item => {
console.log('item: %s', item)
})
lt.map(item => {
return item * 2
})
lt.filter(item => {
return item % 2
})
Event
lt.on('add', item => {
console.log('add item: %s', item)
})
lt.on('del', item => {
console.log('del item: %s', item)
})
Set Tags to a listag
lt.set({
lang: 'js'
})
TEST
Unit Test can be found here:
https://github.com/huan/listag/blob/master/tests/
KNOWN ISSUES & SUPPORT
Github Issue - https://github.com/huan/listag/issues
- Listag can not be
instanceof
because it has beenProxy
-ed
CHANGELOG
v0.8 master
- Upgrade TypeScript to the latest version
- DevOps with npm@next
v0.4 (9th Auth 2016)
- support create listag from listag, keep all tags information right
- add
Listag.item()
method to get a ListagItem from Listag, for read tag information
v0.2.3 (7th Aug 2016)
- add TypeScript Declaration File
- support to exclude a tag value by add minus sign(
-
) to value
v0.1.0 (22 Jul 2016)
- support array with tags powered by ES6 Proxy
TODO
- [ ] support
for ... in Listag
- [ ] support
for ... of Listag
AUTHOR
Zhuohuan LI [email protected] (http://linkedin.com/in/zixia)
COPYRIGHT & LICENSE
- Code & Docs 2016-2018© zixia
- Code released under the ISC license
- Docs released under Creative Commons