Socket
Socket
Sign inDemoInstall

@toolbuilder/make-factory

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @toolbuilder/make-factory

Generate a factory method from a class constructor function. Adds static methods from all super classes.


Version published
Maintainers
1
Install size
19.9 kB
Created

Changelog

Source

0.1.4 (2023-08-12)

Features

  • TypeScript types (8967118)

Readme

Source

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

Keywords

FAQs

Last updated on 12 Aug 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc