Skip to content

Commit

Permalink
added mean, sum, sort
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielJDufour committed Jun 28, 2022
1 parent 86fb4a9 commit bfdbd6d
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 0 deletions.
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,15 @@ The following functions are supported:
- [divide](#divide)
- [floor](#floor)
- [max](#max)
- [mean](#mean)
- [min](#min)
- [multiply](#multiply)
- [pow](#pow)
- [remainder](#remainder)
- [sign](#sign)
- [sort](#sort)
- [subtract](#subtract)
- [sum](#sum)
- [truncate](#truncate)

## install
Expand Down Expand Up @@ -56,6 +59,7 @@ abs("-10");
```

### add
Add two numbers together.
```js
import add from "preciso/add.js";

Expand Down Expand Up @@ -108,6 +112,20 @@ floor("-45.951267538761253765124376412365124736541263451265347126534761524376512
"-46"
```

### mean
```js
import mean from "preciso/mean.js";

mean(["1", "10", "4"])
"5"

mean(["1", "2", "8"], { max_decimal_digits: 3 })
"3.667"

mean(["1", "2", "8"], { ellipsis: true })
"3.666..."
```

### max
```js
import max from "preciso/min.js";
Expand Down Expand Up @@ -173,6 +191,19 @@ sign("-0.00");
""
```

### sort
```js
import sort from "preciso/sort.js"

// default is sorting in ascending order (smallest to greatest)
sort(["1", "2", "3"])
["1", "2", "3"]

// sort in descending order from greatest to smallest
sort(["1", "2", "3"], { direction: "descending" })
["3", "2", "1"]
```

### subtract
```js
import subtract from "preciso/subtract.js";
Expand All @@ -181,6 +212,15 @@ subtract("10", "2.14"); // 10 - 2.14
"7.86"
```

### sum
Similar to add, but can take an array of more than two numbers.
```js
import sum from "preciso/sum.js";

sum(["1", "2", "3"])
"6"
```

### truncate
```js
import truncate from "preciso/truncate.js";
Expand Down
15 changes: 15 additions & 0 deletions mean.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const add = require("./add.js");
const divide = require("./divide.js");

function mean(nums, options) {
let count = 0;
let total = "0";
for (let num of nums) {
count++;
total = add(total, num);
}
return divide(total, count.toString(), options);
}

module.exports = mean;
module.exports.default = mean;
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"long_multiplication.js",
"long_subtraction.js",
"max.js",
"mean.js",
"min.js",
"multiply.js",
"pow.js",
Expand All @@ -36,10 +37,13 @@
"round_last_decimal.js",
"sign.js",
"sign_nonzero.js",
"sort.js",
"subtract.js",
"sum.js",
"truncate.js"
],
"scripts": {
"b": "npm run build",
"build": "npx browserify preciso.js > preciso.min.js",
"f": "npm run format",
"format": "npx prettier --arrow-parens=avoid --print-width=160 --trailing-comma=none --write *.js *.ts",
Expand Down
10 changes: 10 additions & 0 deletions preciso.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const is_infinity = require("./is_infinity.js");
const is_integer = require("./is_integer.js");
const is_zero = require("./is_zero.js");

const mean = require("./mean.js");
const min = require("./min.js");
const max = require("./max.js");
const expand = require("./expand.js");
Expand All @@ -37,6 +38,10 @@ const pow_positive = require("./pow_positive.js");
const sign = require("./sign.js");
const sign_nonzero = require("./sign_nonzero.js");

const sort = require("./sort.js");

const sum = require("./sum.js");

const truncate = require("./truncate.js");

const module_exports = {
Expand All @@ -55,6 +60,7 @@ const module_exports = {
is_integer,
is_zero,

mean,
min,
max,

Expand All @@ -78,6 +84,10 @@ const module_exports = {
sign,
sign_nonzero,

sort,

sum,

subtract,
long_subtraction,

Expand Down
9 changes: 9 additions & 0 deletions sort.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const compare = require("./compare");

function sort(nums, { direction = "ascending" } = { direction: "ascending" }) {
const op = direction === "desc" || direction === "descending" ? "<" : ">";
return nums.sort((a, b) => (compare(a, b) === op ? 1 : -1));
}

module.exports = sort;
module.exports.default = sort;
13 changes: 13 additions & 0 deletions sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const add = require("./add.js");

function sum(nums) {
let total = "0";
// using iterator protocol
for (let num of nums) {
total = add(total, num);
}
return total;
}

module.exports = sum;
module.exports.default = sum;
23 changes: 23 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
is_infinity,
is_integer,
is_zero,
mean,
min,
max,
expand,
Expand All @@ -26,10 +27,32 @@ const {
remainder,
round_last_decimal,
sign,
sort,
sum,
subtract,
truncate
} = preciso;

test("sort", ({ eq }) => {
eq(sort(["1", "2", "3"]), ["1", "2", "3"]);
eq(sort(["1", "2", "3"], { direction: "descending" }), ["3", "2", "1"]);
const nums = ["0", "-10", "-10.213", "12e34", "-1e-424", "3123761254357621"];
eq(sort(nums), ["-10.213", "-10", "-1e-424", "0", "3123761254357621", "12e34"]);
eq(sort(nums, { direction: "descending" }), ["12e34", "3123761254357621", "0", "-1e-424", "-10", "-10.213"]);
});

test("sum", ({ eq }) => {
eq(sum(["1", "2", "3"]), "6");
eq(sum(["1", "2", "8"]), "11");
});

test("mean", ({ eq }) => {
eq(mean(["1", "10", "4"]), "5");
eq(mean(["-1", "2", "8"], { ellipsis: true }), "3");
eq(mean(["1", "2", "8"], { max_decimal_digits: 3 }), "3.667");
eq(mean(["1", "2", "8"], { ellipsis: true }), "3.666...");
});

test("is_infinity", ({ eq }) => {
eq(is_infinity("-inf"), true);
eq(is_infinity("inf"), true);
Expand Down

0 comments on commit bfdbd6d

Please sign in to comment.