// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
// This module is browser compatible.

/**
 * Returns a new array, containing all elements in the given array transformed
 * using the given transformer, except the ones that were transformed to `null`
 * or `undefined`.
 *
 * @template T The type of the elements in the input array.
 * @template O The type of the elements in the output array.
 *
 * @param array The array to map elements from.
 * @param transformer The function to transform each element.
 *
 * @returns A new array with all elements transformed by the given transformer,
 * except the ones that were transformed to `null` or `undefined`.
 *
 * @example Basic usage
 * ```ts
 * import { mapNotNullish } from "@std/collections/map-not-nullish";
 * import { assertEquals } from "@std/assert/assert-equals";
 *
 * const people = [
 *   { middleName: null },
 *   { middleName: "William" },
 *   { middleName: undefined },
 *   { middleName: "Martha" },
 * ];
 * const foundMiddleNames = mapNotNullish(people, (people) => people.middleName);
 *
 * assertEquals(foundMiddleNames, ["William", "Martha"]);
 * ```
 */
export function mapNotNullish<T, O>(
  array: Iterable<T>,
  transformer: (el: T) => O,
): NonNullable<O>[] {
  const result: NonNullable<O>[] = [];

  for (const element of array) {
    const transformedElement = transformer(element);

    if (transformedElement !== undefined && transformedElement !== null) {
      result.push(transformedElement as NonNullable<O>);
    }
  }

  return result;
}

// denoCacheMetadata={"headers":{"alt-svc":"h3=\":443\"; ma=86400","date":"Tue, 28 Jul 2026 12:31:15 GMT","vary":"Origin","cross-origin-resource-policy":"cross-origin","etag":"\"4398f3fef9771a2ba19827253eeb7a98\"","x-jsr-backend":"modules","report-to":"{\"group\":\"cf-nel\",\"max_age\":604800,\"endpoints\":[{\"url\":\"https://a.nel.cloudflare.com/report/v4?s=R8l%2Fkxg6%2BOyaKPfuORjbawjBUIzPm8Dkm333VL62x3AOAQkbL%2FaEbehI3U4pp1iD2J%2F4ABfBGp%2FfCY4IkAvKlqXlItTcKr%2ByEay9gu87MLOPjE72XV2dAO4%3D\"}]}","set-cookie":"__cf_bm=9JUofVYFHR_qy2RdQfNuRFI77AilkF_TH0YQLoNQgEg-1785241874.959268-1.0.1.1-wV5WhRvEA8smP_8pAgqVIwolRezLppcCvGObVcZtvyPItbsEeFFyTAs8WxKrEmGVjAlSfkXL8MlGuQklirWWVWtkPQRgLKmjwWOKwkrdXP6.mokCFsbGh3Ql4ksvFnjR; HttpOnly; SameSite=None; Secure; Path=/; Domain=jsr.io; Expires=Tue, 28 Jul 2026 13:01:15 GMT","cache-control":"public, max-age=31536000, immutable","x-robots-tag":"noindex","access-control-allow-origin":"*","nel":"{\"report_to\":\"cf-nel\",\"success_fraction\":0.0,\"max_age\":604800}","cf-ray":"a223e6564f157db6-LAX","content-type":"text/typescript","content-length":"1553","access-control-expose-headers":"*","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-content-type-options":"nosniff","server":"cloudflare"},"url":"https://jsr.io/@std/collections/0.224.2/map_not_nullish.ts","time":1785241875}