https://github.com/75lb/composite-class
An isomorphic JavaScript class for building composite structures.
https://github.com/75lb/composite-class
composite design-pattern es6 esm isomorphic javascript javascript-library load-anywhere nodejs
Last synced: 8 months ago
JSON representation
An isomorphic JavaScript class for building composite structures.
- Host: GitHub
- URL: https://github.com/75lb/composite-class
- Owner: 75lb
- License: mit
- Created: 2016-02-08T19:06:06.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-08-22T18:23:34.000Z (about 1 year ago)
- Last Synced: 2025-02-02T08:31:51.808Z (8 months ago)
- Topics: composite, design-pattern, es6, esm, isomorphic, javascript, javascript-library, load-anywhere, nodejs
- Language: JavaScript
- Homepage:
- Size: 154 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.hbs
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.npmjs.org/package/composite-class)
[](https://www.npmjs.org/package/composite-class)
[](https://github.com/75lb/composite-class/network/dependents?dependent_type=REPOSITORY)
[](https://github.com/75lb/composite-class/network/dependents?dependent_type=PACKAGE)
[](https://github.com/75lb/composite-class/actions/workflows/node.js.yml)
[](https://github.com/feross/standard)# composite-class
An isomorphic, load-anywhere JavaScript class for building [composite structures](https://en.wikipedia.org/wiki/Composite_pattern). Suitable for use as a super class or mixin.
## Synopsis
The `Composite` class implements the [composite design pattern](https://en.wikipedia.org/wiki/Composite_pattern), useful for building and traversing tree structures. For example, build a composite structure representing the French government:
```js
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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#The_iterator_protocol) interface, therefore can be iterated using standard JavaScript methods.
```js
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
```{{>main}}
## 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:
```js
const Composite = require('composite-class')
```Within Node.js with ECMAScript Module support enabled:
```js
import Composite from 'composite-class'
```Within an modern browser ECMAScript Module:
```js
import Composite from './node_modules/composite-class/index.mjs'
```Old browser (adds `window.Composite`):
```html
```
* * *
© 2016-24 Lloyd Brookes \<75pound@gmail.com\>.
Test suite by [test-runner](https://github.com/test-runner-js/test-runner). Documented by [jsdoc-to-markdown](https://github.com/jsdoc2md/jsdoc-to-markdown).