Skip to content

Commit

Permalink
Merge pull request #86 from leancodepl/feat/85-sort-strings
Browse files Browse the repository at this point in the history
Sort terms alphabetically in ARBs
  • Loading branch information
Albert221 authored Apr 3, 2024
2 parents f43c09e + d39f10b commit d3163e6
Show file tree
Hide file tree
Showing 35 changed files with 31 additions and 10 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2023 LeanCode Sp. z o.o.
Copyright 2024 LeanCode Sp. z o.o.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down
16 changes: 16 additions & 0 deletions convert/poe2arb/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"fmt"
"io"
"regexp"
"slices"
"strings"

"facette.io/natsort"
"github.com/leancodepl/poe2arb/convert"
"github.com/leancodepl/poe2arb/flutter"
orderedmap "github.com/wk8/go-ordered-map/v2"
Expand Down Expand Up @@ -57,6 +59,20 @@ func (c *Converter) Convert(output io.Writer) error {
prefixedRegexp := regexp.MustCompile("(?:([a-zA-Z]+):)?(.*)")
var errs []error

// Sort terms by key alphabetically
slices.SortStableFunc(jsonContents, func(a, b *convert.POETerm) int {
aKey := prefixedRegexp.FindStringSubmatch(a.Term)[2]
bKey := prefixedRegexp.FindStringSubmatch(b.Term)[2]

if aKey == bKey {
return 0
} else if natsort.Compare(aKey, bKey) {
return -1
} else {
return 1
}
})

for _, term := range jsonContents {
// Filter by term prefix
matches := prefixedRegexp.FindStringSubmatch(term.Term)
Expand Down
10 changes: 6 additions & 4 deletions convert/poe2arb/converter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ func TestConverterConvert(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if len(paths) == 0 {
t.Fatal("no test files found")
}

for _, path := range paths {
_, filename := filepath.Split(path)
Expand Down Expand Up @@ -44,7 +47,7 @@ func TestConverterConvert(t *testing.T) {
expect := string(golden)

// Actual test
actual, err := convert(t, string(source), template, requireResourceAttributes, termPrefix)
actual, err := convert(string(source), template, requireResourceAttributes, termPrefix)

assert.NoError(t, err)
assert.Equal(t, expect, actual)
Expand All @@ -70,15 +73,15 @@ func TestConverterConvert(t *testing.T) {
`

t.Run("issue 41 template", func(t *testing.T) {
actual, err := convert(t, issue41Source, true, false, "")
actual, err := convert(issue41Source, true, false, "")

assert.Error(t, err)
assert.EqualError(t, err, `decoding term "testPlural" failed: missing "other" plural category`)
assert.Equal(t, "", actual)
})

t.Run("issue 41 non-template", func(t *testing.T) {
actual, err := convert(t, issue41Source, false, false, "")
actual, err := convert(issue41Source, false, false, "")

assert.NoError(t, err)
assert.Equal(t, "{\n \"@@locale\": \"en\"\n}\n", actual)
Expand All @@ -94,7 +97,6 @@ func flutterMustParseLocale(lang string) flutter.Locale {
}

func convert(
t *testing.T,
input string,
template bool,
requireResourceAttributes bool,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"@@locale": "en",
"noAttrs": "Foobar",
"@noAttrs": {},
"attrs": "Foo {bar}",
"@attrs": {
"placeholders": {
"bar": {
"type": "String"
}
}
}
},
"noAttrs": "Foobar",
"@noAttrs": {}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"@@locale": "en",
"noAttrs": "Foobar",
"attrs": "Foo {bar}",
"@attrs": {
"placeholders": {
"bar": {
"type": "String"
}
}
}
},
"noAttrs": "Foobar"
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
)

require (
facette.io/natsort v0.0.0-20181210072756-2cd4dd1e2dcb
github.com/inconshreveable/mousetrap v1.0.1 // indirect
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
facette.io/natsort v0.0.0-20181210072756-2cd4dd1e2dcb h1:1pSweJFeR3Pqx7uoelppkzeegfUBXL6I2FFAbfXw570=
facette.io/natsort v0.0.0-20181210072756-2cd4dd1e2dcb/go.mod h1:npRYmtaITVom7rcSo+pRURltHSG2r4TQM1cdqJ2dUB0=
github.com/TwiN/go-color v1.4.0 h1:fNbOwOrvup5oj934UragnW0B1WKaAkkB85q19Y7h4ng=
github.com/TwiN/go-color v1.4.0/go.mod h1:0QTVEPlu+AoCyTrho7bXbVkrCkVpdQr7YF7PYWEtSxM=
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
Expand Down

0 comments on commit d3163e6

Please sign in to comment.