xonecas.io
2620
Ja

js-algos / commit b9f9f20

start moving to es6

xonecas2017-12-05 b9f9f20478fc47c49f3ed9e93aee4e0b9f814059 parent 2fb71e8

6 files changed

.babelrc+3−0
@@ -0,0 +1,3 @@
1+{
2+ "preset": ["env"]
3+}
.gitignore+1−0
@@ -0,0 +1 @@
1+node_modules/
Makefile+0−27
@@ -1,27 +0,0 @@
1-all: lint docs module
2-
3-lint:
4- @echo "----> Linting JS..."
5- jslint --browser true --node true --white true js/algos.js
6- uglifyjs --lint js/algos.js > /dev/null
7- jslint --browser true --node true --white true test/*.js
8- uglifyjs --lint test/*.js > /dev/null
9-
10-docs:
11- @echo "----> Making docs..."
12- docco -c css/docco.css js/algos.js
13-
14-module:
15- @echo "----> Creating Module..."
16- cat js/{head,algos,tail}.js | uglifyjs -c -m > algos.min.js
17- cat js/{head,algos,tail}.js | uglifyjs -b > algos.js
18-
19-clean:
20- rm -rf docs algos.min.js
21-
22-test: lint module
23- @echo "----> Testing..."
24- node test/sorts.js
25- node test/levs.js
26-
27-.PHONY: lint docs module clean test
algos.js+0−147
@@ -1,147 +0,0 @@
1-(function() {
2- "use strict";
3- var algos = {};
4- var insertionSort;
5- insertionSort = algos.insertionSort = function(list) {
6- var len = list.length, val, i, hole;
7- for (i = 1; i < len; i += 1) {
8- val = list[i];
9- hole = i;
10- while (hole > 0 && val < list[hole - 1]) {
11- list[hole] = list[hole - 1];
12- hole -= 1;
13- }
14- list[hole] = val;
15- }
16- return list;
17- };
18- var join, mergeSort;
19- join = function(a, b) {
20- var joined = [], ai = 0, bi = 0;
21- while (ai < a.length && bi < b.length) {
22- if (a[ai] < b[bi]) {
23- joined.push(a[ai]);
24- ai += 1;
25- } else {
26- joined.push(b[bi]);
27- bi += 1;
28- }
29- }
30- return joined.concat(a.slice(ai).concat(b.slice(bi)));
31- };
32- mergeSort = algos.mergeSort = function(list) {
33- if (list.length <= 1) {
34- return list;
35- }
36- var m = Math.floor(list.length / 2), a = list.slice(0, m), b = list.slice(m);
37- return join(mergeSort(a), mergeSort(b));
38- };
39- var swap, partition, quickSort;
40- swap = function(list, first, second) {
41- var tmp = list[first];
42- list[first] = list[second];
43- list[second] = tmp;
44- };
45- partition = function(list, left, right) {
46- var pivot = list[Math.floor((left + right) / 2)];
47- while (left <= right) {
48- while (list[left] < pivot) {
49- left += 1;
50- }
51- while (list[right] > pivot) {
52- right -= 1;
53- }
54- if (left <= right) {
55- swap(list, left, right);
56- left += 1;
57- right -= 1;
58- }
59- }
60- return left;
61- };
62- quickSort = algos.quickSort = function(list, left, right) {
63- var idx;
64- if (list.length >= 2) {
65- left = left === undefined ? 0 : left;
66- right = right === undefined ? list.length - 1 : right;
67- idx = partition(list, left, right);
68- if (left < idx - 1) {
69- quickSort(list, left, idx - 1);
70- }
71- if (idx < right) {
72- quickSort(list, idx, right);
73- }
74- }
75- return list;
76- };
77- var randomizedPartition, randomizedQuickSort;
78- randomizedPartition = function(list, left, right) {
79- var pivot = list[Math.floor(Math.random() * (right - left) + left)];
80- while (left <= right) {
81- while (list[left] < pivot) {
82- left += 1;
83- }
84- while (list[right] > pivot) {
85- right -= 1;
86- }
87- if (left <= right) {
88- swap(list, left, right);
89- left += 1;
90- right -= 1;
91- }
92- }
93- return left;
94- };
95- randomizedQuickSort = algos.randomizedQuickSort = function(list, left, right) {
96- var idx;
97- if (list.length >= 2) {
98- left = left === undefined ? 0 : left;
99- right = right === undefined ? list.length - 1 : right;
100- idx = randomizedPartition(list, left, right);
101- if (left < idx - 1) {
102- randomizedQuickSort(list, left, idx - 1);
103- }
104- if (idx < right) {
105- randomizedQuickSort(list, idx, right);
106- }
107- }
108- return list;
109- };
110- var bubbleSort;
111- bubbleSort = algos.bubbleSort = function(list) {
112- var len = list.length, i, j;
113- for (i = 0; i < len; i += 1) {
114- for (j = 0; j < len - i; j += 1) {
115- if (list[j] > list[j + 1]) {
116- swap(list, j, j + 1);
117- }
118- }
119- }
120- return list;
121- };
122- var shuffle;
123- shuffle = algos.shuffle = function(list) {
124- var len = list.length, max = len - 1;
125- while (len) {
126- len -= 1;
127- swap(list, len, Math.floor(Math.random() * max));
128- }
129- return list;
130- };
131- var fuzzy;
132- fuzzy = algos.fuzzy = function(a, b) {
133- if (a === b) {
134- return 0;
135- }
136- if (!a.length || !b.length) {
137- return 0;
138- }
139- return Math.min(fuzzy(a.substring(1), b) + 1, fuzzy(b.substring(1), a) + 1, fuzzy(a.substring(1), b.substring(1)) + (a[0] !== b[0] ? 1 : 0));
140- };
141- if (module && module.exports || exports) {
142- module = module || {};
143- exports = module.exports = algos;
144- } else {
145- window.algos = algos;
146- }
147-})();
algos.min.js+0−1
@@ -1 +0,0 @@
1-(function(){"use strict";var r,t={};r=t.insertionSort=function(r){var t,n,o,e=r.length;for(n=1;e>n;n+=1){for(t=r[n],o=n;o>0&&r[o-1]>t;)r[o]=r[o-1],o-=1;r[o]=t}return r};var n,o;n=function(r,t){for(var n=[],o=0,e=0;r.length>o&&t.length>e;)r[o]<t[e]?(n.push(r[o]),o+=1):(n.push(t[e]),e+=1);return n.concat(r.slice(o).concat(t.slice(e)))},o=t.mergeSort=function(r){if(1>=r.length)return r;var t=Math.floor(r.length/2),e=r.slice(0,t),u=r.slice(t);return n(o(e),o(u))};var e,u,i;e=function(r,t,n){var o=r[ …
package.json+30−0
@@ -0,0 +1,30 @@
1+{
2+ "name": "js-algos",
3+ "version": "1.0.0",
4+ "description": "common algorithms implemented in js",
5+ "main": "algos.js",
6+ "directories": {
7+ "doc": "docs",
8+ "test": "test"
9+ },
10+ "devDependencies": {
11+ "babel-cli": "^6.26.0",
12+ "babel-core": "^6.26.0",
13+ "babel-preset-env": "^1.6.1"
14+ },
15+ "scripts": {
16+ "test": "make test",
17+ "build": "make",
18+ "docs": "make docs"
19+ },
20+ "repository": {
21+ "type": "git",
22+ "url": "git+ssh://git@gitlab.com/xonecas/js-algos.git"
23+ },
24+ "author": "gitlab.com/xonecas",
25+ "bugs": {
26+ "url": "https://gitlab.com/xonecas/js-algos/issues"
27+ },
28+ "homepage": "https://gitlab.com/xonecas/js-algos#README",
29+ "license": "./LICENSE"
30+}