Skip to content

Commit

Permalink
refactored util methods add, sub, mul, div
Browse files Browse the repository at this point in the history
  • Loading branch information
tmhglnd committed Nov 22, 2021
1 parent f220a9e commit bca7a14
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 78 deletions.
13 changes: 13 additions & 0 deletions docs/utility-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,17 +55,30 @@ Basic arithmetic methods that accept arrays in both arguments. Outputlength is a
Util.add([1, 2, 3, 4], [1, 2, 3]);
//=> [ 2, 4, 6, 5 ]

// Works with n-dimensional arrays
Util.add([1, [2, 3]], [10, [20, 30, 40]]);
//=> [ 11, [ 22, 33, 42 ] ]

// Subtract two arrays sequentially
Util.subtract([1, 2, 3, 4], [1, 2, 3]);
//=> [ 0, 0, 0, 3 ]

Util.sub([1, [2, 3]], [10, [20, 30, 40]]);
//=> [ -9, [ -18, -27, -38 ] ]

// Multiply two arrays sequentially
Util.multiply([1, 2, 3, 4], [1, 2, 3]);
//=> [ 1, 4, 9, 4 ]

Util.mul([1, [2, 3]], [10, [20, 30, 40]]);
//=> [ 10, [ 40, 90, 80 ] ]

// Divide two arrays sequentially
Util.divide([1, 2, 3, 4], [1, 2, 3]);
//=> [ 1, 1, 1, 4 ]

