npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

composite-class

v3.0.0-1

Published

An isomorphic JavaScript class for building composite structures. Suitable for use as a super class or mixin.

Downloads

482

Readme

view on npm npm module downloads Gihub repo dependents Gihub package dependents Node.js CI 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(child) ⇒ Composite

Add a child

Kind: instance method of Composite

| Param | Type | Description | | --- | --- | --- | | child | Composite | the child node to add |

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

Composite.mixInto(target)

Kind: static method of Composite

| Param | Type | Description | | --- | --- | --- | | target | object | The target class (or constructor function) to receive the state machine behaviour. |

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-24 Lloyd Brookes <[email protected]>.

Test suite by test-runner. Documented by jsdoc-to-markdown.