All files / src/utils array.ts

100% Statements 9/9
100% Branches 3/3
100% Functions 3/3
100% Lines 5/5

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23          2x                     1x 2x 1x 2x      
/**
 * シーケンスから一意の要素を抽出し、配列で返却します。
 * @param source 対象のシーケンス
 * @returns 一意の要素を格納する配列
 */
export const distinct = <T>(source: T[]) => Array.from(new Set(source));
 
/**
 * 指定したキー セレクター関数に従って、ジェネリック シーケンス内の最大値を返します。
 *
 * C# の `System.Linq.Enumerable.MaxBy` メソッドに相当する処理です。
 * see: https://learn.microsoft.com/ja-jp/dotnet/api/system.linq.enumerable.maxby
 * @param source 最大値を確認する対象となる値のシーケンス
 * @param keySelector 各要素のキーを抽出する関数
 * @returns シーケンス内の最大キーを持つ値
 */
export const maxBy = <T>(source: T[], keySelector: (value: T) => number): T | undefined => {
  if (source.length === 0) return undefined;
  return source.reduce((previousValue, currentValue) =>
    keySelector(currentValue) > keySelector(previousValue) ? currentValue : previousValue
  );
};