Skip to content

Commit

Permalink
updated minimum, maximum
Browse files Browse the repository at this point in the history
  • Loading branch information
tmhglnd committed Nov 15, 2021
1 parent 8d9b8f5 commit b42db09
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
25 changes: 25 additions & 0 deletions docs/utility-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ const Util = require('total-serialism').Utility;
- subtract
- multiply
- divide
- mod
- sum
- minimum
- maximum
- normalize
- flatten
- plot
- draw

Expand Down Expand Up @@ -85,6 +87,11 @@ Return the minimum value from an array (Also part of `.Statistic`)
```js
Util.minimum([-38, -53, -6, 33, 88, 32, -8, 73]);
//=> -53

// Also works with n-dimensional arrays
Stat.minimum([-38, [-53, [-6, 33], 88, 32], [-8, 73]]);
//=> -53

// Alternative: Util.min()
```

Expand All @@ -95,6 +102,11 @@ Return the maximum value from an array (Also part of `.Statistic`)
```js
Util.maximum([-38, -53, -6, 33, 88, 32, -8, 73]);
//=> 88

// Also works with n-dimensional arrays
Stat.maximum([-38, [-53, [-6, 33], 88, 32], [-8, 73]]);
//=> 88

// Alternative: Util.max()
```

Expand All @@ -115,6 +127,19 @@ Util.normalize([5, 12, 4, 17, 3]);
//=> [ 0.14285714285714285, 0.6428571428571429, 0.07142857142857142, 1, 0 ]
```

## flatten

Flatten a multidimensional array to a single dimension. Optionally set the depth for the flattening.

**arguments**
- {Array} -> array to flatten
- {Number} -> depth of flatten

```js
Util.flatten([1, [2, 3, [4, 5], 6], 7]);
// => [ 1, 2, 3, 4, 5, 6, 7 ]
```

## plot

Plot an array of values to the console in the form of an ascii chart and return chart from function. If you just want the chart returned as text and not log to console set { log: false }. Using the asciichart package by x84.
Expand Down
4 changes: 2 additions & 2 deletions src/transform.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,8 +189,8 @@ exports.tFilter = filterType;
function invert(arr=[0], lo, hi){
arr = Array.isArray(arr)? arr : [arr];
if (lo === undefined){
hi = Math.max(...Util.flatten(arr));
lo = Math.min(...Util.flatten(arr));
hi = Util.max(arr);
lo = Util.max(arr);
} else if (hi === undefined){
hi = lo;
}
Expand Down
12 changes: 2 additions & 10 deletions src/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,7 @@ exports.sum = sum;
//
function maximum(a=[0]){
if (!Array.isArray(a)) { return a; }
let m = -Infinity;
for (let i in a){
m = (a[i] > Number(m))? a[i] : m;
}
return m;
return Math.max(...flatten(a));
}
exports.maximum = maximum;
exports.max = maximum;
Expand All @@ -372,11 +368,7 @@ exports.max = maximum;
//
function minimum(a=[0]){
if (!Array.isArray(a)) { return a; }
let m = Infinity;
for (let i in a){
m = (a[i] < Number(m))? a[i] : m;
}
return m;
return Math.min(...flatten(a));
}
exports.minimum = minimum;
exports.min = minimum;
Expand Down
5 changes: 3 additions & 2 deletions test/serialism.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -488,15 +488,16 @@ function testStat(){
test("Stat.maximum()");
test("Stat.maximum(10)");
test("Stat.maximum([-38, -53, -6, 33, 88, 32, -8, 73])");
test("Stat.maximum([-38, [-53, [-6, 33], 88, 32], [-8, 73]])");

test("Stat.minimum()");
test("Stat.minimum(10)");
test("Stat.minimum([-38, -53, -6, 33, 88, 32, -8, 73])");
test("Stat.minimum([-38, -53, -6, 'foo', 33, 'bar'])");
test("Stat.minimum([-38, [-53, [-6, 33], 88, 32], [-8, 73]])");
// test("Stat.minimum([-38, -53, -6, 'foo', 33, 'bar'])");

test("Stat.change()");
test("Stat.change(4)");
test("Stat.change(['foo', 'bar'])");
test("Stat.change([0, 3, 7, 0, 12, 9, 5, 7])");
}

Expand Down

0 comments on commit b42db09

Please sign in to comment.