comparers.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*---------------------------------------------------------------------------------------------
  2. * Copyright (c) Microsoft Corporation. All rights reserved.
  3. * Licensed under the MIT License. See License.txt in the project root for license information.
  4. *--------------------------------------------------------------------------------------------*/
  5. import { IdleValue } from './async.js';
  6. // When comparing large numbers of strings it's better for performance to create an
  7. // Intl.Collator object and use the function provided by its compare property
  8. // than it is to use String.prototype.localeCompare()
  9. // A collator with numeric sorting enabled, and no sensitivity to case, accents or diacritics.
  10. const intlFileNameCollatorBaseNumeric = new IdleValue(() => {
  11. const collator = new Intl.Collator(undefined, { numeric: true, sensitivity: 'base' });
  12. return {
  13. collator: collator,
  14. collatorIsNumeric: collator.resolvedOptions().numeric
  15. };
  16. });
  17. /** Compares filenames without distinguishing the name from the extension. Disambiguates by unicode comparison. */
  18. export function compareFileNames(one, other, caseSensitive = false) {
  19. const a = one || '';
  20. const b = other || '';
  21. const result = intlFileNameCollatorBaseNumeric.value.collator.compare(a, b);
  22. // Using the numeric option will make compare(`foo1`, `foo01`) === 0. Disambiguate.
  23. if (intlFileNameCollatorBaseNumeric.value.collatorIsNumeric && result === 0 && a !== b) {
  24. return a < b ? -1 : 1;
  25. }
  26. return result;
  27. }
  28. export function compareAnything(one, other, lookFor) {
  29. const elementAName = one.toLowerCase();
  30. const elementBName = other.toLowerCase();
  31. // Sort prefix matches over non prefix matches
  32. const prefixCompare = compareByPrefix(one, other, lookFor);
  33. if (prefixCompare) {
  34. return prefixCompare;
  35. }
  36. // Sort suffix matches over non suffix matches
  37. const elementASuffixMatch = elementAName.endsWith(lookFor);
  38. const elementBSuffixMatch = elementBName.endsWith(lookFor);
  39. if (elementASuffixMatch !== elementBSuffixMatch) {
  40. return elementASuffixMatch ? -1 : 1;
  41. }
  42. // Understand file names
  43. const r = compareFileNames(elementAName, elementBName);
  44. if (r !== 0) {
  45. return r;
  46. }
  47. // Compare by name
  48. return elementAName.localeCompare(elementBName);
  49. }
  50. export function compareByPrefix(one, other, lookFor) {
  51. const elementAName = one.toLowerCase();
  52. const elementBName = other.toLowerCase();
  53. // Sort prefix matches over non prefix matches
  54. const elementAPrefixMatch = elementAName.startsWith(lookFor);
  55. const elementBPrefixMatch = elementBName.startsWith(lookFor);
  56. if (elementAPrefixMatch !== elementBPrefixMatch) {
  57. return elementAPrefixMatch ? -1 : 1;
  58. }
  59. // Same prefix: Sort shorter matches to the top to have those on top that match more precisely
  60. else if (elementAPrefixMatch && elementBPrefixMatch) {
  61. if (elementAName.length < elementBName.length) {
  62. return -1;
  63. }
  64. if (elementAName.length > elementBName.length) {
  65. return 1;
  66. }
  67. }
  68. return 0;
  69. }