Skip to content

Commit

Permalink
Bump version to 0.1.0 and fix linting errors
Browse files Browse the repository at this point in the history
  • Loading branch information
felipernb committed May 31, 2014
1 parent c8c77c2 commit c660898
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Makefile
@@ -1,4 +1,4 @@
all: jshint test coverage
all: jshint coverage

setup:
npm install
Expand Down
6 changes: 4 additions & 2 deletions algorithms/graph/dijkstra.js
Expand Up @@ -47,15 +47,17 @@ function dijkstra(graph, s) {
var currNode;
while (!q.isEmpty()) {
currNode = q.extract();
graph.neighbors(currNode).forEach(function (v) {
var neighbors = graph.neighbors(currNode);
for (var i = 0; i < neighbors.length; i++) {
var v = neighbors[i];
// relaxation
var newDistance = distance[currNode] + graph.edge(currNode, v);
if (newDistance < distance[v]) {
distance[v] = newDistance;
previous[v] = currNode;
q.changePriority(v, distance[v]);
}
});
}
}
return {
distance: distance,
Expand Down
5 changes: 3 additions & 2 deletions algorithms/math/gcd.js
Expand Up @@ -65,8 +65,9 @@ var gcdBinaryIterative = function (a, b) {
return a;
}

// Let shift = log(K), where K is the greatest power of 2 dividing both a and b
for (var shift = 0; ((a | b) & 1) == 0; ++shift) {
// Let shift = log(K), where K is the greatest power of 2
// dividing both a and b
for (var shift = 0; ((a | b) & 1) === 0; ++shift) {
a >>= 1;
b >>= 1;
}
Expand Down
14 changes: 5 additions & 9 deletions package.json
@@ -1,26 +1,22 @@
{
"name": "algorithms",
"version": "0.0.1",
"version": "0.1.0",
"description": "Traditional computer science algorithms and data structures implemented in JavaScript",
"directories": {
"test": "test"
},
"main": "main.js",
"dependencies": {},
"devDependencies": {
"coveralls": "*",
"istanbul": "*",
"mocha": "*"
"coveralls": "^2.10.0",
"istanbul": "^0.2.10",
"jshint": "^2.5.1",
"mocha": "^1.20.0"
},
"repository": {
"type": "git",
"url": "https://github.com/felipernb/algorithms.js"
},
"scripts": {
"test": "mocha -R spec --recursive test",
"coverage": "istanbul cover ./node_modules/.bin/_mocha -- -R spec --recursive test",
"coveralls": "cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js"
},
"keywords": [
"computer science",
"cs",
Expand Down
27 changes: 15 additions & 12 deletions test/algorithms/math/gcd.js
Expand Up @@ -38,18 +38,21 @@ describe('GCD', function () {
assert.equal(gcd(35, 49), 7);
});

it('should calculate the correct GCD between two numbers using the binary method', function () {
var gcdb = gcd.binary;
assert.equal(gcdb(1, 0), 1);
assert.equal(gcdb(2, 2), 2);
assert.equal(gcdb(2, 4), 2);
assert.equal(gcdb(4, 2), 2);
assert.equal(gcdb(5, 2), 1);
assert.equal(gcdb(10, 100), 10);
assert.equal(gcdb(10000000, 2), 2);
assert.equal(gcdb(7, 49), 7);
assert.equal(gcdb(7, 5), 1);
assert.equal(gcdb(35, 49), 7);
it('should calculate the correct GCD between two numbers using ' +
'the binary method', function () {
var gcdb = gcd.binary;
assert.equal(gcdb(1, 0), 1);
assert.equal(gcdb(0, 1), 1);
assert.equal(gcdb(0, 0), 0);
assert.equal(gcdb(2, 2), 2);
assert.equal(gcdb(2, 4), 2);
assert.equal(gcdb(4, 2), 2);
assert.equal(gcdb(5, 2), 1);
assert.equal(gcdb(10, 100), 10);
assert.equal(gcdb(10000000, 2), 2);
assert.equal(gcdb(7, 49), 7);
assert.equal(gcdb(7, 5), 1);
assert.equal(gcdb(35, 49), 7);
});
});

Expand Down
31 changes: 16 additions & 15 deletions test/data_structures/priority_queue.js
Expand Up @@ -56,22 +56,23 @@ describe('Min Priority Queue', function () {
assert(q.isEmpty());
});

it('can receive a dictionary with item => priority in construction', function () {
var q = new PriorityQueue({
a: 10,
b: 2091,
c: 4,
d: 1,
e: 5
});
it('can receive a dictionary with item => priority in construction',
function () {
var q = new PriorityQueue({
a: 10,
b: 2091,
c: 4,
d: 1,
e: 5
});

assert(!q.isEmpty());
assert.equal(q.extract(), 'd');
assert.equal(q.extract(), 'c');
assert.equal(q.extract(), 'e');
assert.equal(q.extract(), 'a');
assert.equal(q.extract(), 'b');
});
assert(!q.isEmpty());
assert.equal(q.extract(), 'd');
assert.equal(q.extract(), 'c');
assert.equal(q.extract(), 'e');
assert.equal(q.extract(), 'a');
assert.equal(q.extract(), 'b');
});

it('should be possible to change the priority of an item', function () {
var q = new PriorityQueue({
Expand Down
49 changes: 25 additions & 24 deletions test/util/comparator.js
Expand Up @@ -2,7 +2,7 @@
* Copyright (C) 2014 Felipe Ribeiro
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to
* of this software and associated documentation files (the 'Software'), to
* deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
* sell copies of the Software, and to permit persons to whom the Software is
Expand All @@ -11,7 +11,7 @@
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Expand All @@ -25,28 +25,29 @@ var Comparator = require('../../util/comparator'),
assert = require('assert');

describe('Comparator', function () {
it("Should use a default arithmetic comparison if no function is passed", function () {
var c = new Comparator();
assert.equal(c.compare(1, 1), 0);
assert.equal(c.compare(1, 2), -1);
assert.equal(c.compare(1, 0), 1);
assert.equal(c.compare('a', 'b'), -1);
assert(c.lessThan(0, 1));
assert(!c.lessThan(1, 1));
assert(c.lessThanOrEqual(1, 1));
assert(c.lessThanOrEqual(0, 1));
assert(!c.lessThanOrEqual(1, 0));
assert(c.greaterThan(1, 0));
assert(!c.greaterThan(1, 1));
assert(c.greaterThanOrEqual(1, 1));
assert(c.greaterThanOrEqual(1, 0));
assert(!c.greaterThanOrEqual(0, 1));
assert(c.equal(0, 0));
assert(!c.equal(0, 1));
});
it('Should use a default arithmetic comparison if no function is passed',
function () {
var c = new Comparator();
assert.equal(c.compare(1, 1), 0);
assert.equal(c.compare(1, 2), -1);
assert.equal(c.compare(1, 0), 1);
assert.equal(c.compare('a', 'b'), -1);
assert(c.lessThan(0, 1));
assert(!c.lessThan(1, 1));
assert(c.lessThanOrEqual(1, 1));
assert(c.lessThanOrEqual(0, 1));
assert(!c.lessThanOrEqual(1, 0));
assert(c.greaterThan(1, 0));
assert(!c.greaterThan(1, 1));
assert(c.greaterThanOrEqual(1, 1));
assert(c.greaterThanOrEqual(1, 0));
assert(!c.greaterThanOrEqual(0, 1));
assert(c.equal(0, 0));
assert(!c.equal(0, 1));
});

it("should allow comparison function to be defined by user", function () {
var compareFn = function (a, b) {
it('should allow comparison function to be defined by user', function () {
var compareFn = function () {
return 0;
};
var c = new Comparator(compareFn);
Expand All @@ -68,7 +69,7 @@ describe('Comparator', function () {
assert(c.equal(0, 1));
});

it("Should allow reversing the comparisons", function () {
it('Should allow reversing the comparisons', function () {
var c = new Comparator();
c.reverse();
assert.equal(c.compare(1, 1), 0);
Expand Down
4 changes: 2 additions & 2 deletions util/comparator.js
Expand Up @@ -60,7 +60,7 @@ Comparator.prototype.greaterThanOrEqual = function (a, b) {
};

Comparator.prototype.equal = function (a, b) {
return this.compare(a, b) == 0;
return this.compare(a, b) === 0;
};

/**
Expand All @@ -74,6 +74,6 @@ Comparator.prototype.reverse = function () {
this.compare = function (a, b) {
return originalCompareFn(b, a);
};
}
};

module.exports = Comparator;

0 comments on commit c660898

Please sign in to comment.