xref: /expo/packages/@expo/cli/src/utils/array.ts (revision 8a424beb)
18d307f52SEvan Bacon/** Returns the last index of an item based on a given criteria. */
28d307f52SEvan Baconexport function findLastIndex<T>(array: T[], predicate: (item: T) => boolean) {
38d307f52SEvan Bacon  for (let i = array.length - 1; i >= 0; i--) {
48d307f52SEvan Bacon    if (predicate(array[i])) {
58d307f52SEvan Bacon      return i;
68d307f52SEvan Bacon    }
78d307f52SEvan Bacon  }
88d307f52SEvan Bacon  return -1;
98d307f52SEvan Bacon}
108d307f52SEvan Bacon
118d307f52SEvan Bacon/** Returns a list of items that intersect between two given arrays. */
128d307f52SEvan Baconexport function intersecting<T>(a: T[], b: T[]): T[] {
138d307f52SEvan Bacon  const [c, d] = a.length > b.length ? [a, b] : [b, a];
148d307f52SEvan Bacon  return c.filter((value) => d.includes(value));
158d307f52SEvan Bacon}
16dc51e206SEvan Bacon
173d6e487dSEvan Baconexport function replaceValue<T>(values: T[], original: T, replacement: T): T[] {
183d6e487dSEvan Bacon  const index = values.indexOf(original);
193d6e487dSEvan Bacon  if (index > -1) {
203d6e487dSEvan Bacon    values[index] = replacement;
213d6e487dSEvan Bacon  }
223d6e487dSEvan Bacon  return values;
233d6e487dSEvan Bacon}
243d6e487dSEvan Bacon
25dc51e206SEvan Bacon/** lodash.uniqBy */
26dc51e206SEvan Baconexport function uniqBy<T>(array: T[], key: (item: T) => string): T[] {
27dc51e206SEvan Bacon  const seen: { [key: string]: boolean } = {};
28dc51e206SEvan Bacon  return array.filter((item) => {
29dc51e206SEvan Bacon    const k = key(item);
30dc51e206SEvan Bacon    if (seen[k]) {
31dc51e206SEvan Bacon      return false;
32dc51e206SEvan Bacon    }
33dc51e206SEvan Bacon    seen[k] = true;
34dc51e206SEvan Bacon    return true;
35dc51e206SEvan Bacon  });
36dc51e206SEvan Bacon}
37dc51e206SEvan Bacon
38dc51e206SEvan Bacon/** `lodash.chunk` */
39dc51e206SEvan Baconexport function chunk<T>(array: T[], size: number): T[][] {
40dc51e206SEvan Bacon  const chunked = [];
41dc51e206SEvan Bacon  let index = 0;
42dc51e206SEvan Bacon  while (index < array.length) {
43dc51e206SEvan Bacon    chunked.push(array.slice(index, (index += size)));
44dc51e206SEvan Bacon  }
45dc51e206SEvan Bacon  return chunked;
46dc51e206SEvan Bacon}
478d3f3824SCedric van Putten
488d3f3824SCedric van Putten/** `lodash.groupBy` */
498d3f3824SCedric van Puttenexport function groupBy<T, K extends keyof any>(list: T[], getKey: (item: T) => K): Record<K, T[]> {
50*8a424bebSJames Ide  return list.reduce(
51*8a424bebSJames Ide    (previous, currentItem) => {
528d3f3824SCedric van Putten      const group = getKey(currentItem);
538d3f3824SCedric van Putten      if (!previous[group]) {
548d3f3824SCedric van Putten        previous[group] = [];
558d3f3824SCedric van Putten      }
568d3f3824SCedric van Putten      previous[group].push(currentItem);
578d3f3824SCedric van Putten      return previous;
58*8a424bebSJames Ide    },
59*8a424bebSJames Ide    {} as Record<K, T[]>
60*8a424bebSJames Ide  );
618d3f3824SCedric van Putten}
62