NullUtil: add `filter` (#1324)

This adds a new method called `filter` to the `NullUtil` module.
`filter` enables you to filter all the null-like values out of an array
in a convenient typesafe way. (It's really just a wrapper around
`Array.filter((x) => x != null)` with a type signature.)

Test plan: Unit tests added (for both functionality and type safety).
This commit is contained in:
Dandelion Mané 2019-08-26 13:20:40 +02:00 committed by GitHub
parent ebdd2a05c5
commit 126332096f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 0 deletions

View File

@ -79,3 +79,15 @@ export function orThrow<T>(x: ?T, getErrorMessage: () => string): T {
export function orElse<T>(x: ?T, defaultValue: T): T { export function orElse<T>(x: ?T, defaultValue: T): T {
return x != null ? x : defaultValue; return x != null ? x : defaultValue;
} }
/**
* Filter nulls and undefined out of an array, returning a new array.
*
* The functionality is easy to implement without a util method (just call
* `filter`); however Flow doesn't infer the type of the output array based on
* the callback that was passed to filter. This method basically wraps filter
* in a type-aware way.
*/
export function filter<T>(xs: $ReadOnlyArray<?T>): T[] {
return (xs.filter((x) => x != null): any);
}

View File

@ -132,4 +132,26 @@ describe("util/null", () => {
expect(NullUtil.orElse("", "not me")).toEqual(""); expect(NullUtil.orElse("", "not me")).toEqual("");
}); });
}); });
describe("filter", () => {
it("filters out undefined and null but not other falsey values", () => {
const x = [0, undefined, NaN, null, false, ""];
const f = NullUtil.filter(x);
expect(f).toEqual([0, NaN, false, ""]);
});
it("typechecks as expected", () => {
const rs: $ReadOnlyArray<?string> = ["foo", undefined];
const _: string[] = NullUtil.filter(rs);
});
it("returns a copy of the original array", () => {
const as = [1, 2, 3];
const bs = NullUtil.filter(as);
expect(as).not.toBe(bs);
});
it("doesn't allow bad coercions", () => {
const as = [1, "foo", 2];
// $ExpectFlowError
const _: number[] = NullUtil.filter(as);
});
});
}); });