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

voting

v0.1.0

Published

Voter library for javascript

Downloads

27

Readme

Voting - A voter library for async votes and async statistical analysis in javascript

How to install

This module is designed for node.js, but it is also possible to use it in browsers. At the moment this module does not serve a module for browsers, only a commonJS module. If you want to use it in a browser try to 'compile' it for browsers with browserify.

Require the package:

npm install --save voting

How to use it generally / concept

Voters use boolean values in general, but this module is enhanced with "statistical voters"

Use in node.js

At first you have to require this module.

var voting = require('voting');

This module is structured in an associative array (object) and has the following build in voters:

  • boolean - Voters that handle boolean decision basis.
    • And: All decision basis must be true for a positive decision (logical and).
    • Or: One decision basis must be true for a positive decision (logical or).
    • Ratio: Get the ratio between true and false decision basis.
    • Xor: Only one decision basis must be true for a positive decision (logical xor).
  • statistics - Voters that evaluate decision basis statistically.
    • Average: Get the average of all values as decision.
    • Cv: Get the coefficient of variation of all values as decision.
    • Variance: Get the variance of all values as decision.

Support of synchronous, asynchronous functions and values

The goal of this module is to provide an easy way to vote for decisions with asynchronous functions, but it should also easy to handle values from synchronous functions.

Persist evaluation

With standard configuration this module persists the evaluated value of a decision basis (abstention).

anyVoter.clearBasis();
anyVoter.addDecisionBasis(function (cb) {
    cb(null, true);
});
console.log('This is a function: ', typeof anyVoter.decisionBasis[0]);

anyVoter.decide(function (err, decision) {
    if (err) {
        return console.error(err);
    }
    console.log('This is your decision: ', decision);
});
console.log('Now it is a boolean: ', typeof anyVoter.decisionBasis[0]);

If you do not want to persist values you can switch this option off

anyVoter.persistsEvaluation = false;

Handling of null values

In general null as value is interpreted as no or skipped decision basis.

voter = new voting.boolean.And({
    decisionBasis: [true, null, true]
});
voter.decide(function (err, decision) {
    if (err) {
        return console.error(err);
    }
    console.log('The decision should be true even with a null', decision);
});

Specific use

Boolean votery

more or less the classical voter

And Voter

All decision basis must be true for a positive decision (logical and).

var fs = require('fs');
var voting = require('voting');

var andVoter = new voting.boolean.And();

andVoter.addDecisionBasis(function existsTheFile(cb) {
    fs.exists('path/to/file', cb)
});
andVoter.addDecisionBasis(function isASettingDone(cb) {
    fs.readFile('path/to/config.json', function (err, data) {
        var json;
        if (err) {
            return cb(err);
        }
        try {
            json = JSON.parse(data);
        } catch (error) {
             cb(error);
        }
        cb(null, json.some.setting);
    });
});
andVoter.addDecisionBasis(something.synchronous());

andVoter.decide(function (err, decision) {
    if (err) {
        console.error('There was an error: ', error);
    }
    // Do something with the decision...
});

Or Voter

One decision basis must be true for a positive decision (logical or).

var user = require('./user'); // Just an example
var resource = require('./resource'); // Just an example
var voting = require('voting');

var orVoter = new voting.boolean.Or();

orVoter.addDecisionBasis(user.granted);
orVoter.addDecisionBasis(function resourceNeedNoGranted(cb) {
    resource.acl.read(function (err, user, group, all) {
        if (err) {
            return cb(err);
        }
        cb(null, all);
    });
});

orVoter.decide(function (err, decision) {
    if (err) {
        console.error('There was an error: ', error);
    }
    // Do something with the decision...
});

Ratio Voter

Get the ratio between true and false decision basis.

var socket = require('./socket'); // Just an example
var voting = require('voting');

var ratioVoter = new voting.boolean.Ratio();

var i;

for (i = 0; i < socket.users; i += 1) {
    ratioVoter.addDecisionBasis(socket.users[i].letVote); // True is yes, false is no, null is an abstention
}

ratioVoter.decide(function (err, decision) {
    if (err) {
        console.error('There was an error: ', error);
    }

    // Do something with the decision...
    if (decision >= 0.5) {
        // Decision accepted
    } else {
        // Decision rejected
    }
});

Xor Voter

Only one decision basis must be true for a positive decision (logical xor).

var user = require('./user'); // Just an example
var voting = require('voting');

var xorVoter = new voting.boolean.Xor();

xorVoter.addDecisionBasis(user.isAllowedToDoSomething);
xorVoter.addDecisionBasis(user.hasAlreadyDoSomething);

// Allow only to do something once
xorVoter.decide(function (err, decision) {
    if (err) {
        console.error('There was an error: ', error);
    }
    // Do something with the decision...
});

Statistical voters

- [Average](#average-voter): Get the average of all values as decision.
- [Cv](#coefficient-of-variation-voter): Get the coefficient of variation of all values as decision.
- [Variance](#variance-voter): Get the variance of all values as decision.

Average Voter

Get the average of all values as decision.

var profiles = require('./profiles'); // Just an example

var averageVoter = new voting.statistics.Average();

var i;

for (i = 0; i < profiles.length; i += 1) {
    averageVoter.addDecisionBasis(profiles[i].getAge());
}

averageVoter.decide(function (err, decision) {
    if (err) {
        console.error('There was an error: ', error);
    }
    // Do something with the decision...
});

Coefficient of variation Voter

Get the coefficient of variation of all values as decision.

It works quite the same like the average example above but the result is the CV and the class to use is: voting.statistical.Cv()

Variance Voter

Get the variance of all values as decision.

It works quite the same like the average example above but the result is the variance and the class to use is: voting.statistical.Variance()

Create own voters

You can use the super class to augment new voters. Here an example how the AND voter is created

var Voter = require('voting/voter/super-class');

var BooleanAndVoter = function () {
    Voter.apply(this, arguments);
};

BooleanAndVoter.prototype = {
    '__proto__': Voter.prototype,
    decide: function (cb) {
        Voter.prototype.decide.call(this, function (err, data) {
            var i;
            if (err) {
                return cb(err);
            }
            if (data.length === 0) {
                return cb(null, null);
            }
            for (i = 0; i < data.length; i += 1) {
                if (!data[i]) {
                    return cb(null, false);
                }
            }
            return cb(null, true);
        });
        return this;
    }
};