Skip to content

Commit

Permalink
minor methods optimization, non-critical
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbykoff committed Jun 14, 2016
1 parent 44f392a commit 3a58460
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ It is just an utility tool that makes life easier and also does some maths for y
- Log an array breakdown
- Remove all the empty items from the nested arrays
- Filter items in an array by type
- Get rid of all non-alphanumeric values **(0.4.0)+**
- Get rid of all non-alphanumeric characters **(0.4.0)+**

----------

Expand Down
61 changes: 28 additions & 33 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,31 @@
{
"name": "array-cop",
"version": "0.4.1",
"description": "An [array] utility tool that makes life easier and also does some maths for you.",
"main": "src/array-cop.js",
"scripts": {
"test": "istanbul cover node_modules/mocha/bin/_mocha -- -R spec"
},
"repository": {
"type": "git",
"url": "git+https://github.com/tomkallen/array-cop.git"
},
"keywords": [
"arrays",
"array",
"utils",
"utility",
"maths"
],
"author": "Alex Bykoff <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/tomkallen/array-cop/issues"
},
"homepage": "https://github.com/tomkallen/array-cop#readme",
"devDependencies": {
"chai": "3.5.0",
"istanbul": "0.4.3",
"mocha": "2.5.3"
},
"config": {
"blanket": {
"pattern": "src/array-cop.js"
"name": "array-cop",
"version": "0.4.2",
"description": "An [array] utility tool that makes life easier and also does some maths for you.",
"main": "src/array-cop.js",
"scripts": {
"test": "istanbul cover node_modules/mocha/bin/_mocha -- -R spec"
},
"repository": {
"type": "git",
"url": "git+https://github.com/tomkallen/array-cop.git"
},
"keywords": [
"arrays",
"array",
"utils",
"utility",
"maths"
],
"author": "Alex Bykoff <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/tomkallen/array-cop/issues"
},
"homepage": "https://github.com/tomkallen/array-cop#readme",
"devDependencies": {
"chai": "3.5.0",
"istanbul": "0.4.3",
"mocha": "2.5.3"
}
}
}
40 changes: 20 additions & 20 deletions src/array-cop.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
(function(_) {
var array_ = {
flatten: function(arr) {
/** Flattens an array to a single-dimensional one
*/
/** Flattens an array to a single-dimensional one */

var __ = this;

Expand Down Expand Up @@ -36,7 +35,7 @@
},

sum: function(arr) {
/** Flattens an array and takes only numeric values into a consideration */
/** Returns a sum of all the items. Flattens an array and takes only numeric values into a consideration */

if (Array.isArray(arr)) {
return this.flatten(arr).reduce(function(a, b) {
Expand All @@ -48,13 +47,13 @@

mean: function(arr, type, precision) {
/** Return mean of the array(arr) items. Flattens an array and takes only numeric
* values into a consideration.
*
* type: type of mean - 'ari': arithmetic, 'geo': geometric, 'har': harmonic.
* If omitted then calculates an arithmetic mean.
*
* precision: Number — Optional argument, sets the number of digits after a decimal * point. If omitted then falls back to 2.
*/
* values into a consideration.
*
* type: type of mean - 'ari': arithmetic, 'geo': geometric, 'har': harmonic.
* If omitted then calculates an arithmetic mean.
*
* precision: Number — Optional argument, sets the number of digits after a decimal * point. If omitted then falls back to 2.
*/
if (Array.isArray(arr)) {

var mean = 0,
Expand All @@ -80,7 +79,7 @@
num++;
}
}
mean = (sum / num).toFixed(precision);
mean = (sum / num);
break;

case 'geo':
Expand All @@ -90,7 +89,7 @@
num++;
}
}
mean = Math.pow(mul, 1 / num).toFixed(precision);
mean = Math.pow(mul, 1 / num);
break;

case 'har':
Expand All @@ -105,9 +104,9 @@
}).reduce(function(a, b) {
return a + b
});
mean = (harArray.length / harDenominator).toFixed(precision);
mean = (harArray.length / harDenominator);
}
return mean;
return mean.toFixed(precision);
}
throw new Error("Not an array!");
},
Expand Down Expand Up @@ -137,11 +136,13 @@
return a - b;
});

var h = Math.floor(newArr.length / 2);
var medianItem = Math.floor(newArr.length / 2);

if (newArr.length % 2) {
return newArr[h]
} else return ((newArr[h - 1] + newArr[h]) / 2).toFixed(precision);
// If number of Number items is odd then middle item is the median
return newArr[medianItem]
// Otherwise calculate and average of two items in the middle
} else return ((newArr[medianItem - 1] + newArr[medianItem]) / 2).toFixed(precision);
}
throw new Error("Not an array!");
},
Expand All @@ -152,6 +153,8 @@

arr = this.flatten(arr);

// Generates an object where value in key-value pair is the number of times
// such an item appears in an array
var frequencyMap = arr.reduce(function(obj, item) {
if (obj[item]) {
obj[item]++;
Expand Down Expand Up @@ -250,11 +253,8 @@
if (Array.isArray(arr)) {

arr = this.flatten(arr);

type = type || "string";
type = type.toLowerCase();
logic = logic || 'all'
logic = logic.toLowerCase();

// Going switch() because of may be new logic later on
switch (logic) {
Expand Down
2 changes: 1 addition & 1 deletion src/array-cop.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3a58460

Please sign in to comment.