Skip to content

Commit

Permalink
feat(rxjs-core): mapAndCacheArrayElements() handles null and undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
ersimont committed Nov 23, 2020
1 parent 7829d6b commit 7d19f47
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
Expand Up @@ -111,6 +111,14 @@ describe('mapAndCacheArrayElements()', () => {
}
});

it('treats null and undefined like the empty array', async () => {
await expectPipeResult<number[] | null | undefined, number[]>(
[null, [1, 2], undefined],
mapAndCacheArrayElements(identity, (item) => item * 3),
[[], [3, 6], []],
);
});

it(
'handles `buildCacheKey` throwing an error',
testUserFunctionError(
Expand Down
Expand Up @@ -28,4 +28,4 @@ export const mapAndCacheArrayElements = mapAndCacheElements as <
>(
buildCacheKey: ArrayIteratee<UpstreamType, any>,
buildDownstreamItem: ArrayIteratee<UpstreamType, DownstreamType>,
) => OperatorFunction<UpstreamType[], DownstreamType[]>;
) => OperatorFunction<UpstreamType[] | null | undefined, DownstreamType[]>;
@@ -0,0 +1,49 @@
import { identity } from '@s-libs/micro-dash';
import { Subject } from 'rxjs';
import { mapAndCacheArrayElements } from '../lib/operators';

declare const array: Subject<number[]>;
declare const arrayOrNull: Subject<number[] | null>;
declare const arrayOrUndefined: Subject<number[] | undefined>;
declare const arrayOrNil: Subject<number[] | null | undefined>;

// $ExpectType Observable<Date[]>
array.pipe(
mapAndCacheArrayElements(
identity,
(
// $ExpectType number
val,
) => new Date(val),
),
);
// $ExpectType Observable<Date[]>
arrayOrNull.pipe(
mapAndCacheArrayElements(
identity,
(
// $ExpectType number
val,
) => new Date(val),
),
);
// $ExpectType Observable<Date[]>
arrayOrUndefined.pipe(
mapAndCacheArrayElements(
identity,
(
// $ExpectType number
val,
) => new Date(val),
),
);
// $ExpectType Observable<Date[]>
arrayOrNil.pipe(
mapAndCacheArrayElements(
identity,
(
// $ExpectType number
val,
) => new Date(val),
),
);

0 comments on commit 7d19f47

Please sign in to comment.