{"version":3,"file":"arrayHelpers.mjs","names":[],"sources":["../src/arrayHelpers.ts"],"sourcesContent":["/**\n * Copyright (c) 2025-present, NDLA.\n *\n * This source code is licensed under the GPLv3 license found in the\n * LICENSE file in the root directory of this source tree.\n *\n */\n\n/**\n * Groups an array of items by a key returned by the predicate function.\n * @param arr The array of items to group.\n * @param predicate A function converts the item to a key to group it by.\n * @returns An object where the keys are the result of the predicate function and the values are arrays of items with that key.\n * @example const grouped = groupBy([{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 1, name: 'Charlie' }], item => item.id)\n * // => { 1: [{ id: 1, name: 'Alice' }, { id: 1, name: 'Charlie' }], 2: [{ id: 2, name: 'Bob' }] }\n */\nexport const groupBy = <T, K extends string | number>(\n  arr: readonly T[] | undefined | null,\n  predicate: (item: T) => K,\n): Record<K, T[]> => {\n  const result = {} as Record<K, T[]>;\n  if (!arr || !Array.isArray(arr)) return result;\n\n  for (const item of arr) {\n    const key = predicate(item);\n    if (!Object.hasOwn(result, key)) {\n      result[key] = [];\n    }\n    result[key].push(item);\n  }\n\n  return result;\n};\n/**\n * Remove duplicate items from an array.\n * @param arr The array to remove duplicates from.\n * @returns A new array with duplicate items removed.\n * @example const unique = uniq([1, 2, 3, 1, 2, 4])\n * // => [1, 2, 3, 4]\n */\nexport const uniq = <T>(arr: readonly T[] | undefined | null): T[] =>\n  Array.isArray(arr) ? Array.from(new Set(arr)) : [];\n\n/**\n * Removes duplicate items from an array based the result of a predicate function. The predicate function is called with each item in the array and should return a key to compare items by.\n * The first item with a given key is kept, and all subsequent items with the same key are removed.\n * @param arr The array to remove duplicates from.\n * @param predicate A function that converts an item to a key to compare items by.\n * @returns A new array with duplicate items removed.\n * @example const unique = uniqBy([{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }, { id: 1, name: 'Charlie' }], item => item.id)\n * // => [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]\n */\nexport const uniqBy = <T, K = any>(arr: readonly T[] | undefined | null, predicate: (item: T) => K): T[] => {\n  if (!arr || !Array.isArray(arr) || !arr.length) return [];\n  const result = new Map<K, T>();\n\n  for (const item of arr) {\n    const key = predicate(item) as K;\n    if (!result.has(key)) {\n      result.set(key as K, item);\n    }\n  }\n\n  return Array.from(result.values());\n};\n\n/**\n * Separates an array into two arrays based on the result of a predicate function. The first array contains items where the predicate function returns true, and the second array contains items where the predicate function returns false.\n * @param arr The array to partition.\n * @param predicate A function that returns true or false for each item.\n * @returns A tuple containing two arrays: the first array contains items where the predicate function returns true, and the second array contains items where the predicate function returns false.\n * @example const [even, odd] = partition([1, 2, 3, 4, 5], n => n % 2 === 0)\n * // => [ [2, 4], [1, 3, 5] ]\n */\nexport const partition = <T>(arr: readonly T[] | undefined | null, predicate: (item: T) => boolean): [T[], T[]] => {\n  const truthy: T[] = [];\n  const falsy: T[] = [];\n\n  if (Array.isArray(arr)) {\n    for (const item of arr) {\n      if (predicate(item)) {\n        truthy.push(item);\n      } else {\n        falsy.push(item);\n      }\n    }\n  }\n\n  return [truthy, falsy];\n};\n\n/**\n * Converts an array of items into an object where the keys are the result of a predicate function and the values are the items.\n * The last item with a given key is kept, and all previous items with the same key are overwritten.\n * @param arr The array of items to convert.\n * @param predicate A function that converts an item to a key.\n * @returns An object where the keys are the result of the predicate function and the values are the items.\n * @example const keyed = keyBy([{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }], item => item.id)\n * // => { 1: { id: 1, name: 'Alice' }, 2: { id: 2, name: 'Bob' } }\n */\nexport const keyBy = <T, K extends string | number = string>(\n  arr: readonly T[] | undefined | null,\n  predicate: (item: T) => K,\n): Record<K, T> => {\n  const result = {} as Record<K, T>;\n  if (!arr || !Array.isArray(arr)) return result;\n\n  for (const item of arr) {\n    const key = predicate(item);\n    result[key] = item;\n  }\n  return result;\n};\n\n/**\n * Sorts an array of items based on the result of a predicate function. This is a stable sort, so items with the same value will retain their original order.\n * Beware; Returning mixed values from the predicate function can yield unexpected results. For best results, return strings, booleans, numbers or null/undefined.\n * @param arr The array of items to sort.\n * @param predicate A function that converts an item to a value to sort by.\n * @returns A new array with the items sorted by the result of the predicate function.\n * @example const sorted = sortBy([{ id: 2, name: 'Bob' }, { id: 1, name: 'Alice' }], item => item.id)\n * // => [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]\n */\nexport const sortBy = <T, K = any>(arr: readonly T[] | undefined | null, predicate: (value: T) => K): T[] => {\n  if (!arr || !Array.isArray(arr)) return [];\n\n  return [...arr].sort((a, b) => {\n    const valA = predicate(a);\n    const valB = predicate(b);\n\n    // Handle NaN values like Lodash\n    if (Number.isNaN(valA) && Number.isNaN(valB)) return 0;\n    if (Number.isNaN(valA)) return 1;\n    if (Number.isNaN(valB)) return -1;\n\n    // Handle null and undefined values like Lodash\n    if (valA == null && valB == null) return 0;\n    if (valA == null) return 1;\n    if (valB == null) return -1;\n\n    return valA < valB ? -1 : valA > valB ? 1 : 0;\n  });\n};\n"],"mappings":";;;;;;;;;;;;;;;;AAgBA,MAAa,WACX,KACA,cACmB;CACnB,MAAM,SAAS,EAAE;AACjB,KAAI,CAAC,OAAO,CAAC,MAAM,QAAQ,IAAI,CAAE,QAAO;AAExC,MAAK,MAAM,QAAQ,KAAK;EACtB,MAAM,MAAM,UAAU,KAAK;AAC3B,MAAI,CAAC,OAAO,OAAO,QAAQ,IAAI,CAC7B,QAAO,OAAO,EAAE;AAElB,SAAO,KAAK,KAAK,KAAK;;AAGxB,QAAO;;;;;;;;;AAST,MAAa,QAAW,QACtB,MAAM,QAAQ,IAAI,GAAG,MAAM,KAAK,IAAI,IAAI,IAAI,CAAC,GAAG,EAAE;;;;;;;;;;AAWpD,MAAa,UAAsB,KAAsC,cAAmC;AAC1G,KAAI,CAAC,OAAO,CAAC,MAAM,QAAQ,IAAI,IAAI,CAAC,IAAI,OAAQ,QAAO,EAAE;CACzD,MAAM,yBAAS,IAAI,KAAW;AAE9B,MAAK,MAAM,QAAQ,KAAK;EACtB,MAAM,MAAM,UAAU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