Skip to content

Commit

Permalink
refactor: Consolidate transformers. (#428)
Browse files Browse the repository at this point in the history
  • Loading branch information
vxern authored Sep 17, 2024
2 parents cf53a48 + 1fa8344 commit 5319576
Show file tree
Hide file tree
Showing 38 changed files with 49 additions and 310 deletions.
56 changes: 20 additions & 36 deletions source/constants/localisations.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,32 @@
import type { Language } from "logos:constants/languages";
import type { LocalisationLanguage } from "logos:constants/languages/localisation";
import type { PartOfSpeech } from "logos:constants/parts-of-speech";
import armenian from "logos:constants/transformers/armenian";
import danish from "logos:constants/transformers/danish";
import dutch from "logos:constants/transformers/dutch";
import english from "logos:constants/transformers/english";
import finnish from "logos:constants/transformers/finnish";
import french from "logos:constants/transformers/french";
import german from "logos:constants/transformers/german";
import greek from "logos:constants/transformers/greek";
import hungarian from "logos:constants/transformers/hungarian";
import norwegian from "logos:constants/transformers/norwegian";
import polish from "logos:constants/transformers/polish";
import romanian from "logos:constants/transformers/romanian";
import russian from "logos:constants/transformers/russian";
import silesian from "logos:constants/transformers/silesian";
import spanish from "logos:constants/transformers/spanish";
import swedish from "logos:constants/transformers/swedish";
import turkish from "logos:constants/transformers/turkish";
import pluralisers from "logos:constants/transformers/pluralisers";

type TransformerType = "pluralise";
type Transformer = (matchTerm: string, matches: Record<string, string>) => string | undefined;

const localisations = Object.freeze({
transformers: {
"Armenian/Western": armenian,
"Armenian/Eastern": armenian,
Danish: danish,
Dutch: dutch,
"English/American": english,
"English/British": english,
Finnish: finnish,
French: french,
German: german,
Greek: greek,
Hungarian: hungarian,
"Norwegian/Bokmal": norwegian,
Polish: polish,
Romanian: romanian,
Russian: russian,
Silesian: silesian,
Spanish: spanish,
Swedish: swedish,
Turkish: turkish,
"Armenian/Western": { pluralise: pluralisers.invariant },
"Armenian/Eastern": { pluralise: pluralisers.invariant },
Danish: { pluralise: pluralisers.commonEuropean },
Dutch: { pluralise: pluralisers.commonEuropean },
"English/American": { pluralise: pluralisers.commonEuropean },
"English/British": { pluralise: pluralisers.commonEuropean },
Finnish: { pluralise: pluralisers.commonEuropean },
French: { pluralise: pluralisers.commonEuropean },
German: { pluralise: pluralisers.commonEuropean },
Greek: { pluralise: pluralisers.commonEuropean },
Hungarian: { pluralise: pluralisers.invariant },
"Norwegian/Bokmal": { pluralise: pluralisers.commonEuropean },
Polish: { pluralise: pluralisers.commonSlavic },
Romanian: { pluralise: pluralisers.romanian },
Russian: { pluralise: pluralisers.commonSlavic },
Silesian: { pluralise: pluralisers.commonSlavic },
Spanish: { pluralise: pluralisers.commonEuropean },
Swedish: { pluralise: pluralisers.commonEuropean },
Turkish: { pluralise: pluralisers.invariant },
} satisfies Record<LocalisationLanguage, Record<TransformerType, Transformer>>,
languages: {
Abkhazian: "languages.abkhazian",
Expand Down
9 changes: 0 additions & 9 deletions source/constants/transformers/armenian.ts

This file was deleted.

13 changes: 0 additions & 13 deletions source/constants/transformers/danish.ts

This file was deleted.

13 changes: 0 additions & 13 deletions source/constants/transformers/dutch.ts

This file was deleted.

13 changes: 0 additions & 13 deletions source/constants/transformers/english.ts

This file was deleted.

16 changes: 0 additions & 16 deletions source/constants/transformers/finnish.ts

This file was deleted.

13 changes: 0 additions & 13 deletions source/constants/transformers/german.ts

This file was deleted.

13 changes: 0 additions & 13 deletions source/constants/transformers/greek.ts

This file was deleted.

9 changes: 0 additions & 9 deletions source/constants/transformers/hungarian.ts

This file was deleted.

13 changes: 0 additions & 13 deletions source/constants/transformers/norwegian.ts

This file was deleted.

6 changes: 6 additions & 0 deletions source/constants/transformers/pluralisers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import commonEuropean from "logos:constants/transformers/pluralisers/common-european";
import commonSlavic from "logos:constants/transformers/pluralisers/common-slavic";
import invariant from "logos:constants/transformers/pluralisers/invariant";
import romanian from "logos:constants/transformers/pluralisers/romanian";

export default Object.freeze({ commonEuropean, commonSlavic, invariant, romanian });
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
function pluralise(quantity: string, { one: singular, two: plural }: Record<string, string> = {}): string | undefined {
export default function pluralise(
quantity: string,
{ one: singular, two: plural }: Record<string, string> = {},
): string | undefined {
if (singular === undefined || plural === undefined) {
return undefined;
}
Expand All @@ -9,5 +12,3 @@ function pluralise(quantity: string, { one: singular, two: plural }: Record<stri

return plural;
}

export default { pluralise };
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function pluralise(
export default function pluralise(
quantity: string,
{ one: singular, two: pluralNominative, many: pluralGenitive }: Record<string, string> = {},
): string | undefined {
Expand All @@ -15,12 +15,10 @@ function pluralise(
return pluralGenitive;
}

// Numbers ending in 2, 3 and 4.
// Numbers other than 12, 13 and 14 ending in 2, 3 and 4.
if (["2", "3", "4"].some((digit) => quantity.endsWith(digit))) {
return pluralNominative;
}

return pluralGenitive;
}

export default { pluralise };
7 changes: 7 additions & 0 deletions source/constants/transformers/pluralisers/invariant.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export default function pluralise(_: string, { one: singular }: Record<string, string> = {}): string | undefined {
if (singular === undefined) {
return undefined;
}

return singular;
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
function pluralise(
export default function pluralise(
quantity: string,
{ one: singular, two: pluralSmall, many: pluralLarge }: Record<string, string> = {},
): string | undefined {
Expand All @@ -18,5 +18,3 @@ function pluralise(
// Once the number reaches 20, Romanian begins slotting a 'de' between the number and the plural form of the word.
return pluralLarge;
}

export default { pluralise };
26 changes: 0 additions & 26 deletions source/constants/transformers/russian.ts

This file was deleted.

26 changes: 0 additions & 26 deletions source/constants/transformers/silesian.ts

This file was deleted.

13 changes: 0 additions & 13 deletions source/constants/transformers/spanish.ts

This file was deleted.

13 changes: 0 additions & 13 deletions source/constants/transformers/swedish.ts

This file was deleted.

9 changes: 0 additions & 9 deletions source/constants/transformers/turkish.ts

This file was deleted.

5 changes: 0 additions & 5 deletions test/source/constants/transformers/english.spec.ts

This file was deleted.

5 changes: 0 additions & 5 deletions test/source/constants/transformers/finnish.spec.ts

This file was deleted.

5 changes: 0 additions & 5 deletions test/source/constants/transformers/french.spec.ts

This file was deleted.

5 changes: 0 additions & 5 deletions test/source/constants/transformers/german.spec.ts

This file was deleted.

5 changes: 0 additions & 5 deletions test/source/constants/transformers/greek.spec.ts

This file was deleted.

5 changes: 0 additions & 5 deletions test/source/constants/transformers/hungarian.spec.ts

This file was deleted.

5 changes: 0 additions & 5 deletions test/source/constants/transformers/norwegian.spec.ts

This file was deleted.

9 changes: 9 additions & 0 deletions test/source/constants/transformers/pluralisers.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { describe, it } from "bun:test";
import pluralisers from "logos:constants/transformers/pluralisers";
import { expect } from "chai";

describe("The pluralisers object", () => {
it("is immutable.", () => {
expect(Object.isFrozen(pluralisers)).to.be.true;
});
});
Loading

0 comments on commit 5319576

Please sign in to comment.