Skip to content

Commit

Permalink
Merge pull request #87 from 1nVitr0/feature/collator-ignore-chars
Browse files Browse the repository at this point in the history
Feature/collator-ignore-chars
  • Loading branch information
1nVitr0 authored Mar 30, 2024
2 parents 479073c + 4b37c0d commit 1333902
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ some:
- In addition to the default JavaScript Collator options, the following properties can be supplied
- `locales`: A BCP 47 language tag, or an comma separated array of such strings.
- `customSortOrder`: Custom Sort order in the form of a list of characters
- `customIgnoreCharacters`: A list of characters that are ignored when sorting, e.g. `"'\"()[]{}<>"`
- `sortConsecutiveBlockHeaders`: sorts consecutive block headers, such as a list of `case` statements.
- Default: `true`
- *Language Overridable*
Expand Down
1 change: 1 addition & 0 deletions src/providers/ConfigurationProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export interface NaturalSortOptions {
export interface BlockSortCollatorOptions extends Omit<Intl.CollatorOptions, "usage"> {
locales?: string;
customSortOrder?: string;
customIgnoreCharacters?: string;
}

export interface BlockSortConfiguration {
Expand Down
23 changes: 20 additions & 3 deletions src/providers/StringSortProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BlockSortCollatorOptions } from "./ConfigurationProvider";

export class StringSortProvider extends Intl.Collator {
private customSortOrder?: string;
private customIgnoreCharacters?: RegExp;
private ignoreCase?: boolean;
private direction?: "asc" | "desc";

Expand Down Expand Up @@ -29,22 +30,38 @@ export class StringSortProvider extends Intl.Collator {
this.customSortOrder = customSortOrder;
}

this.customIgnoreCharacters = options?.customIgnoreCharacters
? new RegExp(
`[${options.customIgnoreCharacters.replace(/\\/, "\\\\").replace(/]/g, "\\]").replace(/-/g, "\\-")}]`,
"g"
)
: undefined;
this.ignoreCase = caseFirst === "false" ? true : false;
this.direction = direction ?? "asc";
}

public compare(a: string, b: string): number {
const { customSortOrder, direction, ignoreCase } = this;
const { customSortOrder, customIgnoreCharacters, direction, ignoreCase } = this;
const sign = direction === "asc" ? 1 : -1;

if (customIgnoreCharacters) {
a = a.replace(customIgnoreCharacters, "");
b = b.replace(customIgnoreCharacters, "");
}

if (customSortOrder) {
const minLength = Math.min(a.length, b.length);
for (let i = 0; i < minLength; i++) {
const aIndex = customSortOrder.indexOf(ignoreCase ? a[i].toLowerCase() : a[i]);
const bIndex = customSortOrder.indexOf(ignoreCase ? b[i].toLowerCase() : b[i]);

if (aIndex === -1 || bIndex === -1) continue;
else if (aIndex !== bIndex) return (aIndex - bIndex) * sign;
if (aIndex === -1 || bIndex === -1) {
const diff = super.compare(a[i], b[i]);
if (diff !== 0) return diff * sign;
else continue;
}

if (aIndex !== bIndex) return (aIndex - bIndex) * sign;
else if (i === minLength - 1) return (a.length - b.length) * sign;
}
}
Expand Down
12 changes: 12 additions & 0 deletions src/test/fixtures/custom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,16 @@ export const customSortTests: CustomSortTest[] = [
ranges: [new Range(7, 0, 21, 1)],
collatorOptions: { customSortOrder: '"-_' },
},
{
file: "custom.plaintext.fixture",
compareFile: "custom.plaintext.expect",
ranges: [new Range(23, 0, 37, 1)],
collatorOptions: { customSortOrder: "-_", customIgnoreCharacters: "'\"()[]{}<>" },
},
{
file: "custom.plaintext.fixture",
compareFile: "custom.plaintext.expect",
ranges: [new Range(23, 0, 37, 1)],
collatorOptions: { customSortOrder: '"-_' },
},
];
16 changes: 16 additions & 0 deletions test/fixtures/custom.plaintext.expect
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,19 @@ variable "grafana_address" {
variable "grafana_password" {
type = string
}

variable "grafana" {
type = bool
}
variable "grafana_password" {
type = string
}
variable "helm_chart_version" {
type = string
}
variable "name" {
type = string
}
variable "name_chart_version" {
type = string
}
16 changes: 16 additions & 0 deletions test/fixtures/custom.plaintext.fixture
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,19 @@ variable "grafana_password" {
variable "grafana-count" {
type = number
}

variable "grafana_password" {
type = string
}
variable "name_chart_version" {
type = string
}
variable "grafana" {
type = bool
}
variable "helm_chart_version" {
type = string
}
variable "name" {
type = string
}

0 comments on commit 1333902

Please sign in to comment.