0.1.4 • Published 9 months ago

@toolbuilder/make-factory v0.1.4

Weekly downloads
4
License
MIT
Repository
github
Last release
9 months ago

Make Factory

Generate a factory method from a class constructor function. Adds static methods from all super classes to the factory. Also provides methods to walk prototype chains to get user instance and static methods.

Example

import { makeFactory } from '@toolbuilder/make-factory'

class A {
  static a () {}
}

class B extends A {
  constructor(c, d) { /* something */ }
  static b () {}
}

const Factory = makeFactory(B)

const b = Factory('c', 'd') // equivalent to new B('c', 'd')
Factory.b() // equivalent to B.b()
Factory.a() // equivalent to B.a()

These are the exported methods.

  • makeFactory - creates a factory from a constructor function. The factory includes all static methods
  • getMethods - finds user instance and static methods for a prototype chain
  • getMethodsOfClass - same as getMethods, but for class constructor
  • getMethodsOfInstance - same as getMethods, but for instances

Installation

npm install --save @toolbuilder/make-factory

Documentation

See the TypeScript types or the JSDoc comments in source. For further examples, see the unit tests.

Why

This library is about choice - not being pedantic. In libraries, I like to provide the option to instantiate instances using 'new' or with a factory function. This package is my tool for generating the factory. See Eric Elliott's response for reasons why 'new' can be a problem. Those are good points that need to balanced with your other requirements during development.

Contributing

Contributions are welcome. Please create a pull request.

  • I use pnpm instead of npm.
  • Run the unit tests with pnpm test
  • Package verification requires pnpm to be installed globally.
    • npm install -g pnpm
    • pnpm install
    • pnpm build to build CommonJS and types
    • pnpm run check:packfile to test against Node ES and CommonJS projects, as well as Electron.
    • pnpm run check to run everything and validate the package is ready for commit

Issues

This project uses Github issues.

License

MIT