construct(function () {}, [], $)
);
+/**
+ * Calls the provided callback with the provided argument if the
+ * provided argument is not nullish; otherwise, returns the provided
+ * argument unmodified.
+ */
+export const maybe = ($, callback) => $ == null ? $ : callback($);
+
/**
* Returns whether the provided object inherits from the prototype of
* the provided function.
import {
assert,
assertEquals,
+ assertSpyCall,
+ assertSpyCalls,
assertStrictEquals,
assertThrows,
describe,
it,
+ spy,
} from "./dev-deps.js";
import {
bind,
identity,
isCallable,
isConstructor,
+ maybe,
ordinaryHasInstance,
} from "./function.js";
});
});
+describe("maybe", () => {
+ it("[[Call]] calls if not nullish", () => {
+ const wrapper = spy(() => "success");
+ assertStrictEquals(maybe(0, wrapper), "success");
+ assertSpyCalls(wrapper, 1);
+ assertSpyCall(wrapper, 0, {
+ args: [0],
+ self: undefined,
+ returned: "success",
+ });
+ });
+
+ it("[[Call]] does not call if nullish", () => {
+ const wrapper = spy(() => "success");
+ assertStrictEquals(maybe(null, wrapper), null);
+ assertStrictEquals(maybe(undefined, wrapper), undefined);
+ assertSpyCalls(wrapper, 0);
+ });
+
+ it("[[Construct]] throws an error", () => {
+ assertThrows(() => new maybe(true, ($) => $));
+ });
+
+ describe(".length", () => {
+ it("[[Get]] returns the correct length", () => {
+ assertStrictEquals(maybe.length, 2);
+ });
+ });
+
+ describe(".name", () => {
+ it("[[Get]] returns the correct name", () => {
+ assertStrictEquals(maybe.name, "maybe");
+ });
+ });
+});
+
describe("ordinaryHasInstance", () => {
it("[[Call]] walks the prototype chain", () => {
const constructor = class {