Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 4 commits
  • 8 files changed
  • 0 commit comments
  • 1 contributor
View
5 .gitignore
@@ -12,4 +12,7 @@ logs
results
node_modules
-npm-debug.log
+npm-debug.log
+
+*.swp
+.DS_Store
View
0  History.md
No changes.
View
16 Makefile
@@ -0,0 +1,16 @@
+
+TESTS = test/*.js
+REPORTER = spec
+
+all: clean
+ @node support/compile.js
+
+clean:
+ @rm chai-stats.js
+
+test:
+ @NODE_ENV=test ./node_modules/.bin/mocha \
+ --reporter $(REPORTER) \
+ $(TESTS)
+
+.PHONY: all clean test
View
3  index.js
@@ -0,0 +1,3 @@
+module.exports = (process && process.env && process.env.CHAI_STATS_COV)
+ ? require('./lib-cov/stats')
+ : require('./lib/stats');
View
55 lib/calc.js
@@ -0,0 +1,55 @@
+/*!
+ * Chai Stats - calculation utilities
+ * Copyright (c) 2012 Jake Luer <jake@alogicalparadox.com>
+ * MIT Licenced
+ */
+
+/**
+ * # sum
+ *
+ * Returns the sum of a given array of numbers.
+ *
+ * @param {Array} numbers to sum
+ * @returns sum
+ */
+
+exports.sum = function (nums) {
+ var res = 0;
+ for (var i = 0; i < nums.length; i++)
+ res += nums[i];
+ return res;
+};
+
+/**
+ * # mean
+ *
+ * Returns the mean (average) of a given array of numbers.
+ *
+ * @param {Array} numbers to average
+ * @returs mean
+ */
+
+exports.mean = function (nums) {
+ var sum = exports.sum(nums);
+ return sum / nums.length;
+};
+
+/**
+ * # sdeviation
+ *
+ * Returns the standard deviation of a given array of numbers.
+ *
+ * @param {Array} numbers for stdev
+ * @return standard deviation
+ */
+
+exports.sdeviation = function (nums) {
+ var devs = []
+ , mean = exports.mean(nums);
+ for (var i = 0; i < nums.length; i++)
+ devs.push(nums[i] - mean);
+ for (var d = 0; d < devs.length; d++)
+ devs[d] = Math.pow(devs[d], 2);
+ var davg = exports.sum(devs) / (devs.length - 1);
+ return Math.sqrt(davg);
+};
View
26 lib/stats.js
@@ -0,0 +1,26 @@
+var calc = require('./calc');
+
+module.exports = function (chai, _) {
+
+ _.addProperty(chai.Assertion, 'sum', function () {
+ var obj = _.flag(this, 'object');
+ new chai.Assertion(obj).to.be.instanceOf(Array);
+ _.flag(this, 'object', calc.sum(obj));
+ });
+
+ _.addProperty(chai.Assertion, 'mean', function () {
+ var obj = _.flag(this, 'object');
+ new chai.Assertion(obj).to.be.instanceOf(Array);
+ _.flag(this, 'object', calc.mean(obj));
+ });
+
+ function deviation () {
+ var obj = _.flag(this, 'object');
+ new chai.Assertion(obj).to.be.instanceOf(Array);
+ _.flag(this, 'object', calc.sdeviation(obj));
+ }
+
+ _.addProperty(chai.Assertion, 'stdev', deviation);
+ _.addProperty(chai.Assertion, 'deviation', deviation);
+
+};
View
23 package.json
@@ -0,0 +1,23 @@
+{
+ "author": "Jake Luer <jake@alogicalparadox.com>",
+ "name": "chai-stats",
+ "description": "Statistical and additional numerical assertions for the Chai Assertion Library.",
+ "version": "0.1.0",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/chaijs/chai-stats.git"
+ },
+ "main": "./index",
+ "scripts": {
+ "test": "make test"
+ },
+ "dependencies": {},
+ "devDependencies": {
+ "mocha": "*",
+ "chai": "*"
+ },
+ "optionalDependencies": {},
+ "engines": {
+ "node": "*"
+ }
+}
View
27 test/stats.js
@@ -0,0 +1,27 @@
+if (!chai) {
+ var chai = require('chai')
+ , stats = require('..');
+ chai.use(stats);
+}
+
+var should = chai.should();
+
+describe('Chai Stats', function () {
+
+ describe('Math Basics', function () {
+ var nums = [ 1, 3, 5, 7, 9, ];
+
+ it('should be able to get the sum of a set of numbers', function () {
+ nums.should.have.sum.equal(25);
+ });
+
+ it('should be able to get the mean of a set of numbers', function () {
+ nums.should.have.mean.equal(5);
+ });
+
+ it('should be able to get the standard deviation', function () {
+ nums.should.have.stdev.equal(3.1622776601683795);
+ nums.should.have.deviation.equal(3.1622776601683795);
+ });
+ });
+});

No commit comments for this range

Something went wrong with that request. Please try again.