Comparing version 2.0.0 to 3.0.1
@@ -8,5 +8,10 @@ module.exports = function(grunt) { | ||
grunt.loadNpmTasks('grunt-contrib-clean'); | ||
grunt.config('clean', { | ||
dist: 'dist' | ||
grunt.loadNpmTasks('grunt-eslint'); | ||
grunt.config('eslint', { | ||
dist: { | ||
options: { | ||
configFile: '.eslintrc', | ||
}, | ||
src: ['lib/**/*.js'] | ||
} | ||
}); | ||
@@ -17,47 +22,33 @@ | ||
coverage: { | ||
coverage: { }, | ||
options: { | ||
coverage: {}, | ||
forceExit: true, | ||
match: '.', | ||
matchAll: false, | ||
specFolders: ['test'], | ||
extensions: 'js', | ||
specNameMatcher: '.*-spec', | ||
captureExceptions: true | ||
} | ||
specNameMatcher: 'spec', | ||
captureExceptions: true, | ||
junitreport: { | ||
report: false, | ||
savePath : './build/reports/jasmine/', | ||
useDotNotation: true, | ||
consolidate: true | ||
} | ||
}, | ||
src: ['**/*.js'] | ||
} | ||
}); | ||
grunt.loadNpmTasks('grunt-contrib-copy'); | ||
grunt.config('copy', { | ||
dist: { | ||
files: [{ | ||
expand: true, | ||
flatten: true, | ||
src: 'src/*', | ||
dest: 'dist/' | ||
}] | ||
grunt.loadNpmTasks('grunt-jsdoc'); | ||
grunt.config('jsdoc', { | ||
dist : { | ||
src: ['index.js', 'lib/**/*.js'], | ||
options: { | ||
destination: 'doc', | ||
readme: 'README.md' | ||
} | ||
} | ||
}); | ||
grunt.loadNpmTasks('grunt-contrib-uglify'); | ||
grunt.config('uglify', { | ||
options: { | ||
preserveComments: 'some' | ||
}, | ||
dist: { | ||
files: [{ | ||
expand: true, | ||
cwd: 'src', | ||
src: '**/*.js', | ||
dest: 'dist/', | ||
rename: function (dest, src) { | ||
return dest + src.replace(/\.js$/, '.min.js'); | ||
} | ||
}] | ||
} | ||
}); | ||
grunt.registerTask('dist', [ | ||
'clean:dist', | ||
'copy:dist', | ||
'uglify:dist' | ||
]); | ||
grunt.registerTask('coverage', [ | ||
@@ -68,5 +59,6 @@ 'jasmine_node:coverage' | ||
grunt.registerTask('default', [ | ||
'dist', | ||
'eslint', | ||
'jsdoc', | ||
'coverage' | ||
]); | ||
}; |
{ | ||
"name": "js-sorting", | ||
"version": "2.0.0", | ||
"version": "3.0.1", | ||
"description": "A collection of sorting algorithms written in JavaScript.", | ||
@@ -25,9 +25,8 @@ "scripts": { | ||
"grunt": "^0.4.5", | ||
"grunt-contrib-clean": "^0.5.0", | ||
"grunt-contrib-copy": "^0.5.0", | ||
"grunt-contrib-uglify": "^0.5.0", | ||
"grunt-jasmine-node-coverage": "^0.1.11", | ||
"jasmine-node": "~1.14.3", | ||
"grunt-eslint": "^17.0.0", | ||
"grunt-jasmine-node-coverage": "^0.4.1", | ||
"grunt-jsdoc": "^1.0.0", | ||
"jasmine-node": "^1.14.5", | ||
"jasmine-reporters": ">=0.2.0 <2.0.0" | ||
} | ||
} |
@@ -1,4 +0,3 @@ | ||
# js-sorting | ||
# js-sorting [![NPM version](http://img.shields.io/npm/v/js-sorting.svg?style=flat)](https://www.npmjs.org/package/js-sorting) | ||
[![NPM version](http://img.shields.io/npm/v/js-sorting.svg?style=flat)](https://www.npmjs.org/package/js-sorting) | ||
[![Build Status](http://img.shields.io/travis/Tyriar/js-sorting.svg?style=flat)](http://travis-ci.org/Tyriar/js-sorting) | ||
@@ -8,3 +7,3 @@ [![Code climate](http://img.shields.io/codeclimate/github/Tyriar/js-sorting.svg?style=flat)](https://codeclimate.com/github/Tyriar/js-sorting) | ||
A collection of sorting algorithms written in JavaScript. Each algorithm is enclosed in its own file, wrapped in a [Universal Module Definition (UMD)][1] API to make it easier to use across multiple platforms. | ||
A collection of sorting algorithms written in JavaScript. | ||
@@ -15,37 +14,21 @@ Detailed information on the complexity of each algorithm is located [here][6]. To learn more about how some of the algorithms are implemented, have a look at the [technical articles on my blog][2]. | ||
``` | ||
# via bower | ||
bower install --save js-sorting | ||
# via NPM | ||
```bash | ||
npm install --save js-sorting | ||
``` | ||
## Including | ||
## Usage | ||
**Browser** | ||
See [the source files][4] for a list sorts available and their public interfaces, Here is an example for [merge sort][5]. | ||
```html | ||
<script src="bower_components/js-sorting/src/merge-sort.js"></script> | ||
``` | ||
**Node.JS** | ||
```javascript | ||
var mergeSort = require("merge-sort"); | ||
``` | ||
var mergeSort = require("js-sorting").mergeSort; | ||
## Usage | ||
See [the source files][4] for a list sorts available and their public interfaces, Here is an example for the [merge sort][5]. | ||
```javascript | ||
// Sort normally | ||
mergeSort.sort([5, 3, 2, 4, 1]); | ||
mergeSort([5, 3, 2, 4, 1]); | ||
// Sort in reverse | ||
mergeSort.compare = function (a, b) { | ||
var reverseCompare = function (a, b) { | ||
return b - a; | ||
}; | ||
mergeSort.sort([5, 3, 2, 4, 1]); | ||
mergeSort([5, 3, 2, 4, 1], reverseCompare); | ||
@@ -60,3 +43,3 @@ // Sort complex objects | ||
]; | ||
mergeSort.compare = function (a, b) { | ||
var complexNameSort = function (a, b) { | ||
// Sort by first name first | ||
@@ -70,3 +53,3 @@ if (a.firstname.toLowerCase() < b.firstname.toLowerCase()) return -1; | ||
}; | ||
mergeSort.sort(list); | ||
mergeSort.sort(list, complexNameSort); | ||
``` | ||
@@ -79,14 +62,11 @@ | ||
```javascript | ||
var originalCompare = bubbleSort.compare; | ||
bubbleSort.compare = function (a, b) { | ||
alert('Comparing value at index "' + a + '" with "' + b + '"'); | ||
originalCompare(array, a, b); | ||
bubbleSort.attachCompareObserver = function (array, a, b) { | ||
alert('Comparing "' + array[a] + '" (i=' + a + ') with "' + | ||
array[b] + '" (i=' + b + ')'); | ||
}; | ||
var originalSwap = bubbleSort.swap; | ||
bubbleSort.swap = function (array, a, b) { | ||
bubbleSort.attachSwapObserver(function (array, a, b) { | ||
alert('Swapping "' + array[a] + '" (i=' + a + ') with "' + | ||
array[b] + '" (i=' + b + ')'); | ||
originalSwap(array, a, b); | ||
}; | ||
}); | ||
``` | ||
@@ -102,3 +82,3 @@ | ||
``` | ||
```bash | ||
npm install | ||
@@ -111,20 +91,24 @@ npm test | ||
## License | ||
MIT © [Daniel Imms][7] | ||
MIT © [Daniel Imms](http://www.growingwiththeweb.com) | ||
## See also | ||
* [Tyriar/js-data-structures][3] | ||
* [js-data-structures](https://github.com/Tyriar/js-data-structures) | ||
* [js-design-patterns](https://github.com/Tyriar/js-design-patterns) | ||
* [js-interview-questions](https://github.com/Tyriar/js-interview-questions) | ||
[1]: https://github.com/umdjs/umd/blob/master/returnExportsGlobal.js | ||
[2]: http://www.growingwiththeweb.com/p/explore.html?t=Sorting | ||
[3]: https://github.com/Tyriar/js-data-structures | ||
[4]: https://github.com/Tyriar/js-sorting/tree/master/src | ||
[5]: https://github.com/Tyriar/js-sorting/blob/master/src/merge-sort.js | ||
[6]: https://github.com/Tyriar/js-sorting/blob/master/src/README.md | ||
[7]: http://www.growingwiththeweb.com | ||
[8]: https://github.com/Tyriar/sorting-visualiser | ||
[9]: https://github.com/Tyriar/js-sorting/blob/master/src/insertion-sort.js | ||
[1]: https://github.com/umdjs/umd/blob/master/returnExportsGlobal.js | ||
[2]: http://www.growingwiththeweb.com/p/explore.html?t=Sorting | ||
[4]: lib | ||
[5]: lib/merge-sort.js | ||
[6]: lib/README.md | ||
[8]: https://github.com/Tyriar/sorting-visualiser | ||
[9]: lib/insertion-sort.js |
var testHelper = require("./test-helper"); | ||
var algorithm = require("../src/bubble-sort"); | ||
var algorithm = require("../index").bubbleSort; | ||
@@ -8,2 +8,5 @@ describe("bubble-sort", function () { | ||
testHelper.runCustomComparisonTests(algorithm); | ||
testHelper.runCompareObserverTests(algorithm); | ||
testHelper.runSwapObserverTests(algorithm); | ||
}); |
var testHelper = require("./test-helper"); | ||
var algorithm = require("../src/cocktail-sort"); | ||
var algorithm = require("../index").cocktailSort; | ||
@@ -7,3 +7,6 @@ describe("cocktail-sort", function () { | ||
testHelper.runStringTests(algorithm); | ||
testHelper.runCustomComparisonTests(algorithm) | ||
testHelper.runCustomComparisonTests(algorithm); | ||
testHelper.runCompareObserverTests(algorithm); | ||
testHelper.runSwapObserverTests(algorithm); | ||
}); |
var testHelper = require("./test-helper"); | ||
var algorithm = require("../src/comb-sort"); | ||
var algorithm = require("../index").combSort; | ||
@@ -8,2 +8,5 @@ describe("comb-sort", function () { | ||
testHelper.runCustomComparisonTests(algorithm); | ||
testHelper.runCompareObserverTests(algorithm); | ||
testHelper.runSwapObserverTests(algorithm); | ||
}); |
var testHelper = require("./test-helper"); | ||
var algorithm = require("../src/counting-sort"); | ||
var algorithm = require("../index").countingSort; | ||
describe("counting-sort", function () { | ||
// sort(array, maxValue) for arrays with non-negative values only. | ||
describe("sort(array, maxValue)", function () { | ||
for (var i = 0; i < testHelper.integerTests.length; i++) { | ||
(function (test) { | ||
var sorted = testHelper.getSorted(test); | ||
var original = testHelper.getOriginal(test); | ||
var minValue = sorted[0]; | ||
var maxValue = sorted[sorted.length - 1]; | ||
for (var i = 0; i < testHelper.integerTests.length; i++) { | ||
(function (test) { | ||
var sorted = testHelper.getSorted(test); | ||
var original = testHelper.getOriginal(test); | ||
var minValue = sorted[0]; | ||
var maxValue = sorted[sorted.length - 1]; | ||
if (minValue >= 0) { | ||
it(test.it, function () { | ||
expect(algorithm.sort(original, maxValue)) | ||
.toEqual(sorted); | ||
}); | ||
} | ||
})(testHelper.integerTests[i]); | ||
} | ||
}); | ||
// Test sort(array, minValue, maxValue) for arrays with a specific range. | ||
describe("sort(array, minValue, maxValue)", function () { | ||
for (var i = 0; i < testHelper.integerTests.length; i++) { | ||
(function (test) { | ||
var sorted = testHelper.getSorted(test); | ||
var original = testHelper.getOriginal(test); | ||
var minValue = sorted[0]; | ||
var maxValue = sorted[sorted.length - 1]; | ||
if (minValue >= 0) { | ||
it(test.it, function () { | ||
expect(algorithm.sort(original, minValue, maxValue)) | ||
.toEqual(sorted); | ||
expect(algorithm(original, maxValue)).toEqual(sorted); | ||
}); | ||
})(testHelper.integerTests[i]); | ||
} | ||
}); | ||
describe("sort(...)", function () { | ||
it("should throw an exception with 1 argument", function () { | ||
expect(function () { | ||
algorithm.sort(1); | ||
}).toThrow("Cannot sort with counting sort with 1 arguments"); | ||
}); | ||
it("should throw an exception with > 3 arguments", function () { | ||
expect(function () { | ||
algorithm.sort(1, 2, 3, 4); | ||
}).toThrow("Cannot sort with counting sort with 4 arguments"); | ||
}); | ||
}); | ||
} | ||
})(testHelper.integerTests[i]); | ||
} | ||
}); |
@@ -0,0 +0,0 @@ [{ |
@@ -0,0 +0,0 @@ [{ |
@@ -0,0 +0,0 @@ [{ |
var testHelper = require("./test-helper"); | ||
var algorithm = require("../src/gnome-sort"); | ||
var algorithm = require("../index").gnomeSort; | ||
@@ -8,2 +8,5 @@ describe("gnome-sort", function () { | ||
testHelper.runCustomComparisonTests(algorithm); | ||
testHelper.runCompareObserverTests(algorithm); | ||
testHelper.runSwapObserverTests(algorithm); | ||
}); |
var testHelper = require("./test-helper"); | ||
var algorithm = require("../src/heapsort"); | ||
var algorithm = require("../index").heapsort; | ||
@@ -8,2 +8,5 @@ describe("heapsort", function () { | ||
testHelper.runCustomComparisonTests(algorithm); | ||
testHelper.runCompareObserverTests(algorithm); | ||
testHelper.runSwapObserverTests(algorithm); | ||
}); |
var testHelper = require("./test-helper"); | ||
var algorithm = require("../src/insertion-sort"); | ||
var algorithm = require("../index").insertionSort; | ||
@@ -8,2 +8,5 @@ describe("insertion-sort", function () { | ||
testHelper.runCustomComparisonTests(algorithm); | ||
//testHelper.runCompareObserverTests(algorithm); | ||
testHelper.runShiftObserverTests(algorithm); | ||
}); |
var testHelper = require("./test-helper"); | ||
var algorithm = require("../src/merge-sort-bottom-up"); | ||
var algorithm = require("../index").mergeSortBottomUp; | ||
@@ -4,0 +4,0 @@ describe("merge-sort-bottom-up", function () { |
var testHelper = require("./test-helper"); | ||
var algorithm = require("../src/merge-sort"); | ||
var algorithm = require("../index").mergeSort; | ||
@@ -4,0 +4,0 @@ describe("merge-sort", function () { |
var testHelper = require("./test-helper"); | ||
var algorithm = require("../src/odd-even-sort"); | ||
var algorithm = require("../index").oddEvenSort; | ||
@@ -8,2 +8,5 @@ describe("odd-even-sort", function () { | ||
testHelper.runCustomComparisonTests(algorithm); | ||
testHelper.runCompareObserverTests(algorithm); | ||
testHelper.runSwapObserverTests(algorithm); | ||
}); |
var testHelper = require("./test-helper"); | ||
var algorithm = require("../src/quicksort"); | ||
var algorithm = require("../index").quicksort; | ||
@@ -8,2 +8,5 @@ describe("quicksort", function () { | ||
testHelper.runCustomComparisonTests(algorithm); | ||
testHelper.runCompareObserverTests(algorithm); | ||
testHelper.runSwapObserverTests(algorithm); | ||
}); |
var testHelper = require("./test-helper"); | ||
var algorithm = require("../src/selection-sort"); | ||
var algorithm = require("../index").selectionSort; | ||
@@ -8,2 +8,5 @@ describe("selection-sort", function () { | ||
testHelper.runCustomComparisonTests(algorithm); | ||
testHelper.runCompareObserverTests(algorithm); | ||
testHelper.runSwapObserverTests(algorithm); | ||
}); |
@@ -13,3 +13,3 @@ var testHelper = {}; | ||
it(test.it, function () { | ||
expect(algorithm.sort(testHelper.getOriginal(test))) | ||
expect(algorithm(testHelper.getOriginal(test))) | ||
.toEqual(testHelper.getSorted(test)); | ||
@@ -29,3 +29,3 @@ }); | ||
it(test.it, function () { | ||
expect(algorithm.sort(testHelper.getOriginal(test))) | ||
expect(algorithm(testHelper.getOriginal(test))) | ||
.toEqual(testHelper.getSorted(test)); | ||
@@ -44,6 +44,6 @@ }); | ||
it(test.it, function () { | ||
algorithm.compare = function (a, b) { | ||
var compare = function (a, b) { | ||
return b - a; | ||
}; | ||
expect(algorithm.sort(testHelper.getOriginal(test))) | ||
expect(algorithm(testHelper.getOriginal(test), compare)) | ||
.toEqual(testHelper.getSorted(test)); | ||
@@ -56,6 +56,6 @@ }); | ||
it(test.it, function () { | ||
algorithm.compare = function (a, b) { | ||
var compare = function (a, b) { | ||
return a - b; | ||
}; | ||
expect(algorithm.sort(testHelper.getOriginal(test))) | ||
expect(algorithm(testHelper.getOriginal(test), compare)) | ||
.toEqual(testHelper.getSorted(test)); | ||
@@ -68,2 +68,45 @@ }); | ||
testHelper.runCompareObserverTests = function (algorithm) { | ||
describe('compare observer tests', function () { | ||
it('should call the compare observer', function () { | ||
var wrapper = { | ||
observer: function () {} | ||
}; | ||
spyOn(wrapper, 'observer'); | ||
algorithm.attachCompareObserver(wrapper.observer); | ||
algorithm([2, 1], undefined); | ||
expect(wrapper.observer).toHaveBeenCalled(); | ||
algorithm.attachCompareObserver(); | ||
}); | ||
}); | ||
}; | ||
testHelper.runSwapObserverTests = function (algorithm) { | ||
describe('swap observer tests', function () { | ||
it('should call the swap observer', function () { | ||
var wrapper = { | ||
observer: function () {} | ||
}; | ||
spyOn(wrapper, 'observer'); | ||
algorithm.attachSwapObserver(wrapper.observer); | ||
algorithm([2, 1], undefined); | ||
expect(wrapper.observer).toHaveBeenCalled(); | ||
algorithm.attachSwapObserver(); | ||
}); | ||
}); | ||
}; | ||
testHelper.runShiftObserverTests = function (algorithm) { | ||
describe('shift observer tests', function () { | ||
it('should call the shift observer', function () { | ||
var wrapper = { | ||
observer: function () {} | ||
}; | ||
spyOn(wrapper, 'observer'); | ||
algorithm([5, 4, 1, 2, 3], undefined, wrapper.observer); | ||
expect(wrapper.observer).toHaveBeenCalled(); | ||
}); | ||
}); | ||
}; | ||
// Only test on copies of the test arrays | ||
@@ -70,0 +113,0 @@ testHelper.getOriginal = function (test) { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
6
63047
53
1575
108