Skip to content

Commit

Permalink
fix: Fix alphabetical order of extracted keys (#35)
Browse files Browse the repository at this point in the history
Addresses the issue when the `--sort` flag is used, where keys with
lowercase characters were incorrectly positioned at the bottom of
the file after keys with uppercase characters, breaking the alphabetical
order.

Closes #15
  • Loading branch information
pmpak authored Nov 28, 2023
1 parent eb996e5 commit 5fd0186
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/post-processors/sort-by-key.post-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export class SortByKeyPostProcessor implements PostProcessorInterface {
public name: string = 'SortByKey';

public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
return draft.sort();
const compareFn = new Intl.Collator('en', {sensitivity: 'base'}).compare;
return draft.sort(compareFn);
}
}
36 changes: 36 additions & 0 deletions tests/post-processors/sort-by-key.post-processor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,40 @@ describe('SortByKeyPostProcessor', () => {
z: {value: 'last value', sourceFiles: []}
});
});

it('should perform case insensitive sorting', () => {
const collection = new TranslationCollection({
c: { value: 'letter c', sourceFiles: [] },
j: { value: 'letter j', sourceFiles: [] },
b: { value: 'letter b', sourceFiles: [] },
a: { value: 'letter a', sourceFiles: [] },
h: { value: 'letter h', sourceFiles: [] },
B: { value: 'letter B', sourceFiles: [] },
H: { value: 'letter H', sourceFiles: [] },
i: { value: 'letter i', sourceFiles: [] },
C: { value: 'letter C', sourceFiles: [] },
e: { value: 'letter e', sourceFiles: [] },
f: { value: 'letter f', sourceFiles: [] },
d: { value: 'letter d', sourceFiles: [] },
A: { value: 'letter A', sourceFiles: [] },
g: { value: 'letter g', sourceFiles: [] }
});

expect(processor.process(collection, new TranslationCollection(), new TranslationCollection()).values).to.deep.equal({
A: { value: 'letter A', sourceFiles: [] },
a: { value: 'letter a', sourceFiles: [] },
B: { value: 'letter B', sourceFiles: [] },
b: { value: 'letter b', sourceFiles: [] },
c: { value: 'letter c', sourceFiles: [] },
C: { value: 'letter C', sourceFiles: [] },
d: { value: 'letter d', sourceFiles: [] },
e: { value: 'letter e', sourceFiles: [] },
f: { value: 'letter f', sourceFiles: [] },
g: { value: 'letter g', sourceFiles: [] },
H: { value: 'letter H', sourceFiles: [] },
h: { value: 'letter h', sourceFiles: [] },
i: { value: 'letter i', sourceFiles: [] },
j: { value: 'letter j', sourceFiles: [] }
});
});
});

0 comments on commit 5fd0186

Please sign in to comment.