// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.
import { AssertionError } from "./assertion_error.ts";

/**
 * Make an assertion that actual is not null or undefined.
 * If not then throw.
 *
 * @example
 * ```ts
 * import { assertExists } from "@std/assert/assert-exists";
 *
 * assertExists("something"); // Doesn't throw
 * assertExists(undefined); // Throws
 * ```
 */
export function assertExists<T>(
  actual: T,
  msg?: string,
): asserts actual is NonNullable<T> {
  if (actual === undefined || actual === null) {
    const msgSuffix = msg ? `: ${msg}` : ".";
    msg =
      `Expected actual: "${actual}" to not be null or undefined${msgSuffix}`;
    throw new AssertionError(msg);
  }
}

// denoCacheMetadata={"headers":{"date":"Tue, 28 Jul 2026 12:31:15 GMT","content-length":"762","age":"4009515","server":"cloudflare","access-control-expose-headers":"*","cross-origin-resource-policy":"cross-origin","content-security-policy":"default-src 'none'; script-src 'none'; style-src 'none'; img-src 'none'; font-src 'none'; connect-src 'none'; frame-src 'none'; object-src 'none'; frame-ancestors 'none'; sandbox; form-action 'none';","x-robots-tag":"noindex","set-cookie":"__cf_bm=S_h3xID.kOiGgUM9FkSbF151qqtX6nZKIB5N1w9NNUY-1785241875.3057833-1.0.1.1-AyHyBOxfIGLaso4YmbI3uFtSdtW3xpQgw.uptkTWtPVz..PclmTIIzQtYrTnL7WzLQkE4khPCiuM3SLwrB4xzgl.eUsddFDncrCS_KkG5pQ5oftoegOyYQ6cBbX5xq4S; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","x-content-type-options":"nosniff","alt-svc":"h3=\":443\"; ma=86400","etag":"\"c7c3c03a5514b04cc2ecff6ae90e6774\"","vary":"Origin","x-jsr-backend":"modules","access-control-allow-origin":"*","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=uNfUb5cn967xlvk0%2F1Fj9mqXGL3sCd1TY5%2BNPd7o3PJnSjZ6xep5ea9KW6cxIhWJenaqQt1ZrGJvgZKMI46OrYKpe4dHHeT6U4iDfEEValKIcH8PVLrWt0A%3D\"}]}","accept-ranges":"bytes","cache-control":"public, max-age=31536000, immutable","content-type":"text/typescript","cf-ray":"a223e658ad1a7db6-LAX","cf-cache-status":"HIT"},"url":"https://jsr.io/@std/assert/0.224.0/assert_exists.ts","time":1785241875}