Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BREAKING] Rename forEach to forSome, add forEach matching spec #1637

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 16 additions & 3 deletions __tests__/List.ts
Expand Up @@ -660,17 +660,30 @@ describe('List', () => {
expect(a).toEqual([0, 1, 2, 3, 4]);
});

it('forEach iteration terminates when callback returns false', () => {
it('forSome iteration terminates when callback returns false', () => {
const a: Array<any> = [];
function count(x) {
if (x > 2) {
a.push(x);
if (x >= 2) {
return false;
}
}
const v = List.of(0, 1, 2, 3, 4);
v.forSome(count);
expect(a).toEqual([0, 1, 2]);
});

it('forEach iteration does not terminate when callback returns false', () => {
const a: Array<any> = [];
function count(x) {
a.push(x);
if (x > 2) {
return false;
}
}
const v = List.of(0, 1, 2, 3, 4);
v.forEach(count);
expect(a).toEqual([0, 1, 2]);
expect(a).toEqual([0, 1, 2, 3, 4]);
});

it('concat works like Array.prototype.concat', () => {
Expand Down
2 changes: 1 addition & 1 deletion __tests__/flatten.ts
Expand Up @@ -28,7 +28,7 @@ describe('flatten', () => {
it('gives the correct iteration count', () => {
const nested = fromJS([[1, 2, 3], [4, 5, 6]]);
const flat = nested.flatten();
expect(flat.forEach(x => x < 4)).toEqual(4);
expect(flat.forSome(x => x < 4)).toEqual(4);
});

type SeqType = number | Array<number> | Collection<number, number>;
Expand Down
7 changes: 7 additions & 0 deletions src/CollectionImpl.js
Expand Up @@ -222,6 +222,13 @@ mixin(Collection, {
},

forEach(sideEffect, context) {
assertNotInfinite(this.size);
this.__iterate((...args) => {
sideEffect.apply(context, args);
});
},

forSome(sideEffect, context) {
assertNotInfinite(this.size);
return this.__iterate(context ? sideEffect.bind(context) : sideEffect);
},
Expand Down
12 changes: 10 additions & 2 deletions type-definitions/Immutable.d.ts
Expand Up @@ -4294,14 +4294,22 @@ declare module Immutable {

// Side effects

/**
* The `sideEffect` is executed for every entry in the Collection.
*/
forEach(
sideEffect: (value: V, key: K, iter: this) => any,
context?: any
): this;

/**
* The `sideEffect` is executed for every entry in the Collection.
*
* Unlike `Array#forEach`, if any call of `sideEffect` returns
* Unlike `#forEach`, if any call of `sideEffect` returns
* `false`, the iteration will stop. Returns the number of entries iterated
* (including the last iteration which returned false).
*/
forEach(
forSome(
sideEffect: (value: V, key: K, iter: this) => any,
context?: any
): number;
Expand Down
5 changes: 5 additions & 0 deletions type-definitions/immutable.js.flow
Expand Up @@ -116,6 +116,11 @@ declare class _Collection<K, +V> implements ValueObject {
forEach(
sideEffect: (value: V, key: K, iter: this) => any,
context?: mixed
): this;

forSome(
sideEffect: (value: V, key: K, iter: this) => any,
context?: mixed
): number;

slice(begin?: number, end?: number): this;
Expand Down