Util.div([1, [2, 3]], [10, [20, 30, 40]]);
//=> [ 0.1, [ 0.1, 0.1, 0.05 ] ]
```

## sum
Expand Down
89 changes: 24 additions & 65 deletions src/utility.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,31 +141,23 @@ exports.lerp = _mix;
// @param {Number/Array} -> value to add
// @return {Number/Array}
//
function add(a, v=0){
function add(a=0, v=0){
// if righthand side is array
if (Array.isArray(v)){
a = (Array.isArray(a))? a : [a];
let l1 = a.length, l2 = v.length, r = [];
let l = Math.max(l1, l2);
for (let i=0; i<l; i++){
let a1 = a[i % l1];
let v1 = v[i % l2];
if (Array.isArray(a1) || Array.isArray(v1)){
r[i] = add(a1, v1);
} else {
r[i] = a1 + v1;
}
r[i] = add(a[i % l1], v[i % l2]);
}
return r;
}
// if both are single values
if (!Array.isArray(a)){
return a + v;
}
return a.map(x => {
if (Array.isArray(x)){
return add(x, v);
}
return x + v;
});
// if lefthand side is array
return a.map(x => add(x, v));
}
exports.add = add;

Expand All @@ -177,31 +169,20 @@ exports.add = add;
// @param {Number/Array} -> value to subtract
// @return {Number/Array}
//
function subtract(a, v=0){
function subtract(a=0, v=0){
if (Array.isArray(v)){
a = (Array.isArray(a))? a : [a];
let l1 = a.length, l2 = v.length, r = [];
let l = Math.max(l1, l2);
for (let i=0; i<l; i++){
let a1 = a[i % l1];
let v1 = v[i % l2];
if (Array.isArray(a1) || Array.isArray(v1)){
r[i] = subtract(a1, v1);
} else {
r[i] = a1 - v1;
}
r[i] = subtract(a[i % l1], v[i % l2]);
}
return r;
}
if (!Array.isArray(a)){
return a - v;
}
return a.map(x => {
if (Array.isArray(x)){
return subtract(x, v);
}
return x - v;
});
return a.map(x => subtract(x, v));
}
exports.subtract = subtract;
exports.sub = subtract;
Expand All @@ -214,31 +195,20 @@ exports.sub = subtract;
// @param {Number/Array} -> value to multiply with
// @return {Number/Array}
//
function multiply(a, v=1){
function multiply(a=0, v=1){
if (Array.isArray(v)){
a = (Array.isArray(a))? a : [a];
let l1 = a.length, l2 = v.length, r = [];
let l = Math.max(l1, l2);
for (let i=0; i<l; i++){
let a1 = a[i % l1];
let v1 = v[i % l2];
if (Array.isArray(a1) || Array.isArray(v1)){
r[i] = multiply(a1, v1);
} else {
r[i] = a1 * v1;
}
r[i] = multiply(a[i % l1], v[i % l2]);
}
return r;
}
if (!Array.isArray(a)){
return a * v;
}
return a.map(x => {
if (Array.isArray(x)){
return multiply(x, v);
}
return x * v;
});
return a.map(x => multiply(x, v));
}
exports.multiply = multiply;
exports.mul = multiply;
Expand All @@ -251,31 +221,20 @@ exports.mul = multiply;
// @param {Number/Array} -> value to divide with
// @return {Number/Array}
//
function divide(a, v=1){
function divide(a=0, v=1){
if (Array.isArray(v)){
a = (Array.isArray(a))? a : [a];
let l1 = a.length, l2 = v.length, r = [];
let l = Math.max(l1, l2);
for (let i=0; i<l; i++){
let a1 = a[i % l1];
let v1 = v[i % l2];
if (Array.isArray(a1) || Array.isArray(v1)){
r[i] = divide(a1, v1);
} else {
r[i] = a1 / v1;
}
r[i] = divide(a[i % l1], v[i % l2]);
}
return r;
}
if (!Array.isArray(a)){
return a / v;
}
return a.map(x => {
if (Array.isArray(x)){
return divide(x, v);
}
return x / v;
});
return a.map(x => divide(x, v));
}
exports.divide = divide;
exports.div = divide;
Expand All @@ -295,27 +254,27 @@ exports.flatten = flatten;
exports.flat = flatten;

// Return the remainder after division
// also works in the negative direction
// also works in the negative direction, so wrap starts at 0
//
// @param {Int/Array} -> input value
// @param {Int/Array} -> divisor (optional, default=12)
// @return {Int/Array} -> remainder after division
//
function mod(a, mod=12){
if (Array.isArray(mod)){
function mod(a, m=12){
if (Array.isArray(m)){
a = (Array.isArray(a))? a : [a];
let l1 = a.length, l2 = mod.length, r = [];
let l1 = a.length, l2 = m.length, r = [];
let l = Math.max(l1, l2);
for (let i=0; i<l; i++){
let m = mod[i % l2];
r[i] = ((a[i % l1] % m) + m) % m;
r[i] = mod(a[i % l1], m[i % l2]);
}
return r;
}
if (!Array.isArray(a)){
return ((a % mod) + mod) % mod;
let r = ((a % m) + m) % m;
return isNaN(r)? 0 : r;
}
return a.map(x => ((x % mod) + mod) % mod);
return a.map(x => mod(x, m));
}
exports.mod = mod;

Expand All @@ -328,7 +287,7 @@ function truncate(a=[0]){
if (!Array.isArray(a)){
return Math.trunc(a);
}
return a.map(x => Math.trunc(x));
return a.map(x => truncate(x));
}
exports.truncate = truncate;
exports.trunc = truncate;
Expand Down
51 changes: 38 additions & 13 deletions test/serialism.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,12 @@ function fullTest(){

// testSerial();
// testGen();
testAlgo();
// testAlgo();
// testRand();
// testMod();
// testStat();
// testTranslate();
// testUtil();
testUtil();

pagebreak("All Tests Passed");
console.timeEnd('Total Time');
Expand Down Expand Up @@ -588,9 +588,6 @@ function testTranslate(){
function testUtil(){
pagebreak("Utility");

test("Util.mod(7, 3)");
test("Util.mod([-2, 4, 3, 7], 5)");

test("Util.bound(10.34, 0, 3.14)");
test("Util.bound([-2, 4, 3, 7], 1, 5)");

Expand All @@ -603,25 +600,48 @@ function testUtil(){
test("Util.map(0.5, 0, 1, 0, 2, 0.5)");
test("Util.map([0, 1, 2, 3, 4], 0, 4, -1, 1)");

test("Util.add()");
test("Util.add(5, 2)");
test("Util.add([0, 3, 7], 2)");
test("Util.add([1, 2, 3, 4], [1, 2, 3])");
test("Util.add([1, 2], [1, 2, 3, 4])");
test("Util.add(2, [0, 3, 7])");
test("Util.add([1, 2, 3, 4], [10, 20, 30])");
test("Util.add([1, 2, 3], [10, 20, 30, 40])");
test("Util.add([1, [2, 3]], [10, [20, 30, 40]])");
test("Util.add([1, [2, 3], 4], [10, 20, 30])");

test("Util.sub()");
test("Util.sub(5, 2)");
test("Util.sub([0, 3, 7], 2)");
test("Util.sub([1, 2, 3, 4], [1, 2, 3])");
test("Util.sub([1, 2], [1, 2, 3, 4])");
test("Util.sub(2, [0, 3, 7])");
test("Util.sub([1, 2, 3, 4], [10, 20, 30])");
test("Util.sub([1, 2, 3], [10, 20, 30, 40])");
test("Util.sub([1, [2, 3]], [10, [20, 30, 40]])");
test("Util.sub([1, [2, 3], 4], [10, 20, 30])");

test("Util.mul()");
test("Util.mul(5, 2)");
test("Util.mul([0, 3, 7], 2)");
test("Util.mul([1, 2, 3, 4], [1, 2, 3])");
test("Util.mul([1, 2], [1, 2, 3, 4])");
test("Util.mul(2, [0, 3, 7])");
test("Util.mul([1, 2, 3, 4], [10, 20, 30])");
test("Util.mul([1, 2, 3], [10, 20, 30, 40])");
test("Util.mul([1, [2, 3]], [10, [20, 30, 40]])");
test("Util.mul([1, [2, 3], 4], [10, 20, 30])");

test("Util.div()");
test("Util.div(5, 2)");
test("Util.div([0, 3, 7], 2)");
test("Util.div([1, 2, 3, 4], [1, 2, 3])");
test("Util.div([1, 2], [1, 2, 3, 4])");
test("Util.div(2, [0, 3, 7])");
test("Util.div([1, 2, 3, 4], [10, 20, 30])");
test("Util.div([1, 2, 3], [10, 20, 30, 40])");
test("Util.div([1, [2, 3]], [10, [20, 30, 40]])");
test("Util.div([1, [2, 3], 4], [10, 20, 30])");

test("Util.mod()");
test("Util.mod(7, 3)");
test("Util.mod([0, 3, 7], 2)");
test("Util.mod(2, [0, 3, 7])");
test("Util.mod([-2, 4, 3, 7], 5)");
test("Util.mod([-2, [4, [3, 7]]], 5)");

test("Util.normalize(5)");
test("Util.normalize([0, 3, 7])");
Expand All @@ -631,6 +651,11 @@ function testUtil(){
test("Util.sum([1, 2, 3, 4])");
test("Util.sum([10, 'foo', 11, 'bar', 22])");

test("Util.trunc()");
test("Util.trunc([3.14, 5.12, 6.18])");
test("Util.trunc([[3.14, 5.12], 6.18])");
test("Util.trunc([[3.14, [5.12]], 6.18])");

let drawing = [];
Rand.seed(628);
for (let i=0; i<10; i++){
Expand Down

0 comments on commit bca7a14

Please sign in to comment.