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

feat: removed deprecated call pattern in find & findIndex #7007

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 0 additions & 15 deletions spec-dtslint/operators/find-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,3 @@ it('should support Boolean properly', () => {
// Intentionally weird looking: Because `Observable<boolean>` is `Observable<true | false>` and `true` is the truthy bit.
const o4 = of(false, false, false, false).pipe(find(Boolean)); // $ExpectType Observable<true>
});

it('should support this', () => {
const thisArg = { wanted: 5 };
const a = of(1, 2, 3).pipe(find(function (val) {
const wanted = this.wanted; // $ExpectType number
return val < wanted;
}, thisArg));
});

it('should deprecate thisArg usage', () => {
const a = of(1, 2, 3).pipe(find(Boolean)); // $ExpectNoDeprecation
const b = of(1, 2, 3).pipe(find(Boolean, {})); // $ExpectDeprecation
const c = of(1, 2, 3).pipe(find((value) => Boolean(value))); // $ExpectNoDeprecation
const d = of(1, 2, 3).pipe(find((value) => Boolean(value), {})); // $ExpectDeprecation
});
19 changes: 0 additions & 19 deletions spec-dtslint/operators/findIndex-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ it('should support a predicate that takes a source ', () => {
const o = of('foo', 'bar', 'baz').pipe(findIndex((p, index, source) => p === 'foo')); // $ExpectType Observable<number>
});

it('should support an argument ', () => {
const o = of('foo', 'bar', 'baz').pipe(findIndex(p => p === 'foo', 123)); // $ExpectType Observable<number>
});

it('should enforce types', () => {
const o = of('foo', 'bar', 'baz').pipe(findIndex()); // $ExpectError
});
Expand All @@ -42,18 +38,3 @@ it('should support inference from a predicate that returns any', () => {
}
const a = of(1).pipe(findIndex(isTruthy)); // $ExpectType Observable<number>
});

it('should support this', () => {
const thisArg = { wanted: 5 };
const a = of(1, 2, 3).pipe(findIndex(function (val) {
const wanted = this.wanted; // $ExpectType number
return val < wanted;
}, thisArg));
});

it('should deprecate thisArg usage', () => {
const a = of(1, 2, 3).pipe(findIndex(Boolean)); // $ExpectNoDeprecation
const b = of(1, 2, 3).pipe(findIndex(Boolean, {})); // $ExpectDeprecation
const c = of(1, 2, 3).pipe(findIndex((value) => Boolean(value))); // $ExpectNoDeprecation
const d = of(1, 2, 3).pipe(findIndex((value) => Boolean(value), {})); // $ExpectDeprecation
});
18 changes: 0 additions & 18 deletions spec/operators/find-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,24 +86,6 @@ describe('find', () => {
});
});

it('should work with a custom thisArg', () => {
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot(' --a--b---c-|');
const e1subs = ' ^----! ';
const expected = '-----(b|) ';

const finder = {
target: 'b',
};
const predicate = function (this: typeof finder, value: string) {
return value === this.target;
};

expectObservable(e1.pipe(find(predicate, finder))).toBe(expected);
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});

it('should return undefined if element does not match with predicate', () => {
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot(' --a--b--c--| ');
Expand Down
17 changes: 0 additions & 17 deletions spec/operators/findIndex-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,6 @@ describe('findIndex', () => {
});
});

it('should work with a custom thisArg', () => {
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const sourceValues = { b: 7 };
const e1 = hot(' --a--b---c-|', sourceValues);
const e1subs = ' ^----! ';
const expected = '-----(x|) ';

const predicate = function (this: typeof sourceValues, value: number) {
return value === this.b;
};
const result = e1.pipe(findIndex(predicate, sourceValues));

expectObservable(result).toBe(expected, { x: 1 });
expectSubscriptions(e1.subscriptions).toBe(e1subs);
});
});

it('should return negative index if element does not match with predicate', () => {
testScheduler.run(({ hot, expectObservable, expectSubscriptions }) => {
const e1 = hot(' --a--b--c--| ');
Expand Down
27 changes: 4 additions & 23 deletions src/internal/operators/find.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,9 @@ import { operate } from '../util/lift';
import { createOperatorSubscriber } from './OperatorSubscriber';

export function find<T>(predicate: BooleanConstructor): OperatorFunction<T, TruthyTypesOf<T>>;
/** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */
export function find<T, S extends T, A>(
predicate: (this: A, value: T, index: number, source: Observable<T>) => value is S,
thisArg: A
): OperatorFunction<T, S | undefined>;
export function find<T, S extends T>(
predicate: (value: T, index: number, source: Observable<T>) => value is S
): OperatorFunction<T, S | undefined>;
/** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */
export function find<T, A>(
predicate: (this: A, value: T, index: number, source: Observable<T>) => boolean,
thisArg: A
): OperatorFunction<T, T | undefined>;
export function find<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean): OperatorFunction<T, T | undefined>;
/**
* Emits only the first value emitted by the source Observable that meets some
Expand Down Expand Up @@ -57,23 +47,14 @@ export function find<T>(predicate: (value: T, index: number, source: Observable<
*
* @param {function(value: T, index: number, source: Observable<T>): boolean} predicate
* A function called with each item to test for condition matching.
* @param {any} [thisArg] An optional argument to determine the value of `this`
* in the `predicate` function.
* @return A function that returns an Observable that emits the first item that
* matches the condition.
*/
export function find<T>(
predicate: (value: T, index: number, source: Observable<T>) => boolean,
thisArg?: any
): OperatorFunction<T, T | undefined> {
return operate(createFind(predicate, thisArg, 'value'));
export function find<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean): OperatorFunction<T, T | undefined> {
return operate(createFind(predicate, 'value'));
}

export function createFind<T>(
predicate: (value: T, index: number, source: Observable<T>) => boolean,
thisArg: any,
emit: 'value' | 'index'
) {
export function createFind<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean, emit: 'value' | 'index') {
const findIndex = emit === 'index';
return (source: Observable<T>, subscriber: Subscriber<any>) => {
let index = 0;
Expand All @@ -82,7 +63,7 @@ export function createFind<T>(
subscriber,
(value) => {
const i = index++;
if (predicate.call(thisArg, value, i, source)) {
if (predicate(value, i, source)) {
subscriber.next(findIndex ? i : value);
subscriber.complete();
}
Expand Down
16 changes: 2 additions & 14 deletions src/internal/operators/findIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,6 @@ import { operate } from '../util/lift';
import { createFind } from './find';

export function findIndex<T>(predicate: BooleanConstructor): OperatorFunction<T, T extends Falsy ? -1 : number>;
/** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */
export function findIndex<T>(predicate: BooleanConstructor, thisArg: any): OperatorFunction<T, T extends Falsy ? -1 : number>;
/** @deprecated Use a closure instead of a `thisArg`. Signatures accepting a `thisArg` will be removed in v8. */
export function findIndex<T, A>(
predicate: (this: A, value: T, index: number, source: Observable<T>) => boolean,
thisArg: A
): OperatorFunction<T, number>;
export function findIndex<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean): OperatorFunction<T, number>;

/**
Expand Down Expand Up @@ -51,14 +44,9 @@ export function findIndex<T>(predicate: (value: T, index: number, source: Observ
*
* @param {function(value: T, index: number, source: Observable<T>): boolean} predicate
* A function called with each item to test for condition matching.
* @param {any} [thisArg] An optional argument to determine the value of `this`
* in the `predicate` function.
* @return A function that returns an Observable that emits the index of the
* first item that matches the condition.
*/
export function findIndex<T>(
predicate: (value: T, index: number, source: Observable<T>) => boolean,
thisArg?: any
): OperatorFunction<T, number> {
return operate(createFind(predicate, thisArg, 'index'));
export function findIndex<T>(predicate: (value: T, index: number, source: Observable<T>) => boolean): OperatorFunction<T, number> {
return operate(createFind(predicate, 'index'));
}