Skip to content

Commit

Permalink
allow arrays into the arithmetic macros
Browse files Browse the repository at this point in the history
  • Loading branch information
Kelly Selden committed Jun 15, 2017
1 parent fb878ac commit dcc1f03
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 5 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ source1: 3,
source2: 2,
source3: 1,
value1: difference('source1', 'source2', 'source3'), // 0
value2: difference('source1', difference('source2', 'source3')) // 2
value2: difference('source1', collect('source2', 'source3')) // 2
```

##### `divide`
Expand Down Expand Up @@ -706,7 +706,7 @@ source1: 1,
source2: 2,
source3: 3,
value1: product('source1', 'source2', 'source3'), // 6
value2: product('source1', product('source2', 'source3')) // 6
value2: product('source1', collect('source2', 'source3')) // 6
```

##### `promise.all`
Expand Down Expand Up @@ -804,7 +804,7 @@ source1: 3,
source2: 2,
source3: 1,
value1: quotient('source1', 'source2', 'source3'), // 1.5
value2: quotient('source1', quotient('source2', 'source3')) // 1.5
value2: quotient('source1', collect('source2', 'source3')) // 1.5
```

##### `string.camelize`
Expand Down Expand Up @@ -989,7 +989,7 @@ source1: 1,
source2: 2,
source3: 3,
value1: sum('source1', 'source2', 'source3'), // 6
value2: sum('source1', sum('source2', 'source3')) // 6
value2: sum('source1', collect('source2', 'source3')) // 6
```

##### `tag`
Expand Down
17 changes: 16 additions & 1 deletion addon/-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,22 @@ import lazyCurriedComputed from 'ember-macro-helpers/lazy-curried-computed';

export function reduceKeys(func) {
return curriedComputed((...values) => {
return values.reduce(func);
if (values.length === 0) {
return 0;
}
return values.reduce((total, next, i) => {
if (Array.isArray(next)) {
if (next.length === 0) {
next = 0;
} else {
next = next.reduce(func);
}
}
if (i === 0) {
return next;
}
return func(total, next);
}, null);
});
}

Expand Down
17 changes: 17 additions & 0 deletions tests/integration/add-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { add } from 'ember-awesome-macros';
import { module, test } from 'qunit';
import { A as emberA } from 'ember-array/utils';
import compute from 'ember-macro-test-helpers/compute';

module('Integration | Macro | add');
Expand Down Expand Up @@ -29,6 +30,22 @@ test('adds three numbers', function(assert) {
});
});

test('allows empty arrays', function(assert) {
compute({
assert,
computed: add(emberA()),
strictEqual: 0
});
});

test('adds array members', function(assert) {
compute({
assert,
computed: add(emberA([1, 2]), emberA([3])),
strictEqual: 6
});
});

test('handles all undefined', function(assert) {
compute({
assert,
Expand Down
17 changes: 17 additions & 0 deletions tests/integration/divide-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { divide } from 'ember-awesome-macros';
import { module, test } from 'qunit';
import { A as emberA } from 'ember-array/utils';
import compute from 'ember-macro-test-helpers/compute';

module('Integration | Macro | divide');
Expand Down Expand Up @@ -29,6 +30,22 @@ test('divides three numbers', function(assert) {
});
});

test('allows empty arrays', function(assert) {
compute({
assert,
computed: divide(emberA()),
strictEqual: 0
});
});

test('divides array members', function(assert) {
compute({
assert,
computed: divide(emberA([3, 2]), emberA([1])),
strictEqual: 1.5
});
});

test('handles all undefined', function(assert) {
compute({
assert,
Expand Down
17 changes: 17 additions & 0 deletions tests/integration/multiply-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { multiply } from 'ember-awesome-macros';
import { module, test } from 'qunit';
import { A as emberA } from 'ember-array/utils';
import compute from 'ember-macro-test-helpers/compute';

module('Integration | Macro | multiply');
Expand Down Expand Up @@ -29,6 +30,22 @@ test('multiplies three numbers', function(assert) {
});
});

test('allows empty arrays', function(assert) {
compute({
assert,
computed: multiply(emberA()),
strictEqual: 0
});
});

test('multiplies array members', function(assert) {
compute({
assert,
computed: multiply(emberA([1, 2]), emberA([3])),
strictEqual: 6
});
});

test('handles all undefined', function(assert) {
compute({
assert,
Expand Down
17 changes: 17 additions & 0 deletions tests/integration/subtract-test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { subtract } from 'ember-awesome-macros';
import { module, test } from 'qunit';
import { A as emberA } from 'ember-array/utils';
import compute from 'ember-macro-test-helpers/compute';

module('Integration | Macro | subtract');
Expand Down Expand Up @@ -29,6 +30,22 @@ test('subtracts three numbers', function(assert) {
});
});

test('allows empty arrays', function(assert) {
compute({
assert,
computed: subtract(emberA()),
strictEqual: 0
});
});

test('subtracts array members', function(assert) {
compute({
assert,
computed: subtract(emberA([3, 2]), emberA([1])),
strictEqual: 0
});
});

test('handles all undefined', function(assert) {
compute({
assert,
Expand Down

0 comments on commit dcc1f03

Please sign in to comment.