-
Notifications
You must be signed in to change notification settings - Fork 8
ChainedSet
James edited this page Jun 24, 2017
·
2 revisions
extension of Set
- add(val):
Chain
- prepend(val):
Chain
- values():
Array
- concat(
Iteratable
):Chain
- append(val):
Chain
- merge(
Iteratable
):Chain
unlike ChainedMap, this does not use MergeChain since only simple iteratables are involved
// extends Set
class ChainedSet extends Chainable {
public add(value: any): ChainedSet
public prepend(value: any): ChainedSet
public merge(arr: MergeableArray): ChainedSet
public has(value: any): boolean
public values(): any[]
}
const people = new ChainedSet()
people
.add('sam')
.add('sue')
.prepend('first')
.merge(['merged'])
for (let name of people) // first, sam, sue, merged
class Lists extends Chainable {
constructor(parent) {
super(parent)
this.people = new ChainedSet(this)
this.places = new ChainedSet(this)
}
add(type, value) {
this[type].add(value)
return this
}
toArray() {
return new ChainedSet()
.merge(this.people)
.merge(this.places)
.values()
}
toObject() {
return {
people: this.people.values()
places: this.places.values()
}
}
}
const lists = new Lists()
// with a simple factory like method
lists
.add('people', 'sam')
.add('people', 'sue')
.add('places', 'moon')
// or with property
lists.places.add('sun')
lists.people.add('joe')
// the shorthand methods
lists.people.concat(['frank', 'john'])
lists.people.prepend('first')
lists.people.add('last')
const obj = lists.toObject()
// .people == ['first', 'sam', 'sue', 'frank', 'john', 'last']
// .places == ['moon', 'sun']
const arr = lists.toArray()
// == ['first', 'sam', 'sue', 'frank', 'john', 'last', 'moon', 'sun']