Skip to content

Commit 171402a

Browse files
committed
feat(sorting): case-insensitive sorting method
1 parent 82eb652 commit 171402a

File tree

4 files changed

+52
-17
lines changed

4 files changed

+52
-17
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ ngx-translate-extract [options]
7474
Output
7575
--format, -f Format [string] [choices: "json", "namespaced-json", "pot"] [default: "json"]
7676
--format-indentation, --fi Format indentation (JSON/Namedspaced JSON) [string] [default: " "]
77-
--sort, -s Sort strings in alphabetical order [boolean]
77+
--sort, -s Sort strings in alphabetical order [string] [choices: "", "case-insensitive"]
7878
--clean, -c Remove obsolete strings after merge [boolean]
7979
--replace, -r Replace the contents of output file if it exists (Merges by default) [boolean]
8080

src/cli/cli.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { DirectiveParser } from '../parsers/directive.parser';
88
import { ServiceParser } from '../parsers/service.parser';
99
import { MarkerParser } from '../parsers/marker.parser';
1010
import { PostProcessorInterface } from '../post-processors/post-processor.interface';
11-
import { SortByKeyPostProcessor } from '../post-processors/sort-by-key.post-processor';
11+
import { SortByKeyPostProcessor, SortOptions } from '../post-processors/sort-by-key.post-processor';
1212
import { KeyAsDefaultValuePostProcessor } from '../post-processors/key-as-default-value.post-processor';
1313
import { NullAsDefaultValuePostProcessor } from '../post-processors/null-as-default-value.post-processor';
1414
import { StringAsDefaultValuePostProcessor } from '../post-processors/string-as-default-value.post-processor';
@@ -79,7 +79,8 @@ export const cli = y
7979
.option('sort', {
8080
alias: 's',
8181
describe: 'Sort strings in alphabetical order',
82-
type: 'boolean'
82+
type: 'string',
83+
choices: ['', 'case-insensitive']
8384
})
8485
.option('clean', {
8586
alias: 'c',
@@ -138,8 +139,8 @@ if (cli.keyAsDefaultValue) {
138139
postProcessors.push(new StringAsDefaultValuePostProcessor({ defaultValue: cli.stringAsDefaultValue as string }));
139140
}
140141

141-
if (cli.sort) {
142-
postProcessors.push(new SortByKeyPostProcessor());
142+
if (typeof cli.sort !== 'undefined') {
143+
postProcessors.push(new SortByKeyPostProcessor(cli.sort as SortOptions));
143144
}
144145
extractTask.setPostProcessors(postProcessors);
145146

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,21 @@
11
import { TranslationCollection } from '../utils/translation.collection';
22
import { PostProcessorInterface } from './post-processor.interface';
33

4+
export type SortOptions = '' | 'case-insensitive';
5+
46
export class SortByKeyPostProcessor implements PostProcessorInterface {
57
public name: string = 'SortByKey';
68

9+
constructor(private sortOptions: SortOptions) { }
10+
711
public process(draft: TranslationCollection, extracted: TranslationCollection, existing: TranslationCollection): TranslationCollection {
8-
return draft.sort();
12+
if (this.sortOptions === 'case-insensitive') {
13+
return draft.sort((a, b) => {
14+
return a.toLowerCase().localeCompare(b.toLowerCase());
15+
});
16+
} else {
17+
return draft.sort();
18+
}
919
}
1020
}
21+

tests/post-processors/sort-by-key.post-processor.spec.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,51 @@ import { PostProcessorInterface } from '../../src/post-processors/post-processor
44
import { SortByKeyPostProcessor } from '../../src/post-processors/sort-by-key.post-processor';
55
import { TranslationCollection } from '../../src/utils/translation.collection';
66

7+
const mock = {
8+
ZZ: 'last value',
9+
a: 'a value',
10+
'9': 'a numeric key',
11+
b: 'another value'
12+
};
13+
14+
715
describe('SortByKeyPostProcessor', () => {
816
let processor: PostProcessorInterface;
17+
let collection: TranslationCollection;
18+
let extracted: TranslationCollection;
19+
let existing: TranslationCollection;
920

1021
beforeEach(() => {
11-
processor = new SortByKeyPostProcessor();
22+
collection = new TranslationCollection(mock);
23+
extracted = new TranslationCollection();
24+
existing = new TranslationCollection();
1225
});
1326

14-
it('should sort keys alphanumerically', () => {
15-
const collection = new TranslationCollection({
16-
z: 'last value',
17-
a: 'a value',
27+
it('should sort keys alphanumerically (case sensitive)', () => {
28+
processor = new SortByKeyPostProcessor('');
29+
const sorted = processor.process(collection, extracted, existing).values;
30+
31+
const sortedOutput = {
1832
'9': 'a numeric key',
33+
ZZ: 'last value',
34+
a: 'a value',
1935
b: 'another value'
20-
});
21-
const extracted = new TranslationCollection();
22-
const existing = new TranslationCollection();
36+
};
2337

24-
expect(processor.process(collection, extracted, existing).values).to.deep.equal({
38+
expect(JSON.stringify(sorted)).to.deep.equal(JSON.stringify(sortedOutput));
39+
});
40+
41+
it('should sort keys alphanumerically (case insensitive)', () => {
42+
processor = new SortByKeyPostProcessor('case-insensitive');
43+
const sorted = processor.process(collection, extracted, existing).values;
44+
45+
const sortedOutput = {
2546
'9': 'a numeric key',
2647
a: 'a value',
2748
b: 'another value',
28-
z: 'last value'
29-
});
49+
ZZ: 'last value'
50+
};
51+
52+
expect(JSON.stringify(sorted)).to.deep.equal(JSON.stringify(sortedOutput));
3053
});
3154
});

0 commit comments

Comments
 (0)