Skip to content

Commit

Permalink
Formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
vojtechpavlu committed Apr 8, 2024
1 parent 91147e2 commit df5b089
Show file tree
Hide file tree
Showing 12 changed files with 224 additions and 106 deletions.
2 changes: 1 addition & 1 deletion integration/integration-tests.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
resetModules: true
resetModules: true,
};
9 changes: 6 additions & 3 deletions integration/tests/Plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { getStandard, getValueGenerator, registerPlugin } from '@falbricate/base';
import {
getStandard,
getValueGenerator,
registerPlugin,
} from '@falbricate/base';
import { GeographyPlugin } from '../../src';

describe('Not failing Plugin registration', () => {
Expand All @@ -10,7 +14,7 @@ describe('Not failing Plugin registration', () => {
describe('Registering contents', () => {
it('should register Random Country Generator', () => {
const config = {
countries: ['no', 'it']
countries: ['no', 'it'],
};

expect(() => getValueGenerator('random-country', config)).not.toThrow();
Expand All @@ -20,4 +24,3 @@ describe('Registering contents', () => {
expect(() => getStandard('country-abbr-eu')).not.toThrow();
});
});

2 changes: 1 addition & 1 deletion src/data/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export type NamedGeography = {
type: GeographyType;
name: string;
abbr: string;
}
};

/** Declaration of regions and groups */
export interface Region extends NamedGeography {
Expand Down
11 changes: 8 additions & 3 deletions src/data/countries/european.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,12 @@ export const EUROPEAN_COUNTRIES: Country[] = [
{ type: 'country', abbr: 'az', name: 'Azerbaijan', capital: 'Baku' },
{ type: 'country', abbr: 'by', name: 'Belarus', capital: 'Minsk' },
{ type: 'country', abbr: 'be', name: 'Belgium', capital: 'Brussels' },
{ type: 'country', abbr: 'ba', name: 'Bosnia and Herzegovina', capital: 'Sarajevo' },
{
type: 'country',
abbr: 'ba',
name: 'Bosnia and Herzegovina',
capital: 'Sarajevo',
},
{ type: 'country', abbr: 'bg', name: 'Bulgaria', capital: 'Sofia' },
{ type: 'country', abbr: 'hr', name: 'Croatia', capital: 'Zagreb' },
{ type: 'country', abbr: 'cy', name: 'Cyprus', capital: 'Nicosia' },
Expand All @@ -28,7 +33,7 @@ export const EUROPEAN_COUNTRIES: Country[] = [
{ type: 'country', abbr: 'li', name: 'Liechtenstein', capital: 'Vaduz' },
{ type: 'country', abbr: 'lt', name: 'Lithuania', capital: 'Vilnius' },
{ type: 'country', abbr: 'lu', name: 'Luxembourg', capital: 'Luxembourg' },
{ type: 'country', abbr: 'mt', name: 'Malta', capital: 'St. Paul\'s Bay' },
{ type: 'country', abbr: 'mt', name: 'Malta', capital: "St. Paul's Bay" },
{ type: 'country', abbr: 'md', name: 'Moldova', capital: 'Chisinau' },
{ type: 'country', abbr: 'mc', name: 'Monaco', capital: 'Monaco' },
{ type: 'country', abbr: 'me', name: 'Montenegro', capital: 'Podgorica' },
Expand All @@ -49,5 +54,5 @@ export const EUROPEAN_COUNTRIES: Country[] = [
{ type: 'country', abbr: 'tr', name: 'Turkey', capital: 'Ankara' },
{ type: 'country', abbr: 'ua', name: 'Ukraine', capital: 'Kyiv' },
{ type: 'country', abbr: 'uk', name: 'United Kingdom', capital: 'London' },
{ type: 'country', abbr: 'va', name: 'Vatican', capital: 'Vatican City' }
{ type: 'country', abbr: 'va', name: 'Vatican', capital: 'Vatican City' },
];
157 changes: 115 additions & 42 deletions src/data/regions/european/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,95 +6,168 @@ export const EUROPEAN_REGIONS: Region[] = [
abbr: 'eu',
name: 'European Union',
subs: [
'at', 'be', 'bg', 'hr', 'cy', 'cz', 'dk', 'fi', 'ee', 'fr', 'de',
'gr', 'hu', 'ie', 'it', 'lv', 'lt', 'lu', 'mt', 'nl', 'pl', 'pt',
'ro', 'sk', 'si', 'es', 'se'
]
}, {
'at',
'be',
'bg',
'hr',
'cy',
'cz',
'dk',
'fi',
'ee',
'fr',
'de',
'gr',
'hu',
'ie',
'it',
'lv',
'lt',
'lu',
'mt',
'nl',
'pl',
'pt',
'ro',
'sk',
'si',
'es',
'se',
],
},
{
type: 'region',
abbr: 'not-eu',
name: 'European Countries not in EU',
subs: [
'al', 'ad', 'am', 'az', 'by', 'ba', 'ge', 'is', 'li', 'md', 'mc',
'me', 'mkd', 'no', 'ru', 'sm', 'rs', 'ch', 'tr', 'ua', 'uk', 'va'
]
}, {
'al',
'ad',
'am',
'az',
'by',
'ba',
'ge',
'is',
'li',
'md',
'mc',
'me',
'mkd',
'no',
'ru',
'sm',
'rs',
'ch',
'tr',
'ua',
'uk',
'va',
],
},
{
type: 'region',
abbr: 'europe',
name: 'Europe',
subs: ['eu', 'not-eu']
}, {
subs: ['eu', 'not-eu'],
},
{
type: 'region',
abbr: 'nordics',
name: 'Nordic countries',
subs: ['dk', 'fi', 'no', 'se', 'is']
}, {
subs: ['dk', 'fi', 'no', 'se', 'is'],
},
{
type: 'region',
abbr: 'baltics',
name: 'Baltic states',
subs: ['ee', 'lv', 'lt']
}, {
subs: ['ee', 'lv', 'lt'],
},
{
type: 'region',
abbr: 'v4',
name: 'Visegrád Group',
subs: ['cz', 'hu', 'pl', 'sk']
}, {
subs: ['cz', 'hu', 'pl', 'sk'],
},
{
type: 'region',
abbr: 'benelux',
name: 'Benelux',
subs: ['be', 'nl', 'lu']
}, {
subs: ['be', 'nl', 'lu'],
},
{
type: 'region',
abbr: 'iberia',
name: 'Iberian Penisula',
subs: ['es', 'pt']
}, {
subs: ['es', 'pt'],
},
{
type: 'region',
abbr: 'balkans',
name: 'Balkan Countries',
subs: ['al', 'ba', 'bg', 'hr', 'me', 'mkd', 'rs']
}, {
subs: ['al', 'ba', 'bg', 'hr', 'me', 'mkd', 'rs'],
},
{
type: 'region',
abbr: 'balkans-broad',
name: 'Broad Balkan Countries',
subs: ['balkans', 'gr', 'ro', 'si', 'tr']
}, {
subs: ['balkans', 'gr', 'ro', 'si', 'tr'],
},
{
type: 'region',
abbr: 'n-europe',
name: 'Northern Europe',
subs: ['dk', 'ee', 'fi', 'is', 'ie', 'lv', 'lt', 'no', 'se', 'uk']
}, {
subs: ['dk', 'ee', 'fi', 'is', 'ie', 'lv', 'lt', 'no', 'se', 'uk'],
},
{
type: 'region',
abbr: 's-europe',
name: 'Southwest Europe',
subs: [
'al', 'ad', 'ba', 'hr', 'gr', 'va', 'it', 'mt', 'me', 'pt', 'sm',
'rs', 'si', 'es', 'mkd'
]
}, {
'al',
'ad',
'ba',
'hr',
'gr',
'va',
'it',
'mt',
'me',
'pt',
'sm',
'rs',
'si',
'es',
'mkd',
],
},
{
type: 'region',
abbr: 'w-europe',
name: 'Western Europe',
subs: ['au', 'be', 'fr', 'de', 'li', 'lu', 'mc', 'nl', 'ch']
}, {
subs: ['au', 'be', 'fr', 'de', 'li', 'lu', 'mc', 'nl', 'ch'],
},
{
type: 'region',
abbr: 'e-europe',
name: 'Eastern Europe',
subs: ['bg', 'by', 'cz', 'hu', 'md', 'pl', 'ro', 'ru', 'sk', 'ua']
}, {
subs: ['bg', 'by', 'cz', 'hu', 'md', 'pl', 'ro', 'ru', 'sk', 'ua'],
},
{
type: 'region',
abbr: 'c-europe',
name: 'Central Europe',
subs: ['at', 'de', 'cz', 'sk', 'pl', 'hu']
}, {
subs: ['at', 'de', 'cz', 'sk', 'pl', 'hu'],
},
{
type: 'region',
abbr: 'sw-europe',
name: 'Southwest Europe',
subs: ['es', 'pt']
}, {
subs: ['es', 'pt'],
},
{
type: 'region',
abbr: 'se-europe',
name: 'Southeast Europe',
subs: ['balkans-broad', 'it']
}
];
subs: ['balkans-broad', 'it'],
},
];
24 changes: 13 additions & 11 deletions src/data/registry.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {Country, GeographyElement, NamedGeography, Region} from "./base";
import { Country, GeographyElement, NamedGeography, Region } from './base';
import { EUROPEAN_COUNTRIES } from './countries';
import { EUROPEAN_REGIONS } from './regions';

Expand All @@ -16,24 +16,26 @@ const REGISTRY: GeographyRegistry = {};
*/
export const hasGeographyAbbreviation = (abbr: string): boolean => {
return allGeographyAbbreviations().includes(abbr);
}
};

export const allGeographyAbbreviations = (): string[] => {
return Object.keys(REGISTRY)
}
return Object.keys(REGISTRY);
};

export const addGeography = (geography: GeographyElement) => {
if (allGeographyAbbreviations().includes(geography.abbr)) {
throw new Error(`Geography region with abbreviation '${geography.abbr}' already exists`);
throw new Error(
`Geography region with abbreviation '${geography.abbr}' already exists`,
);
}

if (geography.type === 'region') {
geography = geography as Region
geography = geography as Region;
if (!geography.subs || !Array.isArray(geography.subs)) {
throw new Error(`Region's subregions must be a non-empty array`);
}
} else if (geography.type === 'country') {
geography = geography as Country
geography = geography as Country;
if (!geography.capital) {
throw new Error(`Country has to have a capital city assigned`);
}
Expand All @@ -56,13 +58,13 @@ export const getGeography = (abbreviation: string): Country[] => {
region.subs
.map((subregion) => getGeography(subregion))
.map((subregion) => {
return subregion
return subregion;
})
.forEach((subregion) => {
subregion.forEach((country) => result.push(country));
})
});

return [...new Set(result)]
return [...new Set(result)];
} else if (geography.type === 'country') {
const country = geography as Country;
return [country];
Expand All @@ -76,5 +78,5 @@ export const getGeography = (abbreviation: string): Country[] => {
...EUROPEAN_COUNTRIES,

// Register Regions
...EUROPEAN_REGIONS
...EUROPEAN_REGIONS,
].forEach((region) => addGeography(region));
23 changes: 15 additions & 8 deletions src/generators/RandomCountryGenerator.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
import { GeneratedValue, randomItemFromArray, ValueGenerator, ValueGeneratorConfig } from '@falbricate/base';
import {
GeneratedValue,
randomItemFromArray,
ValueGenerator,
ValueGeneratorConfig,
} from '@falbricate/base';
import { Country, getGeography } from '../data';

export type CountryAs = 'object' | 'name' | 'abbr' | 'capital'
export type CountryAs = 'object' | 'name' | 'abbr' | 'capital';

export type RandomCountryConfig = {
countries: string | string[];
as?: CountryAs;
} & ValueGeneratorConfig & any;
} & ValueGeneratorConfig &
any;

export class RandomCountryGenerator extends ValueGenerator<
GeneratedValue,
RandomCountryConfig
> {

private readonly countries: Country[];

constructor(config: RandomCountryConfig) {
Expand All @@ -37,8 +42,8 @@ export class RandomCountryGenerator extends ValueGenerator<
} else {
throw new Error(
`Unexpected type of countries specification: ` +
`'${this.config.countries}'`
)
`'${this.config.countries}'`,
);
}
}

Expand All @@ -56,7 +61,9 @@ export class RandomCountryGenerator extends ValueGenerator<
return country.capital;

default:
throw new Error(`Unknown type of country property: '${this.config.as}'`);
throw new Error(
`Unknown type of country property: '${this.config.as}'`,
);
}
};
}
}
Loading

0 comments on commit df5b089

Please sign in to comment.