addons/isl/src/ComparisonView/utils.tsxblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import {parsePatch} from 'shared/patch/parse';
b69ab319import type {DiffType, ParsedDiff} from 'shared/patch/types';
b69ab3110
b69ab3111export function parsePatchAndFilter(patch: string): ReturnType<typeof parsePatch> {
b69ab3112 const result = parsePatch(patch);
b69ab3113 return result.filter(
b69ab3114 // empty patches and other weird situations can cause invalid files to get parsed, ignore these entirely
b69ab3115 diff => diff.hunks.length > 0 || diff.newFileName != null || diff.oldFileName != null,
b69ab3116 );
b69ab3117}
b69ab3118
b69ab3119/** Similar to how uncommitted changes are sorted, sort first by type, then by filename. */
b69ab3120export function sortFilesByType(files: Array<ParsedDiff>) {
b69ab3121 files.sort((a, b) => {
b69ab3122 if (a.type === b.type) {
b69ab3123 const pathA = a.newFileName ?? a.oldFileName ?? '';
b69ab3124 const pathB = b.newFileName ?? b.oldFileName ?? '';
b69ab3125 return pathA.localeCompare(pathB);
b69ab3126 } else {
b69ab3127 return (
b69ab3128 (a.type == null ? SORT_LAST : sortKeyForType[a.type]) -
b69ab3129 (b.type == null ? SORT_LAST : sortKeyForType[b.type])
b69ab3130 );
b69ab3131 }
b69ab3132 });
b69ab3133}
b69ab3134const SORT_LAST = 10;
b69ab3135const sortKeyForType: Record<DiffType, number> = {
b69ab3136 Modified: 0,
b69ab3137 Renamed: 1,
b69ab3138 Added: 2,
b69ab3139 Copied: 3,
b69ab3140 Removed: 4,
b69ab3141};