/fp-builder-js

The Builder pattern in functional style on JavaScript

Primary LanguageJavaScriptMIT LicenseMIT

The Builder pattern in functional style on JavaScript

An example implementation of the GoF Builder pattern using Funcional Programming. Immutability achieved by sequential evolving of frozen objects.

Prerequisites

  • Node.js
  • NPM
  • Basic knowledge of Functional Programming and Ramda library

Installing

Cloning

$ git clone https://github.com/dibikhin/fp-builder-js.git

Setting up

$ cd fp-builder-js
$ npm install

Running the tests

$ npm test

Usage

Build a complex message object by filling fields one by one in independent steps - withTitle(), withBody(), etc. Can call the building steps in any order.

pipe() simplifies passing parameters without temporary variables. build() in the end is for consistency and helps with identifying the Builder pattern.

See the full example in ./builder.spec.js.

  const message = pipe(
    withTitle({ title: 'test title' }),
    withBody({ body: 'test body' }),
    withPriority({ priority: 'high' }),
    build,
  )(emptyMessage)

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Authors

  • Roman Dibikhin

Acknowledgments

Thanks to:

  • The "Gang of Four" for the "Design Patterns" book
  • Peter Norvig and Scott Wlaschin for inspiration

Links

License

MIT