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

fluentifyjs

v1.1.1

Published

A tiny fluent interface decorator for JavaScript objects

Downloads

5

Readme

Fluentify

A tiny fluent interface decorator for JavaScript objects.

Build Status

Install :nut_and_bolt:

npm install fluentifyjs

Usage :book:

In simple terms, Fluentify works by allowing you to chain an objects method calls e.g.

const fluentify = require('fluentifyjs');

const api = fluentify({
  foo() {},
  bar() {}
});

api
  .foo()
  .bar()
  .done();

"Wait a minute, where did done come from?" - good question!

done(err, ...results)

done is a function attached to the object by Fluentify and is used to indicate the end of the chain. When each function in the chain is called, rather than being executed, it's added to an internal queue. When done is called, Fluentify will purge the queue invoking each function in the correct order, keeping track of any return values.

Note - if a done property already exists on the object it will be overwritten

Each result is passed to done as an explicit parameter as an array. For example, if foo and bar yield results your done call may look like:

.done((err, foo, bar) => {
  console.log(foo[0]); // foo result
  console.log(bar[0]); // bar result
});

Tip:bulb: - in scenarios where you have lots of result you can make use of the spread operator (ES6 feature) to bundle up your results into an array, or alternatively you can use the arguments object.

If an error occurs at any point during the chain, done is invoked immediately and any remaining function calls are discarded.

Promise support

Prefer a Promise over a callback? Simply omit the callback from your done call and Fluentify will return one e.g.

const results = await api.foo().bar().done();

"How does Fluentify know what return values to store for each call?" - another good question!

callback(err, result)

For each function in the chain Fluentify expects the final argument to be a callback. The callback works just like any other in JavaScript i.e. passes control back to the caller and yields any results or errors.

For the most part, your callback is going to be the same for each function e.g. cb => cb(...). To avoid having to repeat this boilerplate code in every call, simply omit the callback parameter and Fluentify will inject one for you.

Note - callback is required even for non-async code, this helps Fluentify retain order regardless of what type of code is being run

Example - implicit callback

const fluentify = require('fluentifyjs');

const api = fluentify({
  foo(cb) {
    // do cool stuff
    cb(null, 'foo');
  },
  bar(data, cb) {
    // do more cool stuff
    cb(null, 'bar');
  }
});

api
  .foo()
  .bar('1234')
  .done((err, ...results) => {
    console.log(results[0][0]); // foo
    console.log(results[1][0]); // bar
  });

Notice in the above example we didn't pass a callback to either foo or bar

If you do provide a callback, it's important to note that an additional callback inside of this is required in order to pass context back to Fluentify.

Example - explicit callback

api
  .foo()
  .bar('1234', (err, result, cb) => cb(err, `foo${result}`))
  .done((err, ...results) => {
    console.log(results[0][0]); // foo
    console.log(results[1][0]); // foobar
  });

By passing an explicit callback, you take control away from Fluentify as it can't be sure what's going on in your code, for example, you might make an asynchronous call and need to wait for a reply or just do some stuff synchronously. To remove any doubt, a callback is provided as a means of letting Fluentify know when your done so it can resume processing.

Note - the additional callback is injected automatically by Fluentify

Accessing results

As discussed earlier, Fluentify will keep track of each result and pass them to done. However, what if we need access to a particular result earlier than that? With Fluentify, you have two options - FQL or the results function.

Fluentify Query Language (FQL)

Fluentify has it's own little query language that it uses to bind results to parameters on calls in the chain. An FQL query will always start with $<index>

  • $ - prefix used to denote the start of an FQL query
  • <index> - index of the call whose result we want to bind

For example, let's imagine the first call in your chain fetched a user model which looked like:

{
  id: '12345',
  name: 'Bobby Tables',
  email: '[email protected]'
}

Then in the next call, you wanted access to this full result - here's how we'd do that using FQL:

const fluentify = require('fluentifyjs');

const api = fluentify({
  get(url, cb) { ... },
  foo(user, cb) { ... }
});

api
  .get('/users/12345')
  .foo('$1')
  .done(...);

In the above example, Fluentify will bind the result of the get call to the first parameter in the foo call.

FQL supports deep indexing into the object tree. For example, lets assume we only need access to the users email in the foo call, in that case the FQL would look like $1.email, simple!

It also supports indexing into array properties by denoting the index of the item in the array as if it were a property e.g. $1.list.0.

Note - there are no limitations on how deep you can query into the object tree, however, please bear in mind that there is work involved in parsing & traversing so performance may be impacted

results(resultset, cb)

results is a utility function attached to the object by Fluentify and can be used as a way of peeking into underlying result set at a specific point in the chain. It's handled just like any other call in the chain therefore it has a callback and can yield it's own result

const fluentify = require('fluentifyjs');

const api = fluentify({
  foo(cb) {
    return cb(null, 'foo');
  },
  bar(cb) {
    return cb(null, 'bar');
  }
});

api
  .foo()
  .bar()
  .results((arr, cb) => cb(null, `${arr[0]}${arr[1]}`))
  .done((err, foo, bar, foobar) => {
    console.log(foo[0]); // foo
    console.log(bar[0]); // bar
    console.log(foobar[0]); // foobar
  });

And since it yields it's own result, it means it can be used in conjunction with FQL

api
  .foo()
  .bar()
  .results((arr, cb) => cb(null, `${arr[0]}${arr[1]}`))
  .consolidate('$3')
  ...

Note - first parameter of consolidate would be "foobar"

Bugs :beetle:

As much as us developers hate to admit it, sometimes our code doesn't always work as intended :disappointed: If you happen to find a bug with Fluentify please raise an issue and I'll do my best to convince you it's by design!

Contribute :construction:

There are lots of things to like about Fluentify, however, there is always room for improvement. More than happy to accept PRs as long as the change is for the greater good and, of course, includes covering tests.

License

This project is released under the terms of the MIT license.