extra-iterator 0.7.0 → 0.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -321,6 +321,12 @@ export declare class ExtraIterator<T> extends Iterator<T, any, any> {
321
321
  * // returns { _a: 2, _b: 4 }
322
322
  */
323
323
  collect<U>(collectfn: ((iter: Iterable<T>) => U)): U;
324
+ /**
325
+ * Sums the numeric value of all elements in the iterator and returns the total.
326
+ *
327
+ * @example ExtraIterator.from([5, 8, 13]).sum() // returns 26
328
+ */
329
+ sum(): number;
324
330
  /**
325
331
  * Consumes the iterator and returns the number of elements it contained.
326
332
  *
package/dist/index.js CHANGED
@@ -585,6 +585,14 @@ export class ExtraIterator extends Iterator {
585
585
  collect(collectfn) {
586
586
  return collectfn(this);
587
587
  }
588
+ /**
589
+ * Sums the numeric value of all elements in the iterator and returns the total.
590
+ *
591
+ * @example ExtraIterator.from([5, 8, 13]).sum() // returns 26
592
+ */
593
+ sum() {
594
+ return this.reduce((a, b) => a + Number(b), 0);
595
+ }
588
596
  /**
589
597
  * Consumes the iterator and returns the number of elements it contained.
590
598
  *
@@ -150,6 +150,20 @@ describe('ExtraIterator', () => {
150
150
  const sum = iterator.collect(iter => Array.from(iter).reduce((a, b) => a + b, 0));
151
151
  expect(sum).toBe(6);
152
152
  });
153
+ describe('sum', () => {
154
+ it('should sum numbers', () => {
155
+ const iter = ExtraIterator.from([5, 8, 13]);
156
+ expect(iter.sum()).toBe(26);
157
+ });
158
+ it('should sum the numeric value of objects', () => {
159
+ const iter = ExtraIterator.from([
160
+ { valueOf: () => 5 },
161
+ { valueOf: () => 8 },
162
+ { valueOf: () => 13 },
163
+ ]);
164
+ expect(iter.sum()).toBe(26);
165
+ });
166
+ });
153
167
  it('should create a chain of responsibility function', () => {
154
168
  const humanizeDuration = ExtraIterator.from([
155
169
  (next, miliseconds) => miliseconds < 1000 ? `${miliseconds} miliseconds` : next(miliseconds / 1000),
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "description": "An extension of the Iterator class with additional utility helper functions.",
4
4
  "author": "Leonardo Raele <leonardoraele@gmail.com>",
5
5
  "license": "MIT",
6
- "version": "0.7.0",
6
+ "version": "0.8.0",
7
7
  "type": "module",
8
8
  "exports": {
9
9
  ".": "./dist/index.js",