composite-class
An isomorphic JavaScript class for building composite structures. Suitable for use as a super class or mixin.
Last updated 5 years ago by 75lb .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ gnpm install composite-class 
SYNC missed versions from official npm registry.

view on npm npm module downloads Build Status Dependency Status js-standard-style

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 = require('composite-class')

class Person extends Composite {
  constructor (name, role) {
    super()
    this.name = name
    this.role = role
  }

  toString () {
    return `${this.name} [${this.role}]`
  }
}

const government = new Person('Gouvernement de la République française', 'Government')
const headOfState = new Person('Emmanuel Macron', 'Head of State')
const primeMinister = new Person('Édouard Philippe', 'Prime Minister')
const ministerArmedForces = new Person('Florence Parly', 'Minister of the Armed Forces')
const ministerEconomy = new Person('Bruno Le Maire', 'Minister of Finance and the Economy')

government.add(headOfState)
headOfState.add(primeMinister)
primeMinister.add(ministerArmedForces)
primeMinister.add(ministerEconomy)

console.log(government.tree())

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.log('Processing:', person.name)
}

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 = require('composite-class')

Composite ⏏

Kind: Exported class

composite.children : Array

Children

Kind: instance property of Composite

composite.parent : Composite

Parent

Kind: instance property of Composite

composite.add() ⇒ Composite

Add a child

Kind: instance method of Composite

composite.append(child) ⇒ Composite

Kind: instance method of Composite

Param Type Description
child Composite the child node to append

composite.prepend(child) ⇒ Composite

Kind: instance method of Composite

Param Type Description
child Composite the child node to prepend

composite.remove(child) ⇒ Composite

Kind: instance method of Composite

Param Type Description
child Composite the child node to remove

composite.level() ⇒ number

depth level in the tree, 0 being root.

Kind: instance method of Composite

composite.getDescendentCount() ⇒ number

Kind: instance method of Composite

composite.tree() ⇒ string

prints a tree using the .toString() representation of each node in the tree

Kind: instance method of Composite

composite.root() ⇒ Composite

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

composite.parents() ⇒ Array.<Composite>

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 = require('composite-class')

Within Node.js with ECMAScript Module support enabled:

import Composite from 'composite-class'

Within an modern browser ECMAScript Module:

import Composite from './node_modules/composite-class/index.mjs'

Old browser (adds window.Composite):

<script nomodule src="./node_modules/composite-class/dist/index.js"></script>

© 2016-20 Lloyd Brookes <75pound@gmail.com>.

Isomorphic test suite by test-runner and web-runner. Documented by jsdoc-to-markdown.

Current Tags

  • 2.0.1                                ...           latest (5 years ago)

9 Versions

  • 2.0.1                                ...           5 years ago
  • 2.0.0                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
  • 0.3.0                                ...           6 years ago
  • 0.2.1                                ...           8 years ago
  • 0.2.0                                ...           9 years ago
  • 0.1.2                                ...           9 years ago
  • 0.1.1                                ...           9 years ago
  • 0.1.0                                ...           9 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (5)

Copyright 2013 - present © cnpmjs.org | Home |