composite-class
An isomorphic, load-anywhere JavaScript class for building composite structures. Suitable for use as a super class or mixin.
Synopsis
The Composite
class implements the composite design pattern, useful for building and traversing tree structures. For example, build a composite structure representing the French government:
const Composite = { super thisname = name thisrole = role } { return ` []` } const government = 'Gouvernement de la République française' 'Government'const headOfState = 'Emmanuel Macron' 'Head of State'const primeMinister = 'Édouard Philippe' 'Prime Minister'const ministerArmedForces = 'Florence Parly' 'Minister of the Armed Forces'const ministerEconomy = 'Bruno Le Maire' 'Minister of Finance and the Economy' governmentheadOfStateprimeMinisterprimeMinister console
Output.
- Gouvernement de la République française [Government]
- Emmanuel Macron [Head of State]
- Édouard Philippe [Prime Minister]
- Florence Parly [Minister of the Armed Forces]
- Bruno Le Maire [Minister of Finance and the Economy]
The Composite
class implements an iterable interface, therefore can be iterated using standard JavaScript methods.
for const person of government console
Output.
Processing: Gouvernement de la République française
Processing: Emmanuel Macron
Processing: Édouard Philippe
Processing: Florence Parly
Processing: Bruno Le Maire
composite-class
An isomorphic, load-anywhere JavaScript class for building composite structures. Suitable for use as a super class or mixin.
Example
const Composite =
- composite-class
- Composite ⏏
- .children :
Array
- .parent :
Composite
- .add() ⇒
Composite
- .append(child) ⇒
Composite
- .prepend(child) ⇒
Composite
- .remove(child) ⇒
Composite
- .level() ⇒
number
- .getDescendentCount() ⇒
number
- .tree() ⇒
string
- .root() ⇒
Composite
- .Symbol.iterator()
- .inspect()
- .parents() ⇒
Array.<Composite>
- .children :
- Composite ⏏
Composite ⏏
Array
composite.children : Children
Kind: instance property of Composite
Composite
composite.parent : Parent
Kind: instance property of Composite
Composite
composite.add() ⇒ Add a child
Kind: instance method of Composite
Composite
composite.append(child) ⇒ Kind: instance method of Composite
Param | Type | Description |
---|---|---|
child | Composite |
the child node to append |
Composite
composite.prepend(child) ⇒ Kind: instance method of Composite
Param | Type | Description |
---|---|---|
child | Composite |
the child node to prepend |
Composite
composite.remove(child) ⇒ Kind: instance method of Composite
Param | Type | Description |
---|---|---|
child | Composite |
the child node to remove |
number
composite.level() ⇒ depth level in the tree, 0 being root.
Kind: instance method of Composite
number
composite.getDescendentCount() ⇒ Kind: instance method of Composite
string
composite.tree() ⇒ prints a tree using the .toString() representation of each node in the tree
Kind: instance method of Composite
Composite
composite.root() ⇒ Returns the root instance of this tree.
Kind: instance method of Composite
composite.Symbol.iterator()
default iteration strategy
Kind: instance method of Composite
composite.inspect()
Used by node's util.inspect
.
Kind: instance method of Composite
Array.<Composite>
composite.parents() ⇒ Returns an array of ancestors
Kind: instance method of Composite
Load anywhere
This library is compatible with Node.js, the Web and any style of module loader. It can be loaded anywhere, natively without transpilation.
Node.js:
const Composite =
Within Node.js with ECMAScript Module support enabled:
Within an modern browser ECMAScript Module:
Old browser (adds window.Composite
):
© 2016-20 Lloyd Brookes <75pound@gmail.com>.
Isomorphic test suite by test-runner and web-runner. Documented by jsdoc-to-markdown